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 | 2f2855e | 2021-01-30 12:43:40 +0300 | [diff] [blame] | 21 | "os" |
| 22 | "strconv" |
| 23 | "testing" |
| 24 | "time" |
| 25 | |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 26 | mocks "github.com/opencord/voltha-lib-go/v4/pkg/mocks/etcd" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 27 | "github.com/phayes/freeport" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 28 | "github.com/stretchr/testify/assert" |
| 29 | "google.golang.org/grpc/codes" |
| 30 | "google.golang.org/grpc/status" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 31 | ) |
| 32 | |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 33 | const ( |
| 34 | embedEtcdServerHost = "localhost" |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 35 | defaultTimeout = 1 * time.Second |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 36 | defaultPathPrefix = "Prefix" |
| 37 | ) |
| 38 | |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 39 | var ( |
| 40 | embedEtcdServerPort int |
| 41 | dummyEtcdServerPort int |
| 42 | ) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 43 | |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 44 | func TestMain(m *testing.M) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 45 | ctx := context.Background() |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 46 | var err error |
| 47 | embedEtcdServerPort, err = freeport.GetFreePort() |
| 48 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 49 | logger.Fatal(ctx, err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 50 | } |
| 51 | dummyEtcdServerPort, err = freeport.GetFreePort() |
| 52 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 53 | logger.Fatal(ctx, err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 54 | } |
| 55 | peerPort, err := freeport.GetFreePort() |
| 56 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 57 | logger.Fatal(ctx, err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 58 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 59 | 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] | 60 | res := m.Run() |
| 61 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 62 | etcdServer.Stop(ctx) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 63 | os.Exit(res) |
| 64 | } |
| 65 | |
| 66 | func provisionBackendWithEmbeddedEtcdServer(t *testing.T) *Backend { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 67 | ctx := context.Background() |
| 68 | backend := NewBackend(ctx, "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 69 | assert.NotNil(t, backend) |
| 70 | assert.NotNil(t, backend.Client) |
| 71 | return backend |
| 72 | } |
| 73 | |
| 74 | func provisionBackendWithDummyEtcdServer(t *testing.T) *Backend { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 75 | backend := NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(dummyEtcdServerPort), defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 76 | assert.NotNil(t, backend) |
| 77 | assert.NotNil(t, backend.Client) |
| 78 | return backend |
| 79 | } |
| 80 | |
| 81 | // Create instance using Etcd Kvstore |
| 82 | func TestNewBackend_EtcdKvStore(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 83 | address := embedEtcdServerHost + ":" + strconv.Itoa(embedEtcdServerPort) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 84 | backend := NewBackend(context.Background(), "etcd", address, defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 85 | |
| 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 Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 90 | assert.Equal(t, backend.Address, address) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 91 | 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 Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 98 | // Create instance using Invalid Kvstore; instance creation should fail |
| 99 | func TestNewBackend_InvalidKvstore(t *testing.T) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 100 | backend := NewBackend(context.Background(), "unknown", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 101 | |
| 102 | assert.NotNil(t, backend) |
| 103 | assert.Nil(t, backend.Client) |
| 104 | } |
| 105 | |
| 106 | func TestMakePath(t *testing.T) { |
| 107 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 108 | path := backend.makePath(context.Background(), "Suffix") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 109 | assert.Equal(t, defaultPathPrefix+"/Suffix", path) |
| 110 | } |
| 111 | |
| 112 | // Liveness Check against Embedded Etcd Server should return alive state |
| 113 | func TestPerformLivenessCheck_EmbeddedEtcdServer(t *testing.T) { |
| 114 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 115 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 116 | defer cancel() |
| 117 | alive := backend.PerformLivenessCheck(ctx) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 118 | assert.True(t, alive) |
| 119 | } |
| 120 | |
| 121 | // Liveness Check against Dummy Etcd Server should return not-alive state |
| 122 | func TestPerformLivenessCheck_DummyEtcdServer(t *testing.T) { |
| 123 | backend := provisionBackendWithDummyEtcdServer(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.False(t, alive) |
| 128 | } |
| 129 | |
| 130 | // Enabling Liveness Channel before First Liveness Check |
| 131 | func TestEnableLivenessChannel_EmbeddedEtcdServer_BeforeLivenessCheck(t *testing.T) { |
| 132 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 133 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 134 | alive := backend.EnableLivenessChannel(context.Background()) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 135 | 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 |
| 142 | func TestEnableLivenessChannel_EmbeddedEtcdServer_AfterLivenessCheck(t *testing.T) { |
| 143 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 144 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 145 | defer cancel() |
| 146 | backend.PerformLivenessCheck(ctx) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 147 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 148 | alive := backend.EnableLivenessChannel(ctx) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 149 | 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 |
| 156 | func TestUpdateLiveness_AliveStatusChange(t *testing.T) { |
| 157 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 158 | // Enable Liveness Channel and verify initial state is not-alive |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 159 | aliveState := backend.EnableLivenessChannel(context.Background()) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 160 | 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 Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 166 | backend.updateLiveness(context.Background(), true) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 167 | 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 |
| 173 | func TestUpdateLiveness_AliveStatusUnchanged(t *testing.T) { |
| 174 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
| 175 | // Enable Liveness Channel and verify initial state is not-alive |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 176 | aliveState := backend.EnableLivenessChannel(context.Background()) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 177 | 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 Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 182 | backend.updateLiveness(context.Background(), false) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 183 | 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 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, 1, len(backend.liveness)) |
| 193 | assert.Equal(t, false, <-backend.liveness) |
| 194 | assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime) |
| 195 | } |
| 196 | |
| 197 | func 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 Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 219 | if backend.isErrorIndicatingAliveKvstore(context.Background(), tt.arg) != tt.want { |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 220 | t.Errorf("isErrorIndicatingAliveKvstore failed for %s: expected %t but got %t", tt.name, tt.want, !tt.want) |
| 221 | } |
| 222 | }) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func TestPut_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 227 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 228 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 229 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 230 | err := backend.Put(ctx, "key1", []uint8("value1")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 231 | 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 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 237 | kvpair, err := backend.Get(ctx, "key1") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 238 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 239 | 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 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 244 | err = backend.Put(ctx, "key1", []uint8("value11")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 245 | assert.Nil(t, err) |
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("value11"), kvpair.Value) |
| 251 | } |
| 252 | |
| 253 | // Put operation should fail against Dummy Non-existent Etcd Server |
| 254 | func TestPut_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 255 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 256 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 257 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 258 | err := backend.Put(ctx, "key1", []uint8("value1")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 259 | 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 |
| 266 | func TestGet_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 267 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 268 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 269 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 270 | err := backend.Put(ctx, "key2", []uint8("value2")) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 271 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 272 | |
| 273 | // Assert alive state has become true |
| 274 | assert.True(t, backend.alive) |
| 275 | |
| 276 | // Assert that kvstore has this key stored |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 277 | kvpair, err := backend.Get(ctx, "key2") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 278 | 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 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 284 | kvpair, err = backend.Get(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 285 | assert.Nil(t, err) // no error as lookup is successful |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 286 | assert.Nil(t, kvpair) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Get operation should fail against Dummy Non-existent Etcd Server |
| 290 | func TestGet_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 291 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 292 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 293 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 294 | kvpair, err := backend.Get(ctx, "key2") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 295 | 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 |
| 303 | func TestDelete_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 304 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 305 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 306 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 307 | err := backend.Put(ctx, "key3", []uint8("value3")) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 308 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 309 | |
| 310 | // Assert alive state has become true |
| 311 | assert.True(t, backend.alive) |
| 312 | |
| 313 | // Assert that kvstore has this key stored |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 314 | kvpair, err := backend.Get(ctx, "key3") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 315 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 316 | assert.NotNil(t, kvpair) |
| 317 | |
| 318 | // Delete and Assert that key has been removed |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 319 | err = backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 320 | assert.Nil(t, err) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 321 | kvpair, err = backend.Get(ctx, "key3") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 322 | assert.Nil(t, err) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 323 | assert.Nil(t, kvpair) |
| 324 | |
| 325 | // Assert that Delete silently ignores absent key3 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 326 | err = backend.Delete(ctx, "key3") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 327 | assert.Nil(t, err) |
| 328 | } |
| 329 | |
| 330 | // Delete operation should fail against Dummy Non-existent Etcd Server |
| 331 | func TestDelete_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 332 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 333 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 334 | backend := provisionBackendWithDummyEtcdServer(t) |
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.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 |
| 343 | func TestList_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 344 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 345 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 346 | key41 := "key4/subkey1" |
| 347 | key42 := "key4/subkey2" |
| 348 | |
| 349 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 350 | 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 Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 354 | |
| 355 | // Assert alive state has become true |
| 356 | assert.True(t, backend.alive) |
| 357 | |
| 358 | // Assert that Get does not retrieve these Subkeys |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 359 | kvpair, err := backend.Get(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 360 | assert.Nil(t, kvpair) |
| 361 | assert.Nil(t, err) |
| 362 | |
| 363 | // Assert that List operation retrieves these Child Keys |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 364 | kvmap, err := backend.List(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 365 | 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 |
| 377 | func TestList_DummyEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 378 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 379 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 380 | backend := provisionBackendWithDummyEtcdServer(t) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 381 | kvmap, err := backend.List(ctx, "key4") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 382 | 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 |
| 390 | func TestCreateWatch_EmbeddedEtcdServer(t *testing.T) { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 391 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 392 | defer cancel() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 393 | backend := provisionBackendWithEmbeddedEtcdServer(t) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 394 | eventChan := backend.CreateWatch(ctx, "key5", false) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 395 | 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 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 402 | err := backend.Put(ctx, "key5", []uint8("value5")) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 403 | assert.Nil(t, err) |
| 404 | time.Sleep(time.Millisecond * 100) |
| 405 | assert.Equal(t, 1, len(eventChan)) |
| 406 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 407 | backend.DeleteWatch(context.Background(), "key5", eventChan) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 408 | } |
divyadesai | d737eb6 | 2020-03-26 06:52:20 +0000 | [diff] [blame] | 409 | |
| 410 | // Test Create and Delete Watch with prefix for Embedded Etcd Server |
| 411 | func 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 Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 428 | backend.DeleteWatch(context.Background(), "key6", eventChan) |
divyadesai | d737eb6 | 2020-03-26 06:52:20 +0000 | [diff] [blame] | 429 | } |