kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package main |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 21 | "google.golang.org/grpc/codes" |
| 22 | "google.golang.org/grpc/status" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 23 | "testing" |
Scott Baker | ed4a8e7 | 2020-04-17 11:10:20 -0700 | [diff] [blame] | 24 | "time" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 25 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 27 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
| 28 | "github.com/opencord/voltha-openolt-adapter/pkg/mocks" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 29 | ca "github.com/opencord/voltha-protos/v3/go/inter_container" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 30 | "go.etcd.io/etcd/pkg/mock/mockserver" |
| 31 | ) |
| 32 | |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 33 | func 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 | } |
| 43 | func 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 | |
| 71 | func 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 | |
| 82 | func Test_registerWithCore(t *testing.T) { |
| 83 | ad := newMockAdapter() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 84 | ctx := context.TODO() |
| 85 | err := ad.registerWithCore(ctx, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 86 | if err != nil { |
| 87 | t.Errorf("Expected error:nil, got error: %v", err) |
| 88 | } |
| 89 | } |
| 90 | func Test_startInterContainerProxy(t *testing.T) { |
| 91 | ad := newMockAdapter() |
| 92 | kc := &mockKafkaClient{} |
| 93 | ad.kafkaClient = kc |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 94 | ctx := context.TODO() |
| 95 | icp, err := ad.startInterContainerProxy(ctx, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 96 | if icp != nil { |
| 97 | t.Log("Intercontainer proxy ", icp) |
| 98 | } |
| 99 | if err != nil { |
| 100 | t.Errorf("err %v", err) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func 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 Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 111 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 112 | if oolt != nil { |
| 113 | t.Log("Open OLT ", oolt) |
| 114 | } |
| 115 | if err != nil { |
| 116 | t.Errorf("err %v", err) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func 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 |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 127 | address string |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 128 | } |
| 129 | tests := []struct { |
| 130 | name string |
| 131 | args args |
| 132 | wantErr bool |
| 133 | }{ |
| 134 | // TODO: Add test cases. |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 135 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false}, |
| 136 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false}, |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 137 | } |
| 138 | for _, tt := range tests { |
| 139 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 140 | _, err := newKafkaClient(tt.args.clientType, tt.args.address) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 141 | if (err != nil) != tt.wantErr { |
| 142 | t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr) |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | }) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func Test_adapter_setupRequestHandler(t *testing.T) { |
| 151 | |
| 152 | ad := newMockAdapter() |
| 153 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 154 | kip := kafka.NewInterContainerProxy( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 155 | kafka.InterContainerAddress(ad.config.KafkaAdapterAddress), |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 156 | kafka.MsgClient(&mockKafkaClient{}), |
| 157 | kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic})) |
| 158 | |
| 159 | ad.kip = kip |
| 160 | ad.kip.Start() |
| 161 | |
| 162 | oolt, _ := ad.startOpenOLT(context.TODO(), nil, |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 163 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 164 | printBanner() |
| 165 | printVersion() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 166 | ctx := context.TODO() |
| 167 | if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 168 | t.Logf("adapter.setupRequestHandler() error = %v", err) |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | // Kafka client mocker |
| 174 | type mockKafkaClient struct { |
| 175 | } |
| 176 | |
| 177 | func (kc *mockKafkaClient) Start() error { |
| 178 | return nil |
| 179 | } |
| 180 | func (kc *mockKafkaClient) Stop() { |
| 181 | } |
| 182 | func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error { |
| 183 | if topic != nil { |
| 184 | return nil |
| 185 | } |
| 186 | return errors.New("invalid Topic") |
| 187 | } |
| 188 | func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error { |
| 189 | if topic != nil { |
| 190 | return nil |
| 191 | } |
| 192 | return errors.New("invalid Topic") |
| 193 | } |
| 194 | func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) { |
| 195 | if topic != nil { |
| 196 | ch := make(chan *ca.InterContainerMessage) |
| 197 | return ch, nil |
| 198 | } |
| 199 | return nil, errors.New("invalid Topic") |
| 200 | } |
| 201 | func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error { |
| 202 | if topic == nil { |
| 203 | return nil |
| 204 | } |
| 205 | return errors.New("invalid Topic") |
| 206 | } |
| 207 | func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error { |
| 208 | if topic != nil { |
| 209 | return nil |
| 210 | } |
| 211 | return errors.New("invalid topic") |
| 212 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 213 | |
| 214 | func (kc *mockKafkaClient) SendLiveness() error { |
| 215 | return status.Error(codes.Unimplemented, "SendLiveness") |
| 216 | } |
| 217 | |
| 218 | func (kc *mockKafkaClient) EnableLivenessChannel(enable bool) chan bool { |
| 219 | return nil |
| 220 | } |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 221 | |
| 222 | func (kc *mockKafkaClient) EnableHealthinessChannel(enable bool) chan bool { |
| 223 | return nil |
| 224 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 225 | |
Scott Baker | ed4a8e7 | 2020-04-17 11:10:20 -0700 | [diff] [blame] | 226 | func (kc *mockKafkaClient) SubscribeForMetadata(func(fromTopic string, timestamp time.Time)) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 227 | return |
| 228 | } |