Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 db |
| 18 | |
| 19 | import ( |
| 20 | "context" |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 21 | mocks "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 22 | "github.com/phayes/freeport" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
| 24 | "google.golang.org/grpc/codes" |
| 25 | "google.golang.org/grpc/status" |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 26 | "os" |
| 27 | "strconv" |
| 28 | "testing" |
| 29 | "time" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 30 | ) |
| 31 | |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 32 | const ( |
| 33 | embedEtcdServerHost = "localhost" |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 34 | defaultTimeout = 1 * time.Second |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 35 | defaultPathPrefix = "Prefix" |
| 36 | ) |
| 37 | |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 38 | var ( |
| 39 | embedEtcdServerPort int |
| 40 | dummyEtcdServerPort int |
| 41 | ) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 42 | |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 43 | func TestMain(m *testing.M) { |
| 44 | var err error |
| 45 | embedEtcdServerPort, err = freeport.GetFreePort() |
| 46 | if err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 47 | logger.Fatal(err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 48 | } |
| 49 | dummyEtcdServerPort, err = freeport.GetFreePort() |
| 50 | if err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 51 | logger.Fatal(err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 52 | } |
| 53 | peerPort, err := freeport.GetFreePort() |
| 54 | if err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 55 | logger.Fatal(err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 56 | } |
| 57 | etcdServer := mocks.StartEtcdServer(mocks.MKConfig("voltha.db.test", embedEtcdServerPort, peerPort, "voltha.lib.db", "error")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 58 | res := m.Run() |
| 59 | |
| 60 | etcdServer.Stop() |
| 61 | os.Exit(res) |
| 62 | } |
| 63 | |
| 64 | func provisionBackendWithEmbeddedEtcdServer(t *testing.T) *Backend { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 65 | backend := NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 66 | assert.NotNil(t, backend) |
| 67 | assert.NotNil(t, backend.Client) |
| 68 | return backend |
| 69 | } |
| 70 | |
| 71 | func provisionBackendWithDummyEtcdServer(t *testing.T) *Backend { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 72 | backend := NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(dummyEtcdServerPort), defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 73 | assert.NotNil(t, backend) |
| 74 | assert.NotNil(t, backend.Client) |
| 75 | return backend |
| 76 | } |
| 77 | |
| 78 | // Create instance using Etcd Kvstore |
| 79 | func TestNewBackend_EtcdKvStore(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 80 | address := embedEtcdServerHost + ":" + strconv.Itoa(embedEtcdServerPort) |
| 81 | backend := NewBackend("etcd", address, defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 82 | |
| 83 | // Verify all attributes of backend have got set correctly |
| 84 | assert.NotNil(t, backend) |
| 85 | assert.NotNil(t, backend.Client) |
| 86 | assert.Equal(t, backend.StoreType, "etcd") |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 87 | assert.Equal(t, backend.Address, address) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 88 | assert.Equal(t, backend.Timeout, defaultTimeout) |
| 89 | assert.Equal(t, backend.PathPrefix, defaultPathPrefix) |
| 90 | assert.Equal(t, backend.alive, false) // backend is not alive at start |
| 91 | assert.Nil(t, backend.liveness) // no liveness channel is created at start |
| 92 | assert.Equal(t, backend.LivenessChannelInterval, DefaultLivenessChannelInterval) |
| 93 | } |
| 94 | |
| 95 | // Create instance using Consul Kvstore |
| 96 | func TestNewBackend_ConsulKvStore(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 97 | backend := NewBackend("consul", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 98 | |
| 99 | // Verify kvstore type attribute of backend has got set correctly |
| 100 | assert.NotNil(t, backend) |
| 101 | assert.NotNil(t, backend.Client) |
| 102 | assert.Equal(t, backend.StoreType, "consul") |
| 103 | } |
| 104 | |
| 105 | // Create instance using Invalid Kvstore; instance creation should fail |
| 106 | func TestNewBackend_InvalidKvstore(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 107 | backend := NewBackend("unknown", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 108 | |
| 109 | assert.NotNil(t, backend) |
| 110 | assert.Nil(t, backend.Client) |
| 111 | } |
| 112 | |
| 113 | func TestMakePath(t *testing.T) { |
| 114 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 115 | path := backend.makePath("Suffix") |
| 116 | assert.Equal(t, defaultPathPrefix+"/Suffix", path) |
| 117 | } |
| 118 | |
| 119 | // Liveness Check against Embedded Etcd Server should return alive state |
| 120 | func TestPerformLivenessCheck_EmbeddedEtcdServer(t *testing.T) { |
| 121 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 122 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 123 | defer cancel() |
| 124 | alive := backend.PerformLivenessCheck(ctx) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 125 | assert.True(t, alive) |
| 126 | } |
| 127 | |
| 128 | // Liveness Check against Dummy Etcd Server should return not-alive state |
| 129 | func TestPerformLivenessCheck_DummyEtcdServer(t *testing.T) { |
| 130 | backend := provisionBackendWithDummyEtcdServer(t) |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 131 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 132 | defer cancel() |
| 133 | alive := backend.PerformLivenessCheck(ctx) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 134 | assert.False(t, alive) |
| 135 | } |
| 136 | |
| 137 | // Enabling Liveness Channel before First Liveness Check |
| 138 | func TestEnableLivenessChannel_EmbeddedEtcdServer_BeforeLivenessCheck(t *testing.T) { |
| 139 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 140 | |
| 141 | alive := backend.EnableLivenessChannel() |
| 142 | assert.NotNil(t, alive) |
| 143 | assert.Equal(t, 1, len(alive)) |
| 144 | assert.Equal(t, false, <-alive) |
| 145 | assert.NotNil(t, backend.liveness) |
| 146 | } |
| 147 | |
| 148 | // Enabling Liveness Channel after First Liveness Check |
| 149 | func TestEnableLivenessChannel_EmbeddedEtcdServer_AfterLivenessCheck(t *testing.T) { |
| 150 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 151 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 152 | defer cancel() |
| 153 | backend.PerformLivenessCheck(ctx) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 154 | |
| 155 | alive := backend.EnableLivenessChannel() |
| 156 | assert.NotNil(t, alive) |
| 157 | assert.Equal(t, 1, len(alive)) |
| 158 | assert.Equal(t, true, <-alive) |
| 159 | assert.NotNil(t, backend.liveness) |
| 160 | } |
| 161 | |
| 162 | // Update Liveness with alive status change |
| 163 | func TestUpdateLiveness_AliveStatusChange(t *testing.T) { |
| 164 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 165 | // Enable Liveness Channel and verify initial state is not-alive |
| 166 | aliveState := backend.EnableLivenessChannel() |
| 167 | assert.NotNil(t, aliveState) |
| 168 | assert.Equal(t, 1, len(backend.liveness)) |
| 169 | assert.Equal(t, false, <-backend.liveness) |
| 170 | lastUpdateTime := backend.lastLivenessTime |
| 171 | |
| 172 | // Update with changed alive state. Verify alive state push & liveness time update |
| 173 | backend.updateLiveness(true) |
| 174 | assert.Equal(t, 1, len(backend.liveness)) |
| 175 | assert.Equal(t, true, <-backend.liveness) |
| 176 | assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime) |
| 177 | } |
| 178 | |
| 179 | // Update Liveness with same alive status reporting |
| 180 | func TestUpdateLiveness_AliveStatusUnchanged(t *testing.T) { |
| 181 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 182 | // Enable Liveness Channel and verify initial state is not-alive |
| 183 | aliveState := backend.EnableLivenessChannel() |
| 184 | assert.NotNil(t, aliveState) |
| 185 | assert.Equal(t, false, <-backend.liveness) |
| 186 | lastUpdateTime := backend.lastLivenessTime |
| 187 | |
| 188 | // Update with same alive state. Verify no further alive state push |
| 189 | backend.updateLiveness(false) |
| 190 | assert.Equal(t, 0, len(backend.liveness)) |
| 191 | assert.Equal(t, lastUpdateTime, backend.lastLivenessTime) |
| 192 | |
| 193 | // Now set lastUpdateTime 10 min back and push again |
| 194 | tenMinDuration, _ := time.ParseDuration("10m") |
| 195 | backend.lastLivenessTime = time.Now().Add(-tenMinDuration) |
| 196 | lastUpdateTime = backend.lastLivenessTime |
| 197 | |
| 198 | backend.updateLiveness(false) |
| 199 | assert.Equal(t, 1, len(backend.liveness)) |
| 200 | assert.Equal(t, false, <-backend.liveness) |
| 201 | assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime) |
| 202 | } |
| 203 | |
| 204 | func TestIsErrorIndicatingAliveKvstore(t *testing.T) { |
| 205 | tests := []struct { |
| 206 | name string |
| 207 | arg error |
| 208 | want bool |
| 209 | }{ |
| 210 | {"No Error", nil, true}, |
| 211 | {"Request Canceled", context.Canceled, true}, |
| 212 | {"Request Timeout", context.DeadlineExceeded, false}, |
| 213 | {"Etcd Error - InvalidArgument", status.New(codes.InvalidArgument, "").Err(), true}, |
| 214 | {"Etcd Error - DeadlineExceeded", status.New(codes.DeadlineExceeded, "").Err(), false}, |
| 215 | {"Etcd Error - Unavailable", status.New(codes.Unavailable, "").Err(), false}, |
| 216 | {"Etcd Error - DataLoss", status.New(codes.DataLoss, "").Err(), false}, |
| 217 | {"Etcd Error - NotFound", status.New(codes.NotFound, "").Err(), true}, |
| 218 | {"Etcd Error - PermissionDenied ", status.New(codes.PermissionDenied, "").Err(), true}, |
| 219 | {"Etcd Error - FailedPrecondition ", status.New(codes.FailedPrecondition, "").Err(), true}, |
| 220 | } |
| 221 | |
| 222 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 223 | |
| 224 | for _, tt := range tests { |
| 225 | t.Run(tt.name, func(t *testing.T) { |
| 226 | if backend.isErrorIndicatingAliveKvstore(tt.arg) != tt.want { |
| 227 | t.Errorf("isErrorIndicatingAliveKvstore failed for %s: expected %t but got %t", tt.name, tt.want, !tt.want) |
| 228 | } |
| 229 | }) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | func TestPut_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 234 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 235 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 236 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 237 | err := backend.Put(ctx, "key1", []uint8("value1")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 238 | assert.Nil(t, err) |
| 239 | |
| 240 | // Assert alive state has become true |
| 241 | assert.True(t, backend.alive) |
| 242 | |
| 243 | // Assert that kvstore has this value stored |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 244 | kvpair, err := backend.Get(ctx, "key1") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 245 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 246 | assert.NotNil(t, kvpair) |
| 247 | assert.Equal(t, defaultPathPrefix+"/key1", kvpair.Key) |
| 248 | assert.Equal(t, []uint8("value1"), kvpair.Value) |
| 249 | |
| 250 | // Assert that Put overrides the Value |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 251 | err = backend.Put(ctx, "key1", []uint8("value11")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 252 | assert.Nil(t, err) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 253 | kvpair, err = backend.Get(ctx, "key1") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 254 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 255 | assert.NotNil(t, kvpair) |
| 256 | assert.Equal(t, defaultPathPrefix+"/key1", kvpair.Key) |
| 257 | assert.Equal(t, []uint8("value11"), kvpair.Value) |
| 258 | } |
| 259 | |
| 260 | // Put operation should fail against Dummy Non-existent Etcd Server |
| 261 | func TestPut_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 262 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 263 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 264 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 265 | err := backend.Put(ctx, "key1", []uint8("value1")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 266 | assert.NotNil(t, err) |
| 267 | |
| 268 | // Assert alive state is still false |
| 269 | assert.False(t, backend.alive) |
| 270 | } |
| 271 | |
| 272 | // Test Get for existing and non-existing key |
| 273 | func TestGet_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 274 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 275 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 276 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 277 | err := backend.Put(ctx, "key2", []uint8("value2")) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 278 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 279 | |
| 280 | // Assert alive state has become true |
| 281 | assert.True(t, backend.alive) |
| 282 | |
| 283 | // Assert that kvstore has this key stored |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 284 | kvpair, err := backend.Get(ctx, "key2") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 285 | assert.NotNil(t, kvpair) |
| 286 | assert.Nil(t, err) |
| 287 | assert.Equal(t, defaultPathPrefix+"/key2", kvpair.Key) |
| 288 | assert.Equal(t, []uint8("value2"), kvpair.Value) |
| 289 | |
| 290 | // Assert that Get works fine for absent key3 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 291 | kvpair, err = backend.Get(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 292 | assert.Nil(t, err) // no error as lookup is successful |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 293 | assert.Nil(t, kvpair) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // Get operation should fail against Dummy Non-existent Etcd Server |
| 297 | func TestGet_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 298 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 299 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 300 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 301 | kvpair, err := backend.Get(ctx, "key2") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 302 | assert.NotNil(t, err) |
| 303 | assert.Nil(t, kvpair) |
| 304 | |
| 305 | // Assert alive state is still false |
| 306 | assert.False(t, backend.alive) |
| 307 | } |
| 308 | |
| 309 | // Test Delete for existing and non-existing key |
| 310 | func TestDelete_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 311 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 312 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 313 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 314 | err := backend.Put(ctx, "key3", []uint8("value3")) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 315 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 316 | |
| 317 | // Assert alive state has become true |
| 318 | assert.True(t, backend.alive) |
| 319 | |
| 320 | // Assert that kvstore has this key stored |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 321 | kvpair, err := backend.Get(ctx, "key3") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 322 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 323 | assert.NotNil(t, kvpair) |
| 324 | |
| 325 | // Delete and Assert that key has been removed |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 326 | err = backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 327 | assert.Nil(t, err) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 328 | kvpair, err = backend.Get(ctx, "key3") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 329 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 330 | assert.Nil(t, kvpair) |
| 331 | |
| 332 | // Assert that Delete silently ignores absent key3 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 333 | err = backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 334 | assert.Nil(t, err) |
| 335 | } |
| 336 | |
| 337 | // Delete operation should fail against Dummy Non-existent Etcd Server |
| 338 | func TestDelete_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 339 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 340 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 341 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 342 | err := backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 343 | assert.NotNil(t, err) |
| 344 | |
| 345 | // Assert alive state is still false |
| 346 | assert.False(t, backend.alive) |
| 347 | } |
| 348 | |
| 349 | // Test List for series of values under a key path |
| 350 | func TestList_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 351 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 352 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 353 | key41 := "key4/subkey1" |
| 354 | key42 := "key4/subkey2" |
| 355 | |
| 356 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 357 | err := backend.Put(ctx, key41, []uint8("value4-1")) |
| 358 | assert.Nil(t, err) |
| 359 | err = backend.Put(ctx, key42, []uint8("value4-2")) |
| 360 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 361 | |
| 362 | // Assert alive state has become true |
| 363 | assert.True(t, backend.alive) |
| 364 | |
| 365 | // Assert that Get does not retrieve these Subkeys |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 366 | kvpair, err := backend.Get(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 367 | assert.Nil(t, kvpair) |
| 368 | assert.Nil(t, err) |
| 369 | |
| 370 | // Assert that List operation retrieves these Child Keys |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 371 | kvmap, err := backend.List(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 372 | assert.NotNil(t, kvmap) |
| 373 | assert.Nil(t, err) |
| 374 | assert.Equal(t, 2, len(kvmap)) |
| 375 | fullkey41 := defaultPathPrefix + "/" + key41 |
| 376 | fullkey42 := defaultPathPrefix + "/" + key42 |
| 377 | assert.Equal(t, fullkey41, kvmap[fullkey41].Key) |
| 378 | assert.Equal(t, []uint8("value4-1"), kvmap[fullkey41].Value) |
| 379 | assert.Equal(t, fullkey42, kvmap[fullkey42].Key) |
| 380 | assert.Equal(t, []uint8("value4-2"), kvmap[fullkey42].Value) |
| 381 | } |
| 382 | |
| 383 | // List operation should fail against Dummy Non-existent Etcd Server |
| 384 | func TestList_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 385 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 386 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 387 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 388 | kvmap, err := backend.List(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 389 | assert.Nil(t, kvmap) |
| 390 | assert.NotNil(t, err) |
| 391 | |
| 392 | // Assert alive state is still false |
| 393 | assert.False(t, backend.alive) |
| 394 | } |
| 395 | |
| 396 | // Test Create and Delete Watch for Embedded Etcd Server |
| 397 | func TestCreateWatch_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 398 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 399 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 400 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 401 | eventChan := backend.CreateWatch(ctx, "key5", false) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 402 | assert.NotNil(t, eventChan) |
| 403 | assert.Equal(t, 0, len(eventChan)) |
| 404 | |
| 405 | // Assert this method does not change alive state |
| 406 | assert.False(t, backend.alive) |
| 407 | |
| 408 | // Put a value for watched key and event should appear |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 409 | err := backend.Put(ctx, "key5", []uint8("value5")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 410 | assert.Nil(t, err) |
| 411 | time.Sleep(time.Millisecond * 100) |
| 412 | assert.Equal(t, 1, len(eventChan)) |
| 413 | |
| 414 | backend.DeleteWatch("key5", eventChan) |
| 415 | } |
divyadesai | d737eb6 | 2020-03-26 06:52:20 +0000 | [diff] [blame] | 416 | |
| 417 | // Test Create and Delete Watch with prefix for Embedded Etcd Server |
| 418 | func TestCreateWatch_With_Prefix_EmbeddedEtcdServer(t *testing.T) { |
| 419 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 420 | defer cancel() |
| 421 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 422 | eventChan := backend.CreateWatch(ctx, "key6", true) |
| 423 | assert.NotNil(t, eventChan) |
| 424 | assert.Equal(t, 0, len(eventChan)) |
| 425 | |
| 426 | // Assert this method does not change alive state |
| 427 | assert.False(t, backend.alive) |
| 428 | |
| 429 | // Put a value for watched key and event should appear |
| 430 | err := backend.Put(ctx, "key6/is-a-prefix", []uint8("value5")) |
| 431 | assert.Nil(t, err) |
| 432 | time.Sleep(time.Millisecond * 100) |
| 433 | assert.Equal(t, 1, len(eventChan)) |
| 434 | |
| 435 | backend.DeleteWatch("key6", eventChan) |
| 436 | } |