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, |
| 115 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost, |
| 116 | ad.config.KVStorePort, ad.config.KVStoreType) |
| 117 | if oolt != nil { |
| 118 | t.Log("Open OLT ", oolt) |
| 119 | } |
| 120 | if err != nil { |
| 121 | t.Errorf("err %v", err) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func Test_newKafkaClient(t *testing.T) { |
| 126 | a, _ := mockserver.StartMockServers(1) |
| 127 | a.StartAt(0) |
| 128 | defer a.StopAt(0) |
| 129 | adapter := newMockAdapter() |
| 130 | type args struct { |
| 131 | clientType string |
| 132 | host string |
| 133 | port int |
| 134 | } |
| 135 | tests := []struct { |
| 136 | name string |
| 137 | args args |
| 138 | wantErr bool |
| 139 | }{ |
| 140 | // TODO: Add test cases. |
| 141 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 142 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 143 | } |
| 144 | for _, tt := range tests { |
| 145 | t.Run(tt.name, func(t *testing.T) { |
| 146 | _, err := newKafkaClient(tt.args.clientType, tt.args.host, tt.args.port) |
| 147 | if (err != nil) != tt.wantErr { |
| 148 | t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr) |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | }) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func Test_adapter_setupRequestHandler(t *testing.T) { |
| 157 | |
| 158 | ad := newMockAdapter() |
| 159 | |
| 160 | kip, _ := kafka.NewInterContainerProxy( |
| 161 | kafka.InterContainerHost(ad.config.KafkaAdapterHost), |
| 162 | kafka.InterContainerPort(ad.config.KafkaAdapterPort), |
| 163 | kafka.MsgClient(&mockKafkaClient{}), |
| 164 | kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic})) |
| 165 | |
| 166 | ad.kip = kip |
| 167 | ad.kip.Start() |
| 168 | |
| 169 | oolt, _ := ad.startOpenOLT(context.TODO(), nil, |
| 170 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost, |
| 171 | ad.config.KVStorePort, ad.config.KVStoreType) |
| 172 | printBanner() |
| 173 | printVersion() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 174 | ctx := context.TODO() |
| 175 | if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 176 | t.Logf("adapter.setupRequestHandler() error = %v", err) |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | |
| 181 | // Kafka client mocker |
| 182 | type mockKafkaClient struct { |
| 183 | } |
| 184 | |
| 185 | func (kc *mockKafkaClient) Start() error { |
| 186 | return nil |
| 187 | } |
| 188 | func (kc *mockKafkaClient) Stop() { |
| 189 | } |
| 190 | func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error { |
| 191 | if topic != nil { |
| 192 | return nil |
| 193 | } |
| 194 | return errors.New("invalid Topic") |
| 195 | } |
| 196 | func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error { |
| 197 | if topic != nil { |
| 198 | return nil |
| 199 | } |
| 200 | return errors.New("invalid Topic") |
| 201 | } |
| 202 | func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) { |
| 203 | if topic != nil { |
| 204 | ch := make(chan *ca.InterContainerMessage) |
| 205 | return ch, nil |
| 206 | } |
| 207 | return nil, errors.New("invalid Topic") |
| 208 | } |
| 209 | func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error { |
| 210 | if topic == nil { |
| 211 | return nil |
| 212 | } |
| 213 | return errors.New("invalid Topic") |
| 214 | } |
| 215 | func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error { |
| 216 | if topic != nil { |
| 217 | return nil |
| 218 | } |
| 219 | return errors.New("invalid topic") |
| 220 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 221 | |
| 222 | func (kc *mockKafkaClient) SendLiveness() error { |
| 223 | return status.Error(codes.Unimplemented, "SendLiveness") |
| 224 | } |
| 225 | |
| 226 | func (kc *mockKafkaClient) EnableLivenessChannel(enable bool) chan bool { |
| 227 | return nil |
| 228 | } |