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