-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests with coverage for current code base were added. Along with that, an option to run unit tests via `tox` was added to pyproject.tom. `poetry run tox -e unit` Signed-off-by: Martin Kalcok <martin.kalcok@gmail.com>
- Loading branch information
Showing
11 changed files
with
546 additions
and
60 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,20 @@ | ||
import pytest | ||
|
||
from microovn_rebuilder.remote import _CONNECTORS, ConnectorException, create_connector | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"bad_spec", ["foo", "foo,bar", "lxd:vm1,ssh:vm2", "foo:vm1,foo:vm2"] | ||
) | ||
def test_create_connector_invalid_spec(bad_spec): | ||
with pytest.raises(ConnectorException): | ||
create_connector(bad_spec) | ||
|
||
|
||
def test_create_connector(): | ||
connector = create_connector("lxd:vm1,lxd:vm2") | ||
expected_remotes = ["vm1", "vm2"] | ||
expected_type = _CONNECTORS["lxd"] | ||
|
||
assert isinstance(connector, expected_type) | ||
assert connector.remotes == expected_remotes |
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,99 @@ | ||
import subprocess | ||
from unittest import mock | ||
from unittest.mock import MagicMock, call | ||
|
||
import pytest | ||
|
||
from microovn_rebuilder.remote import ConnectorException, lxd | ||
|
||
|
||
def test_update(mocker, lxd_connector, default_targets): | ||
target = list(default_targets)[0] | ||
mock_run_result = MagicMock(spec=subprocess.CompletedProcess) | ||
mock_run_cmd = mocker.patch.object( | ||
lxd_connector, "_run_command", return_value=mock_run_result | ||
) | ||
mock_check_cmd = mocker.patch.object(lxd_connector, "_check_cmd_result") | ||
|
||
expected_run_calls = [] | ||
expected_check_calls = [] | ||
for remote in lxd_connector.remotes: | ||
expected_run_calls.append( | ||
call("lxc", "file", "delete", f"{remote}{target.remote_path}") | ||
) | ||
expected_check_calls.append( | ||
call(mock_run_result, f"[{remote}] Failed to remove remote file") | ||
) | ||
|
||
expected_run_calls.append( | ||
call( | ||
"lxc", "file", "push", target.local_path, f"{remote}{target.remote_path}" | ||
) | ||
) | ||
expected_check_calls.append( | ||
call(mock_run_result, f"[{remote}] Failed to upload file") | ||
) | ||
|
||
expected_run_calls.append( | ||
call("lxc", "exec", remote, "snap", "restart", target.service) | ||
) | ||
expected_check_calls.append( | ||
call(mock_run_result, f"[{remote}] Failed to restart service") | ||
) | ||
|
||
lxd_connector.update(target) | ||
|
||
mock_run_cmd.assert_has_calls(expected_run_calls) | ||
mock_check_cmd.assert_has_calls(expected_check_calls) | ||
|
||
|
||
def test_check_remote(mocker, lxd_connector, remote_deployment_path): | ||
mock_run_result = MagicMock(spec=subprocess.CompletedProcess) | ||
mock_run_command = mocker.patch.object( | ||
lxd_connector, "_run_command", return_value=mock_run_result | ||
) | ||
mock_check_cmd_result = mocker.patch.object(lxd_connector, "_check_cmd_result") | ||
|
||
expected_run_calls = [] | ||
expected_check_calls = [] | ||
for remote in lxd_connector.remotes: | ||
expected_run_calls.append( | ||
call("lxc", "exec", remote, "--", "test", "-d", remote_deployment_path) | ||
) | ||
expected_check_calls.append( | ||
call( | ||
mock_run_result, | ||
f"[{remote}] Remote directory '{remote_deployment_path}' does not exist on LXC instance {remote}", | ||
) | ||
) | ||
|
||
lxd_connector.check_remote(remote_deployment_path) | ||
|
||
mock_run_command.assert_has_calls(expected_run_calls) | ||
mock_check_cmd_result.assert_has_calls(expected_check_calls) | ||
|
||
|
||
def test_run_command(mocker, lxd_connector): | ||
mock_run = mocker.patch.object(lxd.subprocess, "run") | ||
cmd = ["/bin/foo", "bar"] | ||
lxd_connector._run_command(*cmd) | ||
|
||
mock_run.assert_called_once_with(tuple(cmd), capture_output=True) | ||
|
||
|
||
def test_check_cmd_result_no_error(lxd_connector): | ||
result = MagicMock(autospec=subprocess.CompletedProcess) | ||
result.returncode = 0 | ||
|
||
assert lxd_connector._check_cmd_result(result, "extra message") is None | ||
|
||
|
||
def test_check_cmd_result_error(lxd_connector): | ||
result = MagicMock(autospec=subprocess.CompletedProcess) | ||
result.returncode = 1 | ||
|
||
with pytest.raises(ConnectorException) as exc: | ||
extra_msg = "error details foo" | ||
lxd_connector._check_cmd_result(result, extra_msg) | ||
|
||
assert extra_msg in str(exc.value) |
Oops, something went wrong.