From d4d0bc2bbffd767f9994dd4df147ccfef56c1513 Mon Sep 17 00:00:00 2001
From: Hakan Calim <hakan.calim@fau.de>
Date: Tue, 19 Dec 2023 21:45:05 +0100
Subject: [PATCH] NAT-329: completed all 4 scenarios for migrate tests

---
 .../workflows/iptrunk/test_migrate_iptrunk.py | 69 ++++++++++++++-----
 1 file changed, 51 insertions(+), 18 deletions(-)

diff --git a/test/workflows/iptrunk/test_migrate_iptrunk.py b/test/workflows/iptrunk/test_migrate_iptrunk.py
index 7cf8a3de..7be59339 100644
--- a/test/workflows/iptrunk/test_migrate_iptrunk.py
+++ b/test/workflows/iptrunk/test_migrate_iptrunk.py
@@ -1,11 +1,21 @@
+""" in this test four scenarios are tested for the migrate workflow
+
+1. Nokia will be migrated to Juniper
+2. Juniper will be migrated to Nokia
+3. Juniper will be migrade to Juniper
+4. Nokia will be migrate to Nokia
+
+For all scenarios side [0] is used to simplify the validation.
+Actually only netbox validations for Nokia router are performed.
+"""
 from os import PathLike
 from unittest.mock import patch
 
 import pytest
 
 from gso.products import Iptrunk
-from gso.products.product_types.router import Router
 from gso.products.product_blocks.router import RouterVendor
+from gso.products.product_types.router import Router
 from test import USER_CONFIRM_EMPTY_FORM
 from test.conftest import UseJuniperSide
 from test.workflows import (
@@ -31,7 +41,7 @@ def migrate_form_input(
     use_juniper = getattr(request, "param", UseJuniperSide.NONE)
 
     if use_juniper == UseJuniperSide.SIDE_A:
-        # Both side Nokia -> side A will be Juniper
+        # Nokia -> Juniper
         product_id = iptrunk_subscription_factory()
         old_subscription = Iptrunk.from_subscription(product_id)
         new_router = juniper_router_subscription_factory()
@@ -39,15 +49,19 @@ def migrate_form_input(
         new_side_ae_members = faker.generate_juniper_members_list()[0:2]
         lag_name = "ae1"
     elif use_juniper == UseJuniperSide.SIDE_B:
-        # Both side are Nokia -> side b will be Juniper
-        product_id = iptrunk_subscription_factory()
+        # Juniper -> Nokia
+        old_side_a_node = juniper_router_subscription_factory()
+        old_side_a_node = iptrunk_side_subscription_factory(iptrunk_side_node=old_side_a_node)
+        old_side_b_node = juniper_router_subscription_factory()
+        old_side_b_node = iptrunk_side_subscription_factory(iptrunk_side_node=old_side_b_node)
+        product_id = iptrunk_subscription_factory(iptrunk_sides=[old_side_a_node, old_side_b_node])
         old_subscription = Iptrunk.from_subscription(product_id)
-        new_router = juniper_router_subscription_factory()
+        new_router = nokia_router_subscription_factory()
         replace_side = str(old_subscription.iptrunk.iptrunk_sides[0].iptrunk_side_node.subscription.subscription_id)
-        new_side_ae_members = faker.generate_juniper_members_list()[0:2]
-        lag_name = "ae1"
+        new_side_ae_members = faker.generate_nokia_members_list()[0:2]
+        lag_name = "LAG1"
     elif use_juniper == UseJuniperSide.SIDE_BOTH:
-        # Both side are Juniper -> side A will be new Juniper
+        # Juniper -> Juniper
         old_side_a_node = juniper_router_subscription_factory()
         old_side_a_node = iptrunk_side_subscription_factory(iptrunk_side_node=old_side_a_node)
         old_side_b_node = juniper_router_subscription_factory()
@@ -55,11 +69,11 @@ def migrate_form_input(
         product_id = iptrunk_subscription_factory(iptrunk_sides=[old_side_a_node, old_side_b_node])
         old_subscription = Iptrunk.from_subscription(product_id)
         new_router = juniper_router_subscription_factory()
-        replace_side = str(old_subscription.iptrunk.iptrunk_sides[1].iptrunk_side_node.subscription.subscription_id)
+        replace_side = str(old_subscription.iptrunk.iptrunk_sides[0].iptrunk_side_node.subscription.subscription_id)
         new_side_ae_members = faker.generate_juniper_members_list()[0:2]
         lag_name = "ae1"
     else:
-        # Both side are Nokia -> side a will be Nokia
+        # Nokia -> Nokia
         product_id = iptrunk_subscription_factory()
         old_subscription = Iptrunk.from_subscription(product_id)
         new_router = nokia_router_subscription_factory()
@@ -83,6 +97,20 @@ def migrate_form_input(
     ]
 
 
+def interface_lists_are_equal(list1, list2):
+    if len(list1) != len(list2):
+        return False
+
+    for item1 in list1:
+        if not any(
+            item1.interface_name == item2.interface_name and item1.interface_description == item2.interface_description
+            for item2 in list2
+        ):
+            return False
+
+    return True
+
+
 @pytest.mark.parametrize(
     "migrate_form_input",
     [UseJuniperSide.NONE, UseJuniperSide.SIDE_A, UseJuniperSide.SIDE_B, UseJuniperSide.SIDE_BOTH],
@@ -154,8 +182,10 @@ def test_migrate_iptrunk_success(
     new_router = migrate_form_input[2]["new_node"]
     new_lag_interface = migrate_form_input[3]["new_lag_interface"]
     replace_side = migrate_form_input[1]["replace_side"]
+    new_lag_member_interfaces = migrate_form_input[3]["new_lag_member_interfaces"]
 
-    # Get vendor for the migrated node
+    # Get vendor for the new and old migrated node
+    vendor_old = Router.from_subscription(replace_side).router.vendor
     vendor_new = Router.from_subscription(new_router).router.vendor
 
     # Only Nokia will be checked on netbox
@@ -163,19 +193,22 @@ def test_migrate_iptrunk_success(
     num_nokia_reserved = 2 * (vendor_new == RouterVendor.NOKIA)
     num_nokia_attached = 2 * (vendor_new == RouterVendor.NOKIA)
 
+    # Only interfaces lag delete for nokia node is tested
+    num_nokia_lag_del = 1 * (vendor_old == RouterVendor.NOKIA)
+
+    # Only free interfaces when node was nokia
+    num_nokia_free = 2 * (vendor_old == RouterVendor.NOKIA)
+
     # Assert all Netbox calls have been made
-    # This test case is for migrating Nokia to Nokia, juniper to nokia and juniper to juniper.
-    # Therefore, the netbox counts need to set correctly
     assert mocked_create_interface.call_count == num_nokia_lags  # once for creating the LAG on the newly replaced side:
     assert mocked_reserve_interface.call_count == num_nokia_reserved  # Twice for the new interfaces
     assert mocked_attach_interface_to_lag.call_count == num_nokia_attached  # Twice for the new interfaces
     assert mocked_allocate_interface.call_count == num_nokia_attached  # Twice for the new interfaces
-    assert mocked_free_interface.call_count == num_nokia_attached  # Twice for the old interfaces
-    assert mocked_delete_interface.call_count == num_nokia_lags  # once for deleting the LAG on the old replaced side
+    assert mocked_free_interface.call_count == num_nokia_free  # Twice for the old interfaces
+    assert mocked_delete_interface.call_count == num_nokia_lag_del  # once for deleting the LAG on the old replaced side
 
     # Assert the new side is replaced
     assert str(subscription.iptrunk.iptrunk_sides[0].iptrunk_side_node.subscription.subscription_id) == new_router
     assert subscription.iptrunk.iptrunk_sides[0].iptrunk_side_ae_iface == new_lag_interface
-    assert len(subscription.iptrunk.iptrunk_sides[0].iptrunk_side_ae_members) == 2
-    assert subscription.iptrunk.iptrunk_sides[0].iptrunk_side_ae_members[0].interface_name == "Interface0"
-    assert subscription.iptrunk.iptrunk_sides[0].iptrunk_side_ae_members[1].interface_name == "Interface1"
+    existing_members = subscription.iptrunk.iptrunk_sides[0].iptrunk_side_ae_members
+    assert interface_lists_are_equal(new_lag_member_interfaces, existing_members)
-- 
GitLab