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