Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit c62340dd authored by Christopher Andre Marcel INSELIN's avatar Christopher Andre Marcel INSELIN
Browse files

:art: Removed fetching of variables in the teardown

parent 7f05150a
No related branches found
No related tags found
1 merge request!47First merge request for NETBOX Cisco tests
Pipeline #102994 passed
...@@ -15,13 +15,12 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -15,13 +15,12 @@ class TestNetboxDeviceCreate(unittest.TestCase):
# definition of the variables # definition of the variables
@classmethod def setUp(self) -> None:
def setUpClass(cls) -> None:
"""Will create a manufacturer, site, device role and device type""" """Will create a manufacturer, site, device role and device type"""
list_of_ids: dict = {} self.list_of_ids: dict = {}
requests.post( manufacturer = requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/",
headers=HEADERS, headers=HEADERS,
json={ json={
...@@ -31,9 +30,11 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -31,9 +30,11 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5 timeout=5
) )
self.list_of_ids['manufacturer_id'] = json.loads(manufacturer.content)["id"]
# Will create a device role # Will create a device role
requests.post( device_role = requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", url=f"http://{HOST}:{PORT}/api/dcim/device-roles/",
headers=HEADERS, headers=HEADERS,
json={ json={
...@@ -43,23 +44,26 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -43,23 +44,26 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5 timeout=5
) )
self.list_of_ids['device_role_id'] = json.loads(device_role.content)["id"]
#Will create a device type #Will create a device type
manufacturer = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers=HEADERS, timeout=5) device_type = requests.post(
list_of_ids["manufacturer_id"] = json.loads(manufacturer.content)["results"][0]["id"]
requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/device-types/", url=f"http://{HOST}:{PORT}/api/dcim/device-types/",
headers=HEADERS, headers=HEADERS,
json={ json={
"model": "WS-C2960X", "model": "WS-C2960X",
"slug":"ws-c2960x", "slug":"ws-c2960x",
"manufacturer": list_of_ids["manufacturer_id"] "manufacturer": self.list_of_ids["manufacturer_id"]
}, },
timeout=5 timeout=5
) )
self.list_of_ids['device_type_id'] = json.loads(device_type.content)["id"]
# will create a site # will create a site
requests.post(
site = requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/sites/", url=f"http://{HOST}:{PORT}/api/dcim/sites/",
headers=HEADERS, headers=HEADERS,
json={ json={
...@@ -68,21 +72,19 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -68,21 +72,19 @@ class TestNetboxDeviceCreate(unittest.TestCase):
}, },
timeout=5 timeout=5
) )
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS, timeout=5)
list_of_ids["site_id"] = json.loads(site.content)["results"][0]["id"]
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS, timeout=5) self.list_of_ids["site_id"] = json.loads(site.content)["id"]
list_of_ids["device_type_id"] = json.loads(device_type.content)["results"][0]["id"]
# Will create a dummy config context # Will create a dummy config context
requests.post( config_context = requests.post(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/", url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS, headers=HEADERS,
json={ json={
"name": "test_config_context", "name": "test_config_context",
"sites": [list_of_ids["site_id"]], "sites": [self.list_of_ids["site_id"]],
"device_types": [list_of_ids["device_type_id"]], "device_types": [self.list_of_ids["device_type_id"]],
"data": { "data": {
"radius_servers": [ "radius_servers": [
"1.1.1.1", "1.1.1.1",
...@@ -93,9 +95,12 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -93,9 +95,12 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5 timeout=5
) )
self.list_of_ids['config_context_id'] = json.loads(config_context.content)["id"]
# will create a custom field for access vlan # will create a custom field for access vlan
requests.post( access_vlan = requests.post(
url=f"http://{HOST}:{PORT}/api/extras/custom-fields/", url=f"http://{HOST}:{PORT}/api/extras/custom-fields/",
headers=HEADERS, headers=HEADERS,
json={ json={
...@@ -108,9 +113,11 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -108,9 +113,11 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5 timeout=5
) )
self.list_of_ids['custom_field_vlan_id'] = json.loads(access_vlan.content)["id"]
# Will create a custom field for allowed vlans on a trunk interface # Will create a custom field for allowed vlans on a trunk interface
requests.post( allowed_vlans_trunk = requests.post(
url=f"http://{HOST}:{PORT}/api/extras/custom-fields/", url=f"http://{HOST}:{PORT}/api/extras/custom-fields/",
headers=HEADERS, headers=HEADERS,
json={ json={
...@@ -121,120 +128,83 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -121,120 +128,83 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5 timeout=5
) )
self.list_of_ids['allowed_vlans_trunk_id'] = json.loads(allowed_vlans_trunk.content)["id"]
@classmethod
def tearDownClass(cls) -> None:
""" Deleting all data from NETBOX once the all the tests finish running """
list_of_ids: dict = {} def tearDown(self) -> None:
""" Deleting all data from NETBOX once the all the tests finish running """
#deleting the device #deleting the device
device = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers=HEADERS, timeout=5)
list_of_ids['id_device'] = json.loads(device.content)["results"][0]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/devices/{list_of_ids['id_device']}/", url= f"http://{HOST}:{PORT}/api/dcim/devices/{self.list_of_ids['device_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
#deleting the device type #deleting the device type
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS, timeout=5)
list_of_ids['device_type_id'] = json.loads(device_type.content)["results"][0]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/device-types/{list_of_ids['device_type_id']}/", url= f"http://{HOST}:{PORT}/api/dcim/device-types/{self.list_of_ids['device_type_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
#deleting the manufacturer #deleting the manufacturer
manufacturer = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers=HEADERS, timeout=5)
list_of_ids['manufacturer_id'] = json.loads(manufacturer.content)["results"][0]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/manufacturers/{list_of_ids['manufacturer_id']}/", url= f"http://{HOST}:{PORT}/api/dcim/manufacturers/{self.list_of_ids['manufacturer_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
# deleting the device role # deleting the device role
device_role = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers=HEADERS, timeout=5)
list_of_ids['device_role_id'] = json.loads(device_role.content)["results"][0]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/device-roles/{list_of_ids['device_role_id']}/", url= f"http://{HOST}:{PORT}/api/dcim/device-roles/{self.list_of_ids['device_role_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
# deleting the site # deleting the site
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS, timeout=5)
list_of_ids['site_id'] = json.loads(site.content)["results"][0]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/sites/{list_of_ids['site_id']}/", url= f"http://{HOST}:{PORT}/api/dcim/sites/{self.list_of_ids['site_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
# Deleting the config context # Deleting the config context
config_context = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS,
timeout=5
)
list_of_ids['config_context_id'] = json.loads(config_context.content)["results"][0]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/extras/config-contexts/{list_of_ids['config_context_id']}/", url= f"http://{HOST}:{PORT}/api/extras/config-contexts/{self.list_of_ids['config_context_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
# deleting the access_vlan custom_field # deleting the access_vlan custom_field
custom_field = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/custom-fields/",
headers=HEADERS,
timeout=5
)
list_of_ids['custom_field_vlan_id'] = json.loads(custom_field.content)["results"][0]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/extras/custom-fields/{list_of_ids['custom_field_vlan_id']}/", url= f"http://{HOST}:{PORT}/api/extras/custom-fields/{self.list_of_ids['custom_field_vlan_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
list_of_ids['allowed_vlans_trunk_id'] = json.loads(custom_field.content)["results"][1]["id"]
requests.delete( requests.delete(
url= f"http://{HOST}:{PORT}/api/extras/custom-fields/{list_of_ids['allowed_vlans_trunk_id']}/", url= f"http://{HOST}:{PORT}/api/extras/custom-fields/{self.list_of_ids['allowed_vlans_trunk_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
# deleting the allowed_vlans_trunk custom_field # deleting the allowed_vlans_trunk custom_field
def tearDown(self) -> None:
pass
def test_device_creation(self) -> None: def test_device_creation(self) -> None:
"""" getting the necessary IDs """ """" getting the necessary IDs """
list_of_ids: dict = {}
device_type = requests.get(
url=f"http://{HOST}:{PORT}/api/dcim/device-types/",
headers=HEADERS,
timeout=5
)
list_of_ids['device_type_id'] = json.loads(device_type.content)["results"][0]["id"]
device_role = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers=HEADERS, timeout=5)
list_of_ids['device_role_id'] = json.loads(device_role.content)["results"][0]["id"]
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS, timeout=5)
list_of_ids['site_id'] = json.loads(site.content)["results"][0]["id"]
# Creating the device # Creating the device
...@@ -243,14 +213,16 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -243,14 +213,16 @@ class TestNetboxDeviceCreate(unittest.TestCase):
headers=HEADERS, headers=HEADERS,
json={ json={
"name": "test", "name": "test",
"device_type": list_of_ids['device_type_id'], "device_type": self.list_of_ids['device_type_id'],
"role": list_of_ids['device_role_id'], "role": self.list_of_ids['device_role_id'],
"site": list_of_ids['site_id'], "site": self.list_of_ids['site_id'],
"status": "offline" "status": "offline"
}, },
timeout=5 timeout=5
) )
self.list_of_ids['device_id'] = json.loads(response.content)["id"]
self.assertEqual(response.status_code,201) self.assertEqual(response.status_code,201)
self.assertEqual(json.loads(response.content)["name"],"test") self.assertEqual(json.loads(response.content)["name"],"test")
self.assertEqual(json.loads(response.content)["device_type"]["display"],"WS-C2960X") self.assertEqual(json.loads(response.content)["device_type"]["display"],"WS-C2960X")
...@@ -258,36 +230,24 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -258,36 +230,24 @@ class TestNetboxDeviceCreate(unittest.TestCase):
self.assertEqual(json.loads(response.content)["site"]["name"],"ISPRA") self.assertEqual(json.loads(response.content)["site"]["name"],"ISPRA")
self.assertEqual(json.loads(response.content)["status"]["value"],"offline") self.assertEqual(json.loads(response.content)["status"]["value"],"offline")
# Creating the config context # getting the config context data
config_context = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS,
timeout=5
)
list_of_ids['config_context_id'] = json.loads(config_context.content)["results"][0]["id"]
config_context_data = requests.get( config_context_data = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/{list_of_ids['config_context_id']}/", url=f"http://{HOST}:{PORT}/api/extras/config-contexts/{self.list_of_ids['config_context_id']}/",
headers=HEADERS, headers=HEADERS,
timeout=5 timeout=5
) )
data = json.loads(config_context_data.content)["data"] data = json.loads(config_context_data.content)["data"]
# Getting the device_id
device = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers=HEADERS, timeout=5)
list_of_ids['device_id'] = json.loads(device.content)["results"][0]["id"]
# Putting the data from the config context into the local context data of the device # Putting the data from the config context into the local context data of the device
upload_config_context = requests.put( upload_config_context = requests.put(
url=f"http://{HOST}:{PORT}/api/dcim/devices/{list_of_ids['device_id']}/", url=f"http://{HOST}:{PORT}/api/dcim/devices/{self.list_of_ids['device_id']}/",
headers=HEADERS, headers=HEADERS,
json={ json={
"device_type": list_of_ids['device_type_id'], "device_type": self.list_of_ids['device_type_id'],
"role": list_of_ids['device_role_id'], "role": self.list_of_ids['device_role_id'],
"site": list_of_ids['site_id'], "site": self.list_of_ids['site_id'],
"local_context_data": data "local_context_data": data
}, },
timeout=5 timeout=5
...@@ -305,7 +265,7 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -305,7 +265,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
url=f"http://{HOST}:{PORT}/api/dcim/interfaces/", url=f"http://{HOST}:{PORT}/api/dcim/interfaces/",
headers=HEADERS, headers=HEADERS,
json={ json={
"device": list_of_ids['device_id'], "device": self.list_of_ids['device_id'],
"vdcs": [], "vdcs": [],
"name": "GigabitEthernet2/4", "name": "GigabitEthernet2/4",
"type": "1000base-t", "type": "1000base-t",
...@@ -322,7 +282,7 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -322,7 +282,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
# Interface related tests # Interface related tests
self.assertEqual(response.status_code,201) self.assertEqual(response.status_code,201)
self.assertEqual(json.loads(response.content)["device"]["id"], list_of_ids['device_id']) self.assertEqual(json.loads(response.content)["device"]["id"], self.list_of_ids['device_id'])
self.assertEqual(json.loads(response.content)["name"], "GigabitEthernet2/4") self.assertEqual(json.loads(response.content)["name"], "GigabitEthernet2/4")
self.assertEqual(json.loads(response.content)["type"]["value"], "1000base-t") self.assertEqual(json.loads(response.content)["type"]["value"], "1000base-t")
self.assertEqual(json.loads(response.content)["mode"]["value"], "access") self.assertEqual(json.loads(response.content)["mode"]["value"], "access")
...@@ -337,7 +297,7 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -337,7 +297,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
url=f"http://{HOST}:{PORT}/api/dcim/interfaces/", url=f"http://{HOST}:{PORT}/api/dcim/interfaces/",
headers=HEADERS, headers=HEADERS,
json={ json={
"device": list_of_ids['device_id'], "device": self.list_of_ids['device_id'],
"vdcs": [], "vdcs": [],
"name": "GigabitEthernet2/5", "name": "GigabitEthernet2/5",
"type": "1000base-t", "type": "1000base-t",
...@@ -360,7 +320,7 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -360,7 +320,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
url=f"http://{HOST}:{PORT}/api/dcim/interfaces/", url=f"http://{HOST}:{PORT}/api/dcim/interfaces/",
headers=HEADERS, headers=HEADERS,
json={ json={
"device": list_of_ids['device_id'], "device": self.list_of_ids['device_id'],
"vdcs": [], "vdcs": [],
"name": "TenGigabitEthernet1/1", "name": "TenGigabitEthernet1/1",
"type": "10gbase-x-sfpp", "type": "10gbase-x-sfpp",
...@@ -377,7 +337,7 @@ class TestNetboxDeviceCreate(unittest.TestCase): ...@@ -377,7 +337,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
# Trunk interface related tests # Trunk interface related tests
self.assertEqual(trunk_interface.status_code,201) self.assertEqual(trunk_interface.status_code,201)
self.assertEqual(json.loads(trunk_interface.content)["device"]["id"], list_of_ids['device_id']) self.assertEqual(json.loads(trunk_interface.content)["device"]["id"], self.list_of_ids['device_id'])
self.assertEqual(json.loads(trunk_interface.content)["name"], "TenGigabitEthernet1/1") self.assertEqual(json.loads(trunk_interface.content)["name"], "TenGigabitEthernet1/1")
self.assertEqual(json.loads(trunk_interface.content)["type"]["value"], "10gbase-x-sfpp") self.assertEqual(json.loads(trunk_interface.content)["type"]["value"], "10gbase-x-sfpp")
self.assertEqual(json.loads(trunk_interface.content)["mode"]["value"], "tagged") self.assertEqual(json.loads(trunk_interface.content)["mode"]["value"], "tagged")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment