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" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 21 | "os" |
| 22 | "testing" |
| 23 | "time" |
| 24 | |
| 25 | "github.com/opencord/voltha-lib-go/v3/pkg/mocks" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 26 | "github.com/phayes/freeport" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 27 | "github.com/stretchr/testify/assert" |
| 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
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" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 34 | defaultTimeout = 1 |
| 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 { |
| 65 | backend := NewBackend("etcd", embedEtcdServerHost, embedEtcdServerPort, defaultTimeout, defaultPathPrefix) |
| 66 | assert.NotNil(t, backend) |
| 67 | assert.NotNil(t, backend.Client) |
| 68 | return backend |
| 69 | } |
| 70 | |
| 71 | func provisionBackendWithDummyEtcdServer(t *testing.T) *Backend { |
| 72 | backend := NewBackend("etcd", embedEtcdServerHost, dummyEtcdServerPort, defaultTimeout, defaultPathPrefix) |
| 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) { |
| 80 | backend := NewBackend("etcd", embedEtcdServerHost, embedEtcdServerPort, defaultTimeout, defaultPathPrefix) |
| 81 | |
| 82 | // Verify all attributes of backend have got set correctly |
| 83 | assert.NotNil(t, backend) |
| 84 | assert.NotNil(t, backend.Client) |
| 85 | assert.Equal(t, backend.StoreType, "etcd") |
| 86 | assert.Equal(t, backend.Host, embedEtcdServerHost) |
| 87 | assert.Equal(t, backend.Port, embedEtcdServerPort) |
| 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) { |
| 97 | backend := NewBackend("consul", embedEtcdServerHost, embedEtcdServerPort, defaultTimeout, defaultPathPrefix) |
| 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) { |
| 107 | backend := NewBackend("unknown", embedEtcdServerHost, embedEtcdServerPort, defaultTimeout, defaultPathPrefix) |
| 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) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 122 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout*time.Second) |
| 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) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 131 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout*time.Second) |
| 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) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 151 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout*time.Second) |
| 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") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 245 | assert.NotNil(t, kvpair) |
| 246 | assert.Equal(t, defaultPathPrefix+"/key1", kvpair.Key) |
| 247 | assert.Equal(t, []uint8("value1"), kvpair.Value) |
| 248 | |
| 249 | // Assert that Put overrides the Value |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 250 | err = backend.Put(ctx, "key1", []uint8("value11")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 251 | assert.Nil(t, err) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 252 | kvpair, err = backend.Get(ctx, "key1") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 253 | assert.NotNil(t, kvpair) |
| 254 | assert.Equal(t, defaultPathPrefix+"/key1", kvpair.Key) |
| 255 | assert.Equal(t, []uint8("value11"), kvpair.Value) |
| 256 | } |
| 257 | |
| 258 | // Put operation should fail against Dummy Non-existent Etcd Server |
| 259 | func TestPut_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 260 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 261 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 262 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 263 | err := backend.Put(ctx, "key1", []uint8("value1")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 264 | assert.NotNil(t, err) |
| 265 | |
| 266 | // Assert alive state is still false |
| 267 | assert.False(t, backend.alive) |
| 268 | } |
| 269 | |
| 270 | // Test Get for existing and non-existing key |
| 271 | func TestGet_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 272 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 273 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 274 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 275 | err := backend.Put(ctx, "key2", []uint8("value2")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 276 | |
| 277 | // Assert alive state has become true |
| 278 | assert.True(t, backend.alive) |
| 279 | |
| 280 | // Assert that kvstore has this key stored |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 281 | kvpair, err := backend.Get(ctx, "key2") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 282 | assert.NotNil(t, kvpair) |
| 283 | assert.Nil(t, err) |
| 284 | assert.Equal(t, defaultPathPrefix+"/key2", kvpair.Key) |
| 285 | assert.Equal(t, []uint8("value2"), kvpair.Value) |
| 286 | |
| 287 | // Assert that Get works fine for absent key3 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 288 | kvpair, err = backend.Get(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 289 | assert.Nil(t, kvpair) |
| 290 | assert.Nil(t, err) // no error as lookup is successful |
| 291 | } |
| 292 | |
| 293 | // Get operation should fail against Dummy Non-existent Etcd Server |
| 294 | func TestGet_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 295 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 296 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 297 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 298 | kvpair, err := backend.Get(ctx, "key2") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 299 | assert.NotNil(t, err) |
| 300 | assert.Nil(t, kvpair) |
| 301 | |
| 302 | // Assert alive state is still false |
| 303 | assert.False(t, backend.alive) |
| 304 | } |
| 305 | |
| 306 | // Test Delete for existing and non-existing key |
| 307 | func TestDelete_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 308 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 309 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 310 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 311 | err := backend.Put(ctx, "key3", []uint8("value3")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 312 | |
| 313 | // Assert alive state has become true |
| 314 | assert.True(t, backend.alive) |
| 315 | |
| 316 | // Assert that kvstore has this key stored |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 317 | kvpair, err := backend.Get(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 318 | assert.NotNil(t, kvpair) |
| 319 | |
| 320 | // Delete and Assert that key has been removed |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 321 | err = backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 322 | assert.Nil(t, err) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 323 | kvpair, err = backend.Get(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 324 | assert.Nil(t, kvpair) |
| 325 | |
| 326 | // Assert that Delete silently ignores absent key3 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 327 | err = backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 328 | assert.Nil(t, err) |
| 329 | } |
| 330 | |
| 331 | // Delete operation should fail against Dummy Non-existent Etcd Server |
| 332 | func TestDelete_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 333 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 334 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 335 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 336 | err := backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 337 | assert.NotNil(t, err) |
| 338 | |
| 339 | // Assert alive state is still false |
| 340 | assert.False(t, backend.alive) |
| 341 | } |
| 342 | |
| 343 | // Test List for series of values under a key path |
| 344 | func TestList_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 345 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 346 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 347 | key41 := "key4/subkey1" |
| 348 | key42 := "key4/subkey2" |
| 349 | |
| 350 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 351 | err := backend.Put(ctx, key41, []uint8("value4-1")) |
| 352 | assert.Nil(t, err) |
| 353 | err = backend.Put(ctx, key42, []uint8("value4-2")) |
| 354 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 355 | |
| 356 | // Assert alive state has become true |
| 357 | assert.True(t, backend.alive) |
| 358 | |
| 359 | // Assert that Get does not retrieve these Subkeys |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 360 | kvpair, err := backend.Get(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 361 | assert.Nil(t, kvpair) |
| 362 | assert.Nil(t, err) |
| 363 | |
| 364 | // Assert that List operation retrieves these Child Keys |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 365 | kvmap, err := backend.List(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 366 | assert.NotNil(t, kvmap) |
| 367 | assert.Nil(t, err) |
| 368 | assert.Equal(t, 2, len(kvmap)) |
| 369 | fullkey41 := defaultPathPrefix + "/" + key41 |
| 370 | fullkey42 := defaultPathPrefix + "/" + key42 |
| 371 | assert.Equal(t, fullkey41, kvmap[fullkey41].Key) |
| 372 | assert.Equal(t, []uint8("value4-1"), kvmap[fullkey41].Value) |
| 373 | assert.Equal(t, fullkey42, kvmap[fullkey42].Key) |
| 374 | assert.Equal(t, []uint8("value4-2"), kvmap[fullkey42].Value) |
| 375 | } |
| 376 | |
| 377 | // List operation should fail against Dummy Non-existent Etcd Server |
| 378 | func TestList_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 379 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 380 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 381 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 382 | kvmap, err := backend.List(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 383 | assert.Nil(t, kvmap) |
| 384 | assert.NotNil(t, err) |
| 385 | |
| 386 | // Assert alive state is still false |
| 387 | assert.False(t, backend.alive) |
| 388 | } |
| 389 | |
| 390 | // Test Create and Delete Watch for Embedded Etcd Server |
| 391 | func TestCreateWatch_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 392 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 393 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 394 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 395 | eventChan := backend.CreateWatch(ctx, "key5") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 396 | assert.NotNil(t, eventChan) |
| 397 | assert.Equal(t, 0, len(eventChan)) |
| 398 | |
| 399 | // Assert this method does not change alive state |
| 400 | assert.False(t, backend.alive) |
| 401 | |
| 402 | // Put a value for watched key and event should appear |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 403 | err := backend.Put(ctx, "key5", []uint8("value5")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 404 | assert.Nil(t, err) |
| 405 | time.Sleep(time.Millisecond * 100) |
| 406 | assert.Equal(t, 1, len(eventChan)) |
| 407 | |
| 408 | backend.DeleteWatch("key5", eventChan) |
| 409 | } |