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

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

pre-monolitic test

parent f431a8df
No related branches found
No related tags found
1 merge request!47First merge request for NETBOX Cisco tests
......@@ -10,8 +10,6 @@ PORT = os.getenv("PORT", default="8080")
API_TOKEN = os.getenv("API_TOKEN", default="only4testingpurpose")
HEADERS = {"Authorization": f"Token {API_TOKEN}"}
#TODO: Tester ajout des dependances au config context (site, device type)
#TODO: ajout test dependance dans le config context et des differentes valeurs
class TestNetboxDeviceCreate(unittest.TestCase):
"""Test case for device creation, config context update ... """
......@@ -43,21 +41,21 @@ class TestNetboxDeviceCreate(unittest.TestCase):
#Will create a device type
manufacturer_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers=HEADERS, timeout=5)
id = json.loads(manufacturer_id.content)["results"][0]["id"]
manufacturer = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers=HEADERS, timeout=5)
manufactuer_id = json.loads(manufacturer.content)["results"][0]["id"]
requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/device-types/",
headers=HEADERS,
json={
"model": "WS-C2960X",
"slug":"ws-c2960x",
"manufacturer": id
"manufacturer": manufactuer_id
},
timeout=5
)
""" will create a site"""
response = requests.post(
# will create a site
requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/sites/",
headers=HEADERS,
json={
......@@ -66,15 +64,15 @@ class TestNetboxDeviceCreate(unittest.TestCase):
},
timeout=5
)
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS)
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS, timeout=5)
site_id = json.loads(site.content)["results"][0]["id"]
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS)
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS, timeout=5)
device_type_id = json.loads(device_type.content)["results"][0]["id"]
""" Will create a dummy config context """
# Will create a dummy config context
response = requests.post(
requests.post(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS,
json={
......@@ -91,6 +89,21 @@ class TestNetboxDeviceCreate(unittest.TestCase):
timeout=5
)
# will create a custom field
requests.post(
url=f"http://{HOST}:{PORT}/api/extras/custom-fields/",
headers=HEADERS,
json={
"content_types": ["dcim.interface"],
"name": "access_vlan",
"type": "integer",
"validation_minimum": 1,
"validation_maximum": 4096
},
timeout=5
)
@classmethod
def tearDownClass(cls) -> None:
......@@ -98,61 +111,76 @@ class TestNetboxDeviceCreate(unittest.TestCase):
#deleting the device
device_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers=HEADERS)
id = json.loads(device_id.content)["results"][0]["id"]
response_device = requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/devices/{id}/",
device = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers=HEADERS, timeout=5)
id_device = json.loads(device.content)["results"][0]["id"]
requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/devices/{id_device}/",
headers=HEADERS,
timeout=5
)
#deleting the device type
device_type_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS)
id = json.loads(device_type_id.content)["results"][0]["id"]
response_device_type = requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/device-types/{id}/",
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS, timeout=5)
device_type_id = json.loads(device_type.content)["results"][0]["id"]
requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/device-types/{device_type_id}/",
headers=HEADERS,
timeout=5
)
#deleting the manufacturer
manufacturer_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers=HEADERS)
#id = json.loads(manufacturer_id.content)["id"]
#print(json.loads(manufacturer_id.content)["results"][0]["id"])
id = json.loads(manufacturer_id.content)["results"][0]["id"]
#print(type(json.loads(manufacturer_id.content)["results"][0]["id"]))
respose_manufacturer = requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/manufacturers/{id}/",
manufacturer = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers=HEADERS, timeout=5)
manufacturer_id = json.loads(manufacturer.content)["results"][0]["id"]
requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/manufacturers/{manufacturer_id}/",
headers=HEADERS,
timeout=5
)
# deleting the device role
device_role_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers=HEADERS)
id = json.loads(device_role_id.content)["results"][0]["id"]
response_device_role = requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/device-roles/{id}/",
device_role = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers=HEADERS, timeout=5)
device_role_id = json.loads(device_role.content)["results"][0]["id"]
requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/device-roles/{device_role_id}/",
headers=HEADERS,
timeout=5
)
# deleting the site
site_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS)
id = json.loads(site_id.content)["results"][0]["id"]
response_site = requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/sites/{id}/",
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS, timeout=5)
site_id = json.loads(site.content)["results"][0]["id"]
requests.delete(
url= f"http://{HOST}:{PORT}/api/dcim/sites/{site_id}/",
headers=HEADERS,
timeout=5
)
# Deleting the config context
config_context_id = requests.get(url=f"http://{HOST}:{PORT}/api/extras/config-contexts/", headers=HEADERS)
id = json.loads(config_context_id.content)["results"][0]["id"]
response_config_context = requests.delete(
url= f"http://{HOST}:{PORT}/api/extras/config-contexts/{id}/",
config_context = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS,
timeout=5
)
config_context_id = json.loads(config_context.content)["results"][0]["id"]
requests.delete(
url= f"http://{HOST}:{PORT}/api/extras/config-contexts/{config_context_id}/",
headers=HEADERS,
timeout=5
)
# deleting the custom_field
custom_field_vlan = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/custom-fields/",
headers=HEADERS,
timeout=5
)
custom_field_vlan_id = json.loads(custom_field_vlan.content)["results"][0]["id"]
requests.delete(
url= f"http://{HOST}:{PORT}/api/extras/custom-fields/{custom_field_vlan_id}/",
headers=HEADERS,
timeout=5
)
......@@ -162,13 +190,17 @@ class TestNetboxDeviceCreate(unittest.TestCase):
def test_device_creation(self) -> None:
"""" getting the necessary IDs """
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS)
device_type = requests.get(
url=f"http://{HOST}:{PORT}/api/dcim/device-types/",
headers=HEADERS,
timeout=5
)
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)
device_role = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers=HEADERS, timeout=5)
device_role_id = json.loads(device_role.content)["results"][0]["id"]
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS)
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS, timeout=5)
site_id = json.loads(site.content)["results"][0]["id"]
# Creating the device
......@@ -198,23 +230,31 @@ class TestNetboxDeviceCreate(unittest.TestCase):
# First we need the config context ID
config_context = requests.get(url=f"http://{HOST}:{PORT}/api/extras/config-contexts/", headers=HEADERS)
config_context = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS,
timeout=5
)
config_context_id = json.loads(config_context.content)["results"][0]["id"]
config_context_data = requests.get(url=f"http://{HOST}:{PORT}/api/extras/config-contexts/{config_context_id}/", headers=HEADERS)
config_context_data = requests.get(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/{config_context_id}/",
headers=HEADERS,
timeout=5
)
data = json.loads(config_context_data.content)["data"]
print(data)
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS)
device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers=HEADERS, timeout=5)
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)
device_role = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers=HEADERS, timeout=5)
device_role_id = json.loads(device_role.content)["results"][0]["id"]
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS)
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS, timeout=5)
site_id = json.loads(site.content)["results"][0]["id"]
device = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers=HEADERS)
device = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers=HEADERS, timeout=5)
device_id = json.loads(device.content)["results"][0]["id"]
upload_config_context = requests.put(
......@@ -234,7 +274,44 @@ class TestNetboxDeviceCreate(unittest.TestCase):
self.assertEqual(json.loads(upload_config_context.content)["local_context_data"]["radius_servers"][0],"1.1.1.1")
self.assertEqual(json.loads(upload_config_context.content)["local_context_data"]["radius_servers"][1],"2.2.2.2")
def test_interface_access_creation(self) -> None:
""" Test that interface has been created """
device = requests.get(
url=f"http://{HOST}:{PORT}/api/dcim/devices/",
headers=HEADERS,
timeout=5
)
device_id = json.loads(device.content)["results"][0]["id"]
response = requests.post(
url=f"http://{HOST}:{PORT}/api/dcim/interfaces/",
headers=HEADERS,
json={
"device": device_id,
"vdcs": [],
"name": "GigabitEthernet2/4",
"type": "1000base-t",
"mode": "access",
"description": "test",
"duplex": "auto",
"custom_fields": {
"access_vlan": 10
}
},
timeout=5
)
self.assertEqual(response.status_code,201)
self.assertEqual(json.loads(response.content)["device"]["id"], 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")
self.assertEqual(json.loads(response.content)["description"], "test")
self.assertEqual(json.loads(response.content)["speed"], None)
self.assertEqual(json.loads(response.content)["duplex"]["value"], "auto")
self.assertEqual(json.loads(response.content)["custom_fields"]["access_vlan"], 10)
if __name__ == "__main__":
unittest.main()
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