Skip to content

Commit

Permalink
Enhance run_command function
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Aug 17, 2023
1 parent 7eb0bf0 commit 632cf50
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
22 changes: 15 additions & 7 deletions clean_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,23 @@ def check_name(image: str) -> bool:

def run_command(command: list) -> int:
'''Run command'''
with subprocess.Popen(
try:
with subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
) as proc:
if proc.stdout is not None:
for line in proc.stdout:
logging.info(line.decode('utf-8').rstrip())
return proc.wait()
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1, # Line-buffered
shell=False,
) as process:
if process.stdout is not None:
for line in process.stdout:
logging.info(line.rstrip())
exit_code = process.returncode
except subprocess.CalledProcessError as exc:
logging.error("%s", exc)
exit_code = exc.returncode
return exit_code


def clean_registrydir(images: list[str], dry_run: bool = False) -> None:
Expand Down
62 changes: 45 additions & 17 deletions tests/test_clean_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,58 @@ def test_is_container(test_case, monkeypatch):
assert is_container() == test_case['expected_result']


class MockPopen:
def __init__(self, *args, **kwargs):
pass
def test_run_command_success(mocker, caplog):
caplog.set_level(logging.INFO)
process_mock = mocker.MagicMock()
process_mock.__enter__.return_value.stdout = ['stdout_line1\n', 'stdout_line2\n']
process_mock.__enter__.return_value.returncode = 0

def __enter__(self):
return self
mocker.patch('subprocess.Popen', return_value=process_mock)

def __exit__(self, *args, **kwargs):
pass
exit_code = run_command(['some_command'])
assert exit_code == 0

def wait(self):
return 0
assert 'stdout_line1' in caplog.text

@property
def stdout(self):
return [b'Output Line 1\n', b'Output Line 2\n']

def test_run_command_failure(mocker, caplog):
caplog.set_level(logging.INFO)
process_mock = mocker.MagicMock()
process_mock.__enter__.return_value.stdout = ['stderr_line1\n', 'stderr_line2\n']
process_mock.__enter__.return_value.returncode = 1

def test_run_command(monkeypatch):
monkeypatch.setattr(subprocess, 'Popen', MockPopen)
command = ['dummy']
result = run_command(command)
mocker.patch('subprocess.Popen', return_value=process_mock)

assert result == 0
exit_code = run_command(['some_command'])
assert exit_code == 1

assert 'stderr_line1' in caplog.text


def test_run_command_no_output(mocker, caplog):
caplog.set_level(logging.INFO)
process_mock = mocker.MagicMock()
process_mock.__enter__.return_value.stdout = []
process_mock.__enter__.return_value.stderr = []
process_mock.__enter__.return_value.returncode = 0

mocker.patch('subprocess.Popen', return_value=process_mock)

exit_code = run_command(['some_command'])
assert exit_code == 0

assert not caplog.text


def test_run_command_error(mocker, caplog):
caplog.set_level(logging.INFO)
process_mock = mocker.MagicMock()
process_mock.__enter__.side_effect = subprocess.CalledProcessError(2, 'some_command')

mocker.patch('subprocess.Popen', return_value=process_mock)

exit_code = run_command(['some_command'])
assert exit_code == 2


@pytest.fixture
Expand Down

0 comments on commit 632cf50

Please sign in to comment.