blob: 948e63baff23a95ee2b213ba2857702b92152ef7 [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"
21 "github.com/opencord/voltha-go/ro_core/config"
22 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
23 grpcserver "github.com/opencord/voltha-lib-go/v2/pkg/grpc"
24 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Baker555307d2019-11-04 08:58:01 -080025 ic "github.com/opencord/voltha-protos/v2/go/inter_container"
Daniele Rossid68b0b72019-10-31 11:36:05 +010026 "github.com/phayes/freeport"
27 "github.com/stretchr/testify/assert"
28 "strconv"
29 "testing"
30)
31
32type roCore struct {
33 kvClient kvstore.Client
34 config *config.ROCoreFlags
35 halted bool
36 exitChannel chan int
37 grpcServer *grpcserver.GrpcServer
38 core *Core
39 //For test
40 receiverChannels []<-chan *ic.InterContainerMessage
41}
42
43func newROCore(cf *config.ROCoreFlags) *roCore {
44 var roCoreV roCore
45 roCoreV.config = cf
46 roCoreV.halted = false
47 roCoreV.exitChannel = make(chan int, 1)
48 roCoreV.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
49 return &roCoreV
50}
51
52func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
53
54 log.Infow("kv-store-type", log.Fields{"store": storeType})
55 switch storeType {
56 case "consul":
57 return kvstore.NewConsulClient(address, timeout)
58 case "etcd":
59 return kvstore.NewEtcdClient(address, timeout)
60 }
61 return nil, errors.New("unsupported-kv-store")
62}
63
64func MakeTestNewCore() (*config.ROCoreFlags, *roCore) {
65
66 freePort, errP := freeport.GetFreePort()
67 if errP == nil {
68 freePortStr := strconv.Itoa(freePort)
69
70 roCoreFlgs := config.NewROCoreFlags()
71 roC := newROCore(roCoreFlgs)
72 if (roC != nil) && (roCoreFlgs != nil) {
73 addr := "127.0.0.1" + ":" + freePortStr
74 cli, err := newKVClient("etcd", addr, 5)
75 if err == nil {
76 roC.kvClient = cli
77 return roCoreFlgs, roC
78 }
79 }
80 }
81 return nil, nil
82}
83
84func TestNewCore(t *testing.T) {
85
86 roCoreFlgs, roC := MakeTestNewCore()
87 assert.NotNil(t, roCoreFlgs)
88 assert.NotNil(t, roC)
89 core := NewCore("ro_core", roCoreFlgs, roC.kvClient)
90 assert.NotNil(t, core)
91}
92
93func TestNewCoreStartStop(t *testing.T) {
94
95 var ctx context.Context
96
97 roCoreFlgs, roC := MakeTestNewCore()
98 assert.NotNil(t, roCoreFlgs)
99 assert.NotNil(t, roC)
100 core := NewCore("ro_core", roCoreFlgs, roC.kvClient)
101 assert.NotNil(t, core)
102
103 core.Start(ctx)
104 core.Stop(ctx)
105}