github.com/y-taka-23/helm@v2.8.0+incompatible/pkg/repo/chartrepo_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 repo 18 19 import ( 20 "io/ioutil" 21 "net/http" 22 "net/http/httptest" 23 "os" 24 "path/filepath" 25 "reflect" 26 "strings" 27 "testing" 28 "time" 29 30 "k8s.io/helm/pkg/getter" 31 "k8s.io/helm/pkg/helm/environment" 32 "k8s.io/helm/pkg/proto/hapi/chart" 33 ) 34 35 const ( 36 testRepository = "testdata/repository" 37 testURL = "http://example-charts.com" 38 ) 39 40 func TestLoadChartRepository(t *testing.T) { 41 r, err := NewChartRepository(&Entry{ 42 Name: testRepository, 43 URL: testURL, 44 }, getter.All(environment.EnvSettings{})) 45 if err != nil { 46 t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) 47 } 48 49 if err := r.Load(); err != nil { 50 t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) 51 } 52 53 paths := []string{ 54 filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), 55 filepath.Join(testRepository, "sprocket-1.1.0.tgz"), 56 filepath.Join(testRepository, "sprocket-1.2.0.tgz"), 57 filepath.Join(testRepository, "universe/zarthal-1.0.0.tgz"), 58 } 59 60 if r.Config.Name != testRepository { 61 t.Errorf("Expected %s as Name but got %s", testRepository, r.Config.Name) 62 } 63 64 if !reflect.DeepEqual(r.ChartPaths, paths) { 65 t.Errorf("Expected %#v but got %#v\n", paths, r.ChartPaths) 66 } 67 68 if r.Config.URL != testURL { 69 t.Errorf("Expected url for chart repository to be %s but got %s", testURL, r.Config.URL) 70 } 71 } 72 73 func TestIndex(t *testing.T) { 74 r, err := NewChartRepository(&Entry{ 75 Name: testRepository, 76 URL: testURL, 77 }, getter.All(environment.EnvSettings{})) 78 if err != nil { 79 t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) 80 } 81 82 if err := r.Load(); err != nil { 83 t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) 84 } 85 86 err = r.Index() 87 if err != nil { 88 t.Errorf("Error performing index: %v\n", err) 89 } 90 91 tempIndexPath := filepath.Join(testRepository, indexPath) 92 actual, err := LoadIndexFile(tempIndexPath) 93 defer os.Remove(tempIndexPath) // clean up 94 if err != nil { 95 t.Errorf("Error loading index file %v", err) 96 } 97 verifyIndex(t, actual) 98 99 // Re-index and test again. 100 err = r.Index() 101 if err != nil { 102 t.Errorf("Error performing re-index: %s\n", err) 103 } 104 second, err := LoadIndexFile(tempIndexPath) 105 if err != nil { 106 t.Errorf("Error re-loading index file %v", err) 107 } 108 verifyIndex(t, second) 109 } 110 111 func verifyIndex(t *testing.T, actual *IndexFile) { 112 var empty time.Time 113 if actual.Generated == empty { 114 t.Errorf("Generated should be greater than 0: %s", actual.Generated) 115 } 116 117 if actual.APIVersion != APIVersionV1 { 118 t.Error("Expected v1 API") 119 } 120 121 entries := actual.Entries 122 if numEntries := len(entries); numEntries != 3 { 123 t.Errorf("Expected 3 charts to be listed in index file but got %v", numEntries) 124 } 125 126 expects := map[string]ChartVersions{ 127 "frobnitz": { 128 { 129 Metadata: &chart.Metadata{ 130 Name: "frobnitz", 131 Version: "1.2.3", 132 }, 133 }, 134 }, 135 "sprocket": { 136 { 137 Metadata: &chart.Metadata{ 138 Name: "sprocket", 139 Version: "1.2.0", 140 }, 141 }, 142 { 143 Metadata: &chart.Metadata{ 144 Name: "sprocket", 145 Version: "1.1.0", 146 }, 147 }, 148 }, 149 "zarthal": { 150 { 151 Metadata: &chart.Metadata{ 152 Name: "zarthal", 153 Version: "1.0.0", 154 }, 155 }, 156 }, 157 } 158 159 for name, versions := range expects { 160 got, ok := entries[name] 161 if !ok { 162 t.Errorf("Could not find %q entry", name) 163 continue 164 } 165 if len(versions) != len(got) { 166 t.Errorf("Expected %d versions, got %d", len(versions), len(got)) 167 continue 168 } 169 for i, e := range versions { 170 g := got[i] 171 if e.Name != g.Name { 172 t.Errorf("Expected %q, got %q", e.Name, g.Name) 173 } 174 if e.Version != g.Version { 175 t.Errorf("Expected %q, got %q", e.Version, g.Version) 176 } 177 if len(g.Keywords) != 3 { 178 t.Error("Expected 3 keyrwords.") 179 } 180 if len(g.Maintainers) != 2 { 181 t.Error("Expected 2 maintainers.") 182 } 183 if g.Created == empty { 184 t.Error("Expected created to be non-empty") 185 } 186 if g.Description == "" { 187 t.Error("Expected description to be non-empty") 188 } 189 if g.Home == "" { 190 t.Error("Expected home to be non-empty") 191 } 192 if g.Digest == "" { 193 t.Error("Expected digest to be non-empty") 194 } 195 if len(g.URLs) != 1 { 196 t.Error("Expected exactly 1 URL") 197 } 198 } 199 } 200 } 201 202 // startLocalServerForTests Start the local helm server 203 func startLocalServerForTests(handler http.Handler) (*httptest.Server, error) { 204 if handler == nil { 205 fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml") 206 if err != nil { 207 return nil, err 208 } 209 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 210 w.Write(fileBytes) 211 }) 212 } 213 214 return httptest.NewServer(handler), nil 215 } 216 217 func TestFindChartInRepoURL(t *testing.T) { 218 srv, err := startLocalServerForTests(nil) 219 if err != nil { 220 t.Fatal(err) 221 } 222 defer srv.Close() 223 224 chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) 225 if err != nil { 226 t.Errorf("%s", err) 227 } 228 if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz" { 229 t.Errorf("%s is not the valid URL", chartURL) 230 } 231 232 chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) 233 if err != nil { 234 t.Errorf("%s", err) 235 } 236 if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.1.0.tgz" { 237 t.Errorf("%s is not the valid URL", chartURL) 238 } 239 } 240 241 func TestErrorFindChartInRepoURL(t *testing.T) { 242 _, err := FindChartInRepoURL("http://someserver/something", "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) 243 if err == nil { 244 t.Errorf("Expected error for bad chart URL, but did not get any errors") 245 } 246 if err != nil && !strings.Contains(err.Error(), `Looks like "http://someserver/something" is not a valid chart repository or cannot be reached: Get http://someserver/something/index.yaml`) { 247 t.Errorf("Expected error for bad chart URL, but got a different error (%v)", err) 248 } 249 250 srv, err := startLocalServerForTests(nil) 251 if err != nil { 252 t.Fatal(err) 253 } 254 defer srv.Close() 255 256 _, err = FindChartInRepoURL(srv.URL, "nginx1", "", "", "", "", getter.All(environment.EnvSettings{})) 257 if err == nil { 258 t.Errorf("Expected error for chart not found, but did not get any errors") 259 } 260 if err != nil && err.Error() != `chart "nginx1" not found in `+srv.URL+` repository` { 261 t.Errorf("Expected error for chart not found, but got a different error (%v)", err) 262 } 263 264 _, err = FindChartInRepoURL(srv.URL, "nginx1", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) 265 if err == nil { 266 t.Errorf("Expected error for chart not found, but did not get any errors") 267 } 268 if err != nil && err.Error() != `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository` { 269 t.Errorf("Expected error for chart not found, but got a different error (%v)", err) 270 } 271 272 _, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", "", "", "", "", getter.All(environment.EnvSettings{})) 273 if err == nil { 274 t.Errorf("Expected error for no chart URLs available, but did not get any errors") 275 } 276 if err != nil && err.Error() != `chart "chartWithNoURL" has no downloadable URLs` { 277 t.Errorf("Expected error for chart not found, but got a different error (%v)", err) 278 } 279 } 280 281 func TestResolveReferenceURL(t *testing.T) { 282 chartURL, err := ResolveReferenceURL("http://localhost:8123/charts/", "nginx-0.2.0.tgz") 283 if err != nil { 284 t.Errorf("%s", err) 285 } 286 if chartURL != "http://localhost:8123/charts/nginx-0.2.0.tgz" { 287 t.Errorf("%s", chartURL) 288 } 289 290 chartURL, err = ResolveReferenceURL("http://localhost:8123", "https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz") 291 if err != nil { 292 t.Errorf("%s", err) 293 } 294 if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz" { 295 t.Errorf("%s", chartURL) 296 } 297 }