blob: 040318ca8b205b3a85e7be9650e0ecf9f176fb6b [file] [log] [blame]
Girish Kumarca522102019-11-08 11:26:35 +00001/*
Joey Armstrong7f8436c2023-07-09 20:23:27 -04002 * Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors
Girish Kumarca522102019-11-08 11:26:35 +00003
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"
serkant.uluderya2f2855e2021-01-30 12:43:40 +030021 "os"
22 "strconv"
23 "testing"
24 "time"
25
khenaidoo26721882021-08-11 17:42:52 -040026 mocks "github.com/opencord/voltha-lib-go/v7/pkg/mocks/etcd"
khenaidooc7005fc2019-11-18 19:23:57 -050027 "github.com/phayes/freeport"
Girish Kumarca522102019-11-08 11:26:35 +000028 "github.com/stretchr/testify/assert"
29 "google.golang.org/grpc/codes"
30 "google.golang.org/grpc/status"
Girish Kumarca522102019-11-08 11:26:35 +000031)
32
Girish Kumarca522102019-11-08 11:26:35 +000033const (
34 embedEtcdServerHost = "localhost"
Neha Sharma130ac6d2020-04-08 08:46:32 +000035 defaultTimeout = 1 * time.Second
Girish Kumarca522102019-11-08 11:26:35 +000036 defaultPathPrefix = "Prefix"
37)
38
khenaidooc7005fc2019-11-18 19:23:57 -050039var (
40 embedEtcdServerPort int
41 dummyEtcdServerPort int
42)
Girish Kumarca522102019-11-08 11:26:35 +000043
khenaidooc7005fc2019-11-18 19:23:57 -050044func TestMain(m *testing.M) {
Neha Sharma94f16a92020-06-26 04:17:55 +000045 ctx := context.Background()
khenaidooc7005fc2019-11-18 19:23:57 -050046 var err error
47 embedEtcdServerPort, err = freeport.GetFreePort()
48 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000049 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050050 }
51 dummyEtcdServerPort, err = freeport.GetFreePort()
52 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000053 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050054 }
55 peerPort, err := freeport.GetFreePort()
56 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000057 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050058 }
Neha Sharma94f16a92020-06-26 04:17:55 +000059 etcdServer := mocks.StartEtcdServer(ctx, mocks.MKConfig(ctx, "voltha.db.test", embedEtcdServerPort, peerPort, "voltha.lib.db", "error"))
Girish Kumarca522102019-11-08 11:26:35 +000060 res := m.Run()
61
Neha Sharma94f16a92020-06-26 04:17:55 +000062 etcdServer.Stop(ctx)
Girish Kumarca522102019-11-08 11:26:35 +000063 os.Exit(res)
64}
65
66func provisionBackendWithEmbeddedEtcdServer(t *testing.T) *Backend {
Neha Sharma94f16a92020-06-26 04:17:55 +000067 ctx := context.Background()
68 backend := NewBackend(ctx, "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +000069 assert.NotNil(t, backend)
70 assert.NotNil(t, backend.Client)
71 return backend
72}
73
74func provisionBackendWithDummyEtcdServer(t *testing.T) *Backend {
Neha Sharma94f16a92020-06-26 04:17:55 +000075 backend := NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(dummyEtcdServerPort), defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +000076 assert.NotNil(t, backend)
77 assert.NotNil(t, backend.Client)
78 return backend
79}
80
81// Create instance using Etcd Kvstore
82func TestNewBackend_EtcdKvStore(t *testing.T) {
Neha Sharmadd9af392020-04-28 09:03:57 +000083 address := embedEtcdServerHost + ":" + strconv.Itoa(embedEtcdServerPort)
Neha Sharma94f16a92020-06-26 04:17:55 +000084 backend := NewBackend(context.Background(), "etcd", address, defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +000085
86 // Verify all attributes of backend have got set correctly
87 assert.NotNil(t, backend)
88 assert.NotNil(t, backend.Client)
89 assert.Equal(t, backend.StoreType, "etcd")
Neha Sharmadd9af392020-04-28 09:03:57 +000090 assert.Equal(t, backend.Address, address)
Girish Kumarca522102019-11-08 11:26:35 +000091 assert.Equal(t, backend.Timeout, defaultTimeout)
92 assert.Equal(t, backend.PathPrefix, defaultPathPrefix)
93 assert.Equal(t, backend.alive, false) // backend is not alive at start
94 assert.Nil(t, backend.liveness) // no liveness channel is created at start
95 assert.Equal(t, backend.LivenessChannelInterval, DefaultLivenessChannelInterval)
96}
97
Girish Kumarca522102019-11-08 11:26:35 +000098// Create instance using Invalid Kvstore; instance creation should fail
99func TestNewBackend_InvalidKvstore(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000100 backend := NewBackend(context.Background(), "unknown", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
Girish Kumarca522102019-11-08 11:26:35 +0000101
102 assert.NotNil(t, backend)
103 assert.Nil(t, backend.Client)
104}
105
106func TestMakePath(t *testing.T) {
107 backend := provisionBackendWithEmbeddedEtcdServer(t)
Neha Sharma94f16a92020-06-26 04:17:55 +0000108 path := backend.makePath(context.Background(), "Suffix")
Girish Kumarca522102019-11-08 11:26:35 +0000109 assert.Equal(t, defaultPathPrefix+"/Suffix", path)
110}
111
112// Liveness Check against Embedded Etcd Server should return alive state
113func TestPerformLivenessCheck_EmbeddedEtcdServer(t *testing.T) {
114 backend := provisionBackendWithEmbeddedEtcdServer(t)
Neha Sharma130ac6d2020-04-08 08:46:32 +0000115 ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
npujar5bf737f2020-01-16 19:35:25 +0530116 defer cancel()
117 alive := backend.PerformLivenessCheck(ctx)
Girish Kumarca522102019-11-08 11:26:35 +0000118 assert.True(t, alive)
119}
120
121// Liveness Check against Dummy Etcd Server should return not-alive state
122func TestPerformLivenessCheck_DummyEtcdServer(t *testing.T) {
123 backend := provisionBackendWithDummyEtcdServer(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.False(t, alive)
128}
129
130// Enabling Liveness Channel before First Liveness Check
131func TestEnableLivenessChannel_EmbeddedEtcdServer_BeforeLivenessCheck(t *testing.T) {
132 backend := provisionBackendWithEmbeddedEtcdServer(t)
133
Neha Sharma94f16a92020-06-26 04:17:55 +0000134 alive := backend.EnableLivenessChannel(context.Background())
Girish Kumarca522102019-11-08 11:26:35 +0000135 assert.NotNil(t, alive)
136 assert.Equal(t, 1, len(alive))
137 assert.Equal(t, false, <-alive)
138 assert.NotNil(t, backend.liveness)
139}
140
141// Enabling Liveness Channel after First Liveness Check
142func TestEnableLivenessChannel_EmbeddedEtcdServer_AfterLivenessCheck(t *testing.T) {
143 backend := provisionBackendWithEmbeddedEtcdServer(t)
Neha Sharma130ac6d2020-04-08 08:46:32 +0000144 ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
npujar5bf737f2020-01-16 19:35:25 +0530145 defer cancel()
146 backend.PerformLivenessCheck(ctx)
Girish Kumarca522102019-11-08 11:26:35 +0000147
Neha Sharma94f16a92020-06-26 04:17:55 +0000148 alive := backend.EnableLivenessChannel(ctx)
Girish Kumarca522102019-11-08 11:26:35 +0000149 assert.NotNil(t, alive)
150 assert.Equal(t, 1, len(alive))
151 assert.Equal(t, true, <-alive)
152 assert.NotNil(t, backend.liveness)
153}
154
155// Update Liveness with alive status change
156func TestUpdateLiveness_AliveStatusChange(t *testing.T) {
157 backend := provisionBackendWithEmbeddedEtcdServer(t)
158 // Enable Liveness Channel and verify initial state is not-alive
Neha Sharma94f16a92020-06-26 04:17:55 +0000159 aliveState := backend.EnableLivenessChannel(context.Background())
Girish Kumarca522102019-11-08 11:26:35 +0000160 assert.NotNil(t, aliveState)
161 assert.Equal(t, 1, len(backend.liveness))
162 assert.Equal(t, false, <-backend.liveness)
163 lastUpdateTime := backend.lastLivenessTime
164
165 // Update with changed alive state. Verify alive state push & liveness time update
Neha Sharma94f16a92020-06-26 04:17:55 +0000166 backend.updateLiveness(context.Background(), true)
Girish Kumarca522102019-11-08 11:26:35 +0000167 assert.Equal(t, 1, len(backend.liveness))
168 assert.Equal(t, true, <-backend.liveness)
169 assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime)
170}
171
172// Update Liveness with same alive status reporting
173func TestUpdateLiveness_AliveStatusUnchanged(t *testing.T) {
174 backend := provisionBackendWithEmbeddedEtcdServer(t)
175 // Enable Liveness Channel and verify initial state is not-alive
Neha Sharma94f16a92020-06-26 04:17:55 +0000176 aliveState := backend.EnableLivenessChannel(context.Background())
Girish Kumarca522102019-11-08 11:26:35 +0000177 assert.NotNil(t, aliveState)
178 assert.Equal(t, false, <-backend.liveness)
179 lastUpdateTime := backend.lastLivenessTime
180
181 // Update with same alive state. Verify no further alive state push
Neha Sharma94f16a92020-06-26 04:17:55 +0000182 backend.updateLiveness(context.Background(), false)
Girish Kumarca522102019-11-08 11:26:35 +0000183 assert.Equal(t, 0, len(backend.liveness))
184 assert.Equal(t, lastUpdateTime, backend.lastLivenessTime)
185
186 // Now set lastUpdateTime 10 min back and push again
187 tenMinDuration, _ := time.ParseDuration("10m")
188 backend.lastLivenessTime = time.Now().Add(-tenMinDuration)
189 lastUpdateTime = backend.lastLivenessTime
190
Neha Sharma94f16a92020-06-26 04:17:55 +0000191 backend.updateLiveness(context.Background(), false)
Girish Kumarca522102019-11-08 11:26:35 +0000192 assert.Equal(t, 1, len(backend.liveness))
193 assert.Equal(t, false, <-backend.liveness)
194 assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime)
195}
196
197func TestIsErrorIndicatingAliveKvstore(t *testing.T) {
198 tests := []struct {
199 name string
200 arg error
201 want bool
202 }{
203 {"No Error", nil, true},
204 {"Request Canceled", context.Canceled, true},
205 {"Request Timeout", context.DeadlineExceeded, false},
206 {"Etcd Error - InvalidArgument", status.New(codes.InvalidArgument, "").Err(), true},
207 {"Etcd Error - DeadlineExceeded", status.New(codes.DeadlineExceeded, "").Err(), false},
208 {"Etcd Error - Unavailable", status.New(codes.Unavailable, "").Err(), false},
209 {"Etcd Error - DataLoss", status.New(codes.DataLoss, "").Err(), false},
210 {"Etcd Error - NotFound", status.New(codes.NotFound, "").Err(), true},
211 {"Etcd Error - PermissionDenied ", status.New(codes.PermissionDenied, "").Err(), true},
212 {"Etcd Error - FailedPrecondition ", status.New(codes.FailedPrecondition, "").Err(), true},
213 }
214
215 backend := provisionBackendWithEmbeddedEtcdServer(t)
216
217 for _, tt := range tests {
218 t.Run(tt.name, func(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000219 if backend.isErrorIndicatingAliveKvstore(context.Background(), tt.arg) != tt.want {
Girish Kumarca522102019-11-08 11:26:35 +0000220 t.Errorf("isErrorIndicatingAliveKvstore failed for %s: expected %t but got %t", tt.name, tt.want, !tt.want)
221 }
222 })
223 }
224}
225
226func TestPut_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530227 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
228 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000229 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530230 err := backend.Put(ctx, "key1", []uint8("value1"))
Girish Kumarca522102019-11-08 11:26:35 +0000231 assert.Nil(t, err)
232
233 // Assert alive state has become true
234 assert.True(t, backend.alive)
235
236 // Assert that kvstore has this value stored
npujar5bf737f2020-01-16 19:35:25 +0530237 kvpair, err := backend.Get(ctx, "key1")
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800238 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000239 assert.NotNil(t, kvpair)
240 assert.Equal(t, defaultPathPrefix+"/key1", kvpair.Key)
241 assert.Equal(t, []uint8("value1"), kvpair.Value)
242
243 // Assert that Put overrides the Value
npujar5bf737f2020-01-16 19:35:25 +0530244 err = backend.Put(ctx, "key1", []uint8("value11"))
Girish Kumarca522102019-11-08 11:26:35 +0000245 assert.Nil(t, err)
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("value11"), kvpair.Value)
251}
252
253// Put operation should fail against Dummy Non-existent Etcd Server
254func TestPut_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530255 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
256 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000257 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530258 err := backend.Put(ctx, "key1", []uint8("value1"))
Girish Kumarca522102019-11-08 11:26:35 +0000259 assert.NotNil(t, err)
260
261 // Assert alive state is still false
262 assert.False(t, backend.alive)
263}
264
265// Test Get for existing and non-existing key
266func TestGet_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530267 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
268 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000269 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530270 err := backend.Put(ctx, "key2", []uint8("value2"))
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800271 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000272
273 // Assert alive state has become true
274 assert.True(t, backend.alive)
275
276 // Assert that kvstore has this key stored
npujar5bf737f2020-01-16 19:35:25 +0530277 kvpair, err := backend.Get(ctx, "key2")
Girish Kumarca522102019-11-08 11:26:35 +0000278 assert.NotNil(t, kvpair)
279 assert.Nil(t, err)
280 assert.Equal(t, defaultPathPrefix+"/key2", kvpair.Key)
281 assert.Equal(t, []uint8("value2"), kvpair.Value)
282
283 // Assert that Get works fine for absent key3
npujar5bf737f2020-01-16 19:35:25 +0530284 kvpair, err = backend.Get(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000285 assert.Nil(t, err) // no error as lookup is successful
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800286 assert.Nil(t, kvpair)
Girish Kumarca522102019-11-08 11:26:35 +0000287}
288
289// Get operation should fail against Dummy Non-existent Etcd Server
290func TestGet_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530291 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
292 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000293 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530294 kvpair, err := backend.Get(ctx, "key2")
Girish Kumarca522102019-11-08 11:26:35 +0000295 assert.NotNil(t, err)
296 assert.Nil(t, kvpair)
297
298 // Assert alive state is still false
299 assert.False(t, backend.alive)
300}
301
302// Test Delete for existing and non-existing key
303func TestDelete_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530304 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
305 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000306 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530307 err := backend.Put(ctx, "key3", []uint8("value3"))
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800308 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000309
310 // Assert alive state has become true
311 assert.True(t, backend.alive)
312
313 // Assert that kvstore has this key stored
npujar5bf737f2020-01-16 19:35:25 +0530314 kvpair, err := backend.Get(ctx, "key3")
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800315 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000316 assert.NotNil(t, kvpair)
317
318 // Delete and Assert that key has been removed
npujar5bf737f2020-01-16 19:35:25 +0530319 err = backend.Delete(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000320 assert.Nil(t, err)
npujar5bf737f2020-01-16 19:35:25 +0530321 kvpair, err = backend.Get(ctx, "key3")
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800322 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000323 assert.Nil(t, kvpair)
324
325 // Assert that Delete silently ignores absent key3
npujar5bf737f2020-01-16 19:35:25 +0530326 err = backend.Delete(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000327 assert.Nil(t, err)
328}
329
330// Delete operation should fail against Dummy Non-existent Etcd Server
331func TestDelete_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530332 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
333 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000334 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530335 err := backend.Delete(ctx, "key3")
Girish Kumarca522102019-11-08 11:26:35 +0000336 assert.NotNil(t, err)
337
338 // Assert alive state is still false
339 assert.False(t, backend.alive)
340}
341
342// Test List for series of values under a key path
343func TestList_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530344 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
345 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000346 key41 := "key4/subkey1"
347 key42 := "key4/subkey2"
348
349 backend := provisionBackendWithEmbeddedEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530350 err := backend.Put(ctx, key41, []uint8("value4-1"))
351 assert.Nil(t, err)
352 err = backend.Put(ctx, key42, []uint8("value4-2"))
353 assert.Nil(t, err)
Girish Kumarca522102019-11-08 11:26:35 +0000354
355 // Assert alive state has become true
356 assert.True(t, backend.alive)
357
358 // Assert that Get does not retrieve these Subkeys
npujar5bf737f2020-01-16 19:35:25 +0530359 kvpair, err := backend.Get(ctx, "key4")
Girish Kumarca522102019-11-08 11:26:35 +0000360 assert.Nil(t, kvpair)
361 assert.Nil(t, err)
362
363 // Assert that List operation retrieves these Child Keys
npujar5bf737f2020-01-16 19:35:25 +0530364 kvmap, err := backend.List(ctx, "key4")
Girish Kumarca522102019-11-08 11:26:35 +0000365 assert.NotNil(t, kvmap)
366 assert.Nil(t, err)
367 assert.Equal(t, 2, len(kvmap))
368 fullkey41 := defaultPathPrefix + "/" + key41
369 fullkey42 := defaultPathPrefix + "/" + key42
370 assert.Equal(t, fullkey41, kvmap[fullkey41].Key)
371 assert.Equal(t, []uint8("value4-1"), kvmap[fullkey41].Value)
372 assert.Equal(t, fullkey42, kvmap[fullkey42].Key)
373 assert.Equal(t, []uint8("value4-2"), kvmap[fullkey42].Value)
374}
375
376// List operation should fail against Dummy Non-existent Etcd Server
377func TestList_DummyEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530378 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
379 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000380 backend := provisionBackendWithDummyEtcdServer(t)
npujar5bf737f2020-01-16 19:35:25 +0530381 kvmap, err := backend.List(ctx, "key4")
Girish Kumarca522102019-11-08 11:26:35 +0000382 assert.Nil(t, kvmap)
383 assert.NotNil(t, err)
384
385 // Assert alive state is still false
386 assert.False(t, backend.alive)
387}
388
389// Test Create and Delete Watch for Embedded Etcd Server
390func TestCreateWatch_EmbeddedEtcdServer(t *testing.T) {
npujar5bf737f2020-01-16 19:35:25 +0530391 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
392 defer cancel()
Girish Kumarca522102019-11-08 11:26:35 +0000393 backend := provisionBackendWithEmbeddedEtcdServer(t)
divyadesai8bf96862020-02-07 12:24:26 +0000394 eventChan := backend.CreateWatch(ctx, "key5", false)
Girish Kumarca522102019-11-08 11:26:35 +0000395 assert.NotNil(t, eventChan)
396 assert.Equal(t, 0, len(eventChan))
397
398 // Assert this method does not change alive state
399 assert.False(t, backend.alive)
400
401 // Put a value for watched key and event should appear
npujar5bf737f2020-01-16 19:35:25 +0530402 err := backend.Put(ctx, "key5", []uint8("value5"))
Girish Kumarca522102019-11-08 11:26:35 +0000403 assert.Nil(t, err)
404 time.Sleep(time.Millisecond * 100)
405 assert.Equal(t, 1, len(eventChan))
406
Neha Sharma94f16a92020-06-26 04:17:55 +0000407 backend.DeleteWatch(context.Background(), "key5", eventChan)
Girish Kumarca522102019-11-08 11:26:35 +0000408}
divyadesaid737eb62020-03-26 06:52:20 +0000409
410// Test Create and Delete Watch with prefix for Embedded Etcd Server
411func TestCreateWatch_With_Prefix_EmbeddedEtcdServer(t *testing.T) {
412 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
413 defer cancel()
414 backend := provisionBackendWithEmbeddedEtcdServer(t)
415 eventChan := backend.CreateWatch(ctx, "key6", true)
416 assert.NotNil(t, eventChan)
417 assert.Equal(t, 0, len(eventChan))
418
419 // Assert this method does not change alive state
420 assert.False(t, backend.alive)
421
422 // Put a value for watched key and event should appear
423 err := backend.Put(ctx, "key6/is-a-prefix", []uint8("value5"))
424 assert.Nil(t, err)
425 time.Sleep(time.Millisecond * 100)
426 assert.Equal(t, 1, len(eventChan))
427
Neha Sharma94f16a92020-06-26 04:17:55 +0000428 backend.DeleteWatch(context.Background(), "key6", eventChan)
divyadesaid737eb62020-03-26 06:52:20 +0000429}