blob: 3f029b65c6565904924705cd3b0d45ee57021b95 [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"
Matteo Scandolodfa7a972020-11-06 13:03:40 -080021 conf "github.com/opencord/voltha-lib-go/v4/pkg/config"
kdarapuf0c0e382019-09-30 05:26:31 +053022 "testing"
Scott Bakered4a8e72020-04-17 11:10:20 -070023 "time"
kdarapuf0c0e382019-09-30 05:26:31 +053024
Kent Hagermane6ff1012020-07-14 15:07:53 -040025 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
27
Girish Gowdraa09aeab2020-09-14 16:30:52 -070028 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
Scott Bakerdbd960e2020-02-28 08:57:51 -080029 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
30 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070031 ca "github.com/opencord/voltha-protos/v4/go/inter_container"
kdarapuf0c0e382019-09-30 05:26:31 +053032 "go.etcd.io/etcd/pkg/mock/mockserver"
33)
34
kdarapuf0c0e382019-09-30 05:26:31 +053035func newMockAdapter() *adapter {
36 conf := config.NewAdapterFlags()
37 conf.KVStoreType = "etcd"
38 cp := mocks.MockCoreProxy{}
39 ap := mocks.MockAdapterProxy{}
40 ad := newAdapter(conf)
41 ad.coreProxy = &cp
42 ad.adapterProxy = &ap
43 return ad
44}
45func Test_adapter_setKVClient(t *testing.T) {
46 adapt := newMockAdapter()
47 adapt1 := newMockAdapter()
48 adapt1.config.KVStoreType = "consul"
49 adapt2 := newMockAdapter()
50 adapt2.config.KVStoreType = ""
51 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040052 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053053 defer a.StopAt(0)
54 tests := []struct {
55 name string
56 clienttype string
57 adapter *adapter
58 wantErr bool
59 }{
60 {"setKVClient", adapt.config.KVStoreType, adapt, false},
61 {"setKVClient", adapt1.config.KVStoreType, adapt1, false},
62 {"setKVClient", adapt2.config.KVStoreType, adapt2, true},
63 }
64 for _, tt := range tests {
65 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000066 if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr {
kdarapuf0c0e382019-09-30 05:26:31 +053067 t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr)
68 }
69 })
70 }
71}
72
73func Test_adapter_KVClient(t *testing.T) {
74 adapt := newMockAdapter()
75 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -040076 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +053077 defer a.StopAt(0)
78
Neha Sharma96b7bf22020-06-15 10:37:32 +000079 if err := adapt.setKVClient(context.Background()); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +053080 t.Errorf("adapter.setKVClient() error = %v", err)
81 }
82}
83
84func Test_registerWithCore(t *testing.T) {
85 ad := newMockAdapter()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000086 ctx := context.TODO()
87 err := ad.registerWithCore(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053088 if err != nil {
89 t.Errorf("Expected error:nil, got error: %v", err)
90 }
91}
92func Test_startInterContainerProxy(t *testing.T) {
93 ad := newMockAdapter()
94 kc := &mockKafkaClient{}
95 ad.kafkaClient = kc
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000096 ctx := context.TODO()
97 icp, err := ad.startInterContainerProxy(ctx, 1)
kdarapuf0c0e382019-09-30 05:26:31 +053098 if icp != nil {
99 t.Log("Intercontainer proxy ", icp)
100 }
101 if err != nil {
102 t.Errorf("err %v", err)
103 }
104}
105
106func Test_startOpenOLT(t *testing.T) {
107 a, _ := mockserver.StartMockServers(1)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800108 cm := &conf.ConfigManager{}
Kent Hagermane6ff1012020-07-14 15:07:53 -0400109 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530110 defer a.StopAt(0)
111
112 ad := newMockAdapter()
113 oolt, err := ad.startOpenOLT(context.TODO(), nil,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800114 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530115 if oolt != nil {
116 t.Log("Open OLT ", oolt)
117 }
118 if err != nil {
119 t.Errorf("err %v", err)
120 }
121}
122
123func Test_newKafkaClient(t *testing.T) {
124 a, _ := mockserver.StartMockServers(1)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400125 _ = a.StartAt(0)
kdarapuf0c0e382019-09-30 05:26:31 +0530126 defer a.StopAt(0)
127 adapter := newMockAdapter()
128 type args struct {
129 clientType string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000130 address string
kdarapuf0c0e382019-09-30 05:26:31 +0530131 }
132 tests := []struct {
133 name string
134 args args
135 wantErr bool
136 }{
137 // TODO: Add test cases.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000138 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
139 {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false},
kdarapuf0c0e382019-09-30 05:26:31 +0530140 }
141 for _, tt := range tests {
142 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000143 _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address)
kdarapuf0c0e382019-09-30 05:26:31 +0530144 if (err != nil) != tt.wantErr {
145 t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr)
146 return
147 }
148
149 })
150 }
151}
152
153func Test_adapter_setupRequestHandler(t *testing.T) {
154
155 ad := newMockAdapter()
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800156 cm := &conf.ConfigManager{}
kdarapuf0c0e382019-09-30 05:26:31 +0530157
npujarec5762e2020-01-01 14:08:48 +0530158 kip := kafka.NewInterContainerProxy(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000159 kafka.InterContainerAddress(ad.config.KafkaAdapterAddress),
kdarapuf0c0e382019-09-30 05:26:31 +0530160 kafka.MsgClient(&mockKafkaClient{}),
161 kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic}))
162
163 ad.kip = kip
Kent Hagermane6ff1012020-07-14 15:07:53 -0400164 _ = ad.kip.Start(context.Background())
kdarapuf0c0e382019-09-30 05:26:31 +0530165
166 oolt, _ := ad.startOpenOLT(context.TODO(), nil,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800167 ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config, cm)
kdarapuf0c0e382019-09-30 05:26:31 +0530168 printBanner()
169 printVersion()
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000170 ctx := context.TODO()
171 if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil {
kdarapuf0c0e382019-09-30 05:26:31 +0530172 t.Logf("adapter.setupRequestHandler() error = %v", err)
173 }
174
175}
176
177// Kafka client mocker
178type mockKafkaClient struct {
179}
180
Neha Sharma96b7bf22020-06-15 10:37:32 +0000181func (kc *mockKafkaClient) Start(ctx context.Context) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530182 return nil
183}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000184func (kc *mockKafkaClient) Stop(ctx context.Context) {
kdarapuf0c0e382019-09-30 05:26:31 +0530185}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000186func (kc *mockKafkaClient) CreateTopic(ctx context.Context, topic *kafka.Topic, numPartition int, repFactor int) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530187 if topic != nil {
188 return nil
189 }
190 return errors.New("invalid Topic")
191}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000192func (kc *mockKafkaClient) DeleteTopic(ctx context.Context, topic *kafka.Topic) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530193 if topic != nil {
194 return nil
195 }
196 return errors.New("invalid Topic")
197}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000198func (kc *mockKafkaClient) Subscribe(ctx context.Context, topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) {
kdarapuf0c0e382019-09-30 05:26:31 +0530199 if topic != nil {
200 ch := make(chan *ca.InterContainerMessage)
201 return ch, nil
202 }
203 return nil, errors.New("invalid Topic")
204}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000205func (kc *mockKafkaClient) UnSubscribe(ctx context.Context, topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530206 if topic == nil {
207 return nil
208 }
209 return errors.New("invalid Topic")
210}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000211func (kc *mockKafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error {
kdarapuf0c0e382019-09-30 05:26:31 +0530212 if topic != nil {
213 return nil
214 }
215 return errors.New("invalid topic")
216}
cbabu95f21522019-11-13 14:25:18 +0100217
Neha Sharma96b7bf22020-06-15 10:37:32 +0000218func (kc *mockKafkaClient) SendLiveness(ctx context.Context) error {
cbabu95f21522019-11-13 14:25:18 +0100219 return status.Error(codes.Unimplemented, "SendLiveness")
220}
221
Neha Sharma96b7bf22020-06-15 10:37:32 +0000222func (kc *mockKafkaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
cbabu95f21522019-11-13 14:25:18 +0100223 return nil
224}
Scott Baker86fce9a2019-12-12 09:47:17 -0800225
Neha Sharma96b7bf22020-06-15 10:37:32 +0000226func (kc *mockKafkaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool {
Scott Baker86fce9a2019-12-12 09:47:17 -0800227 return nil
228}
npujarec5762e2020-01-01 14:08:48 +0530229
Neha Sharma96b7bf22020-06-15 10:37:32 +0000230func (kc *mockKafkaClient) SubscribeForMetadata(context.Context, func(fromTopic string, timestamp time.Time)) {
npujarec5762e2020-01-01 14:08:48 +0530231}