summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRoberto Bertó <463349+robertoberto@users.noreply.github.com>2025-09-18 13:08:55 -0300
committerGitHub <noreply@github.com>2025-09-18 13:08:55 -0300
commit9a69a954a086df321640dfa65a2ac0ac35a15095 (patch)
tree3c8e41a08dab6e47e4639e09eebcc37c4549b914 /tests
parent85e4714c53b662c45a1f6ee4b6cb0089ad29cc7b (diff)
parentf25d28ab90a266c03fd966ddabc752088da080d6 (diff)
downloadpyvyos-9a69a954a086df321640dfa65a2ac0ac35a15095.tar.gz
pyvyos-9a69a954a086df321640dfa65a2ac0ac35a15095.zip
Merge pull request #18 from eduardormorais/release/0.3.0
Updating version release 0.3.0
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py22
-rw-r--r--tests/modules/__init__.py0
-rw-r--r--tests/modules/test_vy_device.py353
-rw-r--r--tests/test_vy_device.py116
4 files changed, 375 insertions, 116 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..5371206
--- /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", "localhost"),
+ "apikey": os.getenv("VYDEVICE_APIKEY", "api_key"),
+ "port": os.getenv("VYDEVICE_PORT", 443),
+ "protocol": os.getenv("VYDEVICE_PROTOCOL", "https"),
+ "verify": os.getenv("VYDEVICE_VERIFY_SSL", False),
+ "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..096e924
--- /dev/null
+++ b/tests/modules/test_vy_device.py
@@ -0,0 +1,353 @@
+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_multiple_op(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")
+
+
+def test_device_invalid_request(monkeypatch, test_device):
+ def mock_invalid_request(*args, **kwargs):
+ response = requests.Response()
+ response.status_code = 400
+ response.json = lambda: {
+ "success": False,
+ "data": None,
+ "error": "Bad Request",
+ }
+ return response
+
+ monkeypatch.setattr(
+ RestClient,
+ "_execute_request",
+ mock_invalid_request,
+ )
+
+ api_resp = test_device.show(path=["invalid", "path"])
+ assert not api_resp.result
+ assert api_resp.status == 400
+ assert "HTTP Error" in api_resp.error
+
+
+def test_device_error_json_response(monkeypatch, test_device):
+ def mock_error_json_response(*args, **kwargs):
+ response = requests.Response()
+ response.status_code = 200
+ response.data = "This is not a JSON response"
+ return response
+
+ monkeypatch.setattr(
+ RestClient,
+ "_execute_request",
+ mock_error_json_response,
+ )
+
+ api_resp = test_device.show(path=["system", "image"])
+ assert not api_resp.result
+ assert "Invalid response format" in api_resp.error \ No newline at end of file
diff --git a/tests/test_vy_device.py b/tests/test_vy_device.py
deleted file mode 100644
index 84ff0ed..0000000
--- a/tests/test_vy_device.py
+++ /dev/null
@@ -1,116 +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()
-