khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | com "github.com/opencord/voltha-go/adapters/common" |
| 22 | "github.com/golang/protobuf/ptypes/empty" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "github.com/opencord/voltha-go/protos/common" |
| 25 | "github.com/opencord/voltha-go/protos/openflow_13" |
| 26 | "github.com/opencord/voltha-go/protos/voltha" |
| 27 | "github.com/stretchr/testify/assert" |
| 28 | "google.golang.org/grpc" |
| 29 | "google.golang.org/grpc/codes" |
| 30 | "google.golang.org/grpc/metadata" |
| 31 | "google.golang.org/grpc/status" |
| 32 | "os" |
| 33 | "os/exec" |
| 34 | "strings" |
| 35 | "testing" |
| 36 | "time" |
| 37 | ) |
| 38 | |
| 39 | var conn *grpc.ClientConn |
| 40 | var stub voltha.VolthaServiceClient |
| 41 | var testMode string |
| 42 | |
| 43 | /* |
| 44 | Prerequite: These tests require the rw_core to run prior to executing these test cases. |
| 45 | */ |
| 46 | |
| 47 | var devices map[string]*voltha.Device |
| 48 | |
| 49 | //func init() { |
| 50 | // log.AddPackage(log.JSON, log.ErrorLevel, nil) |
| 51 | // log.UpdateAllLoggers(log.Fields{"instanceId": "testing"}) |
| 52 | // log.SetAllLogLevel(log.ErrorLevel) |
| 53 | // |
| 54 | // //Start kafka and Etcd |
| 55 | // startKafkaEtcd() |
| 56 | // time.Sleep(10 * time.Second) //TODO: Find a better way to ascertain they are up |
| 57 | // |
| 58 | // stub = setupGrpcConnection() |
| 59 | // stub = voltha.NewVolthaServiceClient(conn) |
| 60 | // devices = make(map[string]*voltha.Device) |
| 61 | //} |
| 62 | |
| 63 | func setup() { |
| 64 | var err error |
| 65 | |
| 66 | if _, err = log.AddPackage(log.JSON, log.WarnLevel, log.Fields{"instanceId": "testing"}); err != nil { |
| 67 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 68 | } |
| 69 | log.UpdateAllLoggers(log.Fields{"instanceId": "testing"}) |
| 70 | log.SetAllLogLevel(log.ErrorLevel) |
| 71 | |
| 72 | //Start kafka and Etcd |
| 73 | startKafka() |
| 74 | startEtcd() |
| 75 | time.Sleep(10 * time.Second) //TODO: Find a better way to ascertain they are up |
| 76 | |
| 77 | stub = setupGrpcConnection() |
| 78 | testMode = common.TestModeKeys_api_test.String() |
| 79 | devices = make(map[string]*voltha.Device) |
| 80 | } |
| 81 | |
| 82 | func setupGrpcConnection() voltha.VolthaServiceClient { |
| 83 | grpcHostIP := os.Getenv("DOCKER_HOST_IP") |
| 84 | grpcPort := 50057 |
| 85 | grpcHost := fmt.Sprintf("%s:%d", grpcHostIP, grpcPort) |
| 86 | var err error |
| 87 | conn, err = grpc.Dial(grpcHost, grpc.WithInsecure()) |
| 88 | if err != nil { |
| 89 | log.Fatalf("did not connect: %s", err) |
| 90 | } |
| 91 | return voltha.NewVolthaServiceClient(conn) |
| 92 | } |
| 93 | |
| 94 | func clearAllDevices(clearMap bool) { |
| 95 | for key, _ := range devices { |
| 96 | ctx := context.Background() |
| 97 | response, err := stub.DeleteDevice(ctx, &voltha.ID{Id: key}) |
| 98 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 99 | if clearMap { |
| 100 | delete(devices, key) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Verify if all ids are present in the global list of devices |
| 106 | func hasAllIds(ids *voltha.IDs) bool { |
| 107 | if ids == nil && len(devices) == 0 { |
| 108 | return true |
| 109 | } |
| 110 | if ids == nil { |
| 111 | return false |
| 112 | } |
| 113 | for _, id := range ids.Items { |
| 114 | if _, exist := devices[id.Id]; !exist { |
| 115 | return false |
| 116 | } |
| 117 | } |
| 118 | return true |
| 119 | } |
| 120 | |
| 121 | func startKafka() { |
| 122 | fmt.Println("Starting Kafka and Etcd ...") |
| 123 | command := "docker-compose" |
| 124 | cmd := exec.Command(command, "-f", "../../compose/docker-compose-zk-kafka-test.yml", "up", "-d") |
| 125 | if err := cmd.Run(); err != nil { |
| 126 | log.Fatal(err) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func startEtcd() { |
| 131 | fmt.Println("Starting Etcd ...") |
| 132 | command := "docker-compose" |
| 133 | cmd := exec.Command(command, "-f", "../../compose/docker-compose-etcd.yml", "up", "-d") |
| 134 | if err := cmd.Run(); err != nil { |
| 135 | log.Fatal(err) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func stopKafka() { |
| 140 | fmt.Println("Stopping Kafka and Etcd ...") |
| 141 | command := "docker-compose" |
| 142 | cmd := exec.Command(command, "-f", "../../compose/docker-compose-zk-kafka-test.yml", "down") |
| 143 | if err := cmd.Run(); err != nil { |
| 144 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 145 | // containers |
| 146 | log.Warn(err) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func stopEtcd() { |
| 151 | fmt.Println("Stopping Etcd ...") |
| 152 | command := "docker-compose" |
| 153 | cmd := exec.Command(command, "-f", "../../compose/docker-compose-etcd.yml", "down") |
| 154 | if err := cmd.Run(); err != nil { |
| 155 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 156 | // containers |
| 157 | log.Warn(err) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func startCore() { |
| 162 | fmt.Println("Starting voltha core ...") |
| 163 | command := "docker-compose" |
| 164 | cmd := exec.Command(command, "-f", "../../compose/rw_core.yml", "up", "-d") |
| 165 | if err := cmd.Run(); err != nil { |
| 166 | log.Fatal(err) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func stopCore() { |
| 171 | fmt.Println("Stopping voltha core ...") |
| 172 | command := "docker-compose" |
| 173 | cmd := exec.Command(command, "-f", "../../compose/rw_core.yml", "down") |
| 174 | if err := cmd.Run(); err != nil { |
| 175 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 176 | // containers |
| 177 | log.Warn(err) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func startSimulatedOLTAndONUAdapters() { |
| 182 | fmt.Println("Starting simulated OLT and ONU adapters ...") |
| 183 | command := "docker-compose" |
| 184 | cmd := exec.Command(command, "-f", "../../compose/adapters-simulated.yml", "up", "-d") |
| 185 | if err := cmd.Run(); err != nil { |
| 186 | log.Fatal(err) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func stopSimulatedOLTAndONUAdapters() { |
| 191 | fmt.Println("Stopping simulated OLT and ONU adapters ...") |
| 192 | command := "docker-compose" |
| 193 | cmd := exec.Command(command, "-f", "../../compose/adapters-simulated.yml", "down") |
| 194 | if err := cmd.Run(); err != nil { |
| 195 | // ignore error - as this is mostly due network being left behind as its being used by other |
| 196 | // containers |
| 197 | log.Warn(err) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | func TestListDeviceIds(t *testing.T) { |
| 203 | fmt.Println("Testing list Devices Ids ...") |
| 204 | //0. Start kafka and Ectd |
| 205 | startKafka() |
| 206 | startEtcd() |
| 207 | |
| 208 | //1. Start the core |
| 209 | startCore() |
| 210 | |
| 211 | // Wait until it's up - TODO: find a better way to check |
| 212 | time.Sleep(10 * time.Second) |
| 213 | |
| 214 | //2. Create a set of devices into the Core |
| 215 | for i := 0; i < 10; i++ { |
| 216 | ctx := context.Background() |
| 217 | device := &voltha.Device{Type: "simulated_olt"} |
| 218 | response, err := stub.CreateDevice(ctx, device) |
| 219 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 220 | assert.NotNil(t, response) |
| 221 | assert.Nil(t, err) |
| 222 | devices[response.Id] = response |
| 223 | } |
| 224 | |
| 225 | //3. Verify devices have been added correctly |
| 226 | ctx := context.Background() |
| 227 | response, err := stub.ListDeviceIds(ctx, &empty.Empty{}) |
| 228 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 229 | assert.Nil(t, err) |
| 230 | assert.True(t, hasAllIds(response)) |
| 231 | |
| 232 | //4. Stop the core |
| 233 | stopCore() |
| 234 | |
| 235 | //5. Stop Kafka and Etcd |
| 236 | stopKafka() |
| 237 | stopEtcd() |
| 238 | } |
| 239 | |
| 240 | func TestReconcileDevices(t *testing.T) { |
| 241 | fmt.Println("Testing Reconcile Devices ...") |
| 242 | |
| 243 | //0. Start kafka and Ectd |
| 244 | startKafka() |
| 245 | startEtcd() |
| 246 | |
| 247 | //1. Start the core |
| 248 | startCore() |
| 249 | |
| 250 | // Wait until it's up - TODO: find a better way to check |
| 251 | time.Sleep(10 * time.Second) |
| 252 | |
| 253 | //2. Create a set of devices into the Core |
| 254 | for i := 0; i < 10; i++ { |
| 255 | ctx := context.Background() |
| 256 | device := &voltha.Device{Type: "simulated_olt"} |
| 257 | response, err := stub.CreateDevice(ctx, device) |
| 258 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 259 | assert.Nil(t, err) |
| 260 | devices[response.Id] = response |
| 261 | } |
| 262 | //3. Verify devices have been added correctly |
| 263 | ctx := context.Background() |
| 264 | response, err := stub.ListDeviceIds(ctx, &empty.Empty{}) |
| 265 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 266 | assert.Nil(t, err) |
| 267 | assert.True(t, hasAllIds(response)) |
| 268 | |
| 269 | //4. Stop the core and restart it. This will start the core with no data in memory but |
| 270 | // etcd will still have the data. |
| 271 | stopCore() |
| 272 | time.Sleep(5 * time.Second) |
| 273 | startCore() |
| 274 | time.Sleep(10 * time.Second) |
| 275 | |
| 276 | //5. Setup the connection again |
| 277 | stub = setupGrpcConnection() |
| 278 | |
| 279 | //6. Verify there are no devices left |
| 280 | ctx = context.Background() |
| 281 | response, err = stub.ListDeviceIds(ctx, &empty.Empty{}) |
| 282 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 283 | assert.Nil(t, err) |
| 284 | assert.Equal(t, len(response.Items), 0) |
| 285 | |
| 286 | //7. Invoke reconcile with all stored list |
| 287 | toRestore := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
| 288 | for key, _ := range devices { |
| 289 | toRestore.Items = append(toRestore.Items, &voltha.ID{Id: key}) |
| 290 | } |
| 291 | ctx = context.Background() |
| 292 | _, err = stub.ReconcileDevices(ctx, toRestore) |
| 293 | assert.Nil(t, err) |
| 294 | |
| 295 | //8. Verify all devices have been restored |
| 296 | ctx = context.Background() |
| 297 | response, err = stub.ListDeviceIds(ctx, &empty.Empty{}) |
| 298 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 299 | assert.Nil(t, err) |
| 300 | assert.True(t, hasAllIds(response)) |
| 301 | |
| 302 | for _, id := range response.Items { |
| 303 | fmt.Println("id", id.Id) |
| 304 | } |
| 305 | |
| 306 | //9. Store the core |
| 307 | stopCore() |
| 308 | |
| 309 | //10. Stop Kafka and Etcd |
| 310 | stopKafka() |
| 311 | stopEtcd() |
| 312 | } |
| 313 | |
| 314 | func TestDeviceManagement(t *testing.T) { |
| 315 | fmt.Println("Testing Device Management ...") |
| 316 | |
| 317 | numberOfOLTDevices := 1 |
| 318 | |
| 319 | //0. Start kafka and Ectd |
| 320 | startKafka() |
| 321 | startEtcd() |
| 322 | |
| 323 | //1. Start the core |
| 324 | startCore() |
| 325 | |
| 326 | //2. Start the simulated adapters |
| 327 | startSimulatedOLTAndONUAdapters() |
| 328 | |
| 329 | // Wait until the core and adapters sync up |
| 330 | time.Sleep(10 * time.Second) |
| 331 | |
| 332 | //3. Create a set of devices into the Core |
| 333 | devices = make(map[string]*voltha.Device) |
| 334 | logicalDevices := make(map[string]*voltha.LogicalDevice) |
| 335 | for i := 0; i < numberOfOLTDevices; i++ { |
| 336 | ctx := context.Background() |
| 337 | randomMacAddress := strings.ToUpper(com.GetRandomMacAddress()) |
| 338 | device := &voltha.Device{Type: "simulated_olt", MacAddress:randomMacAddress} |
| 339 | response, err := stub.CreateDevice(ctx, device) |
| 340 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 341 | assert.Nil(t, err) |
| 342 | devices[response.Id] = response |
| 343 | } |
| 344 | |
| 345 | //4. Enable all the devices |
| 346 | for id, _ := range devices { |
| 347 | ctx := context.Background() |
| 348 | response, err := stub.EnableDevice(ctx, &common.ID{Id:id}) |
| 349 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 350 | assert.Nil(t, err) |
| 351 | } |
| 352 | |
| 353 | // Wait until all devices have been enabled |
| 354 | if numberOfOLTDevices < 5 { |
| 355 | time.Sleep(3 * time.Second) |
| 356 | } else if numberOfOLTDevices < 20 { |
| 357 | time.Sleep(20 * time.Second) |
| 358 | } else { |
| 359 | time.Sleep(30 * time.Second) |
| 360 | } |
| 361 | //time.Sleep(1 * time.Second * time.Duration(numberOfDevices)) |
| 362 | |
| 363 | //5. Verify that all devices are in enabled state |
| 364 | ctx := context.Background() |
| 365 | response, err := stub.ListDevices(ctx, &empty.Empty{}) |
| 366 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 367 | assert.Nil(t, err) |
| 368 | assert.Equal(t, len(devices)*2, len(response.Items)) |
| 369 | for _, d := range (response.Items) { |
| 370 | devices[d.Id] = d |
| 371 | assert.Equal(t, d.AdminState, voltha.AdminState_ENABLED) |
| 372 | } |
| 373 | |
| 374 | //6. Get the logical devices |
| 375 | ctx = context.Background() |
| 376 | lresponse, lerr := stub.ListLogicalDevices(ctx, &empty.Empty{}) |
| 377 | log.Infow("response", log.Fields{"res": response, "error": lerr}) |
| 378 | assert.Nil(t, lerr) |
| 379 | assert.Equal(t, numberOfOLTDevices, len(lresponse.Items)) |
| 380 | for _, ld := range (lresponse.Items) { |
| 381 | logicalDevices[ld.Id] = ld |
| 382 | // Ensure each logical device have two ports |
| 383 | assert.Equal(t, 2, len(ld.Ports)) |
| 384 | } |
| 385 | |
| 386 | //7. Disable all ONUs & check status & check logical device |
| 387 | for id, d := range devices { |
| 388 | ctx := context.Background() |
| 389 | if d.Type == "simulated_onu" { |
| 390 | response, err := stub.DisableDevice(ctx, &common.ID{Id:id}) |
| 391 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 392 | assert.Nil(t, err) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Wait for all the changes to be populated |
| 397 | time.Sleep(3 * time.Second) |
| 398 | |
| 399 | ctx = context.Background() |
| 400 | response, err = stub.ListDevices(ctx, &empty.Empty{}) |
| 401 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 402 | assert.Nil(t, err) |
| 403 | assert.Equal(t, len(devices), len(response.Items)) |
| 404 | for _, d := range (response.Items) { |
| 405 | if d.Type == "simulated_onu" { |
| 406 | assert.Equal(t, d.AdminState, voltha.AdminState_DISABLED) |
| 407 | devices[d.Id] = d |
| 408 | } else { |
| 409 | assert.Equal(t, d.AdminState, voltha.AdminState_ENABLED) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | ctx = context.Background() |
| 414 | lresponse, lerr = stub.ListLogicalDevices(ctx, &empty.Empty{}) |
| 415 | log.Infow("response", log.Fields{"res": response, "error": lerr}) |
| 416 | assert.Nil(t, lerr) |
| 417 | assert.Equal(t, numberOfOLTDevices, len(lresponse.Items)) |
| 418 | for _, ld := range (lresponse.Items) { |
| 419 | logicalDevices[ld.Id] = ld |
| 420 | // Ensure each logical device have one port - only olt port |
| 421 | assert.Equal(t, 1, len(ld.Ports)) |
| 422 | } |
| 423 | |
| 424 | //8. Enable all ONUs & check status & check logical device |
| 425 | for id, d := range devices { |
| 426 | ctx := context.Background() |
| 427 | if d.Type == "simulated_onu" { |
| 428 | response, err := stub.EnableDevice(ctx, &common.ID{Id:id}) |
| 429 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 430 | assert.Nil(t, err) |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // Wait for all the changes to be populated |
| 435 | time.Sleep(3 * time.Second) |
| 436 | |
| 437 | ctx = context.Background() |
| 438 | response, err = stub.ListDevices(ctx, &empty.Empty{}) |
| 439 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 440 | assert.Nil(t, err) |
| 441 | assert.Equal(t, len(devices), len(response.Items)) |
| 442 | for _, d := range (response.Items) { |
| 443 | assert.Equal(t, d.AdminState, voltha.AdminState_ENABLED) |
| 444 | devices[d.Id] = d |
| 445 | } |
| 446 | |
| 447 | //ctx = context.Background() |
| 448 | //lresponse, lerr = stub.ListLogicalDevices(ctx, &empty.Empty{}) |
| 449 | //log.Infow("response", log.Fields{"res": response, "error": lerr}) |
| 450 | //assert.Nil(t, lerr) |
| 451 | //assert.Equal(t, numberOfOLTDevices, len(lresponse.Items)) |
| 452 | //for _, ld := range (lresponse.Items) { |
| 453 | // logicalDevices[ld.Id] = ld |
| 454 | // // Ensure each logical device have two ports |
| 455 | // assert.Equal(t, 2, len(ld.Ports)) |
| 456 | //} |
| 457 | |
| 458 | //9. Disable all OLTs & check status & check logical device |
| 459 | |
| 460 | //10. Enable all OLTs & Enable all ONUs & check status & check logical device |
| 461 | |
| 462 | //11. Disable all OLTs & check status & check logical device |
| 463 | |
| 464 | //12. Delete all Devices & check status & check logical device |
| 465 | |
| 466 | ////13. Store simulated adapters |
| 467 | //stopSimulatedOLTAndONUAdapters() |
| 468 | // |
| 469 | ////14. Store the core |
| 470 | //stopCore() |
| 471 | // |
| 472 | ////15. Stop Kafka and Etcd |
| 473 | //stopKafka() |
| 474 | //stopEtcd() |
| 475 | } |
| 476 | |
| 477 | |
| 478 | func TestGetDevice(t *testing.T) { |
| 479 | var id common.ID |
| 480 | id.Id = "anyid" |
| 481 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 482 | response, err := stub.GetDevice(ctx, &id) |
| 483 | assert.Nil(t, response) |
| 484 | st, _ := status.FromError(err) |
| 485 | assert.Equal(t, id.Id, st.Message()) |
| 486 | assert.Equal(t, codes.NotFound, st.Code()) |
| 487 | } |
| 488 | |
| 489 | func TestUpdateLogLevelError(t *testing.T) { |
| 490 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 491 | level := voltha.Logging{PackageName: "github.com/opencord/voltha-go/rw_core/core", Level: common.LogLevel_ERROR} |
| 492 | response, err := stub.UpdateLogLevel(ctx, &level) |
| 493 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 494 | assert.Equal(t, &empty.Empty{}, response) |
| 495 | assert.Nil(t, err) |
| 496 | } |
| 497 | |
| 498 | func TestGetVoltha(t *testing.T) { |
| 499 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 500 | response, err := stub.GetVoltha(ctx, &empty.Empty{}) |
| 501 | assert.Nil(t, response) |
| 502 | st, _ := status.FromError(err) |
| 503 | assert.Equal(t, "UnImplemented", st.Message()) |
| 504 | } |
| 505 | |
| 506 | func TestUpdateLogLevelDebug(t *testing.T) { |
| 507 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 508 | level := voltha.Logging{PackageName: "github.com/opencord/voltha-go/rw_core/core", Level: common.LogLevel_DEBUG} |
| 509 | response, err := stub.UpdateLogLevel(ctx, &level) |
| 510 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 511 | assert.Equal(t, &empty.Empty{}, response) |
| 512 | assert.Nil(t, err) |
| 513 | } |
| 514 | |
| 515 | func TestGetCoreInstance(t *testing.T) { |
| 516 | id := &voltha.ID{Id: "getCoreInstance"} |
| 517 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 518 | response, err := stub.GetCoreInstance(ctx, id) |
| 519 | assert.Nil(t, response) |
| 520 | st, _ := status.FromError(err) |
| 521 | assert.Equal(t, "UnImplemented", st.Message()) |
| 522 | } |
| 523 | |
| 524 | func TestGetLogicalDevice(t *testing.T) { |
| 525 | id := &voltha.ID{Id: "getLogicalDevice"} |
| 526 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 527 | response, err := stub.GetLogicalDevice(ctx, id) |
| 528 | assert.Nil(t, response) |
| 529 | st, _ := status.FromError(err) |
| 530 | assert.Equal(t, id.Id, st.Message()) |
| 531 | assert.Equal(t, codes.NotFound, st.Code()) |
| 532 | } |
| 533 | |
| 534 | func TestGetLogicalDevicePort(t *testing.T) { |
| 535 | id := &voltha.LogicalPortId{Id: "GetLogicalDevicePort"} |
| 536 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 537 | response, err := stub.GetLogicalDevicePort(ctx, id) |
| 538 | assert.Nil(t, response) |
| 539 | st, _ := status.FromError(err) |
| 540 | assert.Equal(t, "UnImplemented", st.Message()) |
| 541 | } |
| 542 | |
| 543 | func TestListLogicalDevicePorts(t *testing.T) { |
| 544 | id := &voltha.ID{Id: "listLogicalDevicePorts"} |
| 545 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 546 | response, err := stub.ListLogicalDevicePorts(ctx, id) |
| 547 | assert.Nil(t, response) |
| 548 | st, _ := status.FromError(err) |
| 549 | assert.Equal(t, "UnImplemented", st.Message()) |
| 550 | } |
| 551 | |
| 552 | func TestListLogicalDeviceFlows(t *testing.T) { |
| 553 | id := &voltha.ID{Id: "ListLogicalDeviceFlows"} |
| 554 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 555 | response, err := stub.ListLogicalDeviceFlows(ctx, id) |
| 556 | assert.Nil(t, response) |
| 557 | st, _ := status.FromError(err) |
| 558 | assert.Equal(t, "UnImplemented", st.Message()) |
| 559 | } |
| 560 | |
| 561 | func TestListLogicalDeviceFlowGroups(t *testing.T) { |
| 562 | id := &voltha.ID{Id: "ListLogicalDeviceFlowGroups"} |
| 563 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 564 | response, err := stub.ListLogicalDeviceFlowGroups(ctx, id) |
| 565 | assert.Nil(t, response) |
| 566 | st, _ := status.FromError(err) |
| 567 | assert.Equal(t, "UnImplemented", st.Message()) |
| 568 | } |
| 569 | |
| 570 | func TestListDevices(t *testing.T) { |
| 571 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 572 | response, _ := stub.ListDevices(ctx, &empty.Empty{}) |
| 573 | assert.Equal(t, len(response.Items), 0) |
| 574 | } |
| 575 | |
| 576 | func TestListAdapters(t *testing.T) { |
| 577 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 578 | response, err := stub.ListAdapters(ctx, &empty.Empty{}) |
| 579 | assert.Nil(t, response) |
| 580 | st, _ := status.FromError(err) |
| 581 | assert.Equal(t, "UnImplemented", st.Message()) |
| 582 | } |
| 583 | |
| 584 | func TestListLogicalDevices(t *testing.T) { |
| 585 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 586 | response, _ := stub.ListLogicalDevices(ctx, &empty.Empty{}) |
| 587 | assert.Equal(t, len(response.Items), 0) |
| 588 | } |
| 589 | |
| 590 | func TestListCoreInstances(t *testing.T) { |
| 591 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 592 | response, err := stub.ListCoreInstances(ctx, &empty.Empty{}) |
| 593 | assert.Nil(t, response) |
| 594 | st, _ := status.FromError(err) |
| 595 | assert.Equal(t, "UnImplemented", st.Message()) |
| 596 | } |
| 597 | |
| 598 | func TestCreateDevice(t *testing.T) { |
| 599 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 600 | device := &voltha.Device{Id: "newdevice"} |
| 601 | response, err := stub.CreateDevice(ctx, device) |
| 602 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 603 | assert.Equal(t, &voltha.Device{Id: "newdevice"}, response) |
| 604 | assert.Nil(t, err) |
| 605 | } |
| 606 | |
| 607 | func TestEnableDevice(t *testing.T) { |
| 608 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 609 | id := &voltha.ID{Id: "enabledevice"} |
| 610 | response, err := stub.EnableDevice(ctx, id) |
| 611 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 612 | assert.Equal(t, &empty.Empty{}, response) |
| 613 | assert.Nil(t, err) |
| 614 | } |
| 615 | |
| 616 | func TestDisableDevice(t *testing.T) { |
| 617 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 618 | id := &voltha.ID{Id: "DisableDevice"} |
| 619 | response, err := stub.DisableDevice(ctx, id) |
| 620 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 621 | assert.Equal(t, &empty.Empty{}, response) |
| 622 | assert.Nil(t, err) |
| 623 | } |
| 624 | |
| 625 | func TestRebootDevice(t *testing.T) { |
| 626 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 627 | id := &voltha.ID{Id: "RebootDevice"} |
| 628 | response, err := stub.RebootDevice(ctx, id) |
| 629 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 630 | assert.Equal(t, &empty.Empty{}, response) |
| 631 | assert.Nil(t, err) |
| 632 | } |
| 633 | |
| 634 | func TestDeleteDevice(t *testing.T) { |
| 635 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 636 | id := &voltha.ID{Id: "DeleteDevice"} |
| 637 | response, err := stub.DeleteDevice(ctx, id) |
| 638 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 639 | assert.Equal(t, &empty.Empty{}, response) |
| 640 | assert.Nil(t, err) |
| 641 | } |
| 642 | |
| 643 | func TestEnableLogicalDevicePort(t *testing.T) { |
| 644 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 645 | id := &voltha.LogicalPortId{Id: "EnableLogicalDevicePort"} |
| 646 | response, err := stub.EnableLogicalDevicePort(ctx, id) |
| 647 | if e, ok := status.FromError(err); ok { |
| 648 | log.Infow("response", log.Fields{"error": err, "errorcode": e.Code(), "msg": e.Message()}) |
| 649 | } |
| 650 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 651 | assert.Equal(t, &empty.Empty{}, response) |
| 652 | assert.Nil(t, err) |
| 653 | } |
| 654 | |
| 655 | func TestDisableLogicalDevicePort(t *testing.T) { |
| 656 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 657 | id := &voltha.LogicalPortId{Id: "DisableLogicalDevicePort"} |
| 658 | response, err := stub.DisableLogicalDevicePort(ctx, id) |
| 659 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 660 | assert.Equal(t, &empty.Empty{}, response) |
| 661 | assert.Nil(t, err) |
| 662 | } |
| 663 | |
| 664 | func TestUpdateLogicalDeviceFlowGroupTable(t *testing.T) { |
| 665 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 666 | flow := &openflow_13.FlowGroupTableUpdate{Id: "UpdateLogicalDeviceFlowGroupTable"} |
| 667 | response, err := stub.UpdateLogicalDeviceFlowGroupTable(ctx, flow) |
| 668 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 669 | assert.Equal(t, &empty.Empty{}, response) |
| 670 | assert.Nil(t, err) |
| 671 | } |
| 672 | |
| 673 | func TestGetImageDownloadStatus(t *testing.T) { |
| 674 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 675 | img := &voltha.ImageDownload{Id: "GetImageDownloadStatus"} |
| 676 | response, err := stub.GetImageDownloadStatus(ctx, img) |
| 677 | assert.Nil(t, response) |
| 678 | st, _ := status.FromError(err) |
| 679 | assert.Equal(t, "UnImplemented", st.Message()) |
| 680 | } |
| 681 | |
| 682 | // TODO: complete the remaining tests |
| 683 | |
| 684 | func shutdown() { |
| 685 | conn.Close() |
| 686 | } |
| 687 | |
| 688 | func TestMain(m *testing.M) { |
| 689 | setup() |
| 690 | code := m.Run() |
| 691 | shutdown() |
| 692 | os.Exit(code) |
| 693 | } |