Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Jul 24, 2023
1 parent 91c8fcf commit fbe3768
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/start_server_log.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
trivial:
- capture stdout and stderr content when process failed to start turbo server (https://github.com/ansible-collections/cloud.common/pull/128).
10 changes: 4 additions & 6 deletions plugins/module_utils/turbo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def bind(self):
return True
except (ConnectionRefusedError, FileNotFoundError):
if not running:
start_server_error = self.start_server()
running = start_server_error is None
running, start_server_error = self.start_server()
if attempt == 0:
if not running:
raise EmbeddedModuleUnexpectedFailure(
Expand Down Expand Up @@ -98,10 +97,9 @@ def start_server(self):
stderr=subprocess.PIPE,
)
out, err = p.communicate()
if p.returncode != 0:
return "stdout='{0}' stderr='{1}'".format(
out.decode("utf-8"), err.decode("utf-8")
)
return p.returncode == 0, "stdout='{0}' stderr='{1}'".format(
out.decode("utf-8"), err.decode("utf-8")
)

def communicate(self, data, wait_sleep=0.01):
encoded_data = pickle.dumps((self._plugin, data))
Expand Down
27 changes: 23 additions & 4 deletions tests/unit/plugins/module_utils/turbo/test_turbo_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@ def mocked_func():
assert get_collection_name_from_path() == my_collection_name


class MockPopen:
def __init__(self):
self.returncode = 0

@staticmethod
def communicate():
return b"output log", b"error log"


def mocked_Popen(*args, **kwargs):
return MockPopen()


@pytest.fixture
def test_start_daemon_from_module(monkeypatch):
mocked_Popen = Mock()
monkeypatch.setattr(subprocess, "Popen", mocked_Popen)
turbo_socket = turbo_common.AnsibleTurboSocket(socket_path="/aa")
assert turbo_socket.start_server()
running, error = turbo_socket.start_server()
assert running
mocked_Popen.assert_called_once_with(
[
ANY,
Expand All @@ -54,16 +68,19 @@ def test_start_daemon_from_module(monkeypatch):
],
env=ANY,
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)


@pytest.fixture
def test_start_daemon_from_lookup(monkeypatch):
mocked_Popen = Mock()
monkeypatch.setattr(subprocess, "Popen", mocked_Popen)
turbo_socket = turbo_common.AnsibleTurboSocket(
socket_path="/aa", plugin="lookup", ttl=150
)
assert turbo_socket.start_server()
running, error = turbo_socket.start_server()
assert running
mocked_Popen.assert_called_once_with(
[
ANY,
Expand All @@ -76,6 +93,8 @@ def test_start_daemon_from_lookup(monkeypatch):
],
env=ANY,
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)


Expand Down

0 comments on commit fbe3768

Please sign in to comment.