github.com/y-taka-23/helm@v2.8.0+incompatible/pkg/downloader/manager_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "testing" 22 23 "k8s.io/helm/pkg/chartutil" 24 "k8s.io/helm/pkg/helm/helmpath" 25 ) 26 27 func TestVersionEquals(t *testing.T) { 28 tests := []struct { 29 name, v1, v2 string 30 expect bool 31 }{ 32 {name: "semver match", v1: "1.2.3-beta.11", v2: "1.2.3-beta.11", expect: true}, 33 {name: "semver match, build info", v1: "1.2.3-beta.11+a", v2: "1.2.3-beta.11+b", expect: true}, 34 {name: "string match", v1: "abcdef123", v2: "abcdef123", expect: true}, 35 {name: "semver mismatch", v1: "1.2.3-beta.11", v2: "1.2.3-beta.22", expect: false}, 36 {name: "semver mismatch, invalid semver", v1: "1.2.3-beta.11", v2: "stinkycheese", expect: false}, 37 } 38 39 for _, tt := range tests { 40 if versionEquals(tt.v1, tt.v2) != tt.expect { 41 t.Errorf("%s: failed comparison of %q and %q (expect equal: %t)", tt.name, tt.v1, tt.v2, tt.expect) 42 } 43 } 44 } 45 46 func TestNormalizeURL(t *testing.T) { 47 tests := []struct { 48 name, base, path, expect string 49 }{ 50 {name: "basic URL", base: "https://example.com", path: "http://helm.sh/foo", expect: "http://helm.sh/foo"}, 51 {name: "relative path", base: "https://helm.sh/charts", path: "foo", expect: "https://helm.sh/charts/foo"}, 52 } 53 54 for _, tt := range tests { 55 got, err := normalizeURL(tt.base, tt.path) 56 if err != nil { 57 t.Errorf("%s: error %s", tt.name, err) 58 continue 59 } else if got != tt.expect { 60 t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got) 61 } 62 } 63 } 64 65 func TestFindChartURL(t *testing.T) { 66 b := bytes.NewBuffer(nil) 67 m := &Manager{ 68 Out: b, 69 HelmHome: helmpath.Home("testdata/helmhome"), 70 } 71 repos, err := m.loadChartRepositories() 72 if err != nil { 73 t.Fatal(err) 74 } 75 76 name := "alpine" 77 version := "0.1.0" 78 repoURL := "http://example.com/charts" 79 80 churl, err := findChartURL(name, version, repoURL, repos) 81 if err != nil { 82 t.Fatal(err) 83 } 84 if churl != "https://kubernetes-charts.storage.googleapis.com/alpine-0.1.0.tgz" { 85 t.Errorf("Unexpected URL %q", churl) 86 } 87 88 } 89 90 func TestGetRepoNames(t *testing.T) { 91 b := bytes.NewBuffer(nil) 92 m := &Manager{ 93 Out: b, 94 HelmHome: helmpath.Home("testdata/helmhome"), 95 } 96 tests := []struct { 97 name string 98 req []*chartutil.Dependency 99 expect map[string]string 100 err bool 101 }{ 102 { 103 name: "no repo definition failure", 104 req: []*chartutil.Dependency{ 105 {Name: "oedipus-rex", Repository: "http://example.com/test"}, 106 }, 107 err: true, 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: "no repo definition failure", 118 req: []*chartutil.Dependency{ 119 {Name: "oedipus-rex", Repository: "http://example.com"}, 120 }, 121 expect: map[string]string{"oedipus-rex": "testing"}, 122 }, 123 { 124 name: "repo from local path", 125 req: []*chartutil.Dependency{ 126 {Name: "local-dep", Repository: "file://./testdata/signtest"}, 127 }, 128 expect: map[string]string{"local-dep": "file://./testdata/signtest"}, 129 }, 130 { 131 name: "repo alias (alias:)", 132 req: []*chartutil.Dependency{ 133 {Name: "oedipus-rex", Repository: "alias:testing"}, 134 }, 135 expect: map[string]string{"oedipus-rex": "testing"}, 136 }, 137 { 138 name: "repo alias (@)", 139 req: []*chartutil.Dependency{ 140 {Name: "oedipus-rex", Repository: "@testing"}, 141 }, 142 expect: map[string]string{"oedipus-rex": "testing"}, 143 }, 144 } 145 146 for _, tt := range tests { 147 l, err := m.getRepoNames(tt.req) 148 if err != nil { 149 if tt.err { 150 continue 151 } 152 t.Fatal(err) 153 } 154 155 if tt.err { 156 t.Fatalf("Expected error in test %q", tt.name) 157 } 158 159 // m1 and m2 are the maps we want to compare 160 eq := reflect.DeepEqual(l, tt.expect) 161 if !eq { 162 t.Errorf("%s: expected map %v, got %v", tt.name, l, tt.name) 163 } 164 } 165 }