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 | |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "github.com/opencord/voltha-go/kafka" |
| 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() |
| 86 | err := ad.registerWithCore(1) |
| 87 | if err != nil { |
| 88 | t.Errorf("Expected error:nil, got error: %v", err) |
| 89 | } |
| 90 | } |
| 91 | func Test_startInterContainerProxy(t *testing.T) { |
| 92 | ad := newMockAdapter() |
| 93 | kc := &mockKafkaClient{} |
| 94 | ad.kafkaClient = kc |
| 95 | icp, err := ad.startInterContainerProxy(1) |
| 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, |
| 111 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost, |
| 112 | ad.config.KVStorePort, ad.config.KVStoreType) |
| 113 | if oolt != nil { |
| 114 | t.Log("Open OLT ", oolt) |
| 115 | } |
| 116 | if err != nil { |
| 117 | t.Errorf("err %v", err) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func Test_newKafkaClient(t *testing.T) { |
| 122 | a, _ := mockserver.StartMockServers(1) |
| 123 | a.StartAt(0) |
| 124 | defer a.StopAt(0) |
| 125 | adapter := newMockAdapter() |
| 126 | type args struct { |
| 127 | clientType string |
| 128 | host string |
| 129 | port int |
| 130 | } |
| 131 | tests := []struct { |
| 132 | name string |
| 133 | args args |
| 134 | wantErr bool |
| 135 | }{ |
| 136 | // TODO: Add test cases. |
| 137 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 138 | {"newKafkaClient", args{clientType: "sarama", host: adapter.config.KafkaAdapterHost, port: adapter.config.KafkaAdapterPort}, false}, |
| 139 | } |
| 140 | for _, tt := range tests { |
| 141 | t.Run(tt.name, func(t *testing.T) { |
| 142 | _, err := newKafkaClient(tt.args.clientType, tt.args.host, tt.args.port) |
| 143 | if (err != nil) != tt.wantErr { |
| 144 | t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr) |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | }) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func Test_adapter_setupRequestHandler(t *testing.T) { |
| 153 | |
| 154 | ad := newMockAdapter() |
| 155 | |
| 156 | kip, _ := kafka.NewInterContainerProxy( |
| 157 | kafka.InterContainerHost(ad.config.KafkaAdapterHost), |
| 158 | kafka.InterContainerPort(ad.config.KafkaAdapterPort), |
| 159 | kafka.MsgClient(&mockKafkaClient{}), |
| 160 | kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic})) |
| 161 | |
| 162 | ad.kip = kip |
| 163 | ad.kip.Start() |
| 164 | |
| 165 | oolt, _ := ad.startOpenOLT(context.TODO(), nil, |
| 166 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, 1, ad.config.KVStoreHost, |
| 167 | ad.config.KVStorePort, ad.config.KVStoreType) |
| 168 | printBanner() |
| 169 | printVersion() |
| 170 | if err := ad.setupRequestHandler(ad.config.InstanceID, oolt); err != nil { |
| 171 | t.Logf("adapter.setupRequestHandler() error = %v", err) |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | |
| 176 | // Kafka client mocker |
| 177 | type mockKafkaClient struct { |
| 178 | } |
| 179 | |
| 180 | func (kc *mockKafkaClient) Start() error { |
| 181 | return nil |
| 182 | } |
| 183 | func (kc *mockKafkaClient) Stop() { |
| 184 | } |
| 185 | func (kc *mockKafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error { |
| 186 | if topic != nil { |
| 187 | return nil |
| 188 | } |
| 189 | return errors.New("invalid Topic") |
| 190 | } |
| 191 | func (kc *mockKafkaClient) DeleteTopic(topic *kafka.Topic) error { |
| 192 | if topic != nil { |
| 193 | return nil |
| 194 | } |
| 195 | return errors.New("invalid Topic") |
| 196 | } |
| 197 | func (kc *mockKafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) { |
| 198 | if topic != nil { |
| 199 | ch := make(chan *ca.InterContainerMessage) |
| 200 | return ch, nil |
| 201 | } |
| 202 | return nil, errors.New("invalid Topic") |
| 203 | } |
| 204 | func (kc *mockKafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error { |
| 205 | if topic == nil { |
| 206 | return nil |
| 207 | } |
| 208 | return errors.New("invalid Topic") |
| 209 | } |
| 210 | func (kc *mockKafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error { |
| 211 | if topic != nil { |
| 212 | return nil |
| 213 | } |
| 214 | return errors.New("invalid topic") |
| 215 | } |