github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/tests/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 TestPrintTestsList(t *testing.T) { 36 tests := []struct { 37 description string 38 profiles []string 39 module []string 40 err error 41 expected string 42 }{ 43 { 44 description: "print all tests", 45 expected: `{"tests":[{"testType":"structure-test","structureTest":"structure-test-default","structureTestArgs":null},{"testType":"custom-test","Command":"custom-test-default","TimeoutSeconds":0,"Dependencies":null}]}` + "\n", 46 }, 47 { 48 description: "print all tests for one module", 49 expected: `{"tests":[{"testType":"structure-test","structureTest":"structure-test-default","structureTestArgs":null}]}` + "\n", 50 module: []string{"cfg1"}, 51 }, 52 { 53 description: "print all tests for two activated profiles", 54 expected: `{"tests":[{"testType":"custom-test","Command":"custom-test-profile","TimeoutSeconds":0,"Dependencies":null},{"testType":"structure-test","structureTest":"structure-test-profile","structureTestArgs":null}]}` + "\n", 55 profiles: []string{"custom-test", "structure-test"}, 56 }, 57 { 58 description: "print all tests for one module and an activated profile", 59 expected: `{"tests":[{"testType":"custom-test","Command":"custom-test-profile","TimeoutSeconds":0,"Dependencies":null}]}` + "\n", 60 module: []string{"cfg1"}, 61 profiles: []string{"custom-test"}, 62 }, 63 { 64 description: "actionable error", 65 err: sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")), 66 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", 67 }, 68 { 69 description: "generic error", 70 err: errors.New("some error occurred"), 71 expected: `{"errorCode":"INSPECT_UNKNOWN_ERR","errorMessage":"some error occurred"}` + "\n", 72 }, 73 } 74 75 for _, test := range tests { 76 testutil.Run(t, test.description, func(t *testutil.T) { 77 configSet := parser.SkaffoldConfigSet{ 78 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{ 79 Metadata: latest.Metadata{Name: "cfg1"}, 80 Pipeline: latest.Pipeline{Test: []*latest.TestCase{{StructureTests: []string{"structure-test-default"}}}}, 81 Profiles: []latest.Profile{ 82 {Name: "custom-test", Pipeline: latest.Pipeline{Test: []*latest.TestCase{{CustomTests: []latest.CustomTest{{Command: "custom-test-profile"}}}}}}}, 83 }, SourceFile: "path/to/cfg1"}, 84 &parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{ 85 Metadata: latest.Metadata{Name: "cfg2"}, 86 Pipeline: latest.Pipeline{Test: []*latest.TestCase{{CustomTests: []latest.CustomTest{{Command: "custom-test-default"}}}}}, 87 Profiles: []latest.Profile{ 88 {Name: "structure-test", Pipeline: latest.Pipeline{Test: []*latest.TestCase{{StructureTests: []string{"structure-test-profile"}}}}}}, 89 }, SourceFile: "path/to/cfg2"}, 90 } 91 t.Override(&inspect.GetConfigSet, func(_ context.Context, opts config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) { 92 // mock profile activation 93 var set parser.SkaffoldConfigSet 94 for _, c := range configSet { 95 if len(opts.ConfigurationFilter) > 0 && !stringslice.Contains(opts.ConfigurationFilter, c.Metadata.Name) { 96 continue 97 } 98 for _, pName := range opts.Profiles { 99 for _, profile := range c.Profiles { 100 if profile.Name != pName { 101 continue 102 } 103 c.Test = profile.Test 104 } 105 } 106 set = append(set, c) 107 } 108 return set, test.err 109 }) 110 var buf bytes.Buffer 111 err := PrintTestsList(context.Background(), &buf, inspect.Options{ 112 OutFormat: "json", Modules: test.module, Profiles: test.profiles}) 113 t.CheckError(test.err != nil, err) 114 t.CheckDeepEqual(test.expected, buf.String()) 115 }) 116 } 117 }