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" |
| 21 | "testing" |
| 22 | |
Scott Baker | 5129015 | 2019-10-24 14:23:20 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v2/pkg/kafka" |
| 24 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 25 | "github.com/opencord/voltha-openolt-adapter/config" |
| 26 | "github.com/opencord/voltha-openolt-adapter/mocks" |
| 27 | ca "github.com/opencord/voltha-protos/go/inter_container" |
| 28 | "go.etcd.io/etcd/pkg/mock/mockserver" |
| 29 | ) |
| 30 | |
| 31 | func init() { |
| 32 | log.SetDefaultLogger(log.JSON, log.DebugLevel, nil) |
| 33 | } |
| 34 | |
| 35 | func 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 | } |
| 45 | func 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) |
| 52 | a.StartAt(0) |
| 53 | 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) { |
| 66 | if err := tt.adapter.setKVClient(); (err != nil) != tt.wantErr { |
| 67 | t.Errorf("adapter.setKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func Test_adapter_KVClient(t *testing.T) { |
| 74 | adapt := newMockAdapter() |
| 75 | a, _ := mockserver.StartMockServers(1) |
| 76 | a.StartAt(0) |
| 77 | defer a.StopAt(0) |
| 78 | |
| 79 | if err := adapt.setKVClient(); err != nil { |
| 80 | t.Errorf("adapter.setKVClient() error = %v", err) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func Test_registerWithCore(t *testing.T) { |
| 85 | ad := newMockAdapter() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 86 | ctx := context.TODO() |
| 87 | err := ad.registerWithCore(ctx, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 88 | if err != nil { |
| 89 | t.Errorf("Expected error:nil, got error: %v", err) |
| 90 | } |
| 91 | } |
| 92 | func Test_startInterContainerProxy(t *testing.T) { |
| 93 | ad := newMockAdapter() |
| 94 | kc := &mockKafkaClient{} |
| 95 | ad.kafkaClient = kc |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 96 | ctx := context.TODO() |
| 97 | icp, err := ad.startInterContainerProxy(ctx, 1) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 98 | if icp != nil { |
| 99 | t.Log("Intercontainer proxy ", icp) |
| 100 | } |
| 101 | if err != nil { |
| 102 | t.Errorf("err %v", err) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func Test_startOpenOLT(t *testing.T) { |
| 107 | a, _ := mockserver.StartMockServers(1) |
| 108 | a.StartAt(0) |
| 109 | defer a.StopAt(0) |
| 110 | |
| 111 | ad := newMockAdapter() |
| 112 | oolt, err := ad.startOpenOLT(context.TODO(), nil, |
| 113 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost, |
| 114 | ad.config.KVStorePort, ad.config.KVStoreType) |
| 115 | if oolt != nil { |
| 116 | t.Log("Open OLT ", oolt) |
| 117 | } |
| 118 | if err != nil { |
| 119 | t.Errorf("err %v", err) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func Test_newKafkaClient(t *testing.T) { |
| 124 | a, _ := mockserver.StartMockServers(1) |
| 125 | a.StartAt(0) |
| 126 | defer a.StopAt(0) |
| 127 | adapter := newMockAdapter() |
| 128 | type args struct { |
| 129 | clientType string |
| 130 | host string |
| 131 | port int |
| 132 | } |
| 133 | tests := []struct { |
| 134 | name string |
| 135 | args args |
| 136 | wantErr bool |
| 137 | }{ |
| 138 | // TODO: Add test cases. |
| 139 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 140 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 141 | } |
| 142 | for _, tt := range tests { |
| 143 | t.Run(tt.name, func(t *testing.T) { |
| 144 | _, err := newKafkaClient(tt.args.clientType, tt.args.host, tt.args.port) |
| 145 | if (err != nil) != tt.wantErr { |
| 146 | t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr) |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | }) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func Test_adapter_setupRequestHandler(t *testing.T) { |
| 155 | |
| 156 | ad := newMockAdapter() |
| 157 | |
| 158 | kip, _ := kafka.NewInterContainerProxy( |
| 159 | kafka.InterContainerHost(ad.config.KafkaAdapterHost), |
| 160 | kafka.InterContainerPort(ad.config.KafkaAdapterPort), |
| 161 | kafka.MsgClient(&mockKafkaClient{}), |
| 162 | kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic})) |
| 163 | |
| 164 | ad.kip = kip |
| 165 | ad.kip.Start() |
| 166 | |
| 167 | oolt, _ := ad.startOpenOLT(context.TODO(), nil, |
| 168 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost, |
| 169 | ad.config.KVStorePort, ad.config.KVStoreType) |
| 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 | } |