blob: 617007d0a51ca8c6e43a1d9fdf617899f56d1abb [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
Girish Gowdra4c3d4602021-07-22 16:33:37 -070024 conf "github.com/opencord/voltha-lib-go/v6/pkg/config"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030025
Kent Hagermane6ff1012020-07-14 15:07:53 -040026 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
28
Girish Gowdra4c3d4602021-07-22 16:33:37 -070029 "github.com/opencord/voltha-lib-go/v6/pkg/kafka"
Scott Bakerdbd960e2020-02-28 08:57:51 -080030 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
31 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070032 ca "github.com/opencord/voltha-protos/v4/go/inter_container"
kdarapuf0c0e382019-09-30 05:26:31 +053033 "go.etcd.io/etcd/pkg/mock/mockserver"
34)
35
kdarapuf0c0e382019-09-30 05:26:31 +053036func newMockAdapter() *adapter {
37 conf := config.NewAdapterFlags()
38 conf.KVStoreType = "etcd"
39 cp := mocks.MockCoreProxy{}
40 ap := mocks.MockAdapterProxy{}
41 ad := newAdapter(conf)
42 ad.coreProxy = &cp
43 ad.adapterProxy = &ap
44 return ad
45}
46func Test_adapter_setKVClient(t *testing.T) {
47 adapt := newMockAdapter()
48 adapt1 := newMockAdapter()
serkant.uluderya7b8211e2021-02-24 16:39:18 +030049 adapt1.config.KVStoreType = "etcd"
kdarapuf0c0e382019-09-30 05:26:31 +053050 adapt2 := newMockAdapter()
51 adapt2.config.KVStoreType = ""
52 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040053 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053054 defer a.StopAt(0)
55 tests := []struct {
56 name string
57 clienttype string
58 adapter *adapter
59 wantErr bool
60 }{
61 {"setKVClient", adapt.config.KVStoreType, adapt, false},
62 {"setKVClient", adapt1.config.KVStoreType, adapt1, false},
63 {"setKVClient", adapt2.config.KVStoreType, adapt2, true},
64 }
65 for _, tt := range tests {
66 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000067 if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr {
kdarapuf0c0e382019-09-30 05:26:31 +053068 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
69 }
70 })
71 }
72}
73
74func Test_adapter_KVClient(t *testing.T) {
75 adapt := newMockAdapter()
76 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040077 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053078 defer a.StopAt(0)
79
Neha Sharma96b7bf22020-06-15 10:37:32 +000080 if err := adapt.setKVClient(context.Background()); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +053081 t.Errorf("adapter.setKVClient() error = %v", err)
82 }
83}
84
85func Test_registerWithCore(t *testing.T) {
86 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000087 ctx := context.TODO()
88 err := ad.registerWithCore(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053089 if err != nil {
90 t.Errorf("Expected error:nil, got error: %v", err)
91 }
92}
93func Test_startInterContainerProxy(t *testing.T) {
94 ad := newMockAdapter()
95 kc := &mockKafkaClient{}
96 ad.kafkaClient = kc
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000097 ctx := context.TODO()
98 icp, err := ad.startInterContainerProxy(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053099 if icp != nil {
100 t.Log("Intercontainer proxy ", icp)
101 }
102 if err != nil {
103 t.Errorf("err %v", err)
104 }
105}
106
107func Test_startOpenOLT(t *testing.T) {
108 a, _ := mockserver.StartMockServers(1)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800109 cm := &conf.ConfigManager{}
Kent Hagermane6ff1012020-07-14 15:07:53 -0400110 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530111 defer a.StopAt(0)
112
113 ad := newMockAdapter()
114 oolt, err := ad.startOpenOLT(context.TODO(), nil,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800115 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530116 if oolt != nil {
117 t.Log("Open OLT ", oolt)
118 }
119 if err != nil {
120 t.Errorf("err %v", err)
121 }
122}
123
124func Test_newKafkaClient(t *testing.T) {
125 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400126 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530127 defer a.StopAt(0)
128 adapter := newMockAdapter()
129 type args struct {
130 clientType string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000131 address string
kdarapuf0c0e382019-09-30 05:26:31 +0530132 }
133 tests := []struct {
134 name string
135 args args
136 wantErr bool
137 }{
138 // TODO: Add test cases.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000139 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
140 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530141 }
142 for _, tt := range tests {
143 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000144 _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address)
kdarapuf0c0e382019-09-30 05:26:31 +0530145 if (err != nil) != tt.wantErr {
146 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
147 return
148 }
149
150 })
151 }
152}
153
154func Test_adapter_setupRequestHandler(t *testing.T) {
155
156 ad := newMockAdapter()
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800157 cm := &conf.ConfigManager{}
kdarapuf0c0e382019-09-30 05:26:31 +0530158
npujarec5762e2020-01-01 14:08:48 +0530159 kip := kafka.NewInterContainerProxy(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000160 kafka.InterContainerAddress(ad.config.KafkaAdapterAddress),
kdarapuf0c0e382019-09-30 05:26:31 +0530161 kafka.MsgClient(&mockKafkaClient{}),
162 kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic}))
163
164 ad.kip = kip
Kent Hagermane6ff1012020-07-14 15:07:53 -0400165 _ = ad.kip.Start(context.Background())
kdarapuf0c0e382019-09-30 05:26:31 +0530166
167 oolt, _ := ad.startOpenOLT(context.TODO(), nil,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800168 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530169 printBanner()
170 printVersion()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000171 ctx := context.TODO()
172 if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +0530173 t.Logf("adapter.setupRequestHandler() error = %v", err)
174 }
175
176}
177
178// Kafka client mocker
179type mockKafkaClient struct {
180}
181
Neha Sharma96b7bf22020-06-15 10:37:32 +0000182func (kc *mockKafkaClient) Start(ctx context.Context) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530183 return nil
184}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000185func (kc *mockKafkaClient) Stop(ctx context.Context) {
kdarapuf0c0e382019-09-30 05:26:31 +0530186}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000187func (kc *mockKafkaClient) CreateTopic(ctx context.Context, topic *kafka.Topic, numPartition int, repFactor int) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530188 if topic != nil {
189 return nil
190 }
191 return errors.New("invalid Topic")
192}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000193func (kc *mockKafkaClient) DeleteTopic(ctx context.Context, topic *kafka.Topic) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530194 if topic != nil {
195 return nil
196 }
197 return errors.New("invalid Topic")
198}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000199func (kc *mockKafkaClient) Subscribe(ctx context.Context, topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) {
kdarapuf0c0e382019-09-30 05:26:31 +0530200 if topic != nil {
201 ch := make(chan *ca.InterContainerMessage)
202 return ch, nil
203 }
204 return nil, errors.New("invalid Topic")
205}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000206func (kc *mockKafkaClient) UnSubscribe(ctx context.Context, topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530207 if topic == nil {
208 return nil
209 }
210 return errors.New("invalid Topic")
211}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000212func (kc *mockKafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530213 if topic != nil {
214 return nil
215 }
216 return errors.New("invalid topic")
217}
cbabu95f21522019-11-13 14:25:18 +0100218
Neha Sharma96b7bf22020-06-15 10:37:32 +0000219func (kc *mockKafkaClient) SendLiveness(ctx context.Context) error {
cbabu95f21522019-11-13 14:25:18 +0100220 return status.Error(codes.Unimplemented, "SendLiveness")
221}
222
Neha Sharma96b7bf22020-06-15 10:37:32 +0000223func (kc *mockKafkaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
cbabu95f21522019-11-13 14:25:18 +0100224 return nil
225}
Scott Baker86fce9a2019-12-12 09:47:17 -0800226
Neha Sharma96b7bf22020-06-15 10:37:32 +0000227func (kc *mockKafkaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool {
Scott Baker86fce9a2019-12-12 09:47:17 -0800228 return nil
229}
npujarec5762e2020-01-01 14:08:48 +0530230
Neha Sharma96b7bf22020-06-15 10:37:32 +0000231func (kc *mockKafkaClient) SubscribeForMetadata(context.Context, func(fromTopic string, timestamp time.Time)) {
npujarec5762e2020-01-01 14:08:48 +0530232}