github.com/koderover/helm@v2.17.0+incompatible/pkg/downloader/manager_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package downloader 17 18 import ( 19 "bytes" 20 "reflect" 21 "strings" 22 "testing" 23 24 "k8s.io/helm/pkg/chartutil" 25 "k8s.io/helm/pkg/helm/helmpath" 26 ) 27 28 func TestVersionEquals(t *testing.T) { 29 tests := []struct { 30 name, v1, v2 string 31 expect bool 32 }{ 33 {name: "semver match", v1: "1.2.3-beta.11", v2: "1.2.3-beta.11", expect: true}, 34 {name: "semver match, build info", v1: "1.2.3-beta.11+a", v2: "1.2.3-beta.11+b", expect: true}, 35 {name: "string match", v1: "abcdef123", v2: "abcdef123", expect: true}, 36 {name: "semver mismatch", v1: "1.2.3-beta.11", v2: "1.2.3-beta.22", expect: false}, 37 {name: "semver mismatch, invalid semver", v1: "1.2.3-beta.11", v2: "stinkycheese", expect: false}, 38 } 39 40 for _, tt := range tests { 41 if versionEquals(tt.v1, tt.v2) != tt.expect { 42 t.Errorf("%s: failed comparison of %q and %q (expect equal: %t)", tt.name, tt.v1, tt.v2, tt.expect) 43 } 44 } 45 } 46 47 func TestNormalizeURL(t *testing.T) { 48 tests := []struct { 49 name, base, path, expect string 50 }{ 51 {name: "basic URL", base: "https://example.com", path: "http://helm.sh/foo", expect: "http://helm.sh/foo"}, 52 {name: "relative path", base: "https://helm.sh/charts", path: "foo", expect: "https://helm.sh/charts/foo"}, 53 } 54 55 for _, tt := range tests { 56 got, err := normalizeURL(tt.base, tt.path) 57 if err != nil { 58 t.Errorf("%s: error %s", tt.name, err) 59 continue 60 } else if got != tt.expect { 61 t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got) 62 } 63 } 64 } 65 66 func TestFindChartURL(t *testing.T) { 67 b := bytes.NewBuffer(nil) 68 m := &Manager{ 69 Out: b, 70 HelmHome: helmpath.Home("testdata/helmhome"), 71 } 72 repos, err := m.loadChartRepositories() 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 name := "alpine" 78 version := "0.1.0" 79 repoURL := "http://example.com/charts" 80 81 churl, username, password, err := m.findChartURL(name, version, repoURL, repos) 82 if err != nil { 83 t.Fatal(err) 84 } 85 if churl != "https://charts.helm.sh/stable/alpine-0.1.0.tgz" { 86 t.Errorf("Unexpected URL %q", churl) 87 } 88 if username != "" { 89 t.Errorf("Unexpected username %q", username) 90 } 91 if password != "" { 92 t.Errorf("Unexpected password %q", password) 93 } 94 } 95 96 func TestGetRepoNames(t *testing.T) { 97 b := bytes.NewBuffer(nil) 98 m := &Manager{ 99 Out: b, 100 HelmHome: helmpath.Home("testdata/helmhome"), 101 } 102 tests := []struct { 103 name string 104 req []*chartutil.Dependency 105 expect map[string]string 106 err bool 107 expectedErr string 108 }{ 109 { 110 name: "no repo definition failure -- stable repo", 111 req: []*chartutil.Dependency{ 112 {Name: "oedipus-rex", Repository: "stable"}, 113 }, 114 err: true, 115 }, 116 { 117 name: "dependency repository is url but not exist in repos", 118 req: []*chartutil.Dependency{ 119 {Name: "oedipus-rex", Repository: "http://example.com/test"}, 120 }, 121 expect: map[string]string{"http://example.com/test": "http://example.com/test"}, 122 }, 123 { 124 name: "no repo definition failure", 125 req: []*chartutil.Dependency{ 126 {Name: "oedipus-rex", Repository: "http://example.com"}, 127 }, 128 expect: map[string]string{"oedipus-rex": "testing"}, 129 }, 130 { 131 name: "repo from local path", 132 req: []*chartutil.Dependency{ 133 {Name: "local-dep", Repository: "file://./testdata/signtest"}, 134 }, 135 expect: map[string]string{"local-dep": "file://./testdata/signtest"}, 136 }, 137 { 138 name: "repo alias (alias:)", 139 req: []*chartutil.Dependency{ 140 {Name: "oedipus-rex", Repository: "alias:testing"}, 141 }, 142 expect: map[string]string{"oedipus-rex": "testing"}, 143 }, 144 { 145 name: "repo alias (@)", 146 req: []*chartutil.Dependency{ 147 {Name: "oedipus-rex", Repository: "@testing"}, 148 }, 149 expect: map[string]string{"oedipus-rex": "testing"}, 150 }, 151 { 152 name: "repo from local chart under charts path", 153 req: []*chartutil.Dependency{ 154 {Name: "local-subchart", Repository: ""}, 155 }, 156 expect: map[string]string{}, 157 }, 158 } 159 160 for _, tt := range tests { 161 l, err := m.getRepoNames(tt.req) 162 if err != nil { 163 if tt.err { 164 if !strings.Contains(err.Error(), tt.expectedErr) { 165 t.Fatalf("%s: expected error: %s, got: %s", tt.name, tt.expectedErr, err.Error()) 166 } 167 continue 168 } 169 t.Fatal(err) 170 } 171 172 if tt.err { 173 t.Fatalf("Expected error in test %q", tt.name) 174 } 175 176 // m1 and m2 are the maps we want to compare 177 eq := reflect.DeepEqual(l, tt.expect) 178 if !eq { 179 t.Errorf("%s: expected map %v, got %v", tt.name, l, tt.name) 180 } 181 } 182 }