github.com/sri09kanth/helm@v3.0.0-beta.3+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  	"helm.sh/helm/pkg/chart"
    24  )
    25  
    26  func TestVersionEquals(t *testing.T) {
    27  	tests := []struct {
    28  		name, v1, v2 string
    29  		expect       bool
    30  	}{
    31  		{name: "semver match", v1: "1.2.3-beta.11", v2: "1.2.3-beta.11", expect: true},
    32  		{name: "semver match, build info", v1: "1.2.3-beta.11+a", v2: "1.2.3-beta.11+b", expect: true},
    33  		{name: "string match", v1: "abcdef123", v2: "abcdef123", expect: true},
    34  		{name: "semver mismatch", v1: "1.2.3-beta.11", v2: "1.2.3-beta.22", expect: false},
    35  		{name: "semver mismatch, invalid semver", v1: "1.2.3-beta.11", v2: "stinkycheese", expect: false},
    36  	}
    37  
    38  	for _, tt := range tests {
    39  		if versionEquals(tt.v1, tt.v2) != tt.expect {
    40  			t.Errorf("%s: failed comparison of %q and %q (expect equal: %t)", tt.name, tt.v1, tt.v2, tt.expect)
    41  		}
    42  	}
    43  }
    44  
    45  func TestNormalizeURL(t *testing.T) {
    46  	tests := []struct {
    47  		name, base, path, expect string
    48  	}{
    49  		{name: "basic URL", base: "https://example.com", path: "http://helm.sh/foo", expect: "http://helm.sh/foo"},
    50  		{name: "relative path", base: "https://helm.sh/charts", path: "foo", expect: "https://helm.sh/charts/foo"},
    51  	}
    52  
    53  	for _, tt := range tests {
    54  		got, err := normalizeURL(tt.base, tt.path)
    55  		if err != nil {
    56  			t.Errorf("%s: error %s", tt.name, err)
    57  			continue
    58  		} else if got != tt.expect {
    59  			t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got)
    60  		}
    61  	}
    62  }
    63  
    64  func TestFindChartURL(t *testing.T) {
    65  	var b bytes.Buffer
    66  	m := &Manager{
    67  		Out:              &b,
    68  		RepositoryConfig: repoConfig,
    69  		RepositoryCache:  repoCache,
    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  		RepositoryConfig: repoConfig,
   100  		RepositoryCache:  repoCache,
   101  	}
   102  	tests := []struct {
   103  		name   string
   104  		req    []*chart.Dependency
   105  		expect map[string]string
   106  		err    bool
   107  	}{
   108  		{
   109  			name: "no repo definition failure",
   110  			req: []*chart.Dependency{
   111  				{Name: "oedipus-rex", Repository: "http://example.com/test"},
   112  			},
   113  			err: true,
   114  		},
   115  		{
   116  			name: "no repo definition failure -- stable repo",
   117  			req: []*chart.Dependency{
   118  				{Name: "oedipus-rex", Repository: "stable"},
   119  			},
   120  			err: true,
   121  		},
   122  		{
   123  			name: "no repo definition failure",
   124  			req: []*chart.Dependency{
   125  				{Name: "oedipus-rex", Repository: "http://example.com"},
   126  			},
   127  			expect: map[string]string{"oedipus-rex": "testing"},
   128  		},
   129  		{
   130  			name: "repo from local path",
   131  			req: []*chart.Dependency{
   132  				{Name: "local-dep", Repository: "file://./testdata/signtest"},
   133  			},
   134  			expect: map[string]string{"local-dep": "file://./testdata/signtest"},
   135  		},
   136  		{
   137  			name: "repo alias (alias:)",
   138  			req: []*chart.Dependency{
   139  				{Name: "oedipus-rex", Repository: "alias:testing"},
   140  			},
   141  			expect: map[string]string{"oedipus-rex": "testing"},
   142  		},
   143  		{
   144  			name: "repo alias (@)",
   145  			req: []*chart.Dependency{
   146  				{Name: "oedipus-rex", Repository: "@testing"},
   147  			},
   148  			expect: map[string]string{"oedipus-rex": "testing"},
   149  		},
   150  	}
   151  
   152  	for _, tt := range tests {
   153  		l, err := m.getRepoNames(tt.req)
   154  		if err != nil {
   155  			if tt.err {
   156  				continue
   157  			}
   158  			t.Fatal(err)
   159  		}
   160  
   161  		if tt.err {
   162  			t.Fatalf("Expected error in test %q", tt.name)
   163  		}
   164  
   165  		// m1 and m2 are the maps we want to compare
   166  		eq := reflect.DeepEqual(l, tt.expect)
   167  		if !eq {
   168  			t.Errorf("%s: expected map %v, got %v", tt.name, l, tt.name)
   169  		}
   170  	}
   171  }