diff --git a/client/convert.go b/client/convert.go index 3e62a622..c56122b2 100644 --- a/client/convert.go +++ b/client/convert.go @@ -33,6 +33,7 @@ func (k *Kompose) Convert(options ConvertOptions) ([]runtime.Object, error) { PushImageRegistry: options.PushImageRegistry, CreateDeploymentConfig: k.createDeploymentConfig(options), EmptyVols: false, + Profiles: options.Profiles, Volumes: *options.VolumeType, PVCRequestSize: options.PvcRequestSize, InsecureRepository: k.insecureRepository(options), diff --git a/client/convert_test.go b/client/convert_test.go index b7014936..58fc81d5 100644 --- a/client/convert_test.go +++ b/client/convert_test.go @@ -2,6 +2,8 @@ package client import ( "fmt" + v1 "k8s.io/api/core/v1" + "sort" "testing" "gotest.tools/v3/assert" @@ -69,7 +71,7 @@ func TestConvertWithDefaultOptions(t *testing.T) { client, err := NewClient(WithErrorOnWarning()) assert.Check(t, is.Equal(err, nil)) objects, err := client.Convert(ConvertOptions{ - OutFile: "./testdata/generated/", + ToStdout: true, InputFiles: []string{ "./testdata/docker-compose.yaml", }, @@ -81,3 +83,89 @@ func TestConvertWithDefaultOptions(t *testing.T) { } } } + +func TestConvertWithProfiles(t *testing.T) { + client, err := NewClient(WithErrorOnWarning()) + assert.Check(t, is.Equal(err, nil)) + + type Want struct { + deploymentsNames []string + servicesNames []string + } + + tests := []struct { + name string + options ConvertOptions + want Want + }{ + { + name: "No profiles provided", + options: ConvertOptions{ + ToStdout: true, + InputFiles: []string{ + "./testdata/docker-compose-profiles.yaml", + }, + }, + want: Want{ + deploymentsNames: nil, + servicesNames: nil, + }, + }, + { + name: "All profiles provided", + options: ConvertOptions{ + ToStdout: true, + InputFiles: []string{ + "./testdata/docker-compose-profiles.yaml", + }, + Profiles: []string{"hello", "world"}, + }, + want: Want{ + deploymentsNames: []string{"backend", "frontend", "database"}, + servicesNames: []string{"backend", "frontend", "database"}, + }, + }, + { + name: "All profiles provided", + options: ConvertOptions{ + ToStdout: true, + InputFiles: []string{ + "./testdata/docker-compose-profiles.yaml", + }, + Profiles: []string{"hello"}, + }, + want: Want{ + deploymentsNames: []string{"backend", "frontend"}, + servicesNames: []string{"backend", "frontend"}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + objects, err := client.Convert(tt.options) + assert.Check(t, is.Equal(err, nil)) + + sort.Strings(tt.want.deploymentsNames) + sort.Strings(tt.want.servicesNames) + + var deploymentsNames []string + var servicesNames []string + + for _, object := range objects { + if deployment, ok := object.(*appsv1.Deployment); ok { + deploymentsNames = append(deploymentsNames, deployment.Name) + } + + if service, ok := object.(*v1.Service); ok { + servicesNames = append(servicesNames, service.Name) + } + } + + sort.Strings(deploymentsNames) + sort.Strings(servicesNames) + + assert.Check(t, is.DeepEqual(deploymentsNames, tt.want.deploymentsNames)) + assert.Check(t, is.DeepEqual(servicesNames, tt.want.servicesNames)) + }) + } +} diff --git a/client/testdata/docker-compose-profiles.yaml b/client/testdata/docker-compose-profiles.yaml new file mode 100644 index 00000000..3f42dd2a --- /dev/null +++ b/client/testdata/docker-compose-profiles.yaml @@ -0,0 +1,17 @@ +version: '3' +services: + backend: + image: dummy:tag + profiles: ['hello', 'world'] + ports: + - "80:80" + frontend: + image: dummy:tag + profiles: [ 'hello' ] + ports: + - "80:80" + database: + image: dummy:tag + profiles: [ 'world' ] + ports: + - "80:80" diff --git a/client/types.go b/client/types.go index 35e94abe..a5c73855 100644 --- a/client/types.go +++ b/client/types.go @@ -44,6 +44,7 @@ type ConvertOptions struct { PvcRequestSize string WithKomposeAnnotations *bool InputFiles []string + Profiles []string Provider GenerateNetworkPolicies bool } diff --git a/output.yaml b/output.yaml deleted file mode 100644 index e69de29b..00000000