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" |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 21 | conf "github.com/opencord/voltha-lib-go/v4/pkg/config" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 22 | "testing" |
Scott Baker | ed4a8e7 | 2020-04-17 11:10:20 -0700 | [diff] [blame] | 23 | "time" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 24 | |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 25 | "google.golang.org/grpc/codes" |
| 26 | "google.golang.org/grpc/status" |
| 27 | |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 29 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
| 30 | "github.com/opencord/voltha-openolt-adapter/pkg/mocks" |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 31 | ca "github.com/opencord/voltha-protos/v4/go/inter_container" |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 32 | "go.etcd.io/etcd/pkg/mock/mockserver" |
| 33 | ) |
| 34 | |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 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) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 52 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 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) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 66 | if err := tt.adapter.setKVClient(context.Background()); (err != nil) != tt.wantErr { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 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) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 76 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 77 | defer a.StopAt(0) |
| 78 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 79 | if err := adapt.setKVClient(context.Background()); err != nil { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 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) |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 108 | cm := &conf.ConfigManager{} |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 109 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 110 | defer a.StopAt(0) |
| 111 | |
| 112 | ad := newMockAdapter() |
| 113 | oolt, err := ad.startOpenOLT(context.TODO(), nil, |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 114 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config, cm) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 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) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 125 | _ = a.StartAt(0) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 126 | defer a.StopAt(0) |
| 127 | adapter := newMockAdapter() |
| 128 | type args struct { |
| 129 | clientType string |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 130 | address string |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 131 | } |
| 132 | tests := []struct { |
| 133 | name string |
| 134 | args args |
| 135 | wantErr bool |
| 136 | }{ |
| 137 | // TODO: Add test cases. |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 138 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false}, |
| 139 | {"newKafkaClient", args{clientType: "sarama", address: adapter.config.KafkaAdapterAddress}, false}, |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 140 | } |
| 141 | for _, tt := range tests { |
| 142 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 143 | _, err := newKafkaClient(context.Background(), tt.args.clientType, tt.args.address) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 144 | if (err != nil) != tt.wantErr { |
| 145 | t.Errorf("newKafkaClient() error = %v, wantErr %v", err, tt.wantErr) |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | }) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func Test_adapter_setupRequestHandler(t *testing.T) { |
| 154 | |
| 155 | ad := newMockAdapter() |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 156 | cm := &conf.ConfigManager{} |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 157 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 158 | kip := kafka.NewInterContainerProxy( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 159 | kafka.InterContainerAddress(ad.config.KafkaAdapterAddress), |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 160 | kafka.MsgClient(&mockKafkaClient{}), |
| 161 | kafka.DefaultTopic(&kafka.Topic{Name: ad.config.Topic})) |
| 162 | |
| 163 | ad.kip = kip |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 164 | _ = ad.kip.Start(context.Background()) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 165 | |
| 166 | oolt, _ := ad.startOpenOLT(context.TODO(), nil, |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 167 | ad.coreProxy, ad.adapterProxy, ad.eventProxy, ad.config, cm) |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 168 | printBanner() |
| 169 | printVersion() |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 170 | ctx := context.TODO() |
| 171 | if err := ad.setupRequestHandler(ctx, ad.config.InstanceID, oolt); err != nil { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 172 | t.Logf("adapter.setupRequestHandler() error = %v", err) |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | // Kafka client mocker |
| 178 | type mockKafkaClient struct { |
| 179 | } |
| 180 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 181 | func (kc *mockKafkaClient) Start(ctx context.Context) error { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 182 | return nil |
| 183 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 184 | func (kc *mockKafkaClient) Stop(ctx context.Context) { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 185 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 186 | func (kc *mockKafkaClient) CreateTopic(ctx context.Context, topic *kafka.Topic, numPartition int, repFactor int) error { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 187 | if topic != nil { |
| 188 | return nil |
| 189 | } |
| 190 | return errors.New("invalid Topic") |
| 191 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 192 | func (kc *mockKafkaClient) DeleteTopic(ctx context.Context, topic *kafka.Topic) error { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 193 | if topic != nil { |
| 194 | return nil |
| 195 | } |
| 196 | return errors.New("invalid Topic") |
| 197 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 198 | func (kc *mockKafkaClient) Subscribe(ctx context.Context, topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ca.InterContainerMessage, error) { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 199 | if topic != nil { |
| 200 | ch := make(chan *ca.InterContainerMessage) |
| 201 | return ch, nil |
| 202 | } |
| 203 | return nil, errors.New("invalid Topic") |
| 204 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 205 | func (kc *mockKafkaClient) UnSubscribe(ctx context.Context, topic *kafka.Topic, ch <-chan *ca.InterContainerMessage) error { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 206 | if topic == nil { |
| 207 | return nil |
| 208 | } |
| 209 | return errors.New("invalid Topic") |
| 210 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 211 | func (kc *mockKafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error { |
kdarapu | f0c0e38 | 2019-09-30 05:26:31 +0530 | [diff] [blame] | 212 | if topic != nil { |
| 213 | return nil |
| 214 | } |
| 215 | return errors.New("invalid topic") |
| 216 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 217 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 218 | func (kc *mockKafkaClient) SendLiveness(ctx context.Context) error { |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 219 | return status.Error(codes.Unimplemented, "SendLiveness") |
| 220 | } |
| 221 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 222 | func (kc *mockKafkaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool { |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 223 | return nil |
| 224 | } |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 225 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 226 | func (kc *mockKafkaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool { |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 227 | return nil |
| 228 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 229 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 230 | func (kc *mockKafkaClient) SubscribeForMetadata(context.Context, func(fromTopic string, timestamp time.Time)) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 231 | } |