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

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

Adding test for device creation and local context data

parent 15dc484b
No related branches found
No related tags found
1 merge request!47First merge request for NETBOX Cisco tests
Pipeline #100797 failed
......@@ -10,8 +10,11 @@ PORT = os.getenv("PORT", default="8080")
API_TOKEN = os.getenv("API_TOKEN")
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"""
"""Test case for device creation, config context update ... """
@classmethod
def setUpClass(cls) -> None:
......@@ -62,6 +65,30 @@ class TestNetboxDeviceCreate(unittest.TestCase):
},
timeout=5
)
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS)
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_id = json.loads(device_type.content)["results"][0]["id"]
""" Will create a dummy config context """
response = requests.post(
url=f"http://{HOST}:{PORT}/api/extras/config-contexts/",
headers=HEADERS,
json={
"name": "test_config_context",
"sites": [site_id],
"device_types": [device_type_id],
"data": {
"radius_servers": [
"1.1.1.1",
"2.2.2.2"
]
}
},
timeout=5
)
@classmethod
......@@ -119,6 +146,16 @@ class TestNetboxDeviceCreate(unittest.TestCase):
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}/",
headers=HEADERS,
timeout=5
)
def tearDown(self) -> None:
pass
......@@ -154,6 +191,47 @@ class TestNetboxDeviceCreate(unittest.TestCase):
self.assertEqual(json.loads(response.content)["role"]["name"],"Switch")
self.assertEqual(json.loads(response.content)["site"]["name"],"ISPRA")
self.assertEqual(json.loads(response.content)["status"]["value"],"offline")
def test_put_config_context(self) -> None:
""" Will test the update of config context into the device """
# First we need the config context ID
config_context = requests.get(url=f"http://{HOST}:{PORT}/api/extras/config-contexts/", headers=HEADERS)
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)
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_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_id = json.loads(device_role.content)["results"][0]["id"]
site = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers=HEADERS)
site_id = json.loads(site.content)["results"][0]["id"]
device = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers=HEADERS)
device_id = json.loads(device.content)["results"][0]["id"]
upload_config_context = requests.put(
url=f"http://{HOST}:{PORT}/api/dcim/devices/{device_id}/",
headers=HEADERS,
json={
"device_type": device_type_id,
"role": device_role_id,
"site": site_id,
"local_context_data": data
},
timeout=5
)
self.assertEqual(upload_config_context.status_code,200)
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")
......
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