github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/profiles/list_test.go (about) 1 /* 2 Copyright 2021 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package inspect 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "fmt" 24 "testing" 25 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" 27 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect" 28 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser" 29 sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/errors" 30 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 31 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util/stringslice" 32 "github.com/GoogleContainerTools/skaffold/testutil" 33 ) 34 35 func TestPrintProfilesList(t *testing.T) { 36 tests := []struct { 37 description string 38 configSet parser.SkaffoldConfigSet 39 buildEnv inspect.BuildEnv 40 module []string 41 err error 42 expected string 43 }{ 44 { 45 description: "print all profiles", 46 configSet: parser.SkaffoldConfigSet{ 47 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg1"}, Profiles: []latest.Profile{ 48 {Name: "p1", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}}, 49 {Name: "p2", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{Cluster: &latest.ClusterDetails{}}}}}, 50 }}, SourceFile: "path/to/cfg1"}, 51 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg2"}, Profiles: []latest.Profile{ 52 {Name: "p3", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}}, 53 {Name: "p4", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{}}}}}, 54 }}, SourceFile: "path/to/cfg2"}, 55 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Profiles: []latest.Profile{ 56 {Name: "p5", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}}, 57 }}, SourceFile: "path/to/cfg2"}, 58 }, 59 expected: `{"profiles":[` + 60 `{"name":"p1","path":"path/to/cfg1","module":"cfg1"},` + 61 `{"name":"p2","path":"path/to/cfg1","module":"cfg1"},` + 62 `{"name":"p3","path":"path/to/cfg2","module":"cfg2"},` + 63 `{"name":"p4","path":"path/to/cfg2","module":"cfg2"},` + 64 `{"name":"p5","path":"path/to/cfg2"}` + 65 "]}\n", 66 }, 67 { 68 description: "print all profiles for one module", 69 configSet: parser.SkaffoldConfigSet{ 70 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg1"}, Profiles: []latest.Profile{ 71 {Name: "p1", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}}, 72 {Name: "p2", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{Cluster: &latest.ClusterDetails{}}}}}, 73 }}, SourceFile: "path/to/cfg1"}, 74 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg2"}, Profiles: []latest.Profile{ 75 {Name: "p3", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}}, 76 {Name: "p4", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{}}}}}, 77 }}, SourceFile: "path/to/cfg2"}, 78 }, 79 expected: `{"profiles":[` + 80 `{"name":"p3","path":"path/to/cfg2","module":"cfg2"},` + 81 `{"name":"p4","path":"path/to/cfg2","module":"cfg2"}` + 82 "]}\n", 83 module: []string{"cfg2"}, 84 }, 85 { 86 description: "print all profiles for one module and gcb build-env", 87 configSet: parser.SkaffoldConfigSet{ 88 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg1"}, Profiles: []latest.Profile{ 89 {Name: "p1", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}}, 90 {Name: "p2", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{Cluster: &latest.ClusterDetails{}}}}}, 91 }}, SourceFile: "path/to/cfg1"}, 92 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg2"}, Profiles: []latest.Profile{ 93 {Name: "p3", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}}, 94 {Name: "p4", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{}}}}}, 95 }}, SourceFile: "path/to/cfg2"}, 96 }, 97 expected: `{"profiles":[` + 98 `{"name":"p4","path":"path/to/cfg2","module":"cfg2"}` + 99 "]}\n", 100 module: []string{"cfg2"}, 101 buildEnv: inspect.BuildEnvs.GoogleCloudBuild, 102 }, 103 { 104 description: "actionable error", 105 err: sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")), 106 expected: `{"errorCode":"CONFIG_FILE_NOT_FOUND_ERR","errorMessage":"unable to find configuration file \"path/to/skaffold.yaml\": failed to read file : \"skaffold.yaml\". Check that the specified configuration file exists at \"path/to/skaffold.yaml\"."}` + "\n", 107 }, 108 { 109 description: "generic error", 110 err: errors.New("some error occurred"), 111 expected: `{"errorCode":"INSPECT_UNKNOWN_ERR","errorMessage":"some error occurred"}` + "\n", 112 }, 113 } 114 115 for _, test := range tests { 116 testutil.Run(t, test.description, func(t *testutil.T) { 117 t.Override(&inspect.GetConfigSet, func(ctx context.Context, opts config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) { 118 if len(opts.ConfigurationFilter) == 0 { 119 return test.configSet, test.err 120 } 121 var set parser.SkaffoldConfigSet 122 if stringslice.Contains(opts.ConfigurationFilter, "cfg1") { 123 set = append(set, test.configSet[0]) 124 } 125 if stringslice.Contains(opts.ConfigurationFilter, "cfg2") { 126 set = append(set, test.configSet[1]) 127 } 128 return set, test.err 129 }) 130 var buf bytes.Buffer 131 err := PrintProfilesList(context.Background(), &buf, inspect.Options{OutFormat: "json", Modules: test.module, ProfilesOptions: inspect.ProfilesOptions{BuildEnv: test.buildEnv}}) 132 t.CheckError(test.err != nil, err) 133 t.CheckDeepEqual(test.expected, buf.String()) 134 }) 135 } 136 }