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