-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
309bc52
commit 8323582
Showing
15 changed files
with
296 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from contextlib import suppress | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from core.choices import DataSourceStatusChoices | ||
from factories import DataFileFactory, DataSourceFactory | ||
|
||
from validity.models import VDataFile, VDataSource | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_sync_status(): | ||
data_source = DataSourceFactory() | ||
assert data_source.status != DataSourceStatusChoices.SYNCING | ||
with data_source._sync_status(): | ||
assert data_source.status == DataSourceStatusChoices.SYNCING | ||
assert VDataSource.objects.get(pk=data_source.pk).status == DataSourceStatusChoices.SYNCING | ||
assert data_source.status == DataSourceStatusChoices.COMPLETED | ||
assert VDataSource.objects.get(pk=data_source.pk).status == DataSourceStatusChoices.COMPLETED | ||
|
||
with suppress(Exception): | ||
with data_source._sync_status(): | ||
raise ValueError | ||
assert data_source.status == DataSourceStatusChoices.FAILED | ||
assert VDataSource.objects.get(pk=data_source.pk).status == DataSourceStatusChoices.FAILED | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_partial_sync(monkeypatch): | ||
ds = DataSourceFactory(type="device_polling") | ||
DataFileFactory(source=ds, data="some_contents".encode(), path="file-0.txt") | ||
DataFileFactory(source=ds, path="file-1.txt") | ||
with TemporaryDirectory() as temp_dir: | ||
existing = Path(temp_dir) / "file-1.txt" | ||
new = Path(temp_dir) / "file_new.txt" | ||
existing.write_text("qwe") | ||
new.write_text("rty") | ||
fetch_mock = MagicMock(**{"return_value.fetch.return_value.__enter__.return_value": temp_dir}) | ||
monkeypatch.setattr(ds, "get_backend", fetch_mock) | ||
ds.partial_sync("device_filter") | ||
fetch_mock().fetch.assert_called_once_with("device_filter") | ||
assert {*ds.datafiles.values_list("path", flat=True)} == {"file-0.txt", "file-1.txt", "file_new.txt"} | ||
assert VDataFile.objects.get(path="file-0.txt").data_as_string == "some_contents" | ||
assert VDataFile.objects.get(path="file-1.txt").data_as_string == "qwe" | ||
assert VDataFile.objects.get(path="file_new.txt").data_as_string == "rty" | ||
assert VDataSource.objects.get(pk=ds.pk).status == DataSourceStatusChoices.COMPLETED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import time | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from validity.pollers import NetmikoPoller | ||
|
||
|
||
class TestNetmikoPoller: | ||
@pytest.fixture | ||
def get_mocked_poller(self, monkeypatch): | ||
def _get_poller(credentials, commands, mock): | ||
monkeypatch.setattr(NetmikoPoller, "driver_cls", mock) | ||
return NetmikoPoller(credentials, commands) | ||
|
||
return _get_poller | ||
|
||
@pytest.fixture | ||
def get_mocked_device(self): | ||
def _get_device(primary_ip): | ||
db_ip = Mock(address=Mock(ip=primary_ip)) | ||
return Mock(primary_ip=db_ip) | ||
|
||
return _get_device | ||
|
||
@pytest.mark.django_db | ||
def test_get_driver(self, get_mocked_poller, get_mocked_device): | ||
credentials = {"user": "admin", "password": "1234"} | ||
poller = get_mocked_poller(credentials, [], Mock()) | ||
device = get_mocked_device("1.1.1.1") | ||
assert poller.get_credentials(device) == credentials | {poller.host_param_name: "1.1.1.1"} | ||
assert poller.get_driver(device) == poller.driver_cls.return_value | ||
poller.driver_cls.assert_called_once_with(**credentials, **{poller.host_param_name: "1.1.1.1"}) | ||
|
||
def test_poll_one_command(self, get_mocked_poller): | ||
poller = get_mocked_poller({}, [], Mock()) | ||
driver = Mock(**{"send_command.return_value": 1234}) | ||
command = Mock(parameters={"cli_command": "show ver"}) | ||
assert poller.poll_one_command(driver, command) == 1234 | ||
driver.send_command.assert_called_once_with("show ver") | ||
|
||
@pytest.mark.parametrize("raise_exc", [True, False]) | ||
def test_poll(self, get_mocked_poller, raise_exc, get_mocked_device): | ||
def poll(arg): | ||
time.sleep(0.1) | ||
if raise_exc: | ||
raise OSError | ||
return arg | ||
|
||
commands = [Mock(parameters={"cli_command": "a"}), Mock(parameters={"cli_command": "b"})] | ||
poller = get_mocked_poller({}, commands, Mock(**{"return_value.send_command": poll})) | ||
devices = [get_mocked_device(f"1.1.1.{i}") for i in range(10)] | ||
start = time.time() | ||
results = list(poller.poll(devices)) | ||
assert time.time() - start < 0.3 | ||
assert len(results) == len(commands) * len(devices) | ||
if raise_exc: | ||
assert all(res.error.message.startswith("OSError") for res in results) | ||
else: | ||
assert all(res.result in {"a", "b"} for res in results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
|
||
from validity.utils.dbfields import EncryptedDict, EncryptedString | ||
|
||
|
||
@pytest.fixture | ||
def setup_private_key(monkeypatch): | ||
monkeypatch.setattr(EncryptedString, "secret_key", b"1234567890") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"plain_value", | ||
[ | ||
{"param1": "val1", "param2": "val2"}, | ||
{}, | ||
{"param": ["some", "complex", {"val": "ue"}]}, | ||
], | ||
) | ||
def test_encrypted_dict(plain_value, setup_private_key): | ||
enc_dict = EncryptedDict(plain_value) | ||
assert enc_dict.decrypted == plain_value | ||
assert enc_dict.keys() == enc_dict.encrypted.keys() == plain_value.keys() | ||
assert all(val.startswith("$") and val.endswith("$") and val.count("$") == 3 for val in enc_dict.encrypted.values()) | ||
assert EncryptedDict(enc_dict.encrypted).decrypted == plain_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.