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