blob: 52463488a02c38a8ee1bcfec2868c5c626aab665 [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
127 host string
128 port int
129 }
130 tests := []struct {
131 name string
132 args args
133 wantErr bool
134 }{
135 // TODO: Add test cases.
136 {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false},
137 {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false},
138 }
139 for _, tt := range tests {
140 t.Run(tt.name, func(t *testing.T) {
141 _, err := newKafkaClient(tt.args.clientType, tt.args.host, tt.args.port)
142 if (err != nil) != tt.wantErr {
143 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
144 return
145 }
146
147 })
148 }
149}
150
151func Test_adapter_setupRequestHandler(t *testing.T) {
152
153 ad := newMockAdapter()
154
npujarec5762e2020-01-01 14:08:48 +0530155 kip := kafka.NewInterContainerProxy(
kdarapuf0c0e382019-09-30 05:26:31 +0530156 kafka.InterContainerHost(ad.config.KafkaAdapterHost),
157 kafka.InterContainerPort(ad.config.KafkaAdapterPort),
158 kafka.MsgClient(&mockKafkaClient{}),
159 kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic}))
160
161 ad.kip = kip
162 ad.kip.Start()
163
164 oolt, _ := ad.startOpenOLT(context.TODO(), nil,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530165 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config)
kdarapuf0c0e382019-09-30 05:26:31 +0530166 printBanner()
167 printVersion()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000168 ctx := context.TODO()
169 if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +0530170 t.Logf("adapter.setupRequestHandler() error = %v", err)
171 }
172
173}
174
175// Kafka client mocker
176type mockKafkaClient struct {
177}
178
179func (kc *mockKafkaClient) Start() error {
180 return nil
181}
182func (kc *mockKafkaClient) Stop() {
183}
184func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error {
185 if topic != nil {
186 return nil
187 }
188 return errors.New("invalid Topic")
189}
190func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error {
191 if topic != nil {
192 return nil
193 }
194 return errors.New("invalid Topic")
195}
196func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) {
197 if topic != nil {
198 ch := make(chan *ca.InterContainerMessage)
199 return ch, nil
200 }
201 return nil, errors.New("invalid Topic")
202}
203func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error {
204 if topic == nil {
205 return nil
206 }
207 return errors.New("invalid Topic")
208}
209func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error {
210 if topic != nil {
211 return nil
212 }
213 return errors.New("invalid topic")
214}
cbabu95f21522019-11-13 14:25:18 +0100215
216func (kc *mockKafkaClient) SendLiveness() error {
217 return status.Error(codes.Unimplemented, "SendLiveness")
218}
219
220func (kc *mockKafkaClient) EnableLivenessChannel(enable bool) chan bool {
221 return nil
222}
Scott Baker86fce9a2019-12-12 09:47:17 -0800223
224func (kc *mockKafkaClient) EnableHealthinessChannel(enable bool) chan bool {
225 return nil
226}
npujarec5762e2020-01-01 14:08:48 +0530227
Scott Bakered4a8e72020-04-17 11:10:20 -0700228func (kc *mockKafkaClient) SubscribeForMetadata(func(fromTopic string, timestamp time.Time)) {
npujarec5762e2020-01-01 14:08:48 +0530229 return
230}