blob: 6b6ea7e6d46728e10c7fe97e6567d6f44c7344f8 [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"
21 "testing"
Scott Bakered4a8e72020-04-17 11:10:20 -070022 "time"
kdarapuf0c0e382019-09-30 05:26:31 +053023
Kent Hagermane6ff1012020-07-14 15:07:53 -040024 "google.golang.org/grpc/codes"
25 "google.golang.org/grpc/status"
26
Girish Gowdraa09aeab2020-09-14 16:30:52 -070027 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
Scott Bakerdbd960e2020-02-28 08:57:51 -080028 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
29 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070030 ca "github.com/opencord/voltha-protos/v4/go/inter_container"
kdarapuf0c0e382019-09-30 05:26:31 +053031 "go.etcd.io/etcd/pkg/mock/mockserver"
32)
33
kdarapuf0c0e382019-09-30 05:26:31 +053034func newMockAdapter() *adapter {
35 conf := config.NewAdapterFlags()
36 conf.KVStoreType = "etcd"
37 cp := mocks.MockCoreProxy{}
38 ap := mocks.MockAdapterProxy{}
39 ad := newAdapter(conf)
40 ad.coreProxy = &cp
41 ad.adapterProxy = &ap
42 return ad
43}
44func Test_adapter_setKVClient(t *testing.T) {
45 adapt := newMockAdapter()
46 adapt1 := newMockAdapter()
47 adapt1.config.KVStoreType = "consul"
48 adapt2 := newMockAdapter()
49 adapt2.config.KVStoreType = ""
50 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040051 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053052 defer a.StopAt(0)
53 tests := []struct {
54 name string
55 clienttype string
56 adapter *adapter
57 wantErr bool
58 }{
59 {"setKVClient", adapt.config.KVStoreType, adapt, false},
60 {"setKVClient", adapt1.config.KVStoreType, adapt1, false},
61 {"setKVClient", adapt2.config.KVStoreType, adapt2, true},
62 }
63 for _, tt := range tests {
64 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000065 if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr {
kdarapuf0c0e382019-09-30 05:26:31 +053066 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
67 }
68 })
69 }
70}
71
72func Test_adapter_KVClient(t *testing.T) {
73 adapt := newMockAdapter()
74 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040075 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053076 defer a.StopAt(0)
77
Neha Sharma96b7bf22020-06-15 10:37:32 +000078 if err := adapt.setKVClient(context.Background()); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +053079 t.Errorf("adapter.setKVClient() error = %v", err)
80 }
81}
82
83func Test_registerWithCore(t *testing.T) {
84 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000085 ctx := context.TODO()
86 err := ad.registerWithCore(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053087 if err != nil {
88 t.Errorf("Expected error:nil, got error: %v", err)
89 }
90}
91func Test_startInterContainerProxy(t *testing.T) {
92 ad := newMockAdapter()
93 kc := &mockKafkaClient{}
94 ad.kafkaClient = kc
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000095 ctx := context.TODO()
96 icp, err := ad.startInterContainerProxy(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053097 if icp != nil {
98 t.Log("Intercontainer proxy ", icp)
99 }
100 if err != nil {
101 t.Errorf("err %v", err)
102 }
103}
104
105func Test_startOpenOLT(t *testing.T) {
106 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400107 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530108 defer a.StopAt(0)
109
110 ad := newMockAdapter()
111 oolt, err := ad.startOpenOLT(context.TODO(), nil,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530112 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config)
kdarapuf0c0e382019-09-30 05:26:31 +0530113 if oolt != nil {
114 t.Log("Open OLT ", oolt)
115 }
116 if err != nil {
117 t.Errorf("err %v", err)
118 }
119}
120
121func Test_newKafkaClient(t *testing.T) {
122 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400123 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530124 defer a.StopAt(0)
125 adapter := newMockAdapter()
126 type args struct {
127 clientType string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000128 address string
kdarapuf0c0e382019-09-30 05:26:31 +0530129 }
130 tests := []struct {
131 name string
132 args args
133 wantErr bool
134 }{
135 // TODO: Add test cases.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000136 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
137 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530138 }
139 for _, tt := range tests {
140 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000141 _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address)
kdarapuf0c0e382019-09-30 05:26:31 +0530142 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(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000156 kafka.InterContainerAddress(ad.config.KafkaAdapterAddress),
kdarapuf0c0e382019-09-30 05:26:31 +0530157 kafka.MsgClient(&mockKafkaClient{}),
158 kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic}))
159
160 ad.kip = kip
Kent Hagermane6ff1012020-07-14 15:07:53 -0400161 _ = ad.kip.Start(context.Background())
kdarapuf0c0e382019-09-30 05:26:31 +0530162
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
Neha Sharma96b7bf22020-06-15 10:37:32 +0000178func (kc *mockKafkaClient) Start(ctx context.Context) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530179 return nil
180}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000181func (kc *mockKafkaClient) Stop(ctx context.Context) {
kdarapuf0c0e382019-09-30 05:26:31 +0530182}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000183func (kc *mockKafkaClient) CreateTopic(ctx context.Context, topic *kafka.Topic, numPartition int, repFactor int) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530184 if topic != nil {
185 return nil
186 }
187 return errors.New("invalid Topic")
188}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000189func (kc *mockKafkaClient) DeleteTopic(ctx context.Context, topic *kafka.Topic) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530190 if topic != nil {
191 return nil
192 }
193 return errors.New("invalid Topic")
194}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000195func (kc *mockKafkaClient) Subscribe(ctx context.Context, topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) {
kdarapuf0c0e382019-09-30 05:26:31 +0530196 if topic != nil {
197 ch := make(chan *ca.InterContainerMessage)
198 return ch, nil
199 }
200 return nil, errors.New("invalid Topic")
201}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000202func (kc *mockKafkaClient) UnSubscribe(ctx context.Context, topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530203 if topic == nil {
204 return nil
205 }
206 return errors.New("invalid Topic")
207}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000208func (kc *mockKafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530209 if topic != nil {
210 return nil
211 }
212 return errors.New("invalid topic")
213}
cbabu95f21522019-11-13 14:25:18 +0100214
Neha Sharma96b7bf22020-06-15 10:37:32 +0000215func (kc *mockKafkaClient) SendLiveness(ctx context.Context) error {
cbabu95f21522019-11-13 14:25:18 +0100216 return status.Error(codes.Unimplemented, "SendLiveness")
217}
218
Neha Sharma96b7bf22020-06-15 10:37:32 +0000219func (kc *mockKafkaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
cbabu95f21522019-11-13 14:25:18 +0100220 return nil
221}
Scott Baker86fce9a2019-12-12 09:47:17 -0800222
Neha Sharma96b7bf22020-06-15 10:37:32 +0000223func (kc *mockKafkaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool {
Scott Baker86fce9a2019-12-12 09:47:17 -0800224 return nil
225}
npujarec5762e2020-01-01 14:08:48 +0530226
Neha Sharma96b7bf22020-06-15 10:37:32 +0000227func (kc *mockKafkaClient) SubscribeForMetadata(context.Context, func(fromTopic string, timestamp time.Time)) {
npujarec5762e2020-01-01 14:08:48 +0530228}