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