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