github.com/zsuzhengdu/helm@v3.0.0-beta.3+incompatible/internal/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/repository") 92 for _, tt := range tests { 93 l, err := r.Resolve(tt.req, repoNames) 94 if err != nil { 95 if tt.err { 96 continue 97 } 98 t.Fatal(err) 99 } 100 101 if tt.err { 102 t.Fatalf("Expected error in test %q", tt.name) 103 } 104 105 if h, err := HashReq(tt.expect.Dependencies); err != nil { 106 t.Fatal(err) 107 } else if h != l.Digest { 108 t.Errorf("%q: hashes don't match.", tt.name) 109 } 110 111 // Check fields. 112 if len(l.Dependencies) != len(tt.req) { 113 t.Errorf("%s: wrong number of dependencies in lock", tt.name) 114 } 115 d0 := l.Dependencies[0] 116 e0 := tt.expect.Dependencies[0] 117 if d0.Name != e0.Name { 118 t.Errorf("%s: expected name %s, got %s", tt.name, e0.Name, d0.Name) 119 } 120 if d0.Repository != e0.Repository { 121 t.Errorf("%s: expected repo %s, got %s", tt.name, e0.Repository, d0.Repository) 122 } 123 if d0.Version != e0.Version { 124 t.Errorf("%s: expected version %s, got %s", tt.name, e0.Version, d0.Version) 125 } 126 } 127 } 128 129 func TestHashReq(t *testing.T) { 130 expect := "sha256:d661820b01ed7bcf26eed8f01cf16380e0a76326ba33058d3150f919d9b15bc0" 131 req := []*chart.Dependency{ 132 {Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"}, 133 } 134 h, err := HashReq(req) 135 if err != nil { 136 t.Fatal(err) 137 } 138 if expect != h { 139 t.Errorf("Expected %q, got %q", expect, h) 140 } 141 142 req = []*chart.Dependency{} 143 h, err = HashReq(req) 144 if err != nil { 145 t.Fatal(err) 146 } 147 if expect == h { 148 t.Errorf("Expected %q != %q", expect, h) 149 } 150 }