github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/schema/errors/errors_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 errors 18 19 import ( 20 "errors" 21 "fmt" 22 "testing" 23 24 sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors" 25 "github.com/GoogleContainerTools/skaffold/proto/enums" 26 "github.com/GoogleContainerTools/skaffold/proto/v1" 27 "github.com/GoogleContainerTools/skaffold/testutil" 28 ) 29 30 func TestErrors(t *testing.T) { 31 tests := []struct { 32 err error 33 errCode enums.StatusCode 34 suggestionCode enums.SuggestionCode 35 }{ 36 { 37 err: ConfigParsingError(nil), 38 errCode: proto.StatusCode_CONFIG_FILE_PARSING_ERR, 39 }, 40 { 41 err: MainConfigFileNotFoundErr("", nil), 42 errCode: proto.StatusCode_CONFIG_FILE_NOT_FOUND_ERR, 43 suggestionCode: proto.SuggestionCode_CONFIG_CHECK_FILE_PATH, 44 }, 45 { 46 err: DependencyConfigFileNotFoundErr("", "", nil), 47 errCode: proto.StatusCode_CONFIG_DEPENDENCY_NOT_FOUND_ERR, 48 suggestionCode: proto.SuggestionCode_CONFIG_CHECK_DEPENDENCY_DEFINITION, 49 }, 50 { 51 err: BadConfigFilterErr(nil), 52 errCode: proto.StatusCode_CONFIG_BAD_FILTER_ERR, 53 suggestionCode: proto.SuggestionCode_CONFIG_CHECK_FILTER, 54 }, 55 { 56 err: ZeroConfigsParsedErr(""), 57 errCode: proto.StatusCode_CONFIG_ZERO_FOUND_ERR, 58 }, 59 { 60 err: DuplicateConfigNamesInSameFileErr("", ""), 61 errCode: proto.StatusCode_CONFIG_DUPLICATE_NAMES_SAME_FILE_ERR, 62 suggestionCode: proto.SuggestionCode_CONFIG_CHANGE_NAMES, 63 }, 64 { 65 err: DuplicateConfigNamesAcrossFilesErr("", "", ""), 66 errCode: proto.StatusCode_CONFIG_DUPLICATE_NAMES_ACROSS_FILES_ERR, 67 suggestionCode: proto.SuggestionCode_CONFIG_CHANGE_NAMES, 68 }, 69 { 70 err: ConfigProfileActivationErr("", "", nil), 71 errCode: proto.StatusCode_CONFIG_APPLY_PROFILES_ERR, 72 suggestionCode: proto.SuggestionCode_CONFIG_CHECK_PROFILE_DEFINITION, 73 }, 74 { 75 err: ConfigSetDefaultValuesErr("", "", nil), 76 errCode: proto.StatusCode_CONFIG_DEFAULT_VALUES_ERR, 77 }, 78 { 79 err: ConfigSetAbsFilePathsErr("", "", nil), 80 errCode: proto.StatusCode_CONFIG_FILE_PATHS_SUBSTITUTION_ERR, 81 }, 82 { 83 err: ConfigProfileConflictErr("", ""), 84 errCode: proto.StatusCode_CONFIG_MULTI_IMPORT_PROFILE_CONFLICT_ERR, 85 suggestionCode: proto.SuggestionCode_CONFIG_CHECK_DEPENDENCY_PROFILES_SELECTION, 86 }, 87 } 88 89 for i, test := range tests { 90 testutil.Run(t, fmt.Sprintf("test error definition %d", i), func(t *testutil.T) { 91 var e sErrors.Error 92 if errors.As(test.err, &e) { 93 t.CheckDeepEqual(e.StatusCode(), test.errCode) 94 if test.suggestionCode != proto.SuggestionCode_NIL { 95 t.CheckDeepEqual(1, len(e.Suggestions())) 96 t.CheckDeepEqual(test.suggestionCode, e.Suggestions()[0].SuggestionCode) 97 } 98 } else { 99 t.Fail() 100 } 101 }) 102 } 103 }