| 1 |
import logging |
| 2 |
from unittest.mock import ( |
| 3 |
patch, call |
| 4 |
) |
| 5 |
from pytest import ( |
| 6 |
raises, fixture |
| 7 |
) |
| 8 |
from collections import namedtuple |
| 9 |
|
| 10 |
from kiwi.utils.command_capabilities import CommandCapabilities |
| 11 |
|
| 12 |
from kiwi.exceptions import KiwiCommandCapabilitiesError |
| 13 |
|
| 14 |
|
| 15 |
class TestCommandCapabilities: |
| 16 |
@fixture(autouse=True) |
| 17 |
def inject_fixtures(self, caplog): |
| 18 |
self._caplog = caplog |
| 19 |
|
| 20 |
@patch('kiwi.command.Command.run') |
| 21 |
@patch('kiwi.utils.command_capabilities.Path.which') |
| 22 |
def test_has_option_in_help(self, mock_Path_which, mock_run): |
| 23 |
command_type = namedtuple('command', ['output', 'error']) |
| 24 |
mock_run.return_value = command_type( |
| 25 |
output="Dummy line\n\t--some-flag\n\t--some-other-flag", |
| 26 |
error="Dummy line\n\t--error-flag\n\t--some-other-flag" |
| 27 |
) |
| 28 |
assert CommandCapabilities.has_option_in_help( |
| 29 |
'command', '--some-flag' |
| 30 |
) is True |
| 31 |
assert CommandCapabilities.has_option_in_help( |
| 32 |
'command', '--error-flag' |
| 33 |
) is True |
| 34 |
assert CommandCapabilities.has_option_in_help( |
| 35 |
'command', '--some-flag', help_flags=['subcommand', '-h'] |
| 36 |
) is True |
| 37 |
assert CommandCapabilities.has_option_in_help( |
| 38 |
'command', '--some-other-flag', |
| 39 |
help_flags=['subcommand', '-h'], root='root_dir' |
| 40 |
) is True |
| 41 |
assert CommandCapabilities.has_option_in_help( |
| 42 |
'command', '--non-existing-flag', raise_on_error=False |
| 43 |
) is False |
| 44 |
mock_run.assert_has_calls( |
| 45 |
[ |
| 46 |
call(['command', '--help'], raise_on_error=False), |
| 47 |
call(['command', '--help'], raise_on_error=False), |
| 48 |
call(['command', 'subcommand', '-h'], raise_on_error=False), |
| 49 |
call( |
| 50 |
['chroot', 'root_dir', 'command', 'subcommand', '-h'], |
| 51 |
raise_on_error=False |
| 52 |
), |
| 53 |
call(['command', '--help'], raise_on_error=False) |
| 54 |
] |
| 55 |
) |
| 56 |
|
| 57 |
@patch('kiwi.command.Command.run') |
| 58 |
@patch('kiwi.utils.command_capabilities.Path.which') |
| 59 |
def test_has_option_in_help_command_failure_warning( |
| 60 |
self, mock_Path_which, mock_run |
| 61 |
): |
| 62 |
mock_run.return_value.output = '' |
| 63 |
mock_run.return_value.error = '' |
| 64 |
with self._caplog.at_level(logging.WARNING): |
| 65 |
CommandCapabilities.has_option_in_help( |
| 66 |
'command_that_fails', '--non-existing-flag', |
| 67 |
raise_on_error=False |
| 68 |
) |
| 69 |
|
| 70 |
@patch('kiwi.command.Command.run') |
| 71 |
@patch('kiwi.utils.command_capabilities.Path.which') |
| 72 |
def test_has_option_in_help_command_failure_exception( |
| 73 |
self, mock_Path_which, mock_run |
| 74 |
): |
| 75 |
mock_run.return_value.output = '' |
| 76 |
mock_run.return_value.error = '' |
| 77 |
with raises(KiwiCommandCapabilitiesError): |
| 78 |
CommandCapabilities.has_option_in_help( |
| 79 |
'command_that_fails', '--non-existing-flag' |
| 80 |
) |
| 81 |
|
| 82 |
@patch('kiwi.command.Command.run') |
| 83 |
@patch('kiwi.utils.command_capabilities.Path.which') |
| 84 |
def test_has_option_in_help_command_failure_command_not_found_exception( |
| 85 |
self, mock_Path_which, mock_run |
| 86 |
): |
| 87 |
mock_Path_which.return_value = False |
| 88 |
with raises(KiwiCommandCapabilitiesError): |
| 89 |
CommandCapabilities.has_option_in_help( |
| 90 |
'command_that_does_not_exist', '--help' |
| 91 |
) |
| 92 |
|
| 93 |
@patch('kiwi.command.Command.run') |
| 94 |
def test_check_version(self, mock_run): |
| 95 |
command_type = namedtuple('command', ['output']) |
| 96 |
mock_run.return_value = command_type( |
| 97 |
output="Dummy line\ncommand v1.2.3\n" |
| 98 |
) |
| 99 |
assert CommandCapabilities.check_version('command', (1, 2, 3)) |
| 100 |
assert CommandCapabilities.check_version('command', (1, 1, 3)) |
| 101 |
assert not CommandCapabilities.check_version('command', (1, 3)) |
| 102 |
assert CommandCapabilities.check_version( |
| 103 |
'command', (1, 2, 3), version_flags=['-v'] |
| 104 |
) |
| 105 |
assert CommandCapabilities.check_version( |
| 106 |
'command', (1, 2, 3), version_flags=['-v'], root='root_dir' |
| 107 |
) |
| 108 |
mock_run.assert_has_calls([ |
| 109 |
call(['command', '--version']), |
| 110 |
call(['command', '--version']), |
| 111 |
call(['command', '--version']), |
| 112 |
call(['command', '-v']), |
| 113 |
call(['chroot', 'root_dir', 'command', '-v']) |
| 114 |
]) |
| 115 |
|
| 116 |
@patch('kiwi.command.Command.run') |
| 117 |
def test_check_version_complex_pattern(self, mock_run): |
| 118 |
command_type = namedtuple('command', ['output']) |
| 119 |
mock_run.return_value = command_type( |
| 120 |
output="grub2-mkconfig (GRUB2) 2.02\n" |
| 121 |
) |
| 122 |
assert CommandCapabilities.check_version('command', (2, 2)) is True |
| 123 |
assert CommandCapabilities.check_version('command', (2, 4)) is False |
| 124 |
|
| 125 |
@patch('kiwi.command.Command.run') |
| 126 |
def test_check_version_no_match(self, mock_run): |
| 127 |
command_type = namedtuple('command', ['output']) |
| 128 |
mock_run.return_value = command_type( |
| 129 |
output="Dummy line\ncommand someother stuff\n" |
| 130 |
) |
| 131 |
with raises(KiwiCommandCapabilitiesError): |
| 132 |
CommandCapabilities.check_version('command', (1, 2, 3)) |
| 133 |
|
| 134 |
@patch('kiwi.command.Command.run') |
| 135 |
def test_check_version_failure_warning(self, mock_run): |
| 136 |
def side_effect(): |
| 137 |
raise Exception("Something went wrong") |
| 138 |
|
| 139 |
mock_run.side_effect = side_effect |
| 140 |
with self._caplog.at_level(logging.WARNING): |
| 141 |
CommandCapabilities.check_version( |
| 142 |
'command_that_fails', (1, 2), raise_on_error=False |
| 143 |
) |
| 144 |
|
| 145 |
@patch('kiwi.command.Command.run') |
| 146 |
def test_check_version_failure_exception(self, mock_run): |
| 147 |
def side_effect(): |
| 148 |
raise Exception("Something went wrong") |
| 149 |
|
| 150 |
mock_run.side_effect = side_effect |
| 151 |
with raises(KiwiCommandCapabilitiesError): |
| 152 |
CommandCapabilities.check_version( |
| 153 |
'command_that_fails', '--non-existing-flag' |
| 154 |
) |