[VOL-2193] Create mocks for Kafka Client and Etcd
This commit consists of:
1) A kafka client mock that implements the kafka client interface
under voltha-lib-go/pkg/kafka/client.go
2) An embedded Etcd server that runs in-process and represents an
Etcd server.
Change-Id: I52a36132568e08c596bb4136918bebcb654a3b99
diff --git a/pkg/mocks/etcd_server_test.go b/pkg/mocks/etcd_server_test.go
new file mode 100644
index 0000000..8230869
--- /dev/null
+++ b/pkg/mocks/etcd_server_test.go
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package mocks
+
+import (
+ "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
+ "github.com/stretchr/testify/assert"
+ "log"
+ "os"
+ "testing"
+)
+
+var etcdServer *EtcdServer
+var client *kvstore.EtcdClient
+
+func setup() {
+ etcdServer = StartEtcdServer(nil)
+ if etcdServer == nil {
+ log.Fatal("Embedded server failed to start")
+ }
+ var err error
+ client, err = kvstore.NewEtcdClient("localhost:2379", 10)
+ if err != nil || client == nil {
+ etcdServer.Stop()
+ log.Fatal("Failed to create an Etcd client")
+ }
+}
+
+func TestEtcdServerRW(t *testing.T) {
+ key := "myKey-1"
+ value := "myVal-1"
+ err := client.Put(key, value, 10)
+ assert.Nil(t, err)
+ kvp, err := client.Get(key, 10)
+ assert.Nil(t, err)
+ assert.NotNil(t, kvp)
+ assert.Equal(t, kvp.Key, key)
+ val, err := kvstore.ToString(kvp.Value)
+ assert.Nil(t, err)
+ assert.Equal(t, value, val)
+}
+
+func TestEtcdServerReserve(t *testing.T) {
+ assert.NotNil(t, client)
+ txId := "tnxId-1"
+ val, err := client.Reserve("transactions", txId, 10)
+ assert.Nil(t, err)
+ assert.NotNil(t, val)
+ assert.Equal(t, val, txId)
+}
+
+func shutdown() {
+ if client != nil {
+ client.Close()
+ }
+ if etcdServer != nil {
+ etcdServer.Stop()
+ }
+}
+
+func TestMain(m *testing.M) {
+ setup()
+ code := m.Run()
+ shutdown()
+ os.Exit(code)
+}