diff --git a/golang/internal/helper/image/image_unit_test.go b/golang/internal/helper/image/image_unit_test.go index cff282be6..8894f9fe0 100644 --- a/golang/internal/helper/image/image_unit_test.go +++ b/golang/internal/helper/image/image_unit_test.go @@ -10,38 +10,57 @@ import ( "github.com/stretchr/testify/assert" imageHelper "github.com/dyrector-io/dyrectorio/golang/internal/helper/image" + "github.com/dyrector-io/dyrectorio/golang/internal/pointer" "github.com/dyrector-io/dyrectorio/protobuf/go/agent" ) -func TestRegistryUrl(t *testing.T) { - auth := &imageHelper.RegistryAuth{ - URL: "test", - } - - url := imageHelper.GetRegistryURL(nil, auth) - assert.Equal(t, url, "test") +type RegistryTestCase struct { + Name string + Registry *string + RegistryUrl *string + ExpectedUrl string } -func TestRegistryUrlPriority(t *testing.T) { - registry := "other" - auth := &imageHelper.RegistryAuth{ - URL: "test", +func TestRegistryWithTable(t *testing.T) { + testCases := []RegistryTestCase{ + { + Name: "Test registry url", + Registry: pointer.NewPTR[string](""), + RegistryUrl: pointer.NewPTR[string]("test"), + ExpectedUrl: "test", + }, + { + Name: "Test registry url priority", + Registry: pointer.NewPTR[string]("other"), + RegistryUrl: pointer.NewPTR[string]("test"), + ExpectedUrl: "test", + }, + { + Name: "Test registry url empty", + Registry: nil, + RegistryUrl: nil, + ExpectedUrl: "", + }, + { + Name: "Test registry url registry", + Registry: pointer.NewPTR[string]("other"), + RegistryUrl: nil, + ExpectedUrl: "other", + }, } - url := imageHelper.GetRegistryURL(®istry, auth) - assert.Equal(t, url, "test") -} - -func TestRegistryUrlRegistry(t *testing.T) { - registry := "other" - - url := imageHelper.GetRegistryURL(®istry, nil) - assert.Equal(t, url, "other") -} - -func TestRegistryUrlEmpty(t *testing.T) { - url := imageHelper.GetRegistryURL(nil, nil) - assert.Equal(t, url, "") + for _, tC := range testCases { + t.Run(tC.Name, func(t *testing.T) { + if tC.RegistryUrl == nil { + url := imageHelper.GetRegistryURL(tC.Registry, nil) + assert.Equal(t, url, tC.ExpectedUrl) + } else { + auth := &imageHelper.RegistryAuth{URL: *tC.RegistryUrl} + url := imageHelper.GetRegistryURL(tC.Registry, auth) + assert.Equal(t, url, tC.ExpectedUrl) + } + }) + } } func TestProtoRegistryUrl(t *testing.T) { diff --git a/golang/internal/pointer/pointer.go b/golang/internal/pointer/pointer.go new file mode 100644 index 000000000..25cfe4c0d --- /dev/null +++ b/golang/internal/pointer/pointer.go @@ -0,0 +1,6 @@ +package pointer + +// Creates a new pointer of type T +func NewPTR[T any](value T) *T { + return &value +} diff --git a/golang/internal/pointer/pointer_test.go b/golang/internal/pointer/pointer_test.go new file mode 100644 index 000000000..50c96a085 --- /dev/null +++ b/golang/internal/pointer/pointer_test.go @@ -0,0 +1,15 @@ +package pointer + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewPTR(t *testing.T) { + sPtr := NewPTR[string]("") + assert.Equal(t, *sPtr, "") + + iPtr := NewPTR[int](120) + assert.Equal(t, *iPtr, 120) +}