khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 2 | * Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 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 | |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 17 | package etcd |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 18 | |
| 19 | import ( |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 20 | "context" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 21 | "fmt" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 22 | "os" |
| 23 | "testing" |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 24 | "time" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 25 | |
| 26 | "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore" |
| 27 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 28 | "github.com/phayes/freeport" |
| 29 | "github.com/stretchr/testify/assert" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var etcdServer *EtcdServer |
| 33 | var client *kvstore.EtcdClient |
| 34 | |
| 35 | func setup() { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 36 | ctx := context.Background() |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 37 | clientPort, err := freeport.GetFreePort() |
| 38 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 39 | logger.Fatal(ctx, err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 40 | } |
| 41 | peerPort, err := freeport.GetFreePort() |
| 42 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 43 | logger.Fatal(ctx, err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 44 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 45 | etcdServer = StartEtcdServer(ctx, MKConfig(ctx, "voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error")) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 46 | if etcdServer == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 47 | logger.Fatal(ctx, "Embedded server failed to start") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 48 | } |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 49 | clientAddr := fmt.Sprintf("localhost:%d", clientPort) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 50 | client, err = kvstore.NewEtcdClient(ctx, clientAddr, 10*time.Second, log.WarnLevel) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 51 | if err != nil || client == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 52 | etcdServer.Stop(ctx) |
| 53 | logger.Fatal(ctx, "Failed to create an Etcd client") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestEtcdServerRW(t *testing.T) { |
| 58 | key := "myKey-1" |
| 59 | value := "myVal-1" |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 60 | err := client.Put(context.Background(), key, value) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 61 | assert.Nil(t, err) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 62 | kvp, err := client.Get(context.Background(), key) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 63 | assert.Nil(t, err) |
| 64 | assert.NotNil(t, kvp) |
| 65 | assert.Equal(t, kvp.Key, key) |
| 66 | val, err := kvstore.ToString(kvp.Value) |
| 67 | assert.Nil(t, err) |
| 68 | assert.Equal(t, value, val) |
| 69 | } |
| 70 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 71 | func shutdown() { |
| 72 | if client != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 73 | client.Close(context.Background()) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 74 | } |
| 75 | if etcdServer != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 76 | etcdServer.Stop(context.Background()) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestMain(m *testing.M) { |
| 81 | setup() |
| 82 | code := m.Run() |
| 83 | shutdown() |
| 84 | os.Exit(code) |
| 85 | } |