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