blob: bd5c81a2b21b95c41b3d36db73f15981f3d806c6 [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"
Scott Bakered4a8e72020-04-17 11:10:20 -070024 "time"
kdarapuf0c0e382019-09-30 05:26:31 +053025
Esin Karamanccb714b2019-11-29 15:02:06 +000026 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
Scott Bakerdbd960e2020-02-28 08:57:51 -080027 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
28 "github.com/opencord/voltha-openolt-adapter/pkg/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
kdarapuf0c0e382019-09-30 05:26:31 +053033func newMockAdapter() *adapter {
34 conf := config.NewAdapterFlags()
35 conf.KVStoreType = "etcd"
36 cp := mocks.MockCoreProxy{}
37 ap := mocks.MockAdapterProxy{}
38 ad := newAdapter(conf)
39 ad.coreProxy = &cp
40 ad.adapterProxy = &ap
41 return ad
42}
43func Test_adapter_setKVClient(t *testing.T) {
44 adapt := newMockAdapter()
45 adapt1 := newMockAdapter()
46 adapt1.config.KVStoreType = "consul"
47 adapt2 := newMockAdapter()
48 adapt2.config.KVStoreType = ""
49 a, _ := mockserver.StartMockServers(1)
50 a.StartAt(0)
51 defer a.StopAt(0)
52 tests := []struct {
53 name string
54 clienttype string
55 adapter *adapter
56 wantErr bool
57 }{
58 {"setKVClient", adapt.config.KVStoreType, adapt, false},
59 {"setKVClient", adapt1.config.KVStoreType, adapt1, false},
60 {"setKVClient", adapt2.config.KVStoreType, adapt2, true},
61 }
62 for _, tt := range tests {
63 t.Run(tt.name, func(t *testing.T) {
64 if err := tt.adapter.setKVClient(); (err != nil) != tt.wantErr {
65 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
66 }
67 })
68 }
69}
70
71func Test_adapter_KVClient(t *testing.T) {
72 adapt := newMockAdapter()
73 a, _ := mockserver.StartMockServers(1)
74 a.StartAt(0)
75 defer a.StopAt(0)
76
77 if err := adapt.setKVClient(); err != nil {
78 t.Errorf("adapter.setKVClient() error = %v", err)
79 }
80}
81
82func Test_registerWithCore(t *testing.T) {
83 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000084 ctx := context.TODO()
85 err := ad.registerWithCore(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053086 if err != nil {
87 t.Errorf("Expected error:nil, got error: %v", err)
88 }
89}
90func Test_startInterContainerProxy(t *testing.T) {
91 ad := newMockAdapter()
92 kc := &mockKafkaClient{}
93 ad.kafkaClient = kc
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000094 ctx := context.TODO()
95 icp, err := ad.startInterContainerProxy(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053096 if icp != nil {
97 t.Log("Intercontainer proxy ", icp)
98 }
99 if err != nil {
100 t.Errorf("err %v", err)
101 }
102}
103
104func Test_startOpenOLT(t *testing.T) {
105 a, _ := mockserver.StartMockServers(1)
106 a.StartAt(0)
107 defer a.StopAt(0)
108
109 ad := newMockAdapter()
110 oolt, err := ad.startOpenOLT(context.TODO(), nil,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530111 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config)
kdarapuf0c0e382019-09-30 05:26:31 +0530112 if oolt != nil {
113 t.Log("Open OLT ", oolt)
114 }
115 if err != nil {
116 t.Errorf("err %v", err)
117 }
118}
119
120func Test_newKafkaClient(t *testing.T) {
121 a, _ := mockserver.StartMockServers(1)
122 a.StartAt(0)
123 defer a.StopAt(0)
124 adapter := newMockAdapter()
125 type args struct {
126 clientType string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000127 address string
kdarapuf0c0e382019-09-30 05:26:31 +0530128 }
129 tests := []struct {
130 name string
131 args args
132 wantErr bool
133 }{
134 // TODO: Add test cases.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000135 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
136 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530137 }
138 for _, tt := range tests {
139 t.Run(tt.name, func(t *testing.T) {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000140 _, err := newKafkaClient(tt.args.clientType, tt.args.address)
kdarapuf0c0e382019-09-30 05:26:31 +0530141 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(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000155 kafka.InterContainerAddress(ad.config.KafkaAdapterAddress),
kdarapuf0c0e382019-09-30 05:26:31 +0530156 kafka.MsgClient(&mockKafkaClient{}),
157 kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic}))
158
159 ad.kip = kip
160 ad.kip.Start()
161
162 oolt, _ := ad.startOpenOLT(context.TODO(), nil,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530163 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config)
kdarapuf0c0e382019-09-30 05:26:31 +0530164 printBanner()
165 printVersion()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000166 ctx := context.TODO()
167 if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +0530168 t.Logf("adapter.setupRequestHandler() error = %v", err)
169 }
170
171}
172
173// Kafka client mocker
174type mockKafkaClient struct {
175}
176
177func (kc *mockKafkaClient) Start() error {
178 return nil
179}
180func (kc *mockKafkaClient) Stop() {
181}
182func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error {
183 if topic != nil {
184 return nil
185 }
186 return errors.New("invalid Topic")
187}
188func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error {
189 if topic != nil {
190 return nil
191 }
192 return errors.New("invalid Topic")
193}
194func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) {
195 if topic != nil {
196 ch := make(chan *ca.InterContainerMessage)
197 return ch, nil
198 }
199 return nil, errors.New("invalid Topic")
200}
201func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error {
202 if topic == nil {
203 return nil
204 }
205 return errors.New("invalid Topic")
206}
207func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error {
208 if topic != nil {
209 return nil
210 }
211 return errors.New("invalid topic")
212}
cbabu95f21522019-11-13 14:25:18 +0100213
214func (kc *mockKafkaClient) SendLiveness() error {
215 return status.Error(codes.Unimplemented, "SendLiveness")
216}
217
218func (kc *mockKafkaClient) EnableLivenessChannel(enable bool) chan bool {
219 return nil
220}
Scott Baker86fce9a2019-12-12 09:47:17 -0800221
222func (kc *mockKafkaClient) EnableHealthinessChannel(enable bool) chan bool {
223 return nil
224}
npujarec5762e2020-01-01 14:08:48 +0530225
Scott Bakered4a8e72020-04-17 11:10:20 -0700226func (kc *mockKafkaClient) SubscribeForMetadata(func(fromTopic string, timestamp time.Time)) {
npujarec5762e2020-01-01 14:08:48 +0530227 return
228}