github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/repo/repo_test.go (about) 1 /* 2 Copyright The Helm Authors. 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 "strings" 23 "testing" 24 ) 25 26 const testRepositoriesFile = "testdata/repositories.yaml" 27 28 func TestFile(t *testing.T) { 29 rf := NewFile() 30 rf.Add( 31 &Entry{ 32 Name: "stable", 33 URL: "https://example.com/stable/charts", 34 }, 35 &Entry{ 36 Name: "incubator", 37 URL: "https://example.com/incubator", 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 } 60 61 func TestNewFile(t *testing.T) { 62 expects := NewFile() 63 expects.Add( 64 &Entry{ 65 Name: "stable", 66 URL: "https://example.com/stable/charts", 67 }, 68 &Entry{ 69 Name: "incubator", 70 URL: "https://example.com/incubator", 71 }, 72 ) 73 74 file, err := LoadFile(testRepositoriesFile) 75 if err != nil { 76 t.Errorf("%q could not be loaded: %s", testRepositoriesFile, err) 77 } 78 79 if len(expects.Repositories) != len(file.Repositories) { 80 t.Fatalf("Unexpected repo data: %#v", file.Repositories) 81 } 82 83 for i, expect := range expects.Repositories { 84 got := file.Repositories[i] 85 if expect.Name != got.Name { 86 t.Errorf("Expected name %q, got %q", expect.Name, got.Name) 87 } 88 if expect.URL != got.URL { 89 t.Errorf("Expected url %q, got %q", expect.URL, got.URL) 90 } 91 } 92 } 93 94 func TestRepoFile_Get(t *testing.T) { 95 repo := NewFile() 96 repo.Add( 97 &Entry{ 98 Name: "first", 99 URL: "https://example.com/first", 100 }, 101 &Entry{ 102 Name: "second", 103 URL: "https://example.com/second", 104 }, 105 &Entry{ 106 Name: "third", 107 URL: "https://example.com/third", 108 }, 109 &Entry{ 110 Name: "fourth", 111 URL: "https://example.com/fourth", 112 }, 113 ) 114 115 name := "second" 116 117 entry := repo.Get(name) 118 if entry == nil { 119 t.Fatalf("Expected repo entry %q to be found", name) 120 } 121 122 if entry.URL != "https://example.com/second" { 123 t.Errorf("Expected repo URL to be %q but got %q", "https://example.com/second", entry.URL) 124 } 125 126 entry = repo.Get("nonexistent") 127 if entry != nil { 128 t.Errorf("Got unexpected entry %+v", entry) 129 } 130 } 131 132 func TestRemoveRepository(t *testing.T) { 133 sampleRepository := NewFile() 134 sampleRepository.Add( 135 &Entry{ 136 Name: "stable", 137 URL: "https://example.com/stable/charts", 138 }, 139 &Entry{ 140 Name: "incubator", 141 URL: "https://example.com/incubator", 142 }, 143 ) 144 145 removeRepository := "stable" 146 found := sampleRepository.Remove(removeRepository) 147 if !found { 148 t.Errorf("expected repository %s not found", removeRepository) 149 } 150 151 found = sampleRepository.Has(removeRepository) 152 if found { 153 t.Errorf("repository %s not deleted", removeRepository) 154 } 155 } 156 157 func TestUpdateRepository(t *testing.T) { 158 sampleRepository := NewFile() 159 sampleRepository.Add( 160 &Entry{ 161 Name: "stable", 162 URL: "https://example.com/stable/charts", 163 }, 164 &Entry{ 165 Name: "incubator", 166 URL: "https://example.com/incubator", 167 }, 168 ) 169 newRepoName := "sample" 170 sampleRepository.Update(&Entry{Name: newRepoName, 171 URL: "https://example.com/sample", 172 }) 173 174 if !sampleRepository.Has(newRepoName) { 175 t.Errorf("expected repository %s not found", newRepoName) 176 } 177 repoCount := len(sampleRepository.Repositories) 178 179 sampleRepository.Update(&Entry{Name: newRepoName, 180 URL: "https://example.com/sample", 181 }) 182 183 if repoCount != len(sampleRepository.Repositories) { 184 t.Errorf("invalid number of repositories found %d, expected number of repositories %d", len(sampleRepository.Repositories), repoCount) 185 } 186 } 187 188 func TestWriteFile(t *testing.T) { 189 sampleRepository := NewFile() 190 sampleRepository.Add( 191 &Entry{ 192 Name: "stable", 193 URL: "https://example.com/stable/charts", 194 }, 195 &Entry{ 196 Name: "incubator", 197 URL: "https://example.com/incubator", 198 }, 199 ) 200 201 file, err := ioutil.TempFile("", "helm-repo") 202 if err != nil { 203 t.Errorf("failed to create test-file (%v)", err) 204 } 205 defer os.Remove(file.Name()) 206 if err := sampleRepository.WriteFile(file.Name(), 0644); err != nil { 207 t.Errorf("failed to write file (%v)", err) 208 } 209 210 repos, err := LoadFile(file.Name()) 211 if err != nil { 212 t.Errorf("failed to load file (%v)", err) 213 } 214 for _, repo := range sampleRepository.Repositories { 215 if !repos.Has(repo.Name) { 216 t.Errorf("expected repository %s not found", repo.Name) 217 } 218 } 219 } 220 221 func TestRepoNotExists(t *testing.T) { 222 if _, err := LoadFile("/this/path/does/not/exist.yaml"); err == nil { 223 t.Errorf("expected err to be non-nil when path does not exist") 224 } else if !strings.Contains(err.Error(), "couldn't load repositories file") { 225 t.Errorf("expected prompt `couldn't load repositories file`") 226 } 227 }