| Conditions | 1 |
| Total Lines | 300 |
| Code Lines | 226 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """Module to test the Path class.""" |
||
| 441 | @patch("requests.post") |
||
| 442 | def test_get_disjoint_paths(self, mock_requests_post): |
||
| 443 | """Test get_disjoint_paths method.""" |
||
| 444 | |||
| 445 | controller = MagicMock() |
||
| 446 | controller.get_interface_by_id.side_effect = id_to_interface_mock |
||
| 447 | DynamicPathManager.set_controller(controller) |
||
| 448 | |||
| 449 | evc = MagicMock() |
||
| 450 | evc.secondary_constraints = { |
||
| 451 | "spf_max_path_cost": 20, |
||
| 452 | "mandatory_metrics": { |
||
| 453 | "ownership": "red" |
||
| 454 | } |
||
| 455 | } |
||
| 456 | evc.uni_a.interface.id = "1" |
||
| 457 | evc.uni_z.interface.id = "2" |
||
| 458 | |||
| 459 | # Topo0 |
||
| 460 | paths1 = { |
||
| 461 | "paths": [ |
||
| 462 | { |
||
| 463 | "cost": 11, |
||
| 464 | "hops": [ |
||
| 465 | "00:00:00:00:00:00:00:01:1", |
||
| 466 | "00:00:00:00:00:00:00:01", |
||
| 467 | "00:00:00:00:00:00:00:01:2", |
||
| 468 | "00:00:00:00:00:00:00:02:2", |
||
| 469 | "00:00:00:00:00:00:00:02", |
||
| 470 | "00:00:00:00:00:00:00:02:3", |
||
| 471 | "00:00:00:00:00:00:00:04:2", |
||
| 472 | "00:00:00:00:00:00:00:04", |
||
| 473 | "00:00:00:00:00:00:00:04:3", |
||
| 474 | "00:00:00:00:00:00:00:05:2", |
||
| 475 | "00:00:00:00:00:00:00:05", |
||
| 476 | "00:00:00:00:00:00:00:05:1" |
||
| 477 | ] |
||
| 478 | }, |
||
| 479 | { |
||
| 480 | "cost": 11, |
||
| 481 | "hops": [ |
||
| 482 | "00:00:00:00:00:00:00:01:1", |
||
| 483 | "00:00:00:00:00:00:00:01", |
||
| 484 | "00:00:00:00:00:00:00:01:3", |
||
| 485 | "00:00:00:00:00:00:00:03:2", |
||
| 486 | "00:00:00:00:00:00:00:03", |
||
| 487 | "00:00:00:00:00:00:00:03:3", |
||
| 488 | "00:00:00:00:00:00:00:04:4", |
||
| 489 | "00:00:00:00:00:00:00:04", |
||
| 490 | "00:00:00:00:00:00:00:04:3", |
||
| 491 | "00:00:00:00:00:00:00:05:2", |
||
| 492 | "00:00:00:00:00:00:00:05", |
||
| 493 | "00:00:00:00:00:00:00:05:1" |
||
| 494 | ] |
||
| 495 | }, |
||
| 496 | { |
||
| 497 | "cost": 14, |
||
| 498 | "hops": [ |
||
| 499 | "00:00:00:00:00:00:00:01:1", |
||
| 500 | "00:00:00:00:00:00:00:01", |
||
| 501 | "00:00:00:00:00:00:00:01:2", |
||
| 502 | "00:00:00:00:00:00:00:02:2", |
||
| 503 | "00:00:00:00:00:00:00:02", |
||
| 504 | "00:00:00:00:00:00:00:02:3", |
||
| 505 | "00:00:00:00:00:00:00:04:2", |
||
| 506 | "00:00:00:00:00:00:00:04", |
||
| 507 | "00:00:00:00:00:00:00:04:5", |
||
| 508 | "00:00:00:00:00:00:00:06:2", |
||
| 509 | "00:00:00:00:00:00:00:06", |
||
| 510 | "00:00:00:00:00:00:00:06:3", |
||
| 511 | "00:00:00:00:00:00:00:05:3", |
||
| 512 | "00:00:00:00:00:00:00:05", |
||
| 513 | "00:00:00:00:00:00:00:05:1" |
||
| 514 | ] |
||
| 515 | }, |
||
| 516 | { |
||
| 517 | "cost": 14, |
||
| 518 | "hops": [ |
||
| 519 | "00:00:00:00:00:00:00:01:1", |
||
| 520 | "00:00:00:00:00:00:00:01", |
||
| 521 | "00:00:00:00:00:00:00:01:3", |
||
| 522 | "00:00:00:00:00:00:00:03:2", |
||
| 523 | "00:00:00:00:00:00:00:03", |
||
| 524 | "00:00:00:00:00:00:00:03:3", |
||
| 525 | "00:00:00:00:00:00:00:04:4", |
||
| 526 | "00:00:00:00:00:00:00:04", |
||
| 527 | "00:00:00:00:00:00:00:04:5", |
||
| 528 | "00:00:00:00:00:00:00:06:2", |
||
| 529 | "00:00:00:00:00:00:00:06", |
||
| 530 | "00:00:00:00:00:00:00:06:3", |
||
| 531 | "00:00:00:00:00:00:00:05:3", |
||
| 532 | "00:00:00:00:00:00:00:05", |
||
| 533 | "00:00:00:00:00:00:00:05:1" |
||
| 534 | ] |
||
| 535 | }, |
||
| 536 | { |
||
| 537 | "cost": 17, |
||
| 538 | "hops": [ |
||
| 539 | "00:00:00:00:00:00:00:01:1", |
||
| 540 | "00:00:00:00:00:00:00:01", |
||
| 541 | "00:00:00:00:00:00:00:01:3", |
||
| 542 | "00:00:00:00:00:00:00:03:2", |
||
| 543 | "00:00:00:00:00:00:00:03", |
||
| 544 | "00:00:00:00:00:00:00:03:3", |
||
| 545 | "00:00:00:00:00:00:00:04:4", |
||
| 546 | "00:00:00:00:00:00:00:04", |
||
| 547 | "00:00:00:00:00:00:00:04:5", |
||
| 548 | "00:00:00:00:00:00:00:06:2", |
||
| 549 | "00:00:00:00:00:00:00:06", |
||
| 550 | "00:00:00:00:00:00:00:06:4", |
||
| 551 | "00:00:00:00:00:00:00:07:2", |
||
| 552 | "00:00:00:00:00:00:00:07", |
||
| 553 | "00:00:00:00:00:00:00:07:3", |
||
| 554 | "00:00:00:00:00:00:00:05:4", |
||
| 555 | "00:00:00:00:00:00:00:05", |
||
| 556 | "00:00:00:00:00:00:00:05:1" |
||
| 557 | ] |
||
| 558 | }, |
||
| 559 | ] |
||
| 560 | } |
||
| 561 | |||
| 562 | mock_response = MagicMock() |
||
| 563 | mock_response.status_code = 200 |
||
| 564 | mock_response.json.return_value = paths1 |
||
| 565 | |||
| 566 | # when we dont have the current_path |
||
| 567 | mock_requests_post.return_value = mock_response |
||
| 568 | disjoint_paths = list(DynamicPathManager.get_disjoint_paths(evc, [])) |
||
| 569 | self.assertEqual(disjoint_paths, []) |
||
| 570 | |||
| 571 | current_path = [ |
||
| 572 | Link( |
||
| 573 | id_to_interface_mock("00:00:00:00:00:00:00:01:2"), |
||
| 574 | id_to_interface_mock("00:00:00:00:00:00:00:02:2") |
||
| 575 | ), |
||
| 576 | Link( |
||
| 577 | id_to_interface_mock("00:00:00:00:00:00:00:02:3"), |
||
| 578 | id_to_interface_mock("00:00:00:00:00:00:00:04:2") |
||
| 579 | ), |
||
| 580 | Link( |
||
| 581 | id_to_interface_mock("00:00:00:00:00:00:00:04:3"), |
||
| 582 | id_to_interface_mock("00:00:00:00:00:00:00:05:2") |
||
| 583 | ), |
||
| 584 | ] |
||
| 585 | |||
| 586 | # only one path available from pathfinder (precesilly the |
||
| 587 | # current_path), so the maximum disjoint path will be empty |
||
| 588 | mock_response.json.return_value = {"paths": paths1["paths"][0:1]} |
||
| 589 | mock_requests_post.return_value = mock_response |
||
| 590 | paths = list(DynamicPathManager.get_disjoint_paths(evc, current_path)) |
||
| 591 | self.assertEqual(len(paths), 0) |
||
| 592 | |||
| 593 | expected_disjoint_path = [ |
||
| 594 | Link( |
||
| 595 | id_to_interface_mock("00:00:00:00:00:00:00:01:3"), |
||
| 596 | id_to_interface_mock("00:00:00:00:00:00:00:03:2") |
||
| 597 | ), |
||
| 598 | Link( |
||
| 599 | id_to_interface_mock("00:00:00:00:00:00:00:03:3"), |
||
| 600 | id_to_interface_mock("00:00:00:00:00:00:00:04:4") |
||
| 601 | ), |
||
| 602 | Link( |
||
| 603 | id_to_interface_mock("00:00:00:00:00:00:00:04:5"), |
||
| 604 | id_to_interface_mock("00:00:00:00:00:00:00:06:2") |
||
| 605 | ), |
||
| 606 | Link( |
||
| 607 | id_to_interface_mock("00:00:00:00:00:00:00:06:3"), |
||
| 608 | id_to_interface_mock("00:00:00:00:00:00:00:05:3") |
||
| 609 | ), |
||
| 610 | ] |
||
| 611 | |||
| 612 | # there are one alternative path |
||
| 613 | mock_response.json.return_value = paths1 |
||
| 614 | mock_requests_post.return_value = mock_response |
||
| 615 | paths = list(DynamicPathManager.get_disjoint_paths(evc, current_path)) |
||
| 616 | self.assertEqual(len(paths), 4) |
||
| 617 | # for more information on the paths please refer to EP029 |
||
| 618 | self.assertEqual(len(paths[0]), 4) # path S-Z-W-I-D |
||
| 619 | self.assertEqual(len(paths[1]), 5) # path S-Z-W-I-J-D |
||
| 620 | self.assertEqual(len(paths[2]), 3) # path S-Z-W-D |
||
| 621 | self.assertEqual(len(paths[3]), 4) # path S-X-W-I-D |
||
| 622 | self.assertEqual( |
||
| 623 | [link.id for link in paths[0]], |
||
| 624 | [link.id for link in expected_disjoint_path] |
||
| 625 | ) |
||
| 626 | |||
| 627 | max_paths = 10 |
||
| 628 | expected_call = call( |
||
| 629 | "http://localhost:8181/api/kytos/pathfinder/v2/", |
||
| 630 | json={ |
||
| 631 | **{ |
||
| 632 | "source": evc.uni_a.interface.id, |
||
| 633 | "destination": evc.uni_z.interface.id, |
||
| 634 | "spf_max_paths": max_paths, |
||
| 635 | }, |
||
| 636 | **evc.secondary_constraints |
||
| 637 | }, |
||
| 638 | ) |
||
| 639 | assert mock_requests_post.call_count >= 1 |
||
| 640 | # If secondary_constraints are set they are expected to be parametrized |
||
| 641 | mock_requests_post.assert_has_calls([expected_call]) |
||
| 642 | |||
| 643 | # EP029 Topo2 |
||
| 644 | paths2 = { |
||
| 645 | "paths": [ |
||
| 646 | { |
||
| 647 | "cost": 14, |
||
| 648 | "hops": [ |
||
| 649 | "00:00:00:00:00:00:00:01:1", |
||
| 650 | "00:00:00:00:00:00:00:01", |
||
| 651 | "00:00:00:00:00:00:00:01:2", |
||
| 652 | "00:00:00:00:00:00:00:02:1", |
||
| 653 | "00:00:00:00:00:00:00:02", |
||
| 654 | "00:00:00:00:00:00:00:02:2", |
||
| 655 | "00:00:00:00:00:00:00:03:1", |
||
| 656 | "00:00:00:00:00:00:00:03", |
||
| 657 | "00:00:00:00:00:00:00:03:2", |
||
| 658 | "00:00:00:00:00:00:00:04:1", |
||
| 659 | "00:00:00:00:00:00:00:04", |
||
| 660 | "00:00:00:00:00:00:00:04:2", |
||
| 661 | "00:00:00:00:00:00:00:07:2", |
||
| 662 | "00:00:00:00:00:00:00:07", |
||
| 663 | "00:00:00:00:00:00:00:07:1" |
||
| 664 | ] |
||
| 665 | }, |
||
| 666 | { |
||
| 667 | "cost": 17, |
||
| 668 | "hops": [ |
||
| 669 | "00:00:00:00:00:00:00:01:1", |
||
| 670 | "00:00:00:00:00:00:00:01", |
||
| 671 | "00:00:00:00:00:00:00:01:2", |
||
| 672 | "00:00:00:00:00:00:00:02:1", |
||
| 673 | "00:00:00:00:00:00:00:02", |
||
| 674 | "00:00:00:00:00:00:00:02:3", |
||
| 675 | "00:00:00:00:00:00:00:05:1", |
||
| 676 | "00:00:00:00:00:00:00:05", |
||
| 677 | "00:00:00:00:00:00:00:05:2", |
||
| 678 | "00:00:00:00:00:00:00:06:1", |
||
| 679 | "00:00:00:00:00:00:00:06", |
||
| 680 | "00:00:00:00:00:00:00:06:2", |
||
| 681 | "00:00:00:00:00:00:00:04:3", |
||
| 682 | "00:00:00:00:00:00:00:04", |
||
| 683 | "00:00:00:00:00:00:00:04:2", |
||
| 684 | "00:00:00:00:00:00:00:07:2", |
||
| 685 | "00:00:00:00:00:00:00:07", |
||
| 686 | "00:00:00:00:00:00:00:07:1" |
||
| 687 | ] |
||
| 688 | } |
||
| 689 | ] |
||
| 690 | } |
||
| 691 | |||
| 692 | current_path = [ |
||
| 693 | Link( |
||
| 694 | id_to_interface_mock("00:00:00:00:00:00:00:01:2"), |
||
| 695 | id_to_interface_mock("00:00:00:00:00:00:00:02:1") |
||
| 696 | ), |
||
| 697 | Link( |
||
| 698 | id_to_interface_mock("00:00:00:00:00:00:00:02:2"), |
||
| 699 | id_to_interface_mock("00:00:00:00:00:00:00:03:1") |
||
| 700 | ), |
||
| 701 | Link( |
||
| 702 | id_to_interface_mock("00:00:00:00:00:00:00:03:2"), |
||
| 703 | id_to_interface_mock("00:00:00:00:00:00:00:04:1") |
||
| 704 | ), |
||
| 705 | Link( |
||
| 706 | id_to_interface_mock("00:00:00:00:00:00:00:04:2"), |
||
| 707 | id_to_interface_mock("00:00:00:00:00:00:00:07:2") |
||
| 708 | ), |
||
| 709 | ] |
||
| 710 | |||
| 711 | expected_disjoint_path = [ |
||
| 712 | Link( |
||
| 713 | id_to_interface_mock("00:00:00:00:00:00:00:01:2"), |
||
| 714 | id_to_interface_mock("00:00:00:00:00:00:00:02:1") |
||
| 715 | ), |
||
| 716 | Link( |
||
| 717 | id_to_interface_mock("00:00:00:00:00:00:00:02:3"), |
||
| 718 | id_to_interface_mock("00:00:00:00:00:00:00:05:1") |
||
| 719 | ), |
||
| 720 | Link( |
||
| 721 | id_to_interface_mock("00:00:00:00:00:00:00:05:2"), |
||
| 722 | id_to_interface_mock("00:00:00:00:00:00:00:06:1") |
||
| 723 | ), |
||
| 724 | Link( |
||
| 725 | id_to_interface_mock("00:00:00:00:00:00:00:06:2"), |
||
| 726 | id_to_interface_mock("00:00:00:00:00:00:00:04:3") |
||
| 727 | ), |
||
| 728 | Link( |
||
| 729 | id_to_interface_mock("00:00:00:00:00:00:00:04:2"), |
||
| 730 | id_to_interface_mock("00:00:00:00:00:00:00:07:2") |
||
| 731 | ), |
||
| 732 | ] |
||
| 733 | |||
| 734 | mock_response.json.return_value = {"paths": paths2["paths"]} |
||
| 735 | mock_requests_post.return_value = mock_response |
||
| 736 | paths = list(DynamicPathManager.get_disjoint_paths(evc, current_path)) |
||
| 737 | self.assertEqual(len(paths), 1) |
||
| 738 | self.assertEqual( |
||
| 739 | [link.id for link in paths[0]], |
||
| 740 | [link.id for link in expected_disjoint_path] |
||
| 741 | ) |
||
| 742 |