github.com/migueleliasweb/helm@v2.6.1+incompatible/pkg/repo/chartrepo_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 "net/http" 22 "net/http/httptest" 23 "os" 24 "path/filepath" 25 "reflect" 26 "strings" 27 "testing" 28 "time" 29 30 "k8s.io/helm/pkg/getter" 31 "k8s.io/helm/pkg/helm/environment" 32 "k8s.io/helm/pkg/proto/hapi/chart" 33 ) 34 35 const ( 36 testRepository = "testdata/repository" 37 testURL = "http://example-charts.com" 38 ) 39 40 func TestLoadChartRepository(t *testing.T) { 41 r, err := NewChartRepository(&Entry{ 42 Name: testRepository, 43 URL: testURL, 44 }, getter.All(environment.EnvSettings{})) 45 if err != nil { 46 t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) 47 } 48 49 if err := r.Load(); err != nil { 50 t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) 51 } 52 53 paths := []string{ 54 filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), 55 filepath.Join(testRepository, "sprocket-1.1.0.tgz"), 56 filepath.Join(testRepository, "sprocket-1.2.0.tgz"), 57 } 58 59 if r.Config.Name != testRepository { 60 t.Errorf("Expected %s as Name but got %s", testRepository, r.Config.Name) 61 } 62 63 if !reflect.DeepEqual(r.ChartPaths, paths) { 64 t.Errorf("Expected %#v but got %#v\n", paths, r.ChartPaths) 65 } 66 67 if r.Config.URL != testURL { 68 t.Errorf("Expected url for chart repository to be %s but got %s", testURL, r.Config.URL) 69 } 70 } 71 72 func TestIndex(t *testing.T) { 73 r, err := NewChartRepository(&Entry{ 74 Name: testRepository, 75 URL: testURL, 76 }, getter.All(environment.EnvSettings{})) 77 if err != nil { 78 t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) 79 } 80 81 if err := r.Load(); err != nil { 82 t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) 83 } 84 85 err = r.Index() 86 if err != nil { 87 t.Errorf("Error performing index: %v\n", err) 88 } 89 90 tempIndexPath := filepath.Join(testRepository, indexPath) 91 actual, err := LoadIndexFile(tempIndexPath) 92 defer os.Remove(tempIndexPath) // clean up 93 if err != nil { 94 t.Errorf("Error loading index file %v", err) 95 } 96 verifyIndex(t, actual) 97 98 // Re-index and test again. 99 err = r.Index() 100 if err != nil { 101 t.Errorf("Error performing re-index: %s\n", err) 102 } 103 second, err := LoadIndexFile(tempIndexPath) 104 if err != nil { 105 t.Errorf("Error re-loading index file %v", err) 106 } 107 verifyIndex(t, second) 108 } 109 110 func verifyIndex(t *testing.T, actual *IndexFile) { 111 var empty time.Time 112 if actual.Generated == empty { 113 t.Errorf("Generated should be greater than 0: %s", actual.Generated) 114 } 115 116 if actual.APIVersion != APIVersionV1 { 117 t.Error("Expected v1 API") 118 } 119 120 entries := actual.Entries 121 if numEntries := len(entries); numEntries != 2 { 122 t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries) 123 } 124 125 expects := map[string]ChartVersions{ 126 "frobnitz": { 127 { 128 Metadata: &chart.Metadata{ 129 Name: "frobnitz", 130 Version: "1.2.3", 131 }, 132 }, 133 }, 134 "sprocket": { 135 { 136 Metadata: &chart.Metadata{ 137 Name: "sprocket", 138 Version: "1.2.0", 139 }, 140 }, 141 { 142 Metadata: &chart.Metadata{ 143 Name: "sprocket", 144 Version: "1.1.0", 145 }, 146 }, 147 }, 148 } 149 150 for name, versions := range expects { 151 got, ok := entries[name] 152 if !ok { 153 t.Errorf("Could not find %q entry", name) 154 continue 155 } 156 if len(versions) != len(got) { 157 t.Errorf("Expected %d versions, got %d", len(versions), len(got)) 158 continue 159 } 160 for i, e := range versions { 161 g := got[i] 162 if e.Name != g.Name { 163 t.Errorf("Expected %q, got %q", e.Name, g.Name) 164 } 165 if e.Version != g.Version { 166 t.Errorf("Expected %q, got %q", e.Version, g.Version) 167 } 168 if len(g.Keywords) != 3 { 169 t.Error("Expected 3 keyrwords.") 170 } 171 if len(g.Maintainers) != 2 { 172 t.Error("Expected 2 maintainers.") 173 } 174 if g.Created == empty { 175 t.Error("Expected created to be non-empty") 176 } 177 if g.Description == "" { 178 t.Error("Expected description to be non-empty") 179 } 180 if g.Home == "" { 181 t.Error("Expected home to be non-empty") 182 } 183 if g.Digest == "" { 184 t.Error("Expected digest to be non-empty") 185 } 186 if len(g.URLs) != 1 { 187 t.Error("Expected exactly 1 URL") 188 } 189 } 190 } 191 } 192 193 // startLocalServerForTests Start the local helm server 194 func startLocalServerForTests(handler http.Handler) (*httptest.Server, error) { 195 if handler == nil { 196 fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml") 197 if err != nil { 198 return nil, err 199 } 200 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 201 w.Write(fileBytes) 202 }) 203 } 204 205 return httptest.NewServer(handler), nil 206 } 207 208 func TestFindChartInRepoURL(t *testing.T) { 209 srv, err := startLocalServerForTests(nil) 210 if err != nil { 211 t.Fatal(err) 212 } 213 defer srv.Close() 214 215 chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) 216 if err != nil { 217 t.Errorf("%s", err) 218 } 219 if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz" { 220 t.Errorf("%s is not the valid URL", chartURL) 221 } 222 223 chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) 224 if err != nil { 225 t.Errorf("%s", err) 226 } 227 if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.1.0.tgz" { 228 t.Errorf("%s is not the valid URL", chartURL) 229 } 230 } 231 232 func TestErrorFindChartInRepoURL(t *testing.T) { 233 _, err := FindChartInRepoURL("http://someserver/something", "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) 234 if err == nil { 235 t.Errorf("Expected error for bad chart URL, but did not get any errors") 236 } 237 if err != nil && !strings.Contains(err.Error(), `Looks like "http://someserver/something" is not a valid chart repository or cannot be reached: Get http://someserver/something/index.yaml`) { 238 t.Errorf("Expected error for bad chart URL, but got a different error (%v)", err) 239 } 240 241 srv, err := startLocalServerForTests(nil) 242 if err != nil { 243 t.Fatal(err) 244 } 245 defer srv.Close() 246 247 _, err = FindChartInRepoURL(srv.URL, "nginx1", "", "", "", "", getter.All(environment.EnvSettings{})) 248 if err == nil { 249 t.Errorf("Expected error for chart not found, but did not get any errors") 250 } 251 if err != nil && err.Error() != `chart "nginx1" not found in `+srv.URL+` repository` { 252 t.Errorf("Expected error for chart not found, but got a different error (%v)", err) 253 } 254 255 _, err = FindChartInRepoURL(srv.URL, "nginx1", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) 256 if err == nil { 257 t.Errorf("Expected error for chart not found, but did not get any errors") 258 } 259 if err != nil && err.Error() != `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository` { 260 t.Errorf("Expected error for chart not found, but got a different error (%v)", err) 261 } 262 263 _, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", "", "", "", "", getter.All(environment.EnvSettings{})) 264 if err == nil { 265 t.Errorf("Expected error for no chart URLs available, but did not get any errors") 266 } 267 if err != nil && err.Error() != `chart "chartWithNoURL" has no downloadable URLs` { 268 t.Errorf("Expected error for chart not found, but got a different error (%v)", err) 269 } 270 }