blob: 2d39dd13fb6e57309d7884fa4c9a229d4e4ceab0 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001/*
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
khenaidoob6238b32020-04-07 12:07:36 -040017package etcd
khenaidoo59ce9dd2019-11-11 13:05:32 -050018
19import (
npujar5bf737f2020-01-16 19:35:25 +053020 "context"
khenaidooc7005fc2019-11-18 19:23:57 -050021 "fmt"
khenaidoo59ce9dd2019-11-11 13:05:32 -050022 "os"
23 "testing"
Neha Sharma130ac6d2020-04-08 08:46:32 +000024 "time"
khenaidoo26721882021-08-11 17:42:52 -040025
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"
khenaidoo59ce9dd2019-11-11 13:05:32 -050030)
31
32var etcdServer *EtcdServer
33var client *kvstore.EtcdClient
34
35func setup() {
Neha Sharma94f16a92020-06-26 04:17:55 +000036 ctx := context.Background()
khenaidooc7005fc2019-11-18 19:23:57 -050037 clientPort, err := freeport.GetFreePort()
38 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000039 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050040 }
41 peerPort, err := freeport.GetFreePort()
42 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000043 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050044 }
Neha Sharma94f16a92020-06-26 04:17:55 +000045 etcdServer = StartEtcdServer(ctx, MKConfig(ctx, "voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error"))
khenaidoo59ce9dd2019-11-11 13:05:32 -050046 if etcdServer == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000047 logger.Fatal(ctx, "Embedded server failed to start")
khenaidoo59ce9dd2019-11-11 13:05:32 -050048 }
khenaidooc7005fc2019-11-18 19:23:57 -050049 clientAddr := fmt.Sprintf("localhost:%d", clientPort)
Neha Sharma94f16a92020-06-26 04:17:55 +000050 client, err = kvstore.NewEtcdClient(ctx, clientAddr, 10*time.Second, log.WarnLevel)
khenaidoo59ce9dd2019-11-11 13:05:32 -050051 if err != nil || client == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000052 etcdServer.Stop(ctx)
53 logger.Fatal(ctx, "Failed to create an Etcd client")
khenaidoo59ce9dd2019-11-11 13:05:32 -050054 }
55}
56
57func TestEtcdServerRW(t *testing.T) {
58 key := "myKey-1"
59 value := "myVal-1"
npujar5bf737f2020-01-16 19:35:25 +053060 err := client.Put(context.Background(), key, value)
khenaidoo59ce9dd2019-11-11 13:05:32 -050061 assert.Nil(t, err)
npujar5bf737f2020-01-16 19:35:25 +053062 kvp, err := client.Get(context.Background(), key)
khenaidoo59ce9dd2019-11-11 13:05:32 -050063 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
khenaidoo59ce9dd2019-11-11 13:05:32 -050071func shutdown() {
72 if client != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000073 client.Close(context.Background())
khenaidoo59ce9dd2019-11-11 13:05:32 -050074 }
75 if etcdServer != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000076 etcdServer.Stop(context.Background())
khenaidoo59ce9dd2019-11-11 13:05:32 -050077 }
78}
79
80func TestMain(m *testing.M) {
81 setup()
82 code := m.Run()
83 shutdown()
84 os.Exit(code)
85}