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" |
| 24 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
| 27 | "github.com/opencord/voltha-openolt-adapter/pkg/mocks" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 28 | ca "github.com/opencord/voltha-protos/v3/go/inter_container" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 29 | "go.etcd.io/etcd/pkg/mock/mockserver" |
| 30 | ) |
| 31 | |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 32 | func newMockAdapter() *adapter { |
| 33 | conf := config.NewAdapterFlags() |
| 34 | conf.KVStoreType = "etcd" |
| 35 | cp := mocks.MockCoreProxy{} |
| 36 | ap := mocks.MockAdapterProxy{} |
| 37 | ad := newAdapter(conf) |
| 38 | ad.coreProxy = &cp |
| 39 | ad.adapterProxy = &ap |
| 40 | return ad |
| 41 | } |
| 42 | func Test_adapter_setKVClient(t *testing.T) { |
| 43 | adapt := newMockAdapter() |
| 44 | adapt1 := newMockAdapter() |
| 45 | adapt1.config.KVStoreType = "consul" |
| 46 | adapt2 := newMockAdapter() |
| 47 | adapt2.config.KVStoreType = "" |
| 48 | a, _ := mockserver.StartMockServers(1) |
| 49 | a.StartAt(0) |
| 50 | defer a.StopAt(0) |
| 51 | tests := []struct { |
| 52 | name string |
| 53 | clienttype string |
| 54 | adapter *adapter |
| 55 | wantErr bool |
| 56 | }{ |
| 57 | {"setKVClient", adapt.config.KVStoreType, adapt, false}, |
| 58 | {"setKVClient", adapt1.config.KVStoreType, adapt1, false}, |
| 59 | {"setKVClient", adapt2.config.KVStoreType, adapt2, true}, |
| 60 | } |
| 61 | for _, tt := range tests { |
| 62 | t.Run(tt.name, func(t *testing.T) { |
| 63 | if err := tt.adapter.setKVClient(); (err != nil) != tt.wantErr { |
| 64 | t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 65 | } |
| 66 | }) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func Test_adapter_KVClient(t *testing.T) { |
| 71 | adapt := newMockAdapter() |
| 72 | a, _ := mockserver.StartMockServers(1) |
| 73 | a.StartAt(0) |
| 74 | defer a.StopAt(0) |
| 75 | |
| 76 | if err := adapt.setKVClient(); err != nil { |
| 77 | t.Errorf("adapter.setKVClient() error = %v", err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func Test_registerWithCore(t *testing.T) { |
| 82 | ad := newMockAdapter() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 83 | ctx := context.TODO() |
| 84 | err := ad.registerWithCore(ctx, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 85 | if err != nil { |
| 86 | t.Errorf("Expected error:nil, got error: %v", err) |
| 87 | } |
| 88 | } |
| 89 | func Test_startInterContainerProxy(t *testing.T) { |
| 90 | ad := newMockAdapter() |
| 91 | kc := &mockKafkaClient{} |
| 92 | ad.kafkaClient = kc |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 93 | ctx := context.TODO() |
| 94 | icp, err := ad.startInterContainerProxy(ctx, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 95 | if icp != nil { |
| 96 | t.Log("Intercontainer proxy ", icp) |
| 97 | } |
| 98 | if err != nil { |
| 99 | t.Errorf("err %v", err) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func Test_startOpenOLT(t *testing.T) { |
| 104 | a, _ := mockserver.StartMockServers(1) |
| 105 | a.StartAt(0) |
| 106 | defer a.StopAt(0) |
| 107 | |
| 108 | ad := newMockAdapter() |
| 109 | oolt, err := ad.startOpenOLT(context.TODO(), nil, |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 110 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 111 | if oolt != nil { |
| 112 | t.Log("Open OLT ", oolt) |
| 113 | } |
| 114 | if err != nil { |
| 115 | t.Errorf("err %v", err) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func Test_newKafkaClient(t *testing.T) { |
| 120 | a, _ := mockserver.StartMockServers(1) |
| 121 | a.StartAt(0) |
| 122 | defer a.StopAt(0) |
| 123 | adapter := newMockAdapter() |
| 124 | type args struct { |
| 125 | clientType string |
| 126 | host string |
| 127 | port int |
| 128 | } |
| 129 | tests := []struct { |
| 130 | name string |
| 131 | args args |
| 132 | wantErr bool |
| 133 | }{ |
| 134 | // TODO: Add test cases. |
| 135 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 136 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 137 | } |
| 138 | for _, tt := range tests { |
| 139 | t.Run(tt.name, func(t *testing.T) { |
| 140 | _, err := newKafkaClient(tt.args.clientType, tt.args.host, tt.args.port) |
| 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( |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 155 | kafka.InterContainerHost(ad.config.KafkaAdapterHost), |
| 156 | kafka.InterContainerPort(ad.config.KafkaAdapterPort), |
| 157 | kafka.MsgClient(&mockKafkaClient{}), |
| 158 | kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic})) |
| 159 | |
| 160 | ad.kip = kip |
| 161 | ad.kip.Start() |
| 162 | |
| 163 | oolt, _ := ad.startOpenOLT(context.TODO(), nil, |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 164 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 165 | printBanner() |
| 166 | printVersion() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 167 | ctx := context.TODO() |
| 168 | if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 169 | t.Logf("adapter.setupRequestHandler() error = %v", err) |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | // Kafka client mocker |
| 175 | type mockKafkaClient struct { |
| 176 | } |
| 177 | |
| 178 | func (kc *mockKafkaClient) Start() error { |
| 179 | return nil |
| 180 | } |
| 181 | func (kc *mockKafkaClient) Stop() { |
| 182 | } |
| 183 | func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error { |
| 184 | if topic != nil { |
| 185 | return nil |
| 186 | } |
| 187 | return errors.New("invalid Topic") |
| 188 | } |
| 189 | func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error { |
| 190 | if topic != nil { |
| 191 | return nil |
| 192 | } |
| 193 | return errors.New("invalid Topic") |
| 194 | } |
| 195 | func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) { |
| 196 | if topic != nil { |
| 197 | ch := make(chan *ca.InterContainerMessage) |
| 198 | return ch, nil |
| 199 | } |
| 200 | return nil, errors.New("invalid Topic") |
| 201 | } |
| 202 | func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error { |
| 203 | if topic == nil { |
| 204 | return nil |
| 205 | } |
| 206 | return errors.New("invalid Topic") |
| 207 | } |
| 208 | func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error { |
| 209 | if topic != nil { |
| 210 | return nil |
| 211 | } |
| 212 | return errors.New("invalid topic") |
| 213 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 214 | |
| 215 | func (kc *mockKafkaClient) SendLiveness() error { |
| 216 | return status.Error(codes.Unimplemented, "SendLiveness") |
| 217 | } |
| 218 | |
| 219 | func (kc *mockKafkaClient) EnableLivenessChannel(enable bool) chan bool { |
| 220 | return nil |
| 221 | } |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 222 | |
| 223 | func (kc *mockKafkaClient) EnableHealthinessChannel(enable bool) chan bool { |
| 224 | return nil |
| 225 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 226 | |
| 227 | func (kc *mockKafkaClient) SubscribeForMetadata(func(fromTopic string, timestamp int64)) { |
| 228 | return |
| 229 | } |