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