blob: 98b72a08247fc898e5b3aa3d074f65b455c2bd98 [file] [log] [blame]
Girish Kumarca522102019-11-08 11:26:35 +00001/*
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
17package db
18
19import (
20 "context"
khenaidoob6238b32020-04-07 12:07:36 -040021 mocks "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd"
khenaidooc7005fc2019-11-18 19:23:57 -050022 "github.com/phayes/freeport"
Girish Kumarca522102019-11-08 11:26:35 +000023 "github.com/stretchr/testify/assert"
24 "google.golang.org/grpc/codes"
25 "google.golang.org/grpc/status"
Neha Sharmadd9af392020-04-28 09:03:57 +000026 "os"
27 "strconv"
28 "testing"
29 "time"
Girish Kumarca522102019-11-08 11:26:35 +000030)
31
Girish Kumarca522102019-11-08 11:26:35 +000032const (
33 embedEtcdServerHost = "localhost"
Neha Sharma130ac6d2020-04-08 08:46:32 +000034 defaultTimeout = 1 * time.Second
Girish Kumarca522102019-11-08 11:26:35 +000035 defaultPathPrefix = "Prefix"
36)
37
khenaidooc7005fc2019-11-18 19:23:57 -050038var (
39 embedEtcdServerPort int
40 dummyEtcdServerPort int
41)
Girish Kumarca522102019-11-08 11:26:35 +000042
khenaidooc7005fc2019-11-18 19:23:57 -050043func TestMain(m *testing.M) {
Neha Sharma94f16a92020-06-26 04:17:55 +000044 ctx := context.Background()
khenaidooc7005fc2019-11-18 19:23:57 -050045 var err error
46 embedEtcdServerPort, err = freeport.GetFreePort()
47 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000048 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050049 }
50 dummyEtcdServerPort, err = freeport.GetFreePort()
51 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000052 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050053 }
54 peerPort, err := freeport.GetFreePort()
55 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000056 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050057 }
Neha Sharma94f16a92020-06-26 04:17:55 +000058 etcdServer := mocks.StartEtcdServer(ctx, mocks.MKConfig(ctx, "voltha.db.test", embedEtcdServerPort, peerPort, "voltha.lib.db", "error"))
Girish Kumarca522102019-11-08 11:26:35 +000059 res := m.Run()
60
Neha Sharma94f16a92020-06-26 04:17:55 +000061 etcdServer.Stop(ctx)
Girish Kumarca522102019-11-08 11:26:35 +000062 os.Exit(res)
63}
64
65func provisionBackendWithEmbeddedEtcdServer(t *testing.T) *Backend {
Neha Sharma94f16a92020-06-26 04:17:55 +000066 ctx := context.Background()
67 backend := NewBackend(ctx, "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +000068 assert.NotNil(t, backend)
69 assert.NotNil(t, backend.Client)
70 return backend
71}
72
73func provisionBackendWithDummyEtcdServer(t *testing.T) *Backend {
Neha Sharma94f16a92020-06-26 04:17:55 +000074 backend := NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(dummyEtcdServerPort), defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +000075 assert.NotNil(t, backend)
76 assert.NotNil(t, backend.Client)
77 return backend
78}
79
80// Create instance using Etcd Kvstore
81func TestNewBackend_EtcdKvStore(t *testing.T) {
Neha Sharmadd9af392020-04-28 09:03:57 +000082 address := embedEtcdServerHost + ":" + strconv.Itoa(embedEtcdServerPort)
Neha Sharma94f16a92020-06-26 04:17:55 +000083 backend := NewBackend(context.Background(), "etcd", address, defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +000084
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 Sharmadd9af392020-04-28 09:03:57 +000089 assert.Equal(t, backend.Address, address)
Girish Kumarca522102019-11-08 11:26:35 +000090 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
98func TestNewBackend_ConsulKvStore(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +000099 backend := NewBackend(context.Background(), "consul", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +0000100
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
108func TestNewBackend_InvalidKvstore(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000109 backend := NewBackend(context.Background(), "unknown", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +0000110
111 assert.NotNil(t, backend)
112 assert.Nil(t, backend.Client)
113}
114
115func TestMakePath(t *testing.T) {
116 backend := provisionBackendWithEmbeddedEtcdServer(t)
Neha Sharma94f16a92020-06-26 04:17:55 +0000117 path := backend.makePath(context.Background(), "Suffix")
Girish Kumarca522102019-11-08 11:26:35 +0000118 assert.Equal(t, defaultPathPrefix+"/Suffix", path)
119}
120
121// Liveness Check against Embedded Etcd Server should return alive state
122func TestPerformLivenessCheck_EmbeddedEtcdServer(t *testing.T) {
123 backend := provisionBackendWithEmbeddedEtcdServer(t)
Neha Sharma130ac6d2020-04-08 08:46:32 +0000124 ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
npujar5bf737f2020-01-16 19:35:25 +0530125 defer cancel()
126 alive := backend.PerformLivenessCheck(ctx)
Girish Kumarca522102019-11-08 11:26:35 +0000127 assert.True(t, alive)
128}
129
130// Liveness Check against Dummy Etcd Server should return not-alive state
131func TestPerformLivenessCheck_DummyEtcdServer(t *testing.T) {
132 backend := provisionBackendWithDummyEtcdServer(t)
Neha Sharma130ac6d2020-04-08 08:46:32 +0000133 ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
npujar5bf737f2020-01-16 19:35:25 +0530134 defer cancel()
135 alive := backend.PerformLivenessCheck(ctx)
Girish Kumarca522102019-11-08 11:26:35 +0000136 assert.False(t, alive)
137}
138
139// Enabling Liveness Channel before First Liveness Check
140func TestEnableLivenessChannel_EmbeddedEtcdServer_BeforeLivenessCheck(t *testing.T) {
141 backend := provisionBackendWithEmbeddedEtcdServer(t)
142
Neha Sharma94f16a92020-06-26 04:17:55 +0000143 alive := backend.EnableLivenessChannel(context.Background())
Girish Kumarca522102019-11-08 11:26:35 +0000144 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
151func TestEnableLivenessChannel_EmbeddedEtcdServer_AfterLivenessCheck(t *testing.T) {
152 backend := provisionBackendWithEmbeddedEtcdServer(t)
Neha Sharma130ac6d2020-04-08 08:46:32 +0000153 ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
npujar5bf737f2020-01-16 19:35:25 +0530154 defer cancel()
155 backend.PerformLivenessCheck(ctx)
Girish Kumarca522102019-11-08 11:26:35 +0000156
Neha Sharma94f16a92020-06-26 04:17:55 +0000157 alive := backend.EnableLivenessChannel(ctx)
Girish Kumarca522102019-11-08 11:26:35 +0000158 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
165func TestUpdateLiveness_AliveStatusChange(t *testing.T) {
166 backend := provisionBackendWithEmbeddedEtcdServer(t)
167 // Enable Liveness Channel and verify initial state is not-alive
Neha Sharma94f16a92020-06-26 04:17:55 +0000168 aliveState := backend.EnableLivenessChannel(context.Background())
Girish Kumarca522102019-11-08 11:26:35 +0000169 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 Sharma94f16a92020-06-26 04:17:55 +0000175 backend.updateLiveness(context.Background(), true)
Girish Kumarca522102019-11-08 11:26:35 +0000176 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
182func TestUpdateLiveness_AliveStatusUnchanged(t *testing.T) {
183 backend := provisionBackendWithEmbeddedEtcdServer(t)
184 // Enable Liveness Channel and verify initial state is not-alive
Neha Sharma94f16a92020-06-26 04:17:55 +0000185 aliveState := backend.EnableLivenessChannel(context.Background())
Girish Kumarca522102019-11-08 11:26:35 +0000186 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 Sharma94f16a92020-06-26 04:17:55 +0000191 backend.updateLiveness(context.Background(), false)
Girish Kumarca522102019-11-08 11:26:35 +0000192 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 Sharma94f16a92020-06-26 04:17:55 +0000200 backend.updateLiveness(context.Background(), false)
Girish Kumarca522102019-11-08 11:26:35 +0000201 assert.Equal(t, 1, len(backend.liveness))
202 assert.Equal(t, false, <-backend.liveness)
203 assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime)
204}
205
206func 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 Sharma94f16a92020-06-26 04:17:55 +0000228 if backend.isErrorIndicatingAliveKvstore(context.Background(), tt.arg) != tt.want {
Girish Kumarca522102019-11-08 11:26:35 +0000229 t.Errorf("isErrorIndicatingAliveKvstore failed for %s: expected %t but got %t", tt.name, tt.want, !tt.want)
230 }
231 })
232 }
233}
234
235func TestPut_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530236 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
237 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000238 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530239 err := backend.Put(ctx, "key1", []uint8("value1"))
Girish Kumarca522102019-11-08 11:26:35 +0000240 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
npujar5bf737f2020-01-16 19:35:25 +0530246 kvpair, err := backend.Get(ctx, "key1")
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800247 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000248 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
npujar5bf737f2020-01-16 19:35:25 +0530253 err = backend.Put(ctx, "key1", []uint8("value11"))
Girish Kumarca522102019-11-08 11:26:35 +0000254 assert.Nil(t, err)
npujar5bf737f2020-01-16 19:35:25 +0530255 kvpair, err = backend.Get(ctx, "key1")
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800256 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000257 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
263func TestPut_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530264 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
265 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000266 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530267 err := backend.Put(ctx, "key1", []uint8("value1"))
Girish Kumarca522102019-11-08 11:26:35 +0000268 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
275func TestGet_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530276 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
277 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000278 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530279 err := backend.Put(ctx, "key2", []uint8("value2"))
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800280 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000281
282 // Assert alive state has become true
283 assert.True(t, backend.alive)
284
285 // Assert that kvstore has this key stored
npujar5bf737f2020-01-16 19:35:25 +0530286 kvpair, err := backend.Get(ctx, "key2")
Girish Kumarca522102019-11-08 11:26:35 +0000287 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
npujar5bf737f2020-01-16 19:35:25 +0530293 kvpair, err = backend.Get(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000294 assert.Nil(t, err) // no error as lookup is successful
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800295 assert.Nil(t, kvpair)
Girish Kumarca522102019-11-08 11:26:35 +0000296}
297
298// Get operation should fail against Dummy Non-existent Etcd Server
299func TestGet_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530300 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
301 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000302 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530303 kvpair, err := backend.Get(ctx, "key2")
Girish Kumarca522102019-11-08 11:26:35 +0000304 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
312func TestDelete_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530313 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
314 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000315 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530316 err := backend.Put(ctx, "key3", []uint8("value3"))
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800317 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000318
319 // Assert alive state has become true
320 assert.True(t, backend.alive)
321
322 // Assert that kvstore has this key stored
npujar5bf737f2020-01-16 19:35:25 +0530323 kvpair, err := backend.Get(ctx, "key3")
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800324 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000325 assert.NotNil(t, kvpair)
326
327 // Delete and Assert that key has been removed
npujar5bf737f2020-01-16 19:35:25 +0530328 err = backend.Delete(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000329 assert.Nil(t, err)
npujar5bf737f2020-01-16 19:35:25 +0530330 kvpair, err = backend.Get(ctx, "key3")
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800331 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000332 assert.Nil(t, kvpair)
333
334 // Assert that Delete silently ignores absent key3
npujar5bf737f2020-01-16 19:35:25 +0530335 err = backend.Delete(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000336 assert.Nil(t, err)
337}
338
339// Delete operation should fail against Dummy Non-existent Etcd Server
340func TestDelete_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530341 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
342 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000343 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530344 err := backend.Delete(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000345 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
352func TestList_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530353 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
354 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000355 key41 := "key4/subkey1"
356 key42 := "key4/subkey2"
357
358 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530359 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 Kumarca522102019-11-08 11:26:35 +0000363
364 // Assert alive state has become true
365 assert.True(t, backend.alive)
366
367 // Assert that Get does not retrieve these Subkeys
npujar5bf737f2020-01-16 19:35:25 +0530368 kvpair, err := backend.Get(ctx, "key4")
Girish Kumarca522102019-11-08 11:26:35 +0000369 assert.Nil(t, kvpair)
370 assert.Nil(t, err)
371
372 // Assert that List operation retrieves these Child Keys
npujar5bf737f2020-01-16 19:35:25 +0530373 kvmap, err := backend.List(ctx, "key4")
Girish Kumarca522102019-11-08 11:26:35 +0000374 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
386func TestList_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530387 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
388 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000389 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530390 kvmap, err := backend.List(ctx, "key4")
Girish Kumarca522102019-11-08 11:26:35 +0000391 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
399func TestCreateWatch_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530400 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
401 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000402 backend := provisionBackendWithEmbeddedEtcdServer(t)
divyadesai8bf96862020-02-07 12:24:26 +0000403 eventChan := backend.CreateWatch(ctx, "key5", false)
Girish Kumarca522102019-11-08 11:26:35 +0000404 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
npujar5bf737f2020-01-16 19:35:25 +0530411 err := backend.Put(ctx, "key5", []uint8("value5"))
Girish Kumarca522102019-11-08 11:26:35 +0000412 assert.Nil(t, err)
413 time.Sleep(time.Millisecond * 100)
414 assert.Equal(t, 1, len(eventChan))
415
Neha Sharma94f16a92020-06-26 04:17:55 +0000416 backend.DeleteWatch(context.Background(), "key5", eventChan)
Girish Kumarca522102019-11-08 11:26:35 +0000417}
divyadesaid737eb62020-03-26 06:52:20 +0000418
419// Test Create and Delete Watch with prefix for Embedded Etcd Server
420func 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 Sharma94f16a92020-06-26 04:17:55 +0000437 backend.DeleteWatch(context.Background(), "key6", eventChan)
divyadesaid737eb62020-03-26 06:52:20 +0000438}