github.com/Beeketing/helm@v2.12.1+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  	"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, username, password, 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  	if username != "" {
    88  		t.Errorf("Unexpected username %q", username)
    89  	}
    90  	if password != "" {
    91  		t.Errorf("Unexpected password %q", password)
    92  	}
    93  }
    94  
    95  func TestGetRepoNames(t *testing.T) {
    96  	b := bytes.NewBuffer(nil)
    97  	m := &Manager{
    98  		Out:      b,
    99  		HelmHome: helmpath.Home("testdata/helmhome"),
   100  	}
   101  	tests := []struct {
   102  		name   string
   103  		req    []*chartutil.Dependency
   104  		expect map[string]string
   105  		err    bool
   106  	}{
   107  		{
   108  			name: "no repo definition failure",
   109  			req: []*chartutil.Dependency{
   110  				{Name: "oedipus-rex", Repository: "http://example.com/test"},
   111  			},
   112  			err: true,
   113  		},
   114  		{
   115  			name: "no repo definition failure -- stable repo",
   116  			req: []*chartutil.Dependency{
   117  				{Name: "oedipus-rex", Repository: "stable"},
   118  			},
   119  			err: true,
   120  		},
   121  		{
   122  			name: "no repo definition failure",
   123  			req: []*chartutil.Dependency{
   124  				{Name: "oedipus-rex", Repository: "http://example.com"},
   125  			},
   126  			expect: map[string]string{"oedipus-rex": "testing"},
   127  		},
   128  		{
   129  			name: "repo from local path",
   130  			req: []*chartutil.Dependency{
   131  				{Name: "local-dep", Repository: "file://./testdata/signtest"},
   132  			},
   133  			expect: map[string]string{"local-dep": "file://./testdata/signtest"},
   134  		},
   135  		{
   136  			name: "repo alias (alias:)",
   137  			req: []*chartutil.Dependency{
   138  				{Name: "oedipus-rex", Repository: "alias:testing"},
   139  			},
   140  			expect: map[string]string{"oedipus-rex": "testing"},
   141  		},
   142  		{
   143  			name: "repo alias (@)",
   144  			req: []*chartutil.Dependency{
   145  				{Name: "oedipus-rex", Repository: "@testing"},
   146  			},
   147  			expect: map[string]string{"oedipus-rex": "testing"},
   148  		},
   149  	}
   150  
   151  	for _, tt := range tests {
   152  		l, err := m.getRepoNames(tt.req)
   153  		if err != nil {
   154  			if tt.err {
   155  				continue
   156  			}
   157  			t.Fatal(err)
   158  		}
   159  
   160  		if tt.err {
   161  			t.Fatalf("Expected error in test %q", tt.name)
   162  		}
   163  
   164  		// m1 and m2 are the maps we want to compare
   165  		eq := reflect.DeepEqual(l, tt.expect)
   166  		if !eq {
   167  			t.Errorf("%s: expected map %v, got %v", tt.name, l, tt.name)
   168  		}
   169  	}
   170  }