diff options
| author | eduardormorais <eduardoromorais@gmail.com> | 2025-09-01 19:19:13 -0300 |
|---|---|---|
| committer | eduardormorais <eduardoromorais@gmail.com> | 2025-09-01 19:19:13 -0300 |
| commit | c2b12490527ef60db79250d9d7524a3b970713e1 (patch) | |
| tree | c7762bd777f35e18ee6e7f7955d7a40d12f0d099 /tests | |
| parent | 27a1362ff964d39c2f91f226d7453b65567df6bd (diff) | |
| download | pyvyos-c2b12490527ef60db79250d9d7524a3b970713e1.tar.gz pyvyos-c2b12490527ef60db79250d9d7524a3b970713e1.zip | |
Feature/9930599848 - Configuration and implementation of unit tests for the device
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 22 | ||||
| -rw-r--r-- | tests/modules/__init__.py | 0 | ||||
| -rw-r--r-- | tests/modules/test_vy_device.py | 312 | ||||
| -rw-r--r-- | tests/test_vy_device.py | 126 |
4 files changed, 334 insertions, 126 deletions
diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..9360264 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,22 @@ +import os + +import pytest + +from pyvyos import VyDevice + + +@pytest.fixture(scope="module") +def config_device(): + return { + "hostname": os.getenv("VYDEVICE_HOSTNAME"), + "apikey": os.getenv("VYDEVICE_APIKEY"), + "port": os.getenv("VYDEVICE_PORT"), + "protocol": os.getenv("VYDEVICE_PROTOCOL"), + "verify": os.getenv("VYDEVICE_VERIFY_SSL"), + "timeout": 0, + } + + +@pytest.fixture(scope="module") +def test_device(config_device): + return VyDevice(**config_device) diff --git a/tests/modules/__init__.py b/tests/modules/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/modules/__init__.py diff --git a/tests/modules/test_vy_device.py b/tests/modules/test_vy_device.py new file mode 100644 index 0000000..f5f923e --- /dev/null +++ b/tests/modules/test_vy_device.py @@ -0,0 +1,312 @@ +import random +import string + +import pytest +import requests + +from pyvyos import VyDevice +from pyvyos.rest import RestClient + + +def test_device_invalid_configuration(): + with pytest.raises(ValueError): + VyDevice( + "tst", + "123", + "123", + "123", + 2, + ) + + +def test_device_configuration(test_device): + assert isinstance(test_device.hostname, str) + assert isinstance(test_device.apikey, str) + assert isinstance(test_device.port, int) + assert isinstance(test_device.protocol, str) + assert isinstance(test_device.verify, bool) + + +def test_device_retrieve_show_config(monkeypatch, test_device): + def mock_retrieve_show_config(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": { + "config-management": {"commit-revisions": "100"}, + "host-name": "pyvyos-device", + "name-server": "eth0", + }, + "error": None, + } + + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_retrieve_show_config, + ) + + api_resp = test_device.retrieve_show_config(["system"]) + assert api_resp.result + assert isinstance(api_resp.result, dict) + + +def test_device_configure_set(monkeypatch, test_device): + def mock_configure_set(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": {}, + "error": None, + } + + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_configure_set, + ) + + api_resp = test_device.configure_set( + path=["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"] + ) + assert isinstance(api_resp.result, dict) + + +def test_device_retrieve_return_values(monkeypatch, test_device): + def mock_retrieve_return_values(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": ["192.168.56.100/24"], + "error": None, + } + + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_retrieve_return_values, + ) + + api_resp = test_device.retrieve_return_values( + path=["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"] + ) + assert api_resp.result + assert isinstance(api_resp.result, list) + + +def test_device_configure_delete(monkeypatch, test_device): + def mock_configure_delete(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": {}, + "error": None, + } + + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_configure_delete, + ) + + api_resp = test_device.configure_delete(path=["interfaces", "dummy", "dum1"]) + assert isinstance(api_resp.result, dict) + + +def test_device_generate(monkeypatch, test_device): + def mock_configure_delete(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": f"""'Generating public/private rsa key pair.\n' + 'Your identification has been saved in ' + '/tmp/key_{keyrand}\n' + 'Your public key has been saved in ' + '/tmp/key_39skBBzxc5NvIfE8GzQ7.pub\n' + 'The key fingerprint is:\n' + 'SHA256:7vfukWp093kyngUB+iH6' + 'root@pyvyos-device\n' + "The key's randomart image is:\n" + '+---[RSA 3072]----+\n' + '| . . |\n' + '| o + . - . |\n' + '| . = = = + |\n' + '| o + O - o.|\n' + '| . S B . .oo|\n' + '| o *. ..oo.|\n' + '| . ....o. Eo|\n' + '| . +...+ .o+o|\n' + '| ..o=o++.oo.|\n' + '+----[SHA256]-----+\n'""", + "error": None, + } + + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_configure_delete, + ) + + randstring = "".join( + random.choice(string.ascii_letters + string.digits) for _ in range(20) + ) + keyrand = f"/tmp/key_{randstring}" + + api_resp = test_device.generate(path=["ssh", "client-key", keyrand]) + assert isinstance(api_resp.result, str) + assert keyrand in api_resp.result + + +def test_device_show(monkeypatch, test_device): + def mock_show(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": """'Name Default boot Running \n' + '------------------------ -------------- --------- \n' + '1.5-rolling-202407300021 Yes Yes \n' + """, + "error": None, + } + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_show, + ) + + api_resp = test_device.show(path=["system", "image"]) + assert isinstance(api_resp.result, str) + + +def test_device_reset(monkeypatch, test_device): + def mock_reset(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": """'conntrack-sync is not configured!\n'""", + "error": None, + } + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_reset, + ) + + api_resp = test_device.reset(path=["system", "image"]) + assert isinstance(api_resp.result, str) + + +def test_device_config_file_save(monkeypatch, test_device): + def mock_config_file_save(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": "", + "error": None, + } + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_config_file_save, + ) + + api_resp = test_device.config_file_save(file="/config/teste.config") + assert isinstance(api_resp.result, str) + + +def test_device_config_file_load(monkeypatch, test_device): + def mock_config_file_save(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": None, + "error": None, + } + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_config_file_save, + ) + + api_resp = test_device.config_file_load(file="/config/teste.config") + assert api_resp + + +def test_device_configure_multiple_op(monkeypatch, test_device): + def mock_config_file_save(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": None, + "error": None, + } + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_config_file_save, + ) + + api_resp = test_device.configure_multiple_op( + op_path=[ + { + "op": "set", + "path": ["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"], + }, + { + "op": "delete", + "path": ["interfaces", "dummy", "dum1"], + }, + ] + ) + assert api_resp + + +def test_device_invalid_path_configure_set(monkeypatch, test_device): + def mock_configure_set(*args, **kwargs): + response = requests.Response() + response.status_code = 200 + response.json = lambda: { + "success": True, + "data": {}, + "error": None, + } + + return response + + monkeypatch.setattr( + RestClient, + "_execute_request", + mock_configure_set, + ) + with pytest.raises(ValueError): + test_device.configure_multiple_op(op_path="interfaces") diff --git a/tests/test_vy_device.py b/tests/test_vy_device.py deleted file mode 100644 index 9ef3276..0000000 --- a/tests/test_vy_device.py +++ /dev/null @@ -1,126 +0,0 @@ -import sys -import os -import unittest -from pyvyos.device import VyDevice -from pyvyos.device import ApiResponse -from dotenv import load_dotenv -import os -import pprint -import random, string - -load_dotenv() - -hostname = os.getenv("VYDEVICE_HOSTNAME") -apikey = os.getenv("VYDEVICE_APIKEY") -port = os.getenv("VYDEVICE_PORT") -protocol = os.getenv("VYDEVICE_PROTOCOL") -verify = os.getenv("VYDEVICE_VERIFY_SSL") -if verify == "False": - verify = False -else: - verify = True - - -class TestVyDevice(unittest.TestCase): - def setUp(self): - self.device = VyDevice( - hostname=hostname, - apikey=apikey, - port=port, - protocol=protocol, - verify=verify, - ) - - def test_001_retrieve_show_config(self): - response = self.device.retrieve_show_config([]) - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNotNone(response.result) - self.assertFalse(response.error) - - def test_010_configure_set_interface(self): - response = self.device.configure_set( - path=["interfaces", "dummy", "dum1", "address", "192.168.140.1/24"] - ) - # pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNone(response.result) - self.assertFalse(response.error) - - def test_011_retrieve_return_values(self): - response = self.device.retrieve_return_values( - path=["interfaces", "dummy", "dum1", "address"] - ) - self.assertEqual(response.status, 200) - self.assertIsNotNone(response.result) - self.assertFalse(response.error) - self.assertEqual(response.result, ["192.168.140.1/24"]) - - def test_020_configure_delete_interface(self): - response = self.device.configure_delete(path=["interfaces", "dummy", "dum1"]) - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNone(response.result) - self.assertFalse(response.error) - - def test_050_generate(self): - randstring = "".join( - random.choice(string.ascii_letters + string.digits) for _ in range(20) - ) - keyrand = f"/tmp/key_{randstring}" - response = self.device.generate(path=["ssh", "client-key", keyrand]) - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNotNone(response.result) - self.assertFalse(response.error) - - def test_100_show(self): - response = self.device.show(path=["system", "image"]) - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNotNone(response.result) - self.assertFalse(response.error) - - def test_200_reset(self): - response = self.device.reset(path=["conntrack-sync", "internal-cache"]) - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNotNone(response.result) - self.assertFalse(response.error) - - def test_300_config_file_save(self): - response = self.device.config_file_save(file="/config/test300.config") - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNotNone(response.result) - self.assertFalse(response.error) - - def test_301_config_file_save(self): - response = self.device.config_file_save() - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNotNone(response.result) - self.assertFalse(response.error) - - def test_302_config_file_load(self): - response = self.device.config_file_load(file="/config/test300.config") - pprint.pprint(response) - - self.assertEqual(response.status, 200) - self.assertIsNone(response.result) - self.assertFalse(response.error) - - def tearDown(self): - pass - - -if __name__ == "__main__": - unittest.main() |
