github.com/felipejfc/helm@v2.1.2+incompatible/cmd/helm/resolver/resolver_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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: "cache index failure", 42 req: &chartutil.Requirements{ 43 Dependencies: []*chartutil.Dependency{ 44 {Name: "oedipus-rex", Repository: "http://example.com", Version: "1.0.0"}, 45 }, 46 }, 47 err: true, 48 }, 49 { 50 name: "chart not found failure", 51 req: &chartutil.Requirements{ 52 Dependencies: []*chartutil.Dependency{ 53 {Name: "redis", Repository: "http://example.com", Version: "1.0.0"}, 54 }, 55 }, 56 err: true, 57 }, 58 { 59 name: "constraint not satisfied failure", 60 req: &chartutil.Requirements{ 61 Dependencies: []*chartutil.Dependency{ 62 {Name: "alpine", Repository: "http://example.com", Version: ">=1.0.0"}, 63 }, 64 }, 65 err: true, 66 }, 67 { 68 name: "valid lock", 69 req: &chartutil.Requirements{ 70 Dependencies: []*chartutil.Dependency{ 71 {Name: "alpine", Repository: "http://example.com", Version: ">=0.1.0"}, 72 }, 73 }, 74 expect: &chartutil.RequirementsLock{ 75 Dependencies: []*chartutil.Dependency{ 76 {Name: "alpine", Repository: "http://example.com", Version: "0.2.0"}, 77 }, 78 }, 79 }, 80 } 81 82 repoNames := map[string]string{"alpine": "kubernetes-charts", "redis": "kubernetes-charts"} 83 r := New("testdata/chartpath", "testdata/helmhome") 84 for _, tt := range tests { 85 l, err := r.Resolve(tt.req, repoNames) 86 if err != nil { 87 if tt.err { 88 continue 89 } 90 t.Fatal(err) 91 } 92 93 if tt.err { 94 t.Fatalf("Expected error in test %q", tt.name) 95 } 96 97 if h, err := HashReq(tt.req); err != nil { 98 t.Fatal(err) 99 } else if h != l.Digest { 100 t.Errorf("%q: hashes don't match.", tt.name) 101 } 102 103 // Check fields. 104 if len(l.Dependencies) != len(tt.req.Dependencies) { 105 t.Errorf("%s: wrong number of dependencies in lock", tt.name) 106 } 107 d0 := l.Dependencies[0] 108 e0 := tt.expect.Dependencies[0] 109 if d0.Name != e0.Name { 110 t.Errorf("%s: expected name %s, got %s", tt.name, e0.Name, d0.Name) 111 } 112 if d0.Repository != e0.Repository { 113 t.Errorf("%s: expected repo %s, got %s", tt.name, e0.Repository, d0.Repository) 114 } 115 if d0.Version != e0.Version { 116 t.Errorf("%s: expected version %s, got %s", tt.name, e0.Version, d0.Version) 117 } 118 } 119 } 120 121 func TestHashReq(t *testing.T) { 122 expect := "sha256:e70e41f8922e19558a8bf62f591a8b70c8e4622e3c03e5415f09aba881f13885" 123 req := &chartutil.Requirements{ 124 Dependencies: []*chartutil.Dependency{ 125 {Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"}, 126 }, 127 } 128 h, err := HashReq(req) 129 if err != nil { 130 t.Fatal(err) 131 } 132 if expect != h { 133 t.Errorf("Expected %q, got %q", expect, h) 134 } 135 136 req = &chartutil.Requirements{Dependencies: []*chartutil.Dependency{}} 137 h, err = HashReq(req) 138 if err != nil { 139 t.Fatal(err) 140 } 141 if expect == h { 142 t.Errorf("Expected %q != %q", expect, h) 143 } 144 }