From 6f5da249eb876de3c3086f196e9936c5ac0f7325 Mon Sep 17 00:00:00 2001
From: Neda Moeini <neda.moeini@geant.org>
Date: Mon, 22 Jul 2024 15:07:32 +0200
Subject: [PATCH] Added unit tests for generate inventory function.

---
 test/utils/test_helpers.py                    | 51 ++++++++++++++++++-
 .../workflows/router/test_terminate_router.py |  3 +-
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/test/utils/test_helpers.py b/test/utils/test_helpers.py
index e80e6f30..66f1c4d6 100644
--- a/test/utils/test_helpers.py
+++ b/test/utils/test_helpers.py
@@ -1,9 +1,16 @@
 from unittest.mock import patch
 
 import pytest
+from orchestrator.types import SubscriptionLifecycle
 
+from gso.products import Router
 from gso.products.product_blocks.iptrunk import IptrunkInterfaceBlock
-from gso.utils.helpers import available_interfaces_choices_including_current_members, validate_tt_number
+from gso.products.product_blocks.router import RouterRole
+from gso.utils.helpers import (
+    available_interfaces_choices_including_current_members,
+    generate_inventory_for_active_routers,
+    validate_tt_number,
+)
 from gso.utils.shared_enums import Vendor
 
 
@@ -90,3 +97,45 @@ def test_tt_number(generate_tt_numbers):
 
             with pytest.raises(ValueError, match=err_msg):
                 validate_tt_number(tt_number)
+
+
+def test_generate_inventory_for_active_routers_with_single_active_router(nokia_router_subscription_factory):
+    """Test the generation of inventory for a single active P router."""
+    router = Router.from_subscription(nokia_router_subscription_factory(router_role=RouterRole.P))
+    expected_result = {
+        "all": {
+            "hosts": {
+                router.router.router_fqdn: {
+                    "lo4": str(router.router.router_lo_ipv4_address),
+                    "lo6": str(router.router.router_lo_ipv6_address),
+                    "vendor": str(router.router.vendor),
+                }
+            }
+        }
+    }
+    assert generate_inventory_for_active_routers(RouterRole.P) == expected_result
+
+
+def test_generate_inventory_for_active_routers_with_multiple_routers(nokia_router_subscription_factory):
+    """Test the generation of inventory for multiple active P and PE routers"""
+    for _ in range(5):
+        nokia_router_subscription_factory(router_role=RouterRole.P)
+    for _ in range(3):
+        nokia_router_subscription_factory(router_role=RouterRole.PE)
+    nokia_router_subscription_factory(status=SubscriptionLifecycle.TERMINATED)
+    nokia_router_subscription_factory(status=SubscriptionLifecycle.INITIAL)
+    #  Test the generation of inventory for multiple active P routers.
+    inventory = generate_inventory_for_active_routers(RouterRole.P)
+    assert len(inventory["all"]["hosts"]) == 5
+    inventory = generate_inventory_for_active_routers(RouterRole.PE)
+    assert len(inventory["all"]["hosts"]) == 3
+
+
+def test_generate_inventory_for_active_routers_with_excluded_router(nokia_router_subscription_factory):
+    """Test the generation of inventory for active P routers with an excluded router."""
+    for _ in range(5):
+        nokia_router_subscription_factory(router_role=RouterRole.P)
+    router = nokia_router_subscription_factory(router_role=RouterRole.P)
+    excluded_routers = [Router.from_subscription(router).router.router_fqdn]
+    inventory = generate_inventory_for_active_routers(RouterRole.P, exclude_routers=excluded_routers)
+    assert len(inventory["all"]["hosts"]) == 5   # 6 P routers, the last one is excluded, so 5 P routers are left.
diff --git a/test/workflows/router/test_terminate_router.py b/test/workflows/router/test_terminate_router.py
index 106f00ea..7a9e6bdb 100644
--- a/test/workflows/router/test_terminate_router.py
+++ b/test/workflows/router/test_terminate_router.py
@@ -3,6 +3,7 @@ from unittest.mock import patch
 import pytest
 
 from gso.products import Router
+from gso.products.product_blocks.router import RouterRole
 from test.workflows import assert_complete, assert_lso_interaction_success, extract_state, run_workflow
 
 
@@ -75,7 +76,7 @@ def test_terminate_p_router_full_success(
         data_config_filename,
 ):
     #  Prepare mock values and expected results
-    product_id = nokia_router_subscription_factory(router_role="p")
+    product_id = nokia_router_subscription_factory(router_role=RouterRole.P)
     router_termination_input_form_data = {
         "tt_number": faker.tt_number(),
         "remove_configuration": remove_configuration,
-- 
GitLab