github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+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", 111 req: []*chartutil.Dependency{ 112 {Name: "oedipus-rex", Repository: "http://example.com"}, 113 }, 114 expect: map[string]string{"oedipus-rex": "testing"}, 115 }, 116 { 117 name: "repo from local path", 118 req: []*chartutil.Dependency{ 119 {Name: "local-dep", Repository: "file://./testdata/signtest"}, 120 }, 121 expect: map[string]string{"local-dep": "file://./testdata/signtest"}, 122 }, 123 { 124 name: "repo alias (alias:)", 125 req: []*chartutil.Dependency{ 126 {Name: "oedipus-rex", Repository: "alias:testing"}, 127 }, 128 expect: map[string]string{"oedipus-rex": "testing"}, 129 }, 130 { 131 name: "repo alias (@)", 132 req: []*chartutil.Dependency{ 133 {Name: "oedipus-rex", Repository: "@testing"}, 134 }, 135 expect: map[string]string{"oedipus-rex": "testing"}, 136 }, 137 } 138 139 for _, tt := range tests { 140 l, err := m.getRepoNames(tt.req) 141 if err != nil { 142 if tt.err { 143 continue 144 } 145 t.Fatal(err) 146 } 147 148 if tt.err { 149 t.Fatalf("Expected error in test %q", tt.name) 150 } 151 152 // m1 and m2 are the maps we want to compare 153 eq := reflect.DeepEqual(l, tt.expect) 154 if !eq { 155 t.Errorf("%s: expected map %v, got %v", tt.name, l, tt.name) 156 } 157 } 158 }