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