github.com/openpitrix/helm@v3.0.0-beta.3+incompatible/pkg/storage/driver/secrets_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 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package driver 15 16 import ( 17 "encoding/base64" 18 "encoding/json" 19 "reflect" 20 "testing" 21 22 v1 "k8s.io/api/core/v1" 23 24 rspb "helm.sh/helm/pkg/release" 25 ) 26 27 func TestSecretName(t *testing.T) { 28 c := newTestFixtureSecrets(t) 29 if c.Name() != SecretsDriverName { 30 t.Errorf("Expected name to be %q, got %q", SecretsDriverName, c.Name()) 31 } 32 } 33 34 func TestSecretGet(t *testing.T) { 35 vers := 1 36 name := "smug-pigeon" 37 namespace := "default" 38 key := testKey(name, vers) 39 rel := releaseStub(name, vers, namespace, rspb.StatusDeployed) 40 41 secrets := newTestFixtureSecrets(t, []*rspb.Release{rel}...) 42 43 // get release with key 44 got, err := secrets.Get(key) 45 if err != nil { 46 t.Fatalf("Failed to get release: %s", err) 47 } 48 // compare fetched release with original 49 if !reflect.DeepEqual(rel, got) { 50 t.Errorf("Expected {%v}, got {%v}", rel, got) 51 } 52 } 53 54 func TestUNcompressedSecretGet(t *testing.T) { 55 vers := 1 56 name := "smug-pigeon" 57 namespace := "default" 58 key := testKey(name, vers) 59 rel := releaseStub(name, vers, namespace, rspb.StatusDeployed) 60 61 // Create a test fixture which contains an uncompressed release 62 secret, err := newSecretsObject(key, rel, nil) 63 if err != nil { 64 t.Fatalf("Failed to create secret: %s", err) 65 } 66 b, err := json.Marshal(rel) 67 if err != nil { 68 t.Fatalf("Failed to marshal release: %s", err) 69 } 70 secret.Data["release"] = []byte(base64.StdEncoding.EncodeToString(b)) 71 var mock MockSecretsInterface 72 mock.objects = map[string]*v1.Secret{key: secret} 73 secrets := NewSecrets(&mock) 74 75 // get release with key 76 got, err := secrets.Get(key) 77 if err != nil { 78 t.Fatalf("Failed to get release: %s", err) 79 } 80 // compare fetched release with original 81 if !reflect.DeepEqual(rel, got) { 82 t.Errorf("Expected {%v}, got {%v}", rel, got) 83 } 84 } 85 86 func TestSecretList(t *testing.T) { 87 secrets := newTestFixtureSecrets(t, []*rspb.Release{ 88 releaseStub("key-1", 1, "default", rspb.StatusUninstalled), 89 releaseStub("key-2", 1, "default", rspb.StatusUninstalled), 90 releaseStub("key-3", 1, "default", rspb.StatusDeployed), 91 releaseStub("key-4", 1, "default", rspb.StatusDeployed), 92 releaseStub("key-5", 1, "default", rspb.StatusSuperseded), 93 releaseStub("key-6", 1, "default", rspb.StatusSuperseded), 94 }...) 95 96 // list all deleted releases 97 del, err := secrets.List(func(rel *rspb.Release) bool { 98 return rel.Info.Status == rspb.StatusUninstalled 99 }) 100 // check 101 if err != nil { 102 t.Errorf("Failed to list deleted: %s", err) 103 } 104 if len(del) != 2 { 105 t.Errorf("Expected 2 deleted, got %d:\n%v\n", len(del), del) 106 } 107 108 // list all deployed releases 109 dpl, err := secrets.List(func(rel *rspb.Release) bool { 110 return rel.Info.Status == rspb.StatusDeployed 111 }) 112 // check 113 if err != nil { 114 t.Errorf("Failed to list deployed: %s", err) 115 } 116 if len(dpl) != 2 { 117 t.Errorf("Expected 2 deployed, got %d", len(dpl)) 118 } 119 120 // list all superseded releases 121 ssd, err := secrets.List(func(rel *rspb.Release) bool { 122 return rel.Info.Status == rspb.StatusSuperseded 123 }) 124 // check 125 if err != nil { 126 t.Errorf("Failed to list superseded: %s", err) 127 } 128 if len(ssd) != 2 { 129 t.Errorf("Expected 2 superseded, got %d", len(ssd)) 130 } 131 } 132 133 func TestSecretCreate(t *testing.T) { 134 secrets := newTestFixtureSecrets(t) 135 136 vers := 1 137 name := "smug-pigeon" 138 namespace := "default" 139 key := testKey(name, vers) 140 rel := releaseStub(name, vers, namespace, rspb.StatusDeployed) 141 142 // store the release in a secret 143 if err := secrets.Create(key, rel); err != nil { 144 t.Fatalf("Failed to create release with key %q: %s", key, err) 145 } 146 147 // get the release back 148 got, err := secrets.Get(key) 149 if err != nil { 150 t.Fatalf("Failed to get release with key %q: %s", key, err) 151 } 152 153 // compare created release with original 154 if !reflect.DeepEqual(rel, got) { 155 t.Errorf("Expected {%v}, got {%v}", rel, got) 156 } 157 } 158 159 func TestSecretUpdate(t *testing.T) { 160 vers := 1 161 name := "smug-pigeon" 162 namespace := "default" 163 key := testKey(name, vers) 164 rel := releaseStub(name, vers, namespace, rspb.StatusDeployed) 165 166 secrets := newTestFixtureSecrets(t, []*rspb.Release{rel}...) 167 168 // modify release status code 169 rel.Info.Status = rspb.StatusSuperseded 170 171 // perform the update 172 if err := secrets.Update(key, rel); err != nil { 173 t.Fatalf("Failed to update release: %s", err) 174 } 175 176 // fetch the updated release 177 got, err := secrets.Get(key) 178 if err != nil { 179 t.Fatalf("Failed to get release with key %q: %s", key, err) 180 } 181 182 // check release has actually been updated by comparing modified fields 183 if rel.Info.Status != got.Info.Status { 184 t.Errorf("Expected status %s, got status %s", rel.Info.Status.String(), got.Info.Status.String()) 185 } 186 }