blob: f25d132b5152508f4f949fb48a518cc3e76c05bd [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
Scott Baker51290152019-10-24 14:23:20 -070025 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
26 "github.com/opencord/voltha-lib-go/v2/pkg/log"
kdarapuf0c0e382019-09-30 05:26:31 +053027 "github.com/opencord/voltha-openolt-adapter/config"
28 "github.com/opencord/voltha-openolt-adapter/mocks"
Scott Bakerc6e54cb2019-11-04 09:31:25 -080029 ca "github.com/opencord/voltha-protos/v2/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,
115 ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost,
116 ad.config.KVStorePort, ad.config.KVStoreType)
117 if oolt != nil {
118 t.Log("Open OLT ", oolt)
119 }
120 if err != nil {
121 t.Errorf("err %v", err)
122 }
123}
124
125func Test_newKafkaClient(t *testing.T) {
126 a, _ := mockserver.StartMockServers(1)
127 a.StartAt(0)
128 defer a.StopAt(0)
129 adapter := newMockAdapter()
130 type args struct {
131 clientType string
132 host string
133 port int
134 }
135 tests := []struct {
136 name string
137 args args
138 wantErr bool
139 }{
140 // TODO: Add test cases.
141 {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false},
142 {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false},
143 }
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146 _, err := newKafkaClient(tt.args.clientType, tt.args.host, tt.args.port)
147 if (err != nil) != tt.wantErr {
148 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
149 return
150 }
151
152 })
153 }
154}
155
156func Test_adapter_setupRequestHandler(t *testing.T) {
157
158 ad := newMockAdapter()
159
160 kip, _ := kafka.NewInterContainerProxy(
161 kafka.InterContainerHost(ad.config.KafkaAdapterHost),
162 kafka.InterContainerPort(ad.config.KafkaAdapterPort),
163 kafka.MsgClient(&mockKafkaClient{}),
164 kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic}))
165
166 ad.kip = kip
167 ad.kip.Start()
168
169 oolt, _ := ad.startOpenOLT(context.TODO(), nil,
170 ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost,
171 ad.config.KVStorePort, ad.config.KVStoreType)
172 printBanner()
173 printVersion()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000174 ctx := context.TODO()
175 if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +0530176 t.Logf("adapter.setupRequestHandler() error = %v", err)
177 }
178
179}
180
181// Kafka client mocker
182type mockKafkaClient struct {
183}
184
185func (kc *mockKafkaClient) Start() error {
186 return nil
187}
188func (kc *mockKafkaClient) Stop() {
189}
190func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error {
191 if topic != nil {
192 return nil
193 }
194 return errors.New("invalid Topic")
195}
196func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error {
197 if topic != nil {
198 return nil
199 }
200 return errors.New("invalid Topic")
201}
202func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) {
203 if topic != nil {
204 ch := make(chan *ca.InterContainerMessage)
205 return ch, nil
206 }
207 return nil, errors.New("invalid Topic")
208}
209func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error {
210 if topic == nil {
211 return nil
212 }
213 return errors.New("invalid Topic")
214}
215func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error {
216 if topic != nil {
217 return nil
218 }
219 return errors.New("invalid topic")
220}
cbabu95f21522019-11-13 14:25:18 +0100221
222func (kc *mockKafkaClient) SendLiveness() error {
223 return status.Error(codes.Unimplemented, "SendLiveness")
224}
225
226func (kc *mockKafkaClient) EnableLivenessChannel(enable bool) chan bool {
227 return nil
228}
Scott Baker86fce9a2019-12-12 09:47:17 -0800229
230func (kc *mockKafkaClient) EnableHealthinessChannel(enable bool) chan bool {
231 return nil
232}