blob: b26b2628a47b3bd60265e92b96cb8b30b5c35148 [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"
serkant.uluderyab38671c2019-11-01 09:35:38 -070022 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
Rohan Agrawalee87e642020-04-14 10:22:18 +000023 "github.com/opencord/voltha-lib-go/v3/pkg/log"
khenaidooc7005fc2019-11-18 19:23:57 -050024 "github.com/phayes/freeport"
khenaidoo59ce9dd2019-11-11 13:05:32 -050025 "github.com/stretchr/testify/assert"
khenaidoo59ce9dd2019-11-11 13:05:32 -050026 "os"
27 "testing"
Neha Sharma130ac6d2020-04-08 08:46:32 +000028 "time"
khenaidoo59ce9dd2019-11-11 13:05:32 -050029)
30
31var etcdServer *EtcdServer
32var client *kvstore.EtcdClient
33
34func setup() {
Neha Sharma94f16a92020-06-26 04:17:55 +000035 ctx := context.Background()
khenaidooc7005fc2019-11-18 19:23:57 -050036 clientPort, err := freeport.GetFreePort()
37 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000038 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050039 }
40 peerPort, err := freeport.GetFreePort()
41 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000042 logger.Fatal(ctx, err)
khenaidooc7005fc2019-11-18 19:23:57 -050043 }
Neha Sharma94f16a92020-06-26 04:17:55 +000044 etcdServer = StartEtcdServer(ctx, MKConfig(ctx, "voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error"))
khenaidoo59ce9dd2019-11-11 13:05:32 -050045 if etcdServer == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000046 logger.Fatal(ctx, "Embedded server failed to start")
khenaidoo59ce9dd2019-11-11 13:05:32 -050047 }
khenaidooc7005fc2019-11-18 19:23:57 -050048 clientAddr := fmt.Sprintf("localhost:%d", clientPort)
Neha Sharma94f16a92020-06-26 04:17:55 +000049 client, err = kvstore.NewEtcdClient(ctx, clientAddr, 10*time.Second, log.WarnLevel)
khenaidoo59ce9dd2019-11-11 13:05:32 -050050 if err != nil || client == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000051 etcdServer.Stop(ctx)
52 logger.Fatal(ctx, "Failed to create an Etcd client")
khenaidoo59ce9dd2019-11-11 13:05:32 -050053 }
54}
55
56func TestEtcdServerRW(t *testing.T) {
57 key := "myKey-1"
58 value := "myVal-1"
npujar5bf737f2020-01-16 19:35:25 +053059 err := client.Put(context.Background(), key, value)
khenaidoo59ce9dd2019-11-11 13:05:32 -050060 assert.Nil(t, err)
npujar5bf737f2020-01-16 19:35:25 +053061 kvp, err := client.Get(context.Background(), key)
khenaidoo59ce9dd2019-11-11 13:05:32 -050062 assert.Nil(t, err)
63 assert.NotNil(t, kvp)
64 assert.Equal(t, kvp.Key, key)
65 val, err := kvstore.ToString(kvp.Value)
66 assert.Nil(t, err)
67 assert.Equal(t, value, val)
68}
69
70func TestEtcdServerReserve(t *testing.T) {
71 assert.NotNil(t, client)
72 txId := "tnxId-1"
npujar5bf737f2020-01-16 19:35:25 +053073 val, err := client.Reserve(context.Background(), "transactions", txId, 10)
khenaidoo59ce9dd2019-11-11 13:05:32 -050074 assert.Nil(t, err)
75 assert.NotNil(t, val)
76 assert.Equal(t, val, txId)
77}
78
79func shutdown() {
80 if client != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000081 client.Close(context.Background())
khenaidoo59ce9dd2019-11-11 13:05:32 -050082 }
83 if etcdServer != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000084 etcdServer.Stop(context.Background())
khenaidoo59ce9dd2019-11-11 13:05:32 -050085 }
86}
87
88func TestMain(m *testing.M) {
89 setup()
90 code := m.Run()
91 shutdown()
92 os.Exit(code)
93}