github.com/y-taka-23/helm@v2.8.0+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 import "io/ioutil" 21 import "os" 22 import "strings" 23 24 const testRepositoriesFile = "testdata/repositories.yaml" 25 26 func TestRepoFile(t *testing.T) { 27 rf := NewRepoFile() 28 rf.Add( 29 &Entry{ 30 Name: "stable", 31 URL: "https://example.com/stable/charts", 32 Cache: "stable-index.yaml", 33 }, 34 &Entry{ 35 Name: "incubator", 36 URL: "https://example.com/incubator", 37 Cache: "incubator-index.yaml", 38 }, 39 ) 40 41 if len(rf.Repositories) != 2 { 42 t.Fatal("Expected 2 repositories") 43 } 44 45 if rf.Has("nosuchrepo") { 46 t.Error("Found nonexistent repo") 47 } 48 if !rf.Has("incubator") { 49 t.Error("incubator repo is missing") 50 } 51 52 stable := rf.Repositories[0] 53 if stable.Name != "stable" { 54 t.Error("stable is not named stable") 55 } 56 if stable.URL != "https://example.com/stable/charts" { 57 t.Error("Wrong URL for stable") 58 } 59 if stable.Cache != "stable-index.yaml" { 60 t.Error("Wrong cache name for stable") 61 } 62 } 63 64 func TestNewRepositoriesFile(t *testing.T) { 65 expects := NewRepoFile() 66 expects.Add( 67 &Entry{ 68 Name: "stable", 69 URL: "https://example.com/stable/charts", 70 Cache: "stable-index.yaml", 71 }, 72 &Entry{ 73 Name: "incubator", 74 URL: "https://example.com/incubator", 75 Cache: "incubator-index.yaml", 76 }, 77 ) 78 79 repofile, err := LoadRepositoriesFile(testRepositoriesFile) 80 if err != nil { 81 t.Errorf("%q could not be loaded: %s", testRepositoriesFile, err) 82 } 83 84 if len(expects.Repositories) != len(repofile.Repositories) { 85 t.Fatalf("Unexpected repo data: %#v", repofile.Repositories) 86 } 87 88 for i, expect := range expects.Repositories { 89 got := repofile.Repositories[i] 90 if expect.Name != got.Name { 91 t.Errorf("Expected name %q, got %q", expect.Name, got.Name) 92 } 93 if expect.URL != got.URL { 94 t.Errorf("Expected url %q, got %q", expect.URL, got.URL) 95 } 96 if expect.Cache != got.Cache { 97 t.Errorf("Expected cache %q, got %q", expect.Cache, got.Cache) 98 } 99 } 100 } 101 102 func TestNewPreV1RepositoriesFile(t *testing.T) { 103 r, err := LoadRepositoriesFile("testdata/old-repositories.yaml") 104 if err != nil && err != ErrRepoOutOfDate { 105 t.Fatal(err) 106 } 107 if len(r.Repositories) != 3 { 108 t.Fatalf("Expected 3 repos: %#v", r) 109 } 110 111 // Because they are parsed as a map, we lose ordering. 112 found := false 113 for _, rr := range r.Repositories { 114 if rr.Name == "best-charts-ever" { 115 found = true 116 } 117 } 118 if !found { 119 t.Errorf("expected the best charts ever. Got %#v", r.Repositories) 120 } 121 } 122 123 func TestRemoveRepository(t *testing.T) { 124 sampleRepository := NewRepoFile() 125 sampleRepository.Add( 126 &Entry{ 127 Name: "stable", 128 URL: "https://example.com/stable/charts", 129 Cache: "stable-index.yaml", 130 }, 131 &Entry{ 132 Name: "incubator", 133 URL: "https://example.com/incubator", 134 Cache: "incubator-index.yaml", 135 }, 136 ) 137 138 removeRepository := "stable" 139 found := sampleRepository.Remove(removeRepository) 140 if !found { 141 t.Errorf("expected repository %s not found", removeRepository) 142 } 143 144 found = sampleRepository.Has(removeRepository) 145 if found { 146 t.Errorf("repository %s not deleted", removeRepository) 147 } 148 } 149 150 func TestUpdateRepository(t *testing.T) { 151 sampleRepository := NewRepoFile() 152 sampleRepository.Add( 153 &Entry{ 154 Name: "stable", 155 URL: "https://example.com/stable/charts", 156 Cache: "stable-index.yaml", 157 }, 158 &Entry{ 159 Name: "incubator", 160 URL: "https://example.com/incubator", 161 Cache: "incubator-index.yaml", 162 }, 163 ) 164 newRepoName := "sample" 165 sampleRepository.Update(&Entry{Name: newRepoName, 166 URL: "https://example.com/sample", 167 Cache: "sample-index.yaml", 168 }) 169 170 if !sampleRepository.Has(newRepoName) { 171 t.Errorf("expected repository %s not found", newRepoName) 172 } 173 repoCount := len(sampleRepository.Repositories) 174 175 sampleRepository.Update(&Entry{Name: newRepoName, 176 URL: "https://example.com/sample", 177 Cache: "sample-index.yaml", 178 }) 179 180 if repoCount != len(sampleRepository.Repositories) { 181 t.Errorf("invalid number of repositories found %d, expected number of repositories %d", len(sampleRepository.Repositories), repoCount) 182 } 183 } 184 185 func TestWriteFile(t *testing.T) { 186 sampleRepository := NewRepoFile() 187 sampleRepository.Add( 188 &Entry{ 189 Name: "stable", 190 URL: "https://example.com/stable/charts", 191 Cache: "stable-index.yaml", 192 }, 193 &Entry{ 194 Name: "incubator", 195 URL: "https://example.com/incubator", 196 Cache: "incubator-index.yaml", 197 }, 198 ) 199 200 repoFile, err := ioutil.TempFile("", "helm-repo") 201 if err != nil { 202 t.Errorf("failed to create test-file (%v)", err) 203 } 204 defer os.Remove(repoFile.Name()) 205 if err := sampleRepository.WriteFile(repoFile.Name(), 744); err != nil { 206 t.Errorf("failed to write file (%v)", err) 207 } 208 209 repos, err := LoadRepositoriesFile(repoFile.Name()) 210 if err != nil { 211 t.Errorf("failed to load file (%v)", err) 212 } 213 for _, repo := range sampleRepository.Repositories { 214 if !repos.Has(repo.Name) { 215 t.Errorf("expected repository %s not found", repo.Name) 216 } 217 } 218 } 219 220 func TestRepoNotExists(t *testing.T) { 221 _, err := LoadRepositoriesFile("/this/path/does/not/exist.yaml") 222 if err == nil { 223 t.Errorf("expected err to be non-nil when path does not exist") 224 } else if !strings.Contains(err.Error(), "You might need to run `helm init`") { 225 t.Errorf("expected prompt to run `helm init` when repositories file does not exist") 226 } 227 }