blob: 617856846793814a96023a150755e8d9fc88960c [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"
28)
29
30var etcdServer *EtcdServer
31var client *kvstore.EtcdClient
32
33func setup() {
khenaidooc7005fc2019-11-18 19:23:57 -050034 clientPort, err := freeport.GetFreePort()
35 if err != nil {
khenaidoob332f9b2020-01-16 16:25:26 -050036 logger.Fatal(err)
khenaidooc7005fc2019-11-18 19:23:57 -050037 }
38 peerPort, err := freeport.GetFreePort()
39 if err != nil {
khenaidoob332f9b2020-01-16 16:25:26 -050040 logger.Fatal(err)
khenaidooc7005fc2019-11-18 19:23:57 -050041 }
42 etcdServer = StartEtcdServer(MKConfig("voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error"))
khenaidoo59ce9dd2019-11-11 13:05:32 -050043 if etcdServer == nil {
khenaidoob332f9b2020-01-16 16:25:26 -050044 logger.Fatal("Embedded server failed to start")
khenaidoo59ce9dd2019-11-11 13:05:32 -050045 }
khenaidooc7005fc2019-11-18 19:23:57 -050046 clientAddr := fmt.Sprintf("localhost:%d", clientPort)
Rohan Agrawalee87e642020-04-14 10:22:18 +000047 client, err = kvstore.NewEtcdClient(clientAddr, 10, log.WarnLevel)
khenaidoo59ce9dd2019-11-11 13:05:32 -050048 if err != nil || client == nil {
49 etcdServer.Stop()
khenaidoob332f9b2020-01-16 16:25:26 -050050 logger.Fatal("Failed to create an Etcd client")
khenaidoo59ce9dd2019-11-11 13:05:32 -050051 }
52}
53
54func TestEtcdServerRW(t *testing.T) {
55 key := "myKey-1"
56 value := "myVal-1"
npujar5bf737f2020-01-16 19:35:25 +053057 err := client.Put(context.Background(), key, value)
khenaidoo59ce9dd2019-11-11 13:05:32 -050058 assert.Nil(t, err)
npujar5bf737f2020-01-16 19:35:25 +053059 kvp, err := client.Get(context.Background(), key)
khenaidoo59ce9dd2019-11-11 13:05:32 -050060 assert.Nil(t, err)
61 assert.NotNil(t, kvp)
62 assert.Equal(t, kvp.Key, key)
63 val, err := kvstore.ToString(kvp.Value)
64 assert.Nil(t, err)
65 assert.Equal(t, value, val)
66}
67
68func TestEtcdServerReserve(t *testing.T) {
69 assert.NotNil(t, client)
70 txId := "tnxId-1"
npujar5bf737f2020-01-16 19:35:25 +053071 val, err := client.Reserve(context.Background(), "transactions", txId, 10)
khenaidoo59ce9dd2019-11-11 13:05:32 -050072 assert.Nil(t, err)
73 assert.NotNil(t, val)
74 assert.Equal(t, val, txId)
75}
76
77func shutdown() {
78 if client != nil {
79 client.Close()
80 }
81 if etcdServer != nil {
82 etcdServer.Stop()
83 }
84}
85
86func TestMain(m *testing.M) {
87 setup()
88 code := m.Run()
89 shutdown()
90 os.Exit(code)
91}