blob: 13f760cd7ae5d43049072ff3c8962df2b16490db [file] [log] [blame]
kdarapuf0c0e382019-09-30 05:26:31 +05301/*
2 * Copyright 2018-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 main
17
18import (
19 "context"
20 "errors"
cbabu95f21522019-11-13 14:25:18 +010021 "google.golang.org/grpc/codes"
22 "google.golang.org/grpc/status"
kdarapuf0c0e382019-09-30 05:26:31 +053023 "testing"
24
Esin Karamanccb714b2019-11-29 15:02:06 +000025 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
26 "github.com/opencord/voltha-lib-go/v3/pkg/log"
kdarapuf0c0e382019-09-30 05:26:31 +053027 "github.com/opencord/voltha-openolt-adapter/config"
28 "github.com/opencord/voltha-openolt-adapter/mocks"
Esin Karamanccb714b2019-11-29 15:02:06 +000029 ca "github.com/opencord/voltha-protos/v3/go/inter_container"
kdarapuf0c0e382019-09-30 05:26:31 +053030 "go.etcd.io/etcd/pkg/mock/mockserver"
31)
32
33func init() {
34 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
35}
36
37func newMockAdapter() *adapter {
38 conf := config.NewAdapterFlags()
39 conf.KVStoreType = "etcd"
40 cp := mocks.MockCoreProxy{}
41 ap := mocks.MockAdapterProxy{}
42 ad := newAdapter(conf)
43 ad.coreProxy = &cp
44 ad.adapterProxy = &ap
45 return ad
46}
47func Test_adapter_setKVClient(t *testing.T) {
48 adapt := newMockAdapter()
49 adapt1 := newMockAdapter()
50 adapt1.config.KVStoreType = "consul"
51 adapt2 := newMockAdapter()
52 adapt2.config.KVStoreType = ""
53 a, _ := mockserver.StartMockServers(1)
54 a.StartAt(0)
55 defer a.StopAt(0)
56 tests := []struct {
57 name string
58 clienttype string
59 adapter *adapter
60 wantErr bool
61 }{
62 {"setKVClient", adapt.config.KVStoreType, adapt, false},
63 {"setKVClient", adapt1.config.KVStoreType, adapt1, false},
64 {"setKVClient", adapt2.config.KVStoreType, adapt2, true},
65 }
66 for _, tt := range tests {
67 t.Run(tt.name, func(t *testing.T) {
68 if err := tt.adapter.setKVClient(); (err != nil) != tt.wantErr {
69 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
70 }
71 })
72 }
73}
74
75func Test_adapter_KVClient(t *testing.T) {
76 adapt := newMockAdapter()
77 a, _ := mockserver.StartMockServers(1)
78 a.StartAt(0)
79 defer a.StopAt(0)
80
81 if err := adapt.setKVClient(); err != nil {
82 t.Errorf("adapter.setKVClient() error = %v", err)
83 }
84}
85
86func Test_registerWithCore(t *testing.T) {
87 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000088 ctx := context.TODO()
89 err := ad.registerWithCore(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053090 if err != nil {
91 t.Errorf("Expected error:nil, got error: %v", err)
92 }
93}
94func Test_startInterContainerProxy(t *testing.T) {
95 ad := newMockAdapter()
96 kc := &mockKafkaClient{}
97 ad.kafkaClient = kc
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000098 ctx := context.TODO()
99 icp, err := ad.startInterContainerProxy(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +0530100 if icp != nil {
101 t.Log("Intercontainer proxy ", icp)
102 }
103 if err != nil {
104 t.Errorf("err %v", err)
105 }
106}
107
108func Test_startOpenOLT(t *testing.T) {
109 a, _ := mockserver.StartMockServers(1)
110 a.StartAt(0)
111 defer a.StopAt(0)
112
113 ad := newMockAdapter()
114 oolt, err := ad.startOpenOLT(context.TODO(), nil,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530115 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config)
kdarapuf0c0e382019-09-30 05:26:31 +0530116 if oolt != nil {
117 t.Log("Open OLT ", oolt)
118 }
119 if err != nil {
120 t.Errorf("err %v", err)
121 }
122}
123
124func Test_newKafkaClient(t *testing.T) {
125 a, _ := mockserver.StartMockServers(1)
126 a.StartAt(0)
127 defer a.StopAt(0)
128 adapter := newMockAdapter()
129 type args struct {
130 clientType string
131 host string
132 port int
133 }
134 tests := []struct {
135 name string
136 args args
137 wantErr bool
138 }{
139 // TODO: Add test cases.
140 {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false},
141 {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false},
142 }
143 for _, tt := range tests {
144 t.Run(tt.name, func(t *testing.T) {
145 _, err := newKafkaClient(tt.args.clientType, tt.args.host, tt.args.port)
146 if (err != nil) != tt.wantErr {
147 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
148 return
149 }
150
151 })
152 }
153}
154
155func Test_adapter_setupRequestHandler(t *testing.T) {
156
157 ad := newMockAdapter()
158
npujarec5762e2020-01-01 14:08:48 +0530159 kip := kafka.NewInterContainerProxy(
kdarapuf0c0e382019-09-30 05:26:31 +0530160 kafka.InterContainerHost(ad.config.KafkaAdapterHost),
161 kafka.InterContainerPort(ad.config.KafkaAdapterPort),
162 kafka.MsgClient(&mockKafkaClient{}),
163 kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic}))
164
165 ad.kip = kip
166 ad.kip.Start()
167
168 oolt, _ := ad.startOpenOLT(context.TODO(), nil,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530169 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config)
kdarapuf0c0e382019-09-30 05:26:31 +0530170 printBanner()
171 printVersion()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000172 ctx := context.TODO()
173 if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +0530174 t.Logf("adapter.setupRequestHandler() error = %v", err)
175 }
176
177}
178
179// Kafka client mocker
180type mockKafkaClient struct {
181}
182
183func (kc *mockKafkaClient) Start() error {
184 return nil
185}
186func (kc *mockKafkaClient) Stop() {
187}
188func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error {
189 if topic != nil {
190 return nil
191 }
192 return errors.New("invalid Topic")
193}
194func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error {
195 if topic != nil {
196 return nil
197 }
198 return errors.New("invalid Topic")
199}
200func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) {
201 if topic != nil {
202 ch := make(chan *ca.InterContainerMessage)
203 return ch, nil
204 }
205 return nil, errors.New("invalid Topic")
206}
207func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error {
208 if topic == nil {
209 return nil
210 }
211 return errors.New("invalid Topic")
212}
213func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error {
214 if topic != nil {
215 return nil
216 }
217 return errors.New("invalid topic")
218}
cbabu95f21522019-11-13 14:25:18 +0100219
220func (kc *mockKafkaClient) SendLiveness() error {
221 return status.Error(codes.Unimplemented, "SendLiveness")
222}
223
224func (kc *mockKafkaClient) EnableLivenessChannel(enable bool) chan bool {
225 return nil
226}
Scott Baker86fce9a2019-12-12 09:47:17 -0800227
228func (kc *mockKafkaClient) EnableHealthinessChannel(enable bool) chan bool {
229 return nil
230}
npujarec5762e2020-01-01 14:08:48 +0530231
232func (kc *mockKafkaClient) SubscribeForMetadata(func(fromTopic string, timestamp int64)) {
233 return
234}