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