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