blob: 823086952e04f4acb6d1fc98c54d14f0db6d11b7 [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
17package mocks
18
19import (
20 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
21 "github.com/stretchr/testify/assert"
22 "log"
23 "os"
24 "testing"
25)
26
27var etcdServer *EtcdServer
28var client *kvstore.EtcdClient
29
30func setup() {
31 etcdServer = StartEtcdServer(nil)
32 if etcdServer == nil {
33 log.Fatal("Embedded server failed to start")
34 }
35 var err error
36 client, err = kvstore.NewEtcdClient("localhost:2379", 10)
37 if err != nil || client == nil {
38 etcdServer.Stop()
39 log.Fatal("Failed to create an Etcd client")
40 }
41}
42
43func TestEtcdServerRW(t *testing.T) {
44 key := "myKey-1"
45 value := "myVal-1"
46 err := client.Put(key, value, 10)
47 assert.Nil(t, err)
48 kvp, err := client.Get(key, 10)
49 assert.Nil(t, err)
50 assert.NotNil(t, kvp)
51 assert.Equal(t, kvp.Key, key)
52 val, err := kvstore.ToString(kvp.Value)
53 assert.Nil(t, err)
54 assert.Equal(t, value, val)
55}
56
57func TestEtcdServerReserve(t *testing.T) {
58 assert.NotNil(t, client)
59 txId := "tnxId-1"
60 val, err := client.Reserve("transactions", txId, 10)
61 assert.Nil(t, err)
62 assert.NotNil(t, val)
63 assert.Equal(t, val, txId)
64}
65
66func shutdown() {
67 if client != nil {
68 client.Close()
69 }
70 if etcdServer != nil {
71 etcdServer.Stop()
72 }
73}
74
75func TestMain(m *testing.M) {
76 setup()
77 code := m.Run()
78 shutdown()
79 os.Exit(code)
80}