github.com/y-taka-23/helm@v2.8.0+incompatible/pkg/repo/index_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 "os" 22 "path/filepath" 23 "testing" 24 25 "k8s.io/helm/pkg/getter" 26 "k8s.io/helm/pkg/helm/environment" 27 "k8s.io/helm/pkg/proto/hapi/chart" 28 ) 29 30 const ( 31 testfile = "testdata/local-index.yaml" 32 unorderedTestfile = "testdata/local-index-unordered.yaml" 33 testRepo = "test-repo" 34 ) 35 36 func TestIndexFile(t *testing.T) { 37 i := NewIndexFile() 38 i.Add(&chart.Metadata{Name: "clipper", Version: "0.1.0"}, "clipper-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890") 39 i.Add(&chart.Metadata{Name: "cutter", Version: "0.1.1"}, "cutter-0.1.1.tgz", "http://example.com/charts", "sha256:1234567890abc") 40 i.Add(&chart.Metadata{Name: "cutter", Version: "0.1.0"}, "cutter-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890abc") 41 i.Add(&chart.Metadata{Name: "cutter", Version: "0.2.0"}, "cutter-0.2.0.tgz", "http://example.com/charts", "sha256:1234567890abc") 42 i.SortEntries() 43 44 if i.APIVersion != APIVersionV1 { 45 t.Error("Expected API version v1") 46 } 47 48 if len(i.Entries) != 2 { 49 t.Errorf("Expected 2 charts. Got %d", len(i.Entries)) 50 } 51 52 if i.Entries["clipper"][0].Name != "clipper" { 53 t.Errorf("Expected clipper, got %s", i.Entries["clipper"][0].Name) 54 } 55 56 if len(i.Entries["cutter"]) != 3 { 57 t.Error("Expected two cutters.") 58 } 59 60 // Test that the sort worked. 0.2 should be at the first index for Cutter. 61 if v := i.Entries["cutter"][0].Version; v != "0.2.0" { 62 t.Errorf("Unexpected first version: %s", v) 63 } 64 } 65 66 func TestLoadIndex(t *testing.T) { 67 b, err := ioutil.ReadFile(testfile) 68 if err != nil { 69 t.Fatal(err) 70 } 71 i, err := loadIndex(b) 72 if err != nil { 73 t.Fatal(err) 74 } 75 verifyLocalIndex(t, i) 76 } 77 78 func TestLoadIndexFile(t *testing.T) { 79 i, err := LoadIndexFile(testfile) 80 if err != nil { 81 t.Fatal(err) 82 } 83 verifyLocalIndex(t, i) 84 } 85 86 func TestLoadUnorderedIndex(t *testing.T) { 87 b, err := ioutil.ReadFile(unorderedTestfile) 88 if err != nil { 89 t.Fatal(err) 90 } 91 i, err := loadIndex(b) 92 if err != nil { 93 t.Fatal(err) 94 } 95 verifyLocalIndex(t, i) 96 } 97 98 func TestMerge(t *testing.T) { 99 ind1 := NewIndexFile() 100 ind1.Add(&chart.Metadata{ 101 Name: "dreadnought", 102 Version: "0.1.0", 103 }, "dreadnought-0.1.0.tgz", "http://example.com", "aaaa") 104 105 ind2 := NewIndexFile() 106 ind2.Add(&chart.Metadata{ 107 Name: "dreadnought", 108 Version: "0.2.0", 109 }, "dreadnought-0.2.0.tgz", "http://example.com", "aaaabbbb") 110 ind2.Add(&chart.Metadata{ 111 Name: "doughnut", 112 Version: "0.2.0", 113 }, "doughnut-0.2.0.tgz", "http://example.com", "ccccbbbb") 114 115 ind1.Merge(ind2) 116 117 if len(ind1.Entries) != 2 { 118 t.Errorf("Expected 2 entries, got %d", len(ind1.Entries)) 119 vs := ind1.Entries["dreadnaught"] 120 if len(vs) != 2 { 121 t.Errorf("Expected 2 versions, got %d", len(vs)) 122 } 123 v := vs[0] 124 if v.Version != "0.2.0" { 125 t.Errorf("Expected %q version to be 0.2.0, got %s", v.Name, v.Version) 126 } 127 } 128 129 } 130 131 func TestDownloadIndexFile(t *testing.T) { 132 srv, err := startLocalServerForTests(nil) 133 if err != nil { 134 t.Fatal(err) 135 } 136 defer srv.Close() 137 138 dirName, err := ioutil.TempDir("", "tmp") 139 if err != nil { 140 t.Fatal(err) 141 } 142 defer os.RemoveAll(dirName) 143 144 indexFilePath := filepath.Join(dirName, testRepo+"-index.yaml") 145 r, err := NewChartRepository(&Entry{ 146 Name: testRepo, 147 URL: srv.URL, 148 Cache: indexFilePath, 149 }, getter.All(environment.EnvSettings{})) 150 if err != nil { 151 t.Errorf("Problem creating chart repository from %s: %v", testRepo, err) 152 } 153 154 if err := r.DownloadIndexFile(""); err != nil { 155 t.Errorf("%#v", err) 156 } 157 158 if _, err := os.Stat(indexFilePath); err != nil { 159 t.Errorf("error finding created index file: %#v", err) 160 } 161 162 b, err := ioutil.ReadFile(indexFilePath) 163 if err != nil { 164 t.Errorf("error reading index file: %#v", err) 165 } 166 167 i, err := loadIndex(b) 168 if err != nil { 169 t.Errorf("Index %q failed to parse: %s", testfile, err) 170 return 171 } 172 173 verifyLocalIndex(t, i) 174 } 175 176 func verifyLocalIndex(t *testing.T, i *IndexFile) { 177 numEntries := len(i.Entries) 178 if numEntries != 3 { 179 t.Errorf("Expected 3 entries in index file but got %d", numEntries) 180 } 181 182 alpine, ok := i.Entries["alpine"] 183 if !ok { 184 t.Errorf("'alpine' section not found.") 185 return 186 } 187 188 if l := len(alpine); l != 1 { 189 t.Errorf("'alpine' should have 1 chart, got %d", l) 190 return 191 } 192 193 nginx, ok := i.Entries["nginx"] 194 if !ok || len(nginx) != 2 { 195 t.Error("Expected 2 nginx entries") 196 return 197 } 198 199 expects := []*ChartVersion{ 200 { 201 Metadata: &chart.Metadata{ 202 Name: "alpine", 203 Description: "string", 204 Version: "1.0.0", 205 Keywords: []string{"linux", "alpine", "small", "sumtin"}, 206 Home: "https://github.com/something", 207 }, 208 URLs: []string{ 209 "https://kubernetes-charts.storage.googleapis.com/alpine-1.0.0.tgz", 210 "http://storage2.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz", 211 }, 212 Digest: "sha256:1234567890abcdef", 213 }, 214 { 215 Metadata: &chart.Metadata{ 216 Name: "nginx", 217 Description: "string", 218 Version: "0.2.0", 219 Keywords: []string{"popular", "web server", "proxy"}, 220 Home: "https://github.com/something/else", 221 }, 222 URLs: []string{ 223 "https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz", 224 }, 225 Digest: "sha256:1234567890abcdef", 226 }, 227 { 228 Metadata: &chart.Metadata{ 229 Name: "nginx", 230 Description: "string", 231 Version: "0.1.0", 232 Keywords: []string{"popular", "web server", "proxy"}, 233 Home: "https://github.com/something", 234 }, 235 URLs: []string{ 236 "https://kubernetes-charts.storage.googleapis.com/nginx-0.1.0.tgz", 237 }, 238 Digest: "sha256:1234567890abcdef", 239 }, 240 } 241 tests := []*ChartVersion{alpine[0], nginx[0], nginx[1]} 242 243 for i, tt := range tests { 244 expect := expects[i] 245 if tt.Name != expect.Name { 246 t.Errorf("Expected name %q, got %q", expect.Name, tt.Name) 247 } 248 if tt.Description != expect.Description { 249 t.Errorf("Expected description %q, got %q", expect.Description, tt.Description) 250 } 251 if tt.Version != expect.Version { 252 t.Errorf("Expected version %q, got %q", expect.Version, tt.Version) 253 } 254 if tt.Digest != expect.Digest { 255 t.Errorf("Expected digest %q, got %q", expect.Digest, tt.Digest) 256 } 257 if tt.Home != expect.Home { 258 t.Errorf("Expected home %q, got %q", expect.Home, tt.Home) 259 } 260 261 for i, url := range tt.URLs { 262 if url != expect.URLs[i] { 263 t.Errorf("Expected URL %q, got %q", expect.URLs[i], url) 264 } 265 } 266 for i, kw := range tt.Keywords { 267 if kw != expect.Keywords[i] { 268 t.Errorf("Expected keywords %q, got %q", expect.Keywords[i], kw) 269 } 270 } 271 } 272 } 273 274 func TestIndexDirectory(t *testing.T) { 275 dir := "testdata/repository" 276 index, err := IndexDirectory(dir, "http://localhost:8080") 277 if err != nil { 278 t.Fatal(err) 279 } 280 281 if l := len(index.Entries); l != 3 { 282 t.Fatalf("Expected 3 entries, got %d", l) 283 } 284 285 // Other things test the entry generation more thoroughly. We just test a 286 // few fields. 287 288 corpus := []struct{ chartName, downloadLink string }{ 289 {"frobnitz", "http://localhost:8080/frobnitz-1.2.3.tgz"}, 290 {"zarthal", "http://localhost:8080/universe/zarthal-1.0.0.tgz"}, 291 } 292 293 for _, test := range corpus { 294 cname := test.chartName 295 frobs, ok := index.Entries[cname] 296 if !ok { 297 t.Fatalf("Could not read chart %s", cname) 298 } 299 300 frob := frobs[0] 301 if len(frob.Digest) == 0 { 302 t.Errorf("Missing digest of file %s.", frob.Name) 303 } 304 if frob.URLs[0] != test.downloadLink { 305 t.Errorf("Unexpected URLs: %v", frob.URLs) 306 } 307 if frob.Name != cname { 308 t.Errorf("Expected %q, got %q", cname, frob.Name) 309 } 310 } 311 } 312 313 func TestLoadUnversionedIndex(t *testing.T) { 314 data, err := ioutil.ReadFile("testdata/unversioned-index.yaml") 315 if err != nil { 316 t.Fatal(err) 317 } 318 319 ind, err := loadUnversionedIndex(data) 320 if err != nil { 321 t.Fatal(err) 322 } 323 324 if l := len(ind.Entries); l != 2 { 325 t.Fatalf("Expected 2 entries, got %d", l) 326 } 327 328 if l := len(ind.Entries["mysql"]); l != 3 { 329 t.Fatalf("Expected 3 mysql versions, got %d", l) 330 } 331 } 332 333 func TestIndexAdd(t *testing.T) { 334 i := NewIndexFile() 335 i.Add(&chart.Metadata{Name: "clipper", Version: "0.1.0"}, "clipper-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890") 336 337 if i.Entries["clipper"][0].URLs[0] != "http://example.com/charts/clipper-0.1.0.tgz" { 338 t.Errorf("Expected http://example.com/charts/clipper-0.1.0.tgz, got %s", i.Entries["clipper"][0].URLs[0]) 339 } 340 341 i.Add(&chart.Metadata{Name: "alpine", Version: "0.1.0"}, "/home/charts/alpine-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890") 342 343 if i.Entries["alpine"][0].URLs[0] != "http://example.com/charts/alpine-0.1.0.tgz" { 344 t.Errorf("Expected http://example.com/charts/alpine-0.1.0.tgz, got %s", i.Entries["alpine"][0].URLs[0]) 345 } 346 347 i.Add(&chart.Metadata{Name: "deis", Version: "0.1.0"}, "/home/charts/deis-0.1.0.tgz", "http://example.com/charts/", "sha256:1234567890") 348 349 if i.Entries["deis"][0].URLs[0] != "http://example.com/charts/deis-0.1.0.tgz" { 350 t.Errorf("Expected http://example.com/charts/deis-0.1.0.tgz, got %s", i.Entries["deis"][0].URLs[0]) 351 } 352 }