blob: 5af10488f51647a650caa05d336ab0706946ed50 [file] [log] [blame]
Daniele Rossid68b0b72019-10-31 11:36:05 +01001/*
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 */
16package core
17
18import (
19 "context"
20 "errors"
Divya Desai660dbba2019-10-16 07:06:49 +000021 "fmt"
npujar03b018e2019-11-13 15:29:36 +053022 "testing"
23
Daniele Rossid68b0b72019-10-31 11:36:05 +010024 "github.com/opencord/voltha-go/ro_core/config"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
26 "github.com/opencord/voltha-lib-go/v3/pkg/log"
27 "github.com/opencord/voltha-lib-go/v3/pkg/mocks"
28 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
Daniele Rossid68b0b72019-10-31 11:36:05 +010029 "github.com/phayes/freeport"
30 "github.com/stretchr/testify/assert"
Daniele Rossid68b0b72019-10-31 11:36:05 +010031)
32
33type roCore struct {
34 kvClient kvstore.Client
35 config *config.ROCoreFlags
36 halted bool
37 exitChannel chan int
Daniele Rossid68b0b72019-10-31 11:36:05 +010038 //For test
39 receiverChannels []<-chan *ic.InterContainerMessage
40}
41
42func newROCore(cf *config.ROCoreFlags) *roCore {
43 var roCoreV roCore
44 roCoreV.config = cf
45 roCoreV.halted = false
46 roCoreV.exitChannel = make(chan int, 1)
47 roCoreV.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
48 return &roCoreV
49}
50
51func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
52
53 log.Infow("kv-store-type", log.Fields{"store": storeType})
54 switch storeType {
55 case "consul":
56 return kvstore.NewConsulClient(address, timeout)
57 case "etcd":
58 return kvstore.NewEtcdClient(address, timeout)
59 }
60 return nil, errors.New("unsupported-kv-store")
61}
62
npujar03b018e2019-11-13 15:29:36 +053063func makeTestNewCore() (*config.ROCoreFlags, *roCore) {
Daniele Rossid68b0b72019-10-31 11:36:05 +010064
Divya Desai660dbba2019-10-16 07:06:49 +000065 clientPort, err := freeport.GetFreePort()
66 if err == nil {
67 peerPort, err := freeport.GetFreePort()
68 if err != nil {
69 log.Fatal(err)
70 }
71 etcdServer := mocks.StartEtcdServer(mocks.MKConfig("voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error"))
72 if etcdServer == nil {
73 log.Fatal("Embedded server failed to start")
74 }
75 clientAddr := fmt.Sprintf("localhost:%d", clientPort)
Daniele Rossid68b0b72019-10-31 11:36:05 +010076
77 roCoreFlgs := config.NewROCoreFlags()
78 roC := newROCore(roCoreFlgs)
79 if (roC != nil) && (roCoreFlgs != nil) {
Divya Desai660dbba2019-10-16 07:06:49 +000080 cli, err := newKVClient("etcd", clientAddr, 5)
Daniele Rossid68b0b72019-10-31 11:36:05 +010081 if err == nil {
82 roC.kvClient = cli
83 return roCoreFlgs, roC
84 }
npujar03b018e2019-11-13 15:29:36 +053085 etcdServer.Stop()
86 log.Fatal("Failed to create an Etcd client")
Daniele Rossid68b0b72019-10-31 11:36:05 +010087 }
88 }
89 return nil, nil
90}
91
92func TestNewCore(t *testing.T) {
93
Thomas Lee Se5a44012019-11-07 20:32:24 +053094 var ctx context.Context
95
npujar03b018e2019-11-13 15:29:36 +053096 roCoreFlgs, roC := makeTestNewCore()
Daniele Rossid68b0b72019-10-31 11:36:05 +010097 assert.NotNil(t, roCoreFlgs)
98 assert.NotNil(t, roC)
Thomas Lee Se5a44012019-11-07 20:32:24 +053099 core := NewCore(ctx, "ro_core", roCoreFlgs, roC.kvClient)
Daniele Rossid68b0b72019-10-31 11:36:05 +0100100 assert.NotNil(t, core)
101}
102
103func TestNewCoreStartStop(t *testing.T) {
104
105 var ctx context.Context
106
npujar03b018e2019-11-13 15:29:36 +0530107 roCoreFlgs, roC := makeTestNewCore()
Daniele Rossid68b0b72019-10-31 11:36:05 +0100108 assert.NotNil(t, roCoreFlgs)
109 assert.NotNil(t, roC)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530110 core := NewCore(ctx, "ro_core", roCoreFlgs, roC.kvClient)
Daniele Rossid68b0b72019-10-31 11:36:05 +0100111 assert.NotNil(t, core)
112
Thomas Lee Se5a44012019-11-07 20:32:24 +0530113 err := core.Start(ctx)
114 assert.Nil(t, err)
Daniele Rossid68b0b72019-10-31 11:36:05 +0100115 core.Stop(ctx)
116}