github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/resolver/resolver_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 resolver
    17  
    18  import (
    19  	"testing"
    20  
    21  	"helm.sh/helm/pkg/chart"
    22  )
    23  
    24  func TestResolve(t *testing.T) {
    25  	tests := []struct {
    26  		name   string
    27  		req    []*chart.Dependency
    28  		expect *chart.Lock
    29  		err    bool
    30  	}{
    31  		{
    32  			name: "version failure",
    33  			req: []*chart.Dependency{
    34  				{Name: "oedipus-rex", Repository: "http://example.com", Version: ">a1"},
    35  			},
    36  			err: true,
    37  		},
    38  		{
    39  			name: "cache index failure",
    40  			req: []*chart.Dependency{
    41  				{Name: "oedipus-rex", Repository: "http://example.com", Version: "1.0.0"},
    42  			},
    43  			err: true,
    44  		},
    45  		{
    46  			name: "chart not found failure",
    47  			req: []*chart.Dependency{
    48  				{Name: "redis", Repository: "http://example.com", Version: "1.0.0"},
    49  			},
    50  			err: true,
    51  		},
    52  		{
    53  			name: "constraint not satisfied failure",
    54  			req: []*chart.Dependency{
    55  				{Name: "alpine", Repository: "http://example.com", Version: ">=1.0.0"},
    56  			},
    57  			err: true,
    58  		},
    59  		{
    60  			name: "valid lock",
    61  			req: []*chart.Dependency{
    62  				{Name: "alpine", Repository: "http://example.com", Version: ">=0.1.0"},
    63  			},
    64  			expect: &chart.Lock{
    65  				Dependencies: []*chart.Dependency{
    66  					{Name: "alpine", Repository: "http://example.com", Version: "0.2.0"},
    67  				},
    68  			},
    69  		},
    70  		{
    71  			name: "repo from valid local path",
    72  			req: []*chart.Dependency{
    73  				{Name: "signtest", Repository: "file://../../../../cmd/helm/testdata/testcharts/signtest", Version: "0.1.0"},
    74  			},
    75  			expect: &chart.Lock{
    76  				Dependencies: []*chart.Dependency{
    77  					{Name: "signtest", Repository: "file://../../../../cmd/helm/testdata/testcharts/signtest", Version: "0.1.0"},
    78  				},
    79  			},
    80  		},
    81  		{
    82  			name: "repo from invalid local path",
    83  			req: []*chart.Dependency{
    84  				{Name: "notexist", Repository: "file://../testdata/notexist", Version: "0.1.0"},
    85  			},
    86  			err: true,
    87  		},
    88  	}
    89  
    90  	repoNames := map[string]string{"alpine": "kubernetes-charts", "redis": "kubernetes-charts"}
    91  	r := New("testdata/chartpath", "testdata/helmhome")
    92  	for _, tt := range tests {
    93  		hash, err := HashReq(tt.req)
    94  		if err != nil {
    95  			t.Fatal(err)
    96  		}
    97  
    98  		l, err := r.Resolve(tt.req, repoNames, hash)
    99  		if err != nil {
   100  			if tt.err {
   101  				continue
   102  			}
   103  			t.Fatal(err)
   104  		}
   105  
   106  		if tt.err {
   107  			t.Fatalf("Expected error in test %q", tt.name)
   108  		}
   109  
   110  		if h, err := HashReq(tt.req); err != nil {
   111  			t.Fatal(err)
   112  		} else if h != l.Digest {
   113  			t.Errorf("%q: hashes don't match.", tt.name)
   114  		}
   115  
   116  		// Check fields.
   117  		if len(l.Dependencies) != len(tt.req) {
   118  			t.Errorf("%s: wrong number of dependencies in lock", tt.name)
   119  		}
   120  		d0 := l.Dependencies[0]
   121  		e0 := tt.expect.Dependencies[0]
   122  		if d0.Name != e0.Name {
   123  			t.Errorf("%s: expected name %s, got %s", tt.name, e0.Name, d0.Name)
   124  		}
   125  		if d0.Repository != e0.Repository {
   126  			t.Errorf("%s: expected repo %s, got %s", tt.name, e0.Repository, d0.Repository)
   127  		}
   128  		if d0.Version != e0.Version {
   129  			t.Errorf("%s: expected version %s, got %s", tt.name, e0.Version, d0.Version)
   130  		}
   131  	}
   132  }
   133  
   134  func TestHashReq(t *testing.T) {
   135  	expect := "sha256:d661820b01ed7bcf26eed8f01cf16380e0a76326ba33058d3150f919d9b15bc0"
   136  	req := []*chart.Dependency{
   137  		{Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"},
   138  	}
   139  	h, err := HashReq(req)
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  	if expect != h {
   144  		t.Errorf("Expected %q, got %q", expect, h)
   145  	}
   146  
   147  	req = []*chart.Dependency{}
   148  	h, err = HashReq(req)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  	if expect == h {
   153  		t.Errorf("Expected %q !=  %q", expect, h)
   154  	}
   155  }