|
| 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/parser" |
| 28 | + sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/errors" |
| 29 | + v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" |
| 30 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 31 | +) |
| 32 | + |
| 33 | +func TestPrintModulesList(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + description string |
| 36 | + configSet parser.SkaffoldConfigSet |
| 37 | + err error |
| 38 | + expected string |
| 39 | + }{ |
| 40 | + { |
| 41 | + description: "print modules", |
| 42 | + configSet: parser.SkaffoldConfigSet{ |
| 43 | + &parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{Metadata: v1.Metadata{Name: "cfg1"}}, SourceFile: "path/to/cfg1"}, |
| 44 | + &parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{Metadata: v1.Metadata{Name: "cfg2"}}, SourceFile: "path/to/cfg2"}, |
| 45 | + }, |
| 46 | + expected: `{"modules":[{"name":"cfg1","path":"path/to/cfg1"},{"name":"cfg2","path":"path/to/cfg2"}]}` + "\n", |
| 47 | + }, |
| 48 | + { |
| 49 | + description: "actionable error", |
| 50 | + err: sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")), |
| 51 | + 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", |
| 52 | + }, |
| 53 | + { |
| 54 | + description: "generic error", |
| 55 | + err: errors.New("some error occurred"), |
| 56 | + expected: `{"errorCode":"UNKNOWN_ERROR","errorMessage":"some error occurred"}` + "\n", |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, test := range tests { |
| 61 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 62 | + t.Override(&getConfigSetFunc, func(config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) { |
| 63 | + return test.configSet, test.err |
| 64 | + }) |
| 65 | + var buf bytes.Buffer |
| 66 | + err := PrintModulesList(context.Background(), &buf, Options{OutFormat: "json"}) |
| 67 | + t.CheckNoError(err) |
| 68 | + t.CheckDeepEqual(test.expected, buf.String()) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments