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