From 378c07f2c81d706d4f118365dc53b17e0d17e63a Mon Sep 17 00:00:00 2001
From: Ubuntu <jorge.sasiain@ehu.eus>
Date: Thu, 1 Jun 2023 11:50:05 +0000
Subject: [PATCH] NAT-152 / NAT-198: update tests and add some tests for fail
 cases

---
 test/conftest.py  |  9 ++++++---
 test/test_ipam.py | 48 ++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/test/conftest.py b/test/conftest.py
index d095a49f..92e2af4f 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -36,7 +36,8 @@ def configuration_data():
                         "networks": ["dead:beef::/80"],
                         "mask": 128
                     },
-                    "domain_name": ".lo"
+                    "domain_name": ".lo",
+                    "dns_view": "default"
                 },
                 "TRUNK": {
                     "V4": {
@@ -49,7 +50,8 @@ def configuration_data():
                         "networks": [],
                         "mask": 126
                     },
-                    "domain_name": ".trunk"
+                    "domain_name": ".trunk",
+                    "dns_view": "default"
                 },
                 "GEANT_IP": {
                     "V4": {
@@ -62,7 +64,8 @@ def configuration_data():
                         "networks": [],
                         "mask": 126
                     },
-                    "domain_name": ".geantip"
+                    "domain_name": ".geantip",
+                    "dns_view": "default"
                 }
             },
             "PROVISIONING_PROXY": {
diff --git a/test/test_ipam.py b/test/test_ipam.py
index 2abbdbf3..c72ba2f5 100644
--- a/test/test_ipam.py
+++ b/test/test_ipam.py
@@ -1,4 +1,5 @@
 import ipaddress
+import pytest
 import re
 import responses
 
@@ -32,6 +33,11 @@ def test_new_service_networks(data_config_filename):
         v6=ipaddress.ip_network('dead:beef::18/128')
     )
 
+    # should fail because this service type has networks instead of containers
+    with pytest.raises(AssertionError):
+        service_networks = ipam.new_service_networks(service_type='LO')
+        assert service_networks is None
+
 
 @responses.activate
 def test_new_service_host(data_config_filename):
@@ -56,7 +62,7 @@ def test_new_service_host(data_config_filename):
 
     responses.add(
         method=responses.GET,
-        url=re.compile(r'.*/wapi.*/network.*'),
+        url=re.compile(r'.*/wapi.*/network.*10.255.255.*'),
         json=[
             {
                 "_ref": "network/ZG5zLm5ldHdvcmskMTAuMjU1LjI1NS4yMC8zMi8w:10.255.255.20/32/default",   # noqa: E501
@@ -67,6 +73,19 @@ def test_new_service_host(data_config_filename):
 
     )
 
+    responses.add(
+        method=responses.GET,
+        url=re.compile(r'.*/wapi.*/network.*10.255.254.*'),
+        json=[
+            {
+                "_ref": "network/ZG5zLm5Gd0VHQkRQUjMzLjMwNzIuMzE1LzAyLzI:10.255.254.20/32/default",   # noqa: E501
+                "network": "10.255.254.20/32",
+                "network_view": "default"
+            }
+        ]
+
+    )
+
     responses.add(
         method=responses.GET,
         url=re.compile(r'.*/wapi.*/ipv6network.*'),
@@ -81,13 +100,20 @@ def test_new_service_host(data_config_filename):
 
     responses.add(
         method=responses.POST,
-        url=re.compile(r'.*/wapi.*/network.*/.*?_function=next_available_ip&num=1$'),   # noqa: E501
+        url=re.compile(r'.*/wapi.*/network/.*10.255.255.*?_function=next_available_ip&num=1$'),   # noqa: E501
         json={'ips': ['10.255.255.20']}
     )
 
     responses.add(
         method=responses.POST,
-        url=re.compile(r'.*/wapi.*/ipv6network.*/.*?_function=next_available_ip&num=1$'),   # noqa: E501
+        url=re.compile(r'.*/wapi.*/network/.*10.255.254.*?_function=next_available_ip&num=1$'),   # noqa: E501
+        body="Cannot find 1 available IP address(es) in this network",
+        status=400
+    )
+
+    responses.add(
+        method=responses.POST,
+        url=re.compile(r'.*/wapi.*/ipv6network/.*?_function=next_available_ip&num=1$'),   # noqa: E501
         json={'ips': ['dead:beef::18']}
     )
 
@@ -109,6 +135,7 @@ def test_new_service_host(data_config_filename):
         }
     )
 
+    # test host creation by IP addresses
     service_hosts = ipam.new_service_host(
         hostname='test',
         service_type='TRUNK',
@@ -122,6 +149,7 @@ def test_new_service_host(data_config_filename):
         v6=ipaddress.ip_address('dead:beef::18')
     )
 
+    # test host creation by network addresses
     service_hosts = ipam.new_service_host(
         hostname='test',
         service_type='TRUNK',
@@ -135,6 +163,7 @@ def test_new_service_host(data_config_filename):
         v6=ipaddress.ip_address('dead:beef::18')
     )
 
+    # test host creation by just service_type when service cfg uses networks
     service_hosts = ipam.new_service_host(
         hostname='test',
         service_type='LO'
@@ -144,6 +173,7 @@ def test_new_service_host(data_config_filename):
         v6=ipaddress.ip_address('dead:beef::18')
     )
 
+    # test host creation by just service_type when service cfg uses containers
     service_hosts = ipam.new_service_host(
         hostname='test',
         service_type='TRUNK'
@@ -152,3 +182,15 @@ def test_new_service_host(data_config_filename):
         v4=ipaddress.ip_address('10.255.255.20'),
         v6=ipaddress.ip_address('dead:beef::18')
     )
+
+    # test host creation that should return a no available IP error
+    with pytest.raises(AssertionError):
+        service_hosts = ipam.new_service_host(
+            hostname='test',
+            service_type='TRUNK',
+            service_networks=ipam.ServiceNetworks(
+                v4=ipaddress.ip_network('10.255.254.20/32'),
+                v6=ipaddress.ip_network('dead:beef::18/128')
+            )
+        )
+        assert service_hosts is None
-- 
GitLab