Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1 | // +build integration |
| 2 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 3 | /* |
| 4 | * Copyright 2018-present Open Networking Foundation |
| 5 | |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 18 | package kafka |
| 19 | |
| 20 | import ( |
| 21 | "context" |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 22 | "os" |
| 23 | "testing" |
| 24 | "time" |
| 25 | |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 26 | "github.com/golang/protobuf/ptypes" |
khenaidoo | d4d922e | 2018-08-03 22:35:16 -0400 | [diff] [blame] | 27 | "github.com/google/uuid" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-go/rw_core/core/api" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 29 | kk "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 31 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 32 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 33 | "github.com/stretchr/testify/assert" |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 34 | ) |
| 35 | |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 36 | /* |
| 37 | Prerequite: Start the kafka/zookeeper containers. |
| 38 | */ |
| 39 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 40 | const ( |
| 41 | TEST_RPC_KEY = "" |
| 42 | ) |
| 43 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 44 | var coreKafkaProxy *kk.InterContainerProxy |
| 45 | var adapterKafkaProxy *kk.InterContainerProxy |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 46 | var kafkaPartitionClient kk.Client |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 47 | var deviceDiscoveryTopic string |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 48 | var hostIP string |
| 49 | var kafkaClient kk.Client |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 50 | |
| 51 | func init() { |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 52 | deviceDiscoveryTopic = "deviceDiscovery" |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 53 | hostIP = os.Getenv("DOCKER_HOST_IP") |
| 54 | kafkaClient = kk.NewSaramaClient( |
| 55 | kk.Host(hostIP), |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 56 | kk.Port(9092)) |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 57 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 58 | coreKafkaProxy = kk.NewInterContainerProxy( |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 59 | kk.InterContainerHost(hostIP), |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 60 | kk.InterContainerPort(9092), |
| 61 | kk.DefaultTopic(&kk.Topic{Name: "Core"}), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 62 | kk.MsgClient(kafkaClient), |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 63 | kk.DeviceDiscoveryTopic(&kk.Topic{Name: deviceDiscoveryTopic})) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 64 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 65 | adapterKafkaProxy = kk.NewInterContainerProxy( |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 66 | kk.InterContainerHost(hostIP), |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 67 | kk.InterContainerPort(9092), |
| 68 | kk.DefaultTopic(&kk.Topic{Name: "Adapter"}), |
| 69 | kk.MsgClient(kafkaClient)) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 70 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 71 | kafkaPartitionClient = kk.NewSaramaClient( |
| 72 | kk.ConsumerType(kk.PartitionConsumer), |
| 73 | kk.Host(hostIP), |
| 74 | kk.Port(9092), |
| 75 | kk.AutoCreateTopic(true), |
| 76 | kk.ProducerFlushFrequency(5)) |
| 77 | kafkaPartitionClient.Start() |
| 78 | |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 79 | coreKafkaProxy.Start() |
| 80 | adapterKafkaProxy.Start() |
| 81 | subscribeTarget(coreKafkaProxy) |
| 82 | } |
| 83 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 84 | func subscribeTarget(kmp *kk.InterContainerProxy) { |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 85 | topic := kk.Topic{Name: "Core"} |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 86 | requestProxy := &api.AdapterRequestHandlerProxy{TestMode: true} |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 87 | kmp.SubscribeWithRequestHandlerInterface(topic, requestProxy) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 88 | } |
| 89 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 90 | func waitForRPCMessage(topic kk.Topic, ch <-chan *ic.InterContainerMessage, doneCh chan string) { |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 91 | for msg := range ch { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 92 | logger.Debugw("Got-RPC-message", log.Fields{"msg": msg}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 93 | // Unpack message |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 94 | requestBody := &ic.InterContainerRequestBody{} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 95 | if err := ptypes.UnmarshalAny(msg.Body, requestBody); err != nil { |
| 96 | doneCh <- "Error" |
| 97 | } else { |
| 98 | doneCh <- requestBody.Rpc |
| 99 | } |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 104 | //func TestSubscribeUnsubscribe(t *testing.T) { |
| 105 | // // First subscribe to the specific topic |
| 106 | // topic := kk.Topic{Name: "Core"} |
| 107 | // ch, err := coreKafkaProxy.Subs(topic) |
| 108 | // assert.NotNil(t, ch) |
| 109 | // assert.Nil(t, err) |
| 110 | // // Create a channel to receive a response |
| 111 | // waitCh := make(chan string) |
| 112 | // // Wait for a message |
| 113 | // go waitForRPCMessage(topic, ch, waitCh) |
| 114 | // // Send the message - don't care of the response |
| 115 | // rpc := "AnyRPCRequestForTest" |
| 116 | // adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, true) |
| 117 | // // Wait for the result on ouw own channel |
| 118 | // result := <-waitCh |
| 119 | // assert.Equal(t, result, rpc) |
| 120 | // close(waitCh) |
| 121 | // err = coreKafkaProxy.UnSubscribe(topic, ch) |
| 122 | // assert.Nil(t, err) |
| 123 | //} |
| 124 | // |
| 125 | //func TestMultipleSubscribeUnsubscribe(t *testing.T) { |
| 126 | // // First subscribe to the specific topic |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 127 | // //log.SetPackageLogLevel("github.com/opencord/voltha-lib-go/v3/pkg/kafka", log.DebugLevel) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 128 | // var err error |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 129 | // var ch1 <-chan *ic.InterContainerMessage |
| 130 | // var ch2 <-chan *ic.InterContainerMessage |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 131 | // topic := kk.Topic{Name: "Core"} |
| 132 | // ch1, err = coreKafkaProxy.Subscribe(topic) |
| 133 | // assert.NotNil(t, ch1) |
| 134 | // assert.Nil(t, err) |
| 135 | // // Create a channel to receive responses |
| 136 | // waitCh := make(chan string) |
| 137 | // ch2, err = coreKafkaProxy.Subscribe(topic) |
| 138 | // assert.NotNil(t, ch2) |
| 139 | // assert.Nil(t, err) |
| 140 | // // Wait for a message |
| 141 | // go waitForRPCMessage(topic, ch2, waitCh) |
| 142 | // go waitForRPCMessage(topic, ch1, waitCh) |
| 143 | // |
| 144 | // // Send the message - don't care of the response |
| 145 | // rpc := "AnyRPCRequestForTest" |
| 146 | // adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, true) |
| 147 | // // Wait for the result on ouw own channel |
| 148 | // |
| 149 | // responses := 0 |
| 150 | // for msg := range waitCh { |
| 151 | // assert.Equal(t, msg, rpc) |
| 152 | // responses = responses + 1 |
| 153 | // if responses > 1 { |
| 154 | // break |
| 155 | // } |
| 156 | // } |
| 157 | // assert.Equal(t, responses, 2) |
| 158 | // close(waitCh) |
| 159 | // err = coreKafkaProxy.UnSubscribe(topic, ch1) |
| 160 | // assert.Nil(t, err) |
| 161 | // err = coreKafkaProxy.UnSubscribe(topic, ch2) |
| 162 | // assert.Nil(t, err) |
| 163 | //} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 164 | |
| 165 | func TestIncorrectAPI(t *testing.T) { |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 166 | log.SetPackageLogLevel("github.com/opencord/voltha-lib-go/v3/pkg/kafka", log.ErrorLevel) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 167 | trnsId := uuid.New().String() |
| 168 | protoMsg := &voltha.Device{Id: trnsId} |
| 169 | args := make([]*kk.KVArg, 1) |
| 170 | args[0] = &kk.KVArg{ |
| 171 | Key: "device", |
| 172 | Value: protoMsg, |
| 173 | } |
| 174 | rpc := "IncorrectAPI" |
| 175 | topic := kk.Topic{Name: "Core"} |
| 176 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 177 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 178 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 179 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 180 | assert.Equal(t, status, false) |
| 181 | //Unpack the result into the actual proto object |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 182 | unpackResult := &ic.Error{} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 183 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 184 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 185 | } |
| 186 | assert.NotNil(t, unpackResult) |
| 187 | } |
| 188 | |
| 189 | func TestIncorrectAPIParams(t *testing.T) { |
| 190 | trnsId := uuid.New().String() |
| 191 | protoMsg := &voltha.Device{Id: trnsId} |
| 192 | args := make([]*kk.KVArg, 1) |
| 193 | args[0] = &kk.KVArg{ |
| 194 | Key: "device", |
| 195 | Value: protoMsg, |
| 196 | } |
| 197 | rpc := "GetDevice" |
| 198 | topic := kk.Topic{Name: "Core"} |
| 199 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 200 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 201 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 202 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 203 | assert.Equal(t, status, false) |
| 204 | //Unpack the result into the actual proto object |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 205 | unpackResult := &ic.Error{} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 206 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 207 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 208 | } |
| 209 | assert.NotNil(t, unpackResult) |
| 210 | } |
| 211 | |
| 212 | func TestGetDevice(t *testing.T) { |
| 213 | trnsId := uuid.New().String() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 214 | protoMsg := &voltha.ID{Id: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 215 | args := make([]*kk.KVArg, 1) |
| 216 | args[0] = &kk.KVArg{ |
| 217 | Key: "deviceID", |
| 218 | Value: protoMsg, |
| 219 | } |
| 220 | rpc := "GetDevice" |
| 221 | topic := kk.Topic{Name: "Core"} |
| 222 | expectedResponse := &voltha.Device{Id: trnsId} |
| 223 | timeout := time.Duration(50) * time.Millisecond |
| 224 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 225 | defer cancel() |
| 226 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 227 | status, result := adapterKafkaProxy.InvokeRPC(ctx, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 228 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 229 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 230 | assert.Equal(t, status, true) |
| 231 | unpackResult := &voltha.Device{} |
| 232 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 233 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 234 | } |
| 235 | assert.Equal(t, unpackResult, expectedResponse) |
| 236 | } |
| 237 | |
| 238 | func TestGetDeviceTimeout(t *testing.T) { |
| 239 | trnsId := uuid.New().String() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 240 | protoMsg := &voltha.ID{Id: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 241 | args := make([]*kk.KVArg, 1) |
| 242 | args[0] = &kk.KVArg{ |
| 243 | Key: "deviceID", |
| 244 | Value: protoMsg, |
| 245 | } |
| 246 | rpc := "GetDevice" |
| 247 | topic := kk.Topic{Name: "Core"} |
| 248 | timeout := time.Duration(2) * time.Millisecond |
| 249 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 250 | defer cancel() |
| 251 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 252 | status, result := adapterKafkaProxy.InvokeRPC(ctx, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 253 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 254 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 255 | assert.Equal(t, status, false) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 256 | unpackResult := &ic.Error{} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 257 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 258 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 259 | } |
| 260 | assert.NotNil(t, unpackResult) |
| 261 | } |
| 262 | |
| 263 | func TestGetChildDevice(t *testing.T) { |
| 264 | trnsId := uuid.New().String() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 265 | protoMsg := &voltha.ID{Id: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 266 | args := make([]*kk.KVArg, 1) |
| 267 | args[0] = &kk.KVArg{ |
| 268 | Key: "deviceID", |
| 269 | Value: protoMsg, |
| 270 | } |
| 271 | rpc := "GetChildDevice" |
| 272 | topic := kk.Topic{Name: "Core"} |
| 273 | expectedResponse := &voltha.Device{Id: trnsId} |
| 274 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 275 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 276 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 277 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 278 | assert.Equal(t, status, true) |
| 279 | unpackResult := &voltha.Device{} |
| 280 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 281 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 282 | } |
| 283 | assert.Equal(t, unpackResult, expectedResponse) |
| 284 | } |
| 285 | |
| 286 | func TestGetChildDevices(t *testing.T) { |
| 287 | trnsId := uuid.New().String() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 288 | protoMsg := &voltha.ID{Id: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 289 | args := make([]*kk.KVArg, 1) |
| 290 | args[0] = &kk.KVArg{ |
| 291 | Key: "deviceID", |
| 292 | Value: protoMsg, |
| 293 | } |
| 294 | rpc := "GetChildDevices" |
| 295 | topic := kk.Topic{Name: "Core"} |
| 296 | expectedResponse := &voltha.Device{Id: trnsId} |
| 297 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 298 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 299 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 300 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 301 | assert.Equal(t, status, true) |
| 302 | unpackResult := &voltha.Device{} |
| 303 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 304 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 305 | } |
| 306 | assert.Equal(t, unpackResult, expectedResponse) |
| 307 | } |
| 308 | |
| 309 | func TestGetPorts(t *testing.T) { |
| 310 | trnsId := uuid.New().String() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 311 | protoArg1 := &voltha.ID{Id: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 312 | args := make([]*kk.KVArg, 2) |
| 313 | args[0] = &kk.KVArg{ |
| 314 | Key: "deviceID", |
| 315 | Value: protoArg1, |
| 316 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 317 | protoArg2 := &ic.IntType{Val: 1} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 318 | args[1] = &kk.KVArg{ |
| 319 | Key: "portType", |
| 320 | Value: protoArg2, |
| 321 | } |
| 322 | rpc := "GetPorts" |
| 323 | topic := kk.Topic{Name: "Core"} |
| 324 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 325 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 326 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 327 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 328 | assert.Equal(t, status, true) |
| 329 | unpackResult := &voltha.Ports{} |
| 330 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 331 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 332 | } |
| 333 | expectedLen := len(unpackResult.Items) >= 1 |
| 334 | assert.Equal(t, true, expectedLen) |
| 335 | } |
| 336 | |
| 337 | func TestGetPortsMissingArgs(t *testing.T) { |
| 338 | trnsId := uuid.New().String() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 339 | protoArg1 := &voltha.ID{Id: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 340 | args := make([]*kk.KVArg, 1) |
| 341 | args[0] = &kk.KVArg{ |
| 342 | Key: "deviceID", |
| 343 | Value: protoArg1, |
| 344 | } |
| 345 | rpc := "GetPorts" |
| 346 | topic := kk.Topic{Name: "Core"} |
| 347 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 348 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 349 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 350 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 351 | assert.Equal(t, status, false) |
| 352 | //Unpack the result into the actual proto object |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 353 | unpackResult := &ic.Error{} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 354 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 355 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 356 | } |
| 357 | assert.NotNil(t, unpackResult) |
| 358 | } |
| 359 | |
| 360 | func TestChildDeviceDetected(t *testing.T) { |
| 361 | trnsId := uuid.New().String() |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 362 | protoArg1 := &ic.StrType{Val: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 363 | args := make([]*kk.KVArg, 5) |
| 364 | args[0] = &kk.KVArg{ |
| 365 | Key: "deviceID", |
| 366 | Value: protoArg1, |
| 367 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 368 | protoArg2 := &ic.IntType{Val: 1} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 369 | args[1] = &kk.KVArg{ |
| 370 | Key: "parentPortNo", |
| 371 | Value: protoArg2, |
| 372 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 373 | protoArg3 := &ic.StrType{Val: "great_onu"} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 374 | args[2] = &kk.KVArg{ |
| 375 | Key: "childDeviceType", |
| 376 | Value: protoArg3, |
| 377 | } |
| 378 | protoArg4 := &voltha.Device_ProxyAddress{DeviceId: trnsId, ChannelId: 100} |
| 379 | args[3] = &kk.KVArg{ |
| 380 | Key: "proxyAddress", |
| 381 | Value: protoArg4, |
| 382 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 383 | protoArg5 := &ic.IntType{Val: 1} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 384 | args[4] = &kk.KVArg{ |
| 385 | Key: "portType", |
| 386 | Value: protoArg5, |
| 387 | } |
| 388 | |
| 389 | rpc := "ChildDeviceDetected" |
| 390 | topic := kk.Topic{Name: "Core"} |
| 391 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 392 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 393 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 394 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 395 | assert.Equal(t, status, true) |
| 396 | assert.Nil(t, result) |
| 397 | } |
| 398 | |
| 399 | func TestChildDeviceDetectedNoWait(t *testing.T) { |
| 400 | trnsId := uuid.New().String() |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 401 | protoArg1 := &ic.StrType{Val: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 402 | args := make([]*kk.KVArg, 5) |
| 403 | args[0] = &kk.KVArg{ |
| 404 | Key: "deviceID", |
| 405 | Value: protoArg1, |
| 406 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 407 | protoArg2 := &ic.IntType{Val: 1} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 408 | args[1] = &kk.KVArg{ |
| 409 | Key: "parentPortNo", |
| 410 | Value: protoArg2, |
| 411 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 412 | protoArg3 := &ic.StrType{Val: "great_onu"} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 413 | args[2] = &kk.KVArg{ |
| 414 | Key: "childDeviceType", |
| 415 | Value: protoArg3, |
| 416 | } |
| 417 | protoArg4 := &voltha.Device_ProxyAddress{DeviceId: trnsId, ChannelId: 100} |
| 418 | args[3] = &kk.KVArg{ |
| 419 | Key: "proxyAddress", |
| 420 | Value: protoArg4, |
| 421 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 422 | protoArg5 := &ic.IntType{Val: 1} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 423 | args[4] = &kk.KVArg{ |
| 424 | Key: "portType", |
| 425 | Value: protoArg5, |
| 426 | } |
| 427 | |
| 428 | rpc := "ChildDeviceDetected" |
| 429 | topic := kk.Topic{Name: "Core"} |
| 430 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 431 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, false, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 432 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 433 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 434 | assert.Equal(t, status, true) |
| 435 | assert.Nil(t, result) |
| 436 | } |
| 437 | |
| 438 | func TestChildDeviceDetectedMissingArgs(t *testing.T) { |
| 439 | trnsId := uuid.New().String() |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 440 | protoArg1 := &ic.StrType{Val: trnsId} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 441 | args := make([]*kk.KVArg, 4) |
| 442 | args[0] = &kk.KVArg{ |
| 443 | Key: "deviceID", |
| 444 | Value: protoArg1, |
| 445 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 446 | protoArg2 := &ic.IntType{Val: 1} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 447 | args[1] = &kk.KVArg{ |
| 448 | Key: "parentPortNo", |
| 449 | Value: protoArg2, |
| 450 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 451 | protoArg3 := &ic.StrType{Val: "great_onu"} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 452 | args[2] = &kk.KVArg{ |
| 453 | Key: "childDeviceType", |
| 454 | Value: protoArg3, |
| 455 | } |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 456 | |
| 457 | rpc := "ChildDeviceDetected" |
| 458 | topic := kk.Topic{Name: "Core"} |
| 459 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 460 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 461 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 462 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 463 | assert.Equal(t, status, false) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 464 | unpackResult := &ic.Error{} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 465 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 466 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 467 | } |
| 468 | assert.NotNil(t, unpackResult) |
| 469 | } |
| 470 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 471 | func TestDeviceStateChange(t *testing.T) { |
| 472 | log.SetAllLogLevel(log.DebugLevel) |
| 473 | trnsId := uuid.New().String() |
| 474 | protoArg1 := &voltha.ID{Id: trnsId} |
| 475 | args := make([]*kk.KVArg, 4) |
| 476 | args[0] = &kk.KVArg{ |
| 477 | Key: "device_id", |
| 478 | Value: protoArg1, |
| 479 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 480 | protoArg2 := &ic.IntType{Val: 1} |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 481 | args[1] = &kk.KVArg{ |
| 482 | Key: "oper_status", |
| 483 | Value: protoArg2, |
| 484 | } |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 485 | protoArg3 := &ic.IntType{Val: 1} |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 486 | args[2] = &kk.KVArg{ |
| 487 | Key: "connect_status", |
| 488 | Value: protoArg3, |
| 489 | } |
| 490 | |
| 491 | rpc := "DeviceStateUpdate" |
| 492 | topic := kk.Topic{Name: "Core"} |
| 493 | start := time.Now() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 494 | status, result := adapterKafkaProxy.InvokeRPC(nil, rpc, &topic, &topic, true, TEST_RPC_KEY, args...) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 495 | elapsed := time.Since(start) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 496 | logger.Infow("Result", log.Fields{"status": status, "result": result, "time": elapsed}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 497 | assert.Equal(t, status, true) |
| 498 | assert.Nil(t, result) |
| 499 | } |
| 500 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 501 | func subscribeToTopic(topic *kk.Topic, waitingChannel chan *ic.InterContainerMessage) error { |
| 502 | var ch <-chan *ic.InterContainerMessage |
| 503 | var err error |
| 504 | if ch, err = kafkaPartitionClient.Subscribe(topic); err != nil { |
| 505 | return nil |
| 506 | } |
| 507 | msg := <-ch |
| 508 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 509 | logger.Debugw("msg-received", log.Fields{"msg": msg}) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 510 | waitingChannel <- msg |
| 511 | return nil |
| 512 | } |
| 513 | |
| 514 | func TestDeviceDiscovery(t *testing.T) { |
| 515 | // Create an intercontainer proxy - similar to the Core |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 516 | testProxy := kk.NewInterContainerProxy( |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 517 | kk.InterContainerHost(hostIP), |
| 518 | kk.InterContainerPort(9092), |
| 519 | kk.DefaultTopic(&kk.Topic{Name: "Test"}), |
| 520 | kk.MsgClient(kafkaClient), |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 521 | kk.DeviceDiscoveryTopic(&kk.Topic{Name: deviceDiscoveryTopic})) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 522 | |
| 523 | // First start to wait for the message |
| 524 | waitingChannel := make(chan *ic.InterContainerMessage) |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 525 | go subscribeToTopic(&kk.Topic{Name: deviceDiscoveryTopic}, waitingChannel) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 526 | |
| 527 | // Sleep to make sure the consumer is ready |
| 528 | time.Sleep(time.Millisecond * 100) |
| 529 | |
| 530 | // Send the message |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 531 | go testProxy.DeviceDiscovered("TestDeviceId", "TestDevicetype", "TestParentId", "myPODName") |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 532 | |
| 533 | msg := <-waitingChannel |
| 534 | totalTime := (time.Now().UnixNano() - msg.Header.Timestamp) / int64(time.Millisecond) |
| 535 | assert.Equal(t, msg.Header.Type, ic.MessageType_DEVICE_DISCOVERED) |
| 536 | // Unpack message |
| 537 | dd := &ic.DeviceDiscovered{} |
| 538 | err := ptypes.UnmarshalAny(msg.Body, dd) |
| 539 | assert.Nil(t, err) |
| 540 | assert.Equal(t, dd.Id, "TestDeviceId") |
| 541 | assert.Equal(t, dd.DeviceType, "TestDevicetype") |
| 542 | assert.Equal(t, dd.ParentId, "TestParentId") |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 543 | assert.Equal(t, dd.Publisher, "myPODName") |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 544 | logger.Debugw("TotalTime", log.Fields{"time": totalTime}) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 545 | } |
| 546 | |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 547 | func TestStopKafkaProxy(t *testing.T) { |
| 548 | adapterKafkaProxy.Stop() |
| 549 | coreKafkaProxy.Stop() |
| 550 | } |
| 551 | |
| 552 | //func TestMain(m *testing.T) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 553 | // logger.Info("Main") |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 554 | //} |