github.com/cloudposse/helm@v2.2.3+incompatible/pkg/repo/repo_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 "testing" 20 21 const testRepositoriesFile = "testdata/repositories.yaml" 22 23 func TestRepoFile(t *testing.T) { 24 rf := NewRepoFile() 25 rf.Add( 26 &Entry{ 27 Name: "stable", 28 URL: "https://example.com/stable/charts", 29 Cache: "stable-index.yaml", 30 }, 31 &Entry{ 32 Name: "incubator", 33 URL: "https://example.com/incubator", 34 Cache: "incubator-index.yaml", 35 }, 36 ) 37 38 if len(rf.Repositories) != 2 { 39 t.Fatal("Expected 2 repositories") 40 } 41 42 if rf.Has("nosuchrepo") { 43 t.Error("Found nonexistent repo") 44 } 45 if !rf.Has("incubator") { 46 t.Error("incubator repo is missing") 47 } 48 49 stable := rf.Repositories[0] 50 if stable.Name != "stable" { 51 t.Error("stable is not named stable") 52 } 53 if stable.URL != "https://example.com/stable/charts" { 54 t.Error("Wrong URL for stable") 55 } 56 if stable.Cache != "stable-index.yaml" { 57 t.Error("Wrong cache name for stable") 58 } 59 } 60 61 func TestNewRepositoriesFile(t *testing.T) { 62 expects := NewRepoFile() 63 expects.Add( 64 &Entry{ 65 Name: "stable", 66 URL: "https://example.com/stable/charts", 67 Cache: "stable-index.yaml", 68 }, 69 &Entry{ 70 Name: "incubator", 71 URL: "https://example.com/incubator", 72 Cache: "incubator-index.yaml", 73 }, 74 ) 75 76 repofile, err := LoadRepositoriesFile(testRepositoriesFile) 77 if err != nil { 78 t.Errorf("%q could not be loaded: %s", testRepositoriesFile, err) 79 } 80 81 if len(expects.Repositories) != len(repofile.Repositories) { 82 t.Fatalf("Unexpected repo data: %#v", repofile.Repositories) 83 } 84 85 for i, expect := range expects.Repositories { 86 got := repofile.Repositories[i] 87 if expect.Name != got.Name { 88 t.Errorf("Expected name %q, got %q", expect.Name, got.Name) 89 } 90 if expect.URL != got.URL { 91 t.Errorf("Expected url %q, got %q", expect.URL, got.URL) 92 } 93 if expect.Cache != got.Cache { 94 t.Errorf("Expected cache %q, got %q", expect.Cache, got.Cache) 95 } 96 } 97 } 98 99 func TestNewPreV1RepositoriesFile(t *testing.T) { 100 r, err := LoadRepositoriesFile("testdata/old-repositories.yaml") 101 if err != nil && err != ErrRepoOutOfDate { 102 t.Fatal(err) 103 } 104 if len(r.Repositories) != 3 { 105 t.Fatalf("Expected 3 repos: %#v", r) 106 } 107 108 // Because they are parsed as a map, we lose ordering. 109 found := false 110 for _, rr := range r.Repositories { 111 if rr.Name == "best-charts-ever" { 112 found = true 113 } 114 } 115 if !found { 116 t.Errorf("expected the best charts ever. Got %#v", r.Repositories) 117 } 118 }