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