github.com/lrills/helm@v2.8.1+incompatible/pkg/storage/driver/memory_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package driver 18 19 import ( 20 "fmt" 21 "reflect" 22 "testing" 23 24 rspb "k8s.io/helm/pkg/proto/hapi/release" 25 ) 26 27 func TestMemoryName(t *testing.T) { 28 if mem := NewMemory(); mem.Name() != MemoryDriverName { 29 t.Errorf("Expected name to be %q, got %q", MemoryDriverName, mem.Name()) 30 } 31 } 32 33 func TestMemoryCreate(t *testing.T) { 34 var tests = []struct { 35 desc string 36 rls *rspb.Release 37 err bool 38 }{ 39 { 40 "create should success", 41 releaseStub("rls-c", 1, "default", rspb.Status_DEPLOYED), 42 false, 43 }, 44 { 45 "create should fail (release already exists)", 46 releaseStub("rls-a", 1, "default", rspb.Status_DEPLOYED), 47 true, 48 }, 49 } 50 51 ts := tsFixtureMemory(t) 52 for _, tt := range tests { 53 key := testKey(tt.rls.Name, tt.rls.Version) 54 rls := tt.rls 55 56 if err := ts.Create(key, rls); err != nil { 57 if !tt.err { 58 t.Fatalf("failed to create %q: %s", tt.desc, err) 59 } 60 } 61 } 62 } 63 64 func TestMemoryGet(t *testing.T) { 65 var tests = []struct { 66 desc string 67 key string 68 err bool 69 }{ 70 {"release key should exist", "rls-a.v1", false}, 71 {"release key should not exist", "rls-a.v5", true}, 72 } 73 74 ts := tsFixtureMemory(t) 75 for _, tt := range tests { 76 if _, err := ts.Get(tt.key); err != nil { 77 if !tt.err { 78 t.Fatalf("Failed %q to get '%s': %q\n", tt.desc, tt.key, err) 79 } 80 } 81 } 82 } 83 84 func TestMemoryQuery(t *testing.T) { 85 var tests = []struct { 86 desc string 87 xlen int 88 lbs map[string]string 89 }{ 90 { 91 "should be 2 query results", 92 2, 93 map[string]string{"STATUS": "DEPLOYED"}, 94 }, 95 } 96 97 ts := tsFixtureMemory(t) 98 for _, tt := range tests { 99 l, err := ts.Query(tt.lbs) 100 if err != nil { 101 t.Fatalf("Failed to query: %s\n", err) 102 } 103 104 if tt.xlen != len(l) { 105 t.Fatalf("Expected %d results, actual %d\n", tt.xlen, len(l)) 106 } 107 } 108 } 109 110 func TestMemoryUpdate(t *testing.T) { 111 var tests = []struct { 112 desc string 113 key string 114 rls *rspb.Release 115 err bool 116 }{ 117 { 118 "update release status", 119 "rls-a.v4", 120 releaseStub("rls-a", 4, "default", rspb.Status_SUPERSEDED), 121 false, 122 }, 123 { 124 "update release does not exist", 125 "rls-z.v1", 126 releaseStub("rls-z", 1, "default", rspb.Status_DELETED), 127 true, 128 }, 129 } 130 131 ts := tsFixtureMemory(t) 132 for _, tt := range tests { 133 if err := ts.Update(tt.key, tt.rls); err != nil { 134 if !tt.err { 135 t.Fatalf("Failed %q: %s\n", tt.desc, err) 136 } 137 continue 138 } 139 140 r, err := ts.Get(tt.key) 141 if err != nil { 142 t.Fatalf("Failed to get: %s\n", err) 143 } 144 145 if !reflect.DeepEqual(r, tt.rls) { 146 t.Fatalf("Expected %s, actual %s\n", tt.rls, r) 147 } 148 } 149 } 150 151 func TestMemoryDelete(t *testing.T) { 152 var tests = []struct { 153 desc string 154 key string 155 err bool 156 }{ 157 {"release key should exist", "rls-a.v1", false}, 158 {"release key should not exist", "rls-a.v5", true}, 159 } 160 161 ts := tsFixtureMemory(t) 162 start, err := ts.Query(map[string]string{"NAME": "rls-a"}) 163 if err != nil { 164 t.Errorf("Query failed: %s", err) 165 } 166 startLen := len(start) 167 for _, tt := range tests { 168 if rel, err := ts.Delete(tt.key); err != nil { 169 if !tt.err { 170 t.Fatalf("Failed %q to get '%s': %q\n", tt.desc, tt.key, err) 171 } 172 continue 173 } else if fmt.Sprintf("%s.v%d", rel.Name, rel.Version) != tt.key { 174 t.Fatalf("Asked for delete on %s, but deleted %d", tt.key, rel.Version) 175 } 176 _, err := ts.Get(tt.key) 177 if err == nil { 178 t.Errorf("Expected an error when asking for a deleted key") 179 } 180 } 181 182 // Make sure that the deleted records are gone. 183 end, err := ts.Query(map[string]string{"NAME": "rls-a"}) 184 if err != nil { 185 t.Errorf("Query failed: %s", err) 186 } 187 endLen := len(end) 188 189 if startLen <= endLen { 190 t.Errorf("expected start %d to be less than end %d", startLen, endLen) 191 for _, ee := range end { 192 t.Logf("Name: %s, Version: %d", ee.Name, ee.Version) 193 } 194 } 195 196 }