Commit a518d59e authored by Simon Oliver Tveit's avatar Simon Oliver Tveit
Browse files

Attempt at separating integration from more unit-ish tests (Draft)

parent b40b7b50
host: 0.0.0.0
httpd_url: "https://cnaas_httpd:1443/api/v1.0/firmware"
verify_tls: False
jwtcert: /home/simon/repos/cnaas-nms/src/cnaas_nms/api/tests/data/public.pem
verify_tls_device: False
cafile: /opt/cnaas/cacert/rootCA.crt
cakeyfile: /opt/cnaas/cacert/rootCA.key
certpath: /tmp/devicecerts/
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEPW8bkkVIq4BX8eWwlUOUYbJhiGDv
K/6xY5T0BsvV6pbMoIUfgeThVOq5I3CmXxLt+qyPska6ol9fTN7woZLsCg==
-----END PUBLIC KEY-----
import yaml
import pkg_resources
import os
import json
import unittest
from unittest.mock import patch, MagicMock
from cnaas_nms.db.device import Device, DeviceState, DeviceType
from cnaas_nms.db.job import Job
from cnaas_nms.db.session import clear_test_db, sqla_test_session
from ipaddress import IPv4Address
class DeviceUnitTests(unittest.TestCase):
@patch('cnaas_nms.tools.get_apidata.get_apidata')
def setUp(self, test_get_apidata):
test_apidata = {"jwtcert": "/home/simon/repos/cnaas-nms/src/cnaas_nms/api/tests/data/public.pem"}
test_get_apidata.return_value=test_apidata
from cnaas_nms.api import app
self.app = app.app
self.client = self.app.test_client()
os.environ["USERNAME_DHCP_BOOT"] = "cnaas"
os.environ["PASSWORD_DHCP_BOOT"] = "cnaas"
os.environ["USERNAME_DISCOVERED"] = "cnaas"
os.environ["PASSWORD_DISCOVERED"] = "cnaas"
os.environ["USERNAME_INIT"] = "cnaas"
os.environ["PASSWORD_INIT"] = "cnaas"
os.environ["USERNAME_MANAGED"] = "cnaas"
os.environ["PASSWORD_MANAGED"] = "cnaas"
self.patchers = []
self.patchers.append(patch('flask_jwt_extended.view_decorators.verify_jwt_in_request'))
self.patchers.append(patch('cnaas_nms.api.device.sqla_session', new=sqla_test_session))
self.patchers.append(patch('cnaas_nms.scheduler.scheduler.sqla_session', new=sqla_test_session))
for patcher in self.patchers:
patcher.start()
def tearDown(self) -> None:
clear_test_db()
for patcher in self.patchers:
patcher.stop()
def create_test_device(self, hostname="unittest", id=None):
device = Device(
id=id,
ztp_mac="08002708a8be",
hostname=hostname,
platform="eos",
management_ip=IPv4Address("10.0.1.22"),
state=DeviceState.MANAGED,
device_type=DeviceType.DIST,
)
return device
def test_get_device_by_id(self):
new_device = self.create_test_device(id=1)
with sqla_test_session() as session:
session.add(new_device)
result = self.client.get(f'/api/v1.0/device/{1}')
self.assertEqual(result.status_code, 200)
json_data = json.loads(result.data.decode())
devices = json_data["data"]["devices"]
self.assertEqual(len(devices), 1)
device = devices[0]
self.assertEqual(device["hostname"], "unittest")
def test_get_device_by_hostname(self):
hostname = "device1"
new_device = self.create_test_device(hostname)
with sqla_test_session() as session:
session.add(new_device)
result = self.client.get(f'/api/v1.0/device/{hostname}')
self.assertEqual(result.status_code, 200)
json_data = json.loads(result.data.decode())
devices = json_data["data"]["devices"]
self.assertEqual(len(devices), 1)
device = devices[0]
self.assertEqual(device["hostname"], hostname)
def test_get_device_nonexisting(self):
result = self.client.get(f'/api/v1.0/device/{1}')
self.assertEqual(result.status_code, 404)
def test_get_devices(self):
hostnames = ["device1", "device2"]
devices = [self.create_test_device(hostname=hostname) for hostname in hostnames]
with sqla_test_session() as session:
for device in devices:
session.add(device)
result = self.client.get(f'/api/v1.0/devices')
self.assertEqual(result.status_code, 200)
json_data = json.loads(result.data.decode())
r_devices = json_data["data"]["devices"]
r_hostnames = [device["hostname"] for device in r_devices]
self.assertCountEqual(r_hostnames, hostnames)
@patch('cnaas_nms.confpush.underlay.find_free_mgmt_lo_ip', return_value="10.0.0.1")
@patch('cnaas_nms.confpush.underlay.find_free_infra_ip', return_value="10.0.0.2")
def test_post_valid_device(self, mgmt_lo_ip, infra_ip):
device_data = {
"hostname": "unittestdevice",
"site_id": 1,
"management_ip": "10.1.2.3",
"dhcp_ip": "11.1.2.3",
"ztp_mac": "0800275C091F",
"platform": "eos",
"state": "MANAGED",
"device_type": "DIST",
}
result = self.client.post('/api/v1.0/device', json=device_data)
self.assertEqual(result.status_code, 200)
json_data = json.loads(result.data.decode())
with sqla_test_session() as session:
inserted_device = session.query(Device).filter(Device.hostname == device_data['hostname']).one_or_none()
self.assertIsNotNone(inserted_device)
@patch('cnaas_nms.confpush.underlay.find_free_mgmt_lo_ip', return_value="10.0.0.1")
@patch('cnaas_nms.confpush.underlay.find_free_infra_ip', return_value="10.0.0.2")
def test_post_invalid_platform(self, mgmt_lo_ip, infra_ip):
device_data = {
"hostname": "unittestdevice",
"site_id": 1,
"management_ip": "10.1.2.3",
"dhcp_ip": "11.1.2.3",
"ztp_mac": "0800275C091F",
"platform": "eosw",
"state": "MANAGED",
"device_type": "DIST",
}
result = self.client.post('/api/v1.0/device', json=device_data)
self.assertEqual(result.status_code, 400)
with sqla_test_session() as session:
inserted_device = session.query(Device).filter(Device.hostname == device_data['hostname']).one_or_none()
self.assertIsNone(inserted_device)
def test_delete_device(self):
new_device = self.create_test_device(id=1)
with sqla_test_session() as session:
session.add(new_device)
result = self.client.delete(f'/api/v1.0/device/{1}')
self.assertEqual(result.status_code, 200)
with sqla_test_session() as session:
deleted_device = session.query(Device).filter(Device.id == 1).one_or_none()
self.assertIsNone(deleted_device)
def test_delete_device_nonexisting(self):
result = self.client.delete(f'/api/v1.0/device/{1}')
self.assertEqual(result.status_code, 404)
def test_put_device(self):
new_device = self.create_test_device(hostname="original", id=1)
with sqla_test_session() as session:
session.add(new_device)
device_data = {"hostname": "new"}
result = self.client.put(f'/api/v1.0/device/{1}', json=device_data)
self.assertEqual(result.status_code, 200)
with sqla_test_session() as session:
modified_device = session.query(Device).filter(Device.id == 1).one_or_none()
self.assertEqual(modified_device.hostname, "new")
def test_device_init(self):
#new_device = self.create_test_device(id=1)
new_device = Device(
id=1,
ztp_mac="08002708a8be",
hostname="disc_device",
platform="eos",
management_ip=IPv4Address("10.0.1.22"),
state=DeviceState.DISCOVERED,
device_type=DeviceType.DIST,
)
with sqla_test_session() as session:
session.add(new_device)
#from cnaas_nms.scheduler.scheduler import Scheduler
#scheduler = Scheduler()
#job_id = scheduler.add_onetime_job(
# 'cnaas_nms.confpush.init_device:init_device_step2',
# when=1,
# scheduled_by="mordi",
# kwargs={'device_id': 1, 'iteration': 1})
#print("jobid", job_id)
device_data = {
"hostname": "distcheck",
"device_type": "DIST"
}
result = self.client.post(f'/api/v1.0/device_initcheck/{1}',
json=device_data)
json_data = json.loads(result.data.decode())
print(json_data)
print("HEI")
self.assertEqual(result.status_code, 200)
#with sqla_test_session() as session:
# job1 = session.query(Job).filter(Job.id == job_id).one_or_none()
# print(job1.id)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment