github.com/felipejfc/helm@v2.1.2+incompatible/cmd/helm/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/cmd/helm/helmpath" 24 "k8s.io/helm/pkg/chartutil" 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 118 for _, tt := range tests { 119 l, err := m.getRepoNames(tt.req) 120 if err != nil { 121 if tt.err { 122 continue 123 } 124 t.Fatal(err) 125 } 126 127 if tt.err { 128 t.Fatalf("Expected error in test %q", tt.name) 129 } 130 131 // m1 and m2 are the maps we want to compare 132 eq := reflect.DeepEqual(l, tt.expect) 133 if !eq { 134 t.Errorf("%s: expected map %v, got %v", tt.name, l, tt.name) 135 } 136 } 137 } 138 139 func TestUrlsAreEqual(t *testing.T) { 140 for _, tt := range []struct { 141 a, b string 142 match bool 143 }{ 144 {"http://example.com", "http://example.com", true}, 145 {"http://example.com", "http://another.example.com", false}, 146 {"https://example.com", "https://example.com", true}, 147 {"http://example.com/", "http://example.com", true}, 148 {"https://example.com", "http://example.com", false}, 149 {"http://example.com/foo", "http://example.com/foo/", true}, 150 {"http://example.com/foo//", "http://example.com/foo/", true}, 151 {"http://example.com/./foo/", "http://example.com/foo/", true}, 152 {"http://example.com/bar/../foo/", "http://example.com/foo/", true}, 153 {"/foo", "/foo", true}, 154 {"/foo", "/foo/", true}, 155 {"/foo/.", "/foo/", true}, 156 } { 157 if tt.match != urlsAreEqual(tt.a, tt.b) { 158 t.Errorf("Expected %q==%q to be %t", tt.a, tt.b, tt.match) 159 } 160 } 161 }