github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/lint/rules/chartfile_test.go (about) 1 /* 2 Copyright The Helm 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 rules 18 19 import ( 20 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24 25 "github.com/pkg/errors" 26 27 "helm.sh/helm/pkg/chart" 28 "helm.sh/helm/pkg/chartutil" 29 "helm.sh/helm/pkg/lint/support" 30 ) 31 32 const ( 33 badChartDir = "testdata/badchartfile" 34 goodChartDir = "testdata/goodone" 35 ) 36 37 var ( 38 badChartFilePath = filepath.Join(badChartDir, "Chart.yaml") 39 goodChartFilePath = filepath.Join(goodChartDir, "Chart.yaml") 40 nonExistingChartFilePath = filepath.Join(os.TempDir(), "Chart.yaml") 41 ) 42 43 var badChart, _ = chartutil.LoadChartfile(badChartFilePath) 44 var goodChart, _ = chartutil.LoadChartfile(goodChartFilePath) 45 46 // Validation functions Test 47 func TestValidateChartYamlNotDirectory(t *testing.T) { 48 _ = os.Mkdir(nonExistingChartFilePath, os.ModePerm) 49 defer os.Remove(nonExistingChartFilePath) 50 51 err := validateChartYamlNotDirectory(nonExistingChartFilePath) 52 if err == nil { 53 t.Errorf("validateChartYamlNotDirectory to return a linter error, got no error") 54 } 55 } 56 57 func TestValidateChartYamlFormat(t *testing.T) { 58 err := validateChartYamlFormat(errors.New("Read error")) 59 if err == nil { 60 t.Errorf("validateChartYamlFormat to return a linter error, got no error") 61 } 62 63 err = validateChartYamlFormat(nil) 64 if err != nil { 65 t.Errorf("validateChartYamlFormat to return no error, got a linter error") 66 } 67 } 68 69 func TestValidateChartName(t *testing.T) { 70 err := validateChartName(badChart) 71 if err == nil { 72 t.Errorf("validateChartName to return a linter error, got no error") 73 } 74 } 75 76 func TestValidateChartNameDirMatch(t *testing.T) { 77 err := validateChartNameDirMatch(goodChartDir, goodChart) 78 if err != nil { 79 t.Errorf("validateChartNameDirMatch to return no error, gor a linter error") 80 } 81 // It has not name 82 err = validateChartNameDirMatch(badChartDir, badChart) 83 if err == nil { 84 t.Errorf("validatechartnamedirmatch to return a linter error, got no error") 85 } 86 87 // Wrong path 88 err = validateChartNameDirMatch(badChartDir, goodChart) 89 if err == nil { 90 t.Errorf("validatechartnamedirmatch to return a linter error, got no error") 91 } 92 } 93 94 func TestValidateChartVersion(t *testing.T) { 95 var failTest = []struct { 96 Version string 97 ErrorMsg string 98 }{ 99 {"", "version is required"}, 100 {"0", "0 is less than or equal to 0"}, 101 {"waps", "'waps' is not a valid SemVer"}, 102 {"-3", "'-3' is not a valid SemVer"}, 103 } 104 105 var successTest = []string{"0.0.1", "0.0.1+build", "0.0.1-beta"} 106 107 for _, test := range failTest { 108 badChart.Version = test.Version 109 err := validateChartVersion(badChart) 110 if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) { 111 t.Errorf("validateChartVersion(%s) to return \"%s\", got no error", test.Version, test.ErrorMsg) 112 } 113 } 114 115 for _, version := range successTest { 116 badChart.Version = version 117 err := validateChartVersion(badChart) 118 if err != nil { 119 t.Errorf("validateChartVersion(%s) to return no error, got a linter error", version) 120 } 121 } 122 } 123 124 func TestValidateChartMaintainer(t *testing.T) { 125 var failTest = []struct { 126 Name string 127 Email string 128 ErrorMsg string 129 }{ 130 {"", "", "each maintainer requires a name"}, 131 {"", "test@test.com", "each maintainer requires a name"}, 132 {"John Snow", "wrongFormatEmail.com", "invalid email"}, 133 } 134 135 var successTest = []struct { 136 Name string 137 Email string 138 }{ 139 {"John Snow", ""}, 140 {"John Snow", "john@winterfell.com"}, 141 } 142 143 for _, test := range failTest { 144 badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}} 145 err := validateChartMaintainer(badChart) 146 if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) { 147 t.Errorf("validateChartMaintainer(%s, %s) to return \"%s\", got no error", test.Name, test.Email, test.ErrorMsg) 148 } 149 } 150 151 for _, test := range successTest { 152 badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}} 153 err := validateChartMaintainer(badChart) 154 if err != nil { 155 t.Errorf("validateChartMaintainer(%s, %s) to return no error, got %s", test.Name, test.Email, err.Error()) 156 } 157 } 158 } 159 160 func TestValidateChartSources(t *testing.T) { 161 var failTest = []string{"", "RiverRun", "john@winterfell", "riverrun.io"} 162 var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish"} 163 for _, test := range failTest { 164 badChart.Sources = []string{test} 165 err := validateChartSources(badChart) 166 if err == nil || !strings.Contains(err.Error(), "invalid source URL") { 167 t.Errorf("validateChartSources(%s) to return \"invalid source URL\", got no error", test) 168 } 169 } 170 171 for _, test := range successTest { 172 badChart.Sources = []string{test} 173 err := validateChartSources(badChart) 174 if err != nil { 175 t.Errorf("validateChartSources(%s) to return no error, got %s", test, err.Error()) 176 } 177 } 178 } 179 180 func TestValidateChartIconPresence(t *testing.T) { 181 err := validateChartIconPresence(badChart) 182 if err == nil { 183 t.Errorf("validateChartIconPresence to return a linter error, got no error") 184 } 185 } 186 187 func TestValidateChartIconURL(t *testing.T) { 188 var failTest = []string{"RiverRun", "john@winterfell", "riverrun.io"} 189 var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish.png"} 190 for _, test := range failTest { 191 badChart.Icon = test 192 err := validateChartIconURL(badChart) 193 if err == nil || !strings.Contains(err.Error(), "invalid icon URL") { 194 t.Errorf("validateChartIconURL(%s) to return \"invalid icon URL\", got no error", test) 195 } 196 } 197 198 for _, test := range successTest { 199 badChart.Icon = test 200 err := validateChartSources(badChart) 201 if err != nil { 202 t.Errorf("validateChartIconURL(%s) to return no error, got %s", test, err.Error()) 203 } 204 } 205 } 206 207 func TestChartfile(t *testing.T) { 208 linter := support.Linter{ChartDir: badChartDir} 209 Chartfile(&linter) 210 msgs := linter.Messages 211 212 if len(msgs) != 7 { 213 t.Errorf("Expected 7 errors, got %d", len(msgs)) 214 } 215 216 if !strings.Contains(msgs[0].Err.Error(), "name is required") { 217 t.Errorf("Unexpected message 0: %s", msgs[0].Err) 218 } 219 220 if !strings.Contains(msgs[1].Err.Error(), "directory name (badchartfile) and chart name () must be the same") { 221 t.Errorf("Unexpected message 1: %s", msgs[1].Err) 222 } 223 224 if !strings.Contains(msgs[2].Err.Error(), "apiVersion is required. The value must be either \"v1\" or \"v2\"") { 225 t.Errorf("Unexpected message 2: %s", msgs[2].Err) 226 } 227 228 if !strings.Contains(msgs[3].Err.Error(), "version 0.0.0 is less than or equal to 0") { 229 t.Errorf("Unexpected message 3: %s", msgs[3].Err) 230 } 231 232 if !strings.Contains(msgs[4].Err.Error(), "icon is recommended") { 233 t.Errorf("Unexpected message 4: %s", msgs[4].Err) 234 } 235 236 if !strings.Contains(msgs[5].Err.Error(), "chart type is not valid in apiVersion") { 237 t.Errorf("Unexpected message 5: %s", msgs[5].Err) 238 } 239 240 if !strings.Contains(msgs[6].Err.Error(), "dependencies are not valid in the Chart file with apiVersion") { 241 t.Errorf("Unexpected message 6: %s", msgs[6].Err) 242 } 243 244 }