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):
# definition of the variables
@classmethod
def setUpClass(cls) -> None:
def setUp(self) -> None:
"""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/",
headers=HEADERS,
json={
......@@ -31,9 +30,11 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5
)
self.list_of_ids['manufacturer_id'] = json.loads(manufacturer.content)["id"]
# Will create a device role
requests.post(
device_role = requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/device-roles/",
headers=HEADERS,
json={
......@@ -43,23 +44,26 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5
)
self.list_of_ids['device_role_id'] = json.loads(device_role.content)["id"]
#Will create a device type
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.post(
device_type = requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/device-types/",
headers=HEADERS,
json={
"model": "WS-C2960X",
"slug":"ws-c2960x",
"manufacturer": list_of_ids["manufacturer_id"]
"manufacturer": self.list_of_ids["manufacturer_id"]
},
timeout=5
)
self.list_of_ids['device_type_id'] = json.loads(device_type.content)["id"]
# will create a site
requests.post(
site = requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/sites/",
headers=HEADERS,
json={
......@@ -68,21 +72,19 @@ class TestNetboxDeviceCreate(unittest.TestCase):
},
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)
list_of_ids["device_type_id"] = json.loads(device_type.content)["results"][0]["id"]
self.list_of_ids["site_id"] = json.loads(site.content)["id"]
# Will create a dummy config context
requests.post(
config_context = requests.post(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS,
json={
"name": "test_config_context",
"sites": [list_of_ids["site_id"]],
"device_types": [list_of_ids["device_type_id"]],
"sites": [self.list_of_ids["site_id"]],
"device_types": [self.list_of_ids["device_type_id"]],
"data": {
"radius_servers": [
"1.1.1.1",
......@@ -93,9 +95,12 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5
)
self.list_of_ids['config_context_id'] = json.loads(config_context.content)["id"]
# will create a custom field for access vlan
requests.post(
access_vlan = requests.post(
url=f"http://{HOST}:{PORT}/api/extras/custom-fields/",
headers=HEADERS,
json={
......@@ -108,9 +113,11 @@ class TestNetboxDeviceCreate(unittest.TestCase):
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
requests.post(
allowed_vlans_trunk = requests.post(
url=f"http://{HOST}:{PORT}/api/extras/custom-fields/",
headers=HEADERS,
json={
......@@ -121,120 +128,83 @@ class TestNetboxDeviceCreate(unittest.TestCase):
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
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(
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,
timeout=5
)
#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(
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,
timeout=5
)
#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(
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,
timeout=5
)
# 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(
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,
timeout=5
)
# 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(
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,
timeout=5
)
# 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(
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,
timeout=5
)
# 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(
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,
timeout=5
)
list_of_ids['allowed_vlans_trunk_id'] = json.loads(custom_field.content)["results"][1]["id"]
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,
timeout=5
)
# deleting the allowed_vlans_trunk custom_field
def tearDown(self) -> None:
pass
def test_device_creation(self) -> None:
"""" 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
......@@ -243,14 +213,16 @@ class TestNetboxDeviceCreate(unittest.TestCase):
headers=HEADERS,
json={
"name": "test",
"device_type": list_of_ids['device_type_id'],
"role": list_of_ids['device_role_id'],
"site": list_of_ids['site_id'],
"device_type": self.list_of_ids['device_type_id'],
"role": self.list_of_ids['device_role_id'],
"site": self.list_of_ids['site_id'],
"status": "offline"
},
timeout=5
)
self.list_of_ids['device_id'] = json.loads(response.content)["id"]
self.assertEqual(response.status_code,201)
self.assertEqual(json.loads(response.content)["name"],"test")
self.assertEqual(json.loads(response.content)["device_type"]["display"],"WS-C2960X")
......@@ -258,36 +230,24 @@ class TestNetboxDeviceCreate(unittest.TestCase):
self.assertEqual(json.loads(response.content)["site"]["name"],"ISPRA")
self.assertEqual(json.loads(response.content)["status"]["value"],"offline")
# Creating 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"]
# getting the config context data
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,
timeout=5
)
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
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,
json={
"device_type": list_of_ids['device_type_id'],
"role": list_of_ids['device_role_id'],
"site": list_of_ids['site_id'],
"device_type": self.list_of_ids['device_type_id'],
"role": self.list_of_ids['device_role_id'],
"site": self.list_of_ids['site_id'],
"local_context_data": data
},
timeout=5
......@@ -305,7 +265,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
url=f"http://{HOST}:{PORT}/api/dcim/interfaces/",
headers=HEADERS,
json={
"device": list_of_ids['device_id'],
"device": self.list_of_ids['device_id'],
"vdcs": [],
"name": "GigabitEthernet2/4",
"type": "1000base-t",
......@@ -322,7 +282,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
# Interface related tests
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)["type"]["value"], "1000base-t")
self.assertEqual(json.loads(response.content)["mode"]["value"], "access")
......@@ -337,7 +297,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
url=f"http://{HOST}:{PORT}/api/dcim/interfaces/",
headers=HEADERS,
json={
"device": list_of_ids['device_id'],
"device": self.list_of_ids['device_id'],
"vdcs": [],
"name": "GigabitEthernet2/5",
"type": "1000base-t",
......@@ -360,7 +320,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
url=f"http://{HOST}:{PORT}/api/dcim/interfaces/",
headers=HEADERS,
json={
"device": list_of_ids['device_id'],
"device": self.list_of_ids['device_id'],
"vdcs": [],
"name": "TenGigabitEthernet1/1",
"type": "10gbase-x-sfpp",
......@@ -377,7 +337,7 @@ class TestNetboxDeviceCreate(unittest.TestCase):
# Trunk interface related tests
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)["type"]["value"], "10gbase-x-sfpp")
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