khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package mocks |
| 18 | |
| 19 | import ( |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 20 | "fmt" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 22 | "github.com/phayes/freeport" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 24 | "os" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | var etcdServer *EtcdServer |
| 29 | var client *kvstore.EtcdClient |
| 30 | |
| 31 | func setup() { |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 32 | clientPort, err := freeport.GetFreePort() |
| 33 | if err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame^] | 34 | logger.Fatal(err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 35 | } |
| 36 | peerPort, err := freeport.GetFreePort() |
| 37 | if err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame^] | 38 | logger.Fatal(err) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 39 | } |
| 40 | etcdServer = StartEtcdServer(MKConfig("voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error")) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 41 | if etcdServer == nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame^] | 42 | logger.Fatal("Embedded server failed to start") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 43 | } |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 44 | clientAddr := fmt.Sprintf("localhost:%d", clientPort) |
| 45 | client, err = kvstore.NewEtcdClient(clientAddr, 10) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 46 | if err != nil || client == nil { |
| 47 | etcdServer.Stop() |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame^] | 48 | logger.Fatal("Failed to create an Etcd client") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestEtcdServerRW(t *testing.T) { |
| 53 | key := "myKey-1" |
| 54 | value := "myVal-1" |
| 55 | err := client.Put(key, value, 10) |
| 56 | assert.Nil(t, err) |
| 57 | kvp, err := client.Get(key, 10) |
| 58 | assert.Nil(t, err) |
| 59 | assert.NotNil(t, kvp) |
| 60 | assert.Equal(t, kvp.Key, key) |
| 61 | val, err := kvstore.ToString(kvp.Value) |
| 62 | assert.Nil(t, err) |
| 63 | assert.Equal(t, value, val) |
| 64 | } |
| 65 | |
| 66 | func TestEtcdServerReserve(t *testing.T) { |
| 67 | assert.NotNil(t, client) |
| 68 | txId := "tnxId-1" |
| 69 | val, err := client.Reserve("transactions", txId, 10) |
| 70 | assert.Nil(t, err) |
| 71 | assert.NotNil(t, val) |
| 72 | assert.Equal(t, val, txId) |
| 73 | } |
| 74 | |
| 75 | func shutdown() { |
| 76 | if client != nil { |
| 77 | client.Close() |
| 78 | } |
| 79 | if etcdServer != nil { |
| 80 | etcdServer.Stop() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestMain(m *testing.M) { |
| 85 | setup() |
| 86 | code := m.Run() |
| 87 | shutdown() |
| 88 | os.Exit(code) |
| 89 | } |