| 1 |
from unittest.mock import ( |
| 2 |
patch, Mock |
| 3 |
) |
| 4 |
from pytest import raises |
| 5 |
|
| 6 |
from kiwi.boxbuild.box_container_build import BoxContainerBuild |
| 7 |
from kiwi.exceptions import KiwiBoxBuildError |
| 8 |
|
| 9 |
|
| 10 |
class TestBoxContainerBuild: |
| 11 |
@patch('kiwi.boxbuild.box_container_build.BoxDownload') |
| 12 |
def setup(self, mock_BoxDownload): |
| 13 |
self.box = Mock() |
| 14 |
self.box.fetch_container.return_value = 'some' |
| 15 |
mock_BoxDownload.return_value = self.box |
| 16 |
self.build = BoxContainerBuild( |
| 17 |
boxname='suse', arch='x86_64' |
| 18 |
) |
| 19 |
|
| 20 |
@patch('kiwi.boxbuild.box_container_build.BoxDownload') |
| 21 |
def setup_method(self, cls, mock_BoxDownload): |
| 22 |
self.setup() |
| 23 |
|
| 24 |
@patch('os.path.exists') |
| 25 |
@patch('os.environ') |
| 26 |
@patch('os.system') |
| 27 |
@patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir') |
| 28 |
def test_run_shared_path_does_not_exist( |
| 29 |
self, mock_create_build_target_dir, mock_os_system, |
| 30 |
mock_os_environ, mock_os_path_exists |
| 31 |
): |
| 32 |
def exists(path): |
| 33 |
if path.endswith('/var/tmp/repos'): |
| 34 |
return False |
| 35 |
return True |
| 36 |
|
| 37 |
mock_os_path_exists.side_effect = exists |
| 38 |
with raises(KiwiBoxBuildError): |
| 39 |
self.build.run( |
| 40 |
[ |
| 41 |
'--debug', '--type', 'oem', 'system', 'build', |
| 42 |
'--description', 'desc', '--target-dir', 'target' |
| 43 |
], |
| 44 |
keep_open=True, |
| 45 |
kiwi_version='9.22.1', |
| 46 |
custom_shared_path='/var/tmp/repos' |
| 47 |
) |
| 48 |
|
| 49 |
@patch('os.path.exists') |
| 50 |
@patch('os.environ') |
| 51 |
@patch('os.system') |
| 52 |
@patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir') |
| 53 |
def test_run_image_description_does_not_exist( |
| 54 |
self, mock_create_build_target_dir, mock_os_system, |
| 55 |
mock_os_environ, mock_os_path_exists |
| 56 |
): |
| 57 |
def exists(path): |
| 58 |
if path.endswith('desc'): |
| 59 |
return False |
| 60 |
return True |
| 61 |
|
| 62 |
mock_os_path_exists.side_effect = exists |
| 63 |
with raises(KiwiBoxBuildError): |
| 64 |
self.build.run( |
| 65 |
[ |
| 66 |
'--debug', '--type', 'oem', 'system', 'build', |
| 67 |
'--description', 'desc', '--target-dir', 'target' |
| 68 |
], |
| 69 |
keep_open=True, |
| 70 |
kiwi_version='9.22.1', |
| 71 |
custom_shared_path='/var/tmp/repos' |
| 72 |
) |
| 73 |
|
| 74 |
@patch('os.path.isdir') |
| 75 |
@patch('os.path.exists') |
| 76 |
@patch('os.environ') |
| 77 |
@patch('os.system') |
| 78 |
@patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir') |
| 79 |
@patch('kiwi.boxbuild.box_container_build.NamedTemporaryFile') |
| 80 |
def test_run_build_failed( |
| 81 |
self, mock_NamedTemporaryFile, mock_create_build_target_dir, |
| 82 |
mock_os_system, mock_os_environ, mock_os_path_exists, |
| 83 |
mock_os_path_isdir |
| 84 |
): |
| 85 |
def exists(path): |
| 86 |
if path.endswith('target/result.code'): |
| 87 |
return True |
| 88 |
if path.endswith('/var/tmp/repos'): |
| 89 |
return True |
| 90 |
if path.endswith('desc'): |
| 91 |
return True |
| 92 |
return False |
| 93 |
|
| 94 |
mock_os_path_isdir.return_value = True |
| 95 |
mock_os_path_exists.side_effect = exists |
| 96 |
tmpfile = Mock() |
| 97 |
tmpfile.name = 'tmpfile' |
| 98 |
mock_NamedTemporaryFile.return_value = tmpfile |
| 99 |
with patch('builtins.open', create=True) as mock_open: |
| 100 |
file_handle = mock_open.return_value.__enter__.return_value |
| 101 |
file_handle.readline.return_value = '1' |
| 102 |
with raises(KiwiBoxBuildError): |
| 103 |
self.build.run( |
| 104 |
[ |
| 105 |
'--debug', '--type', 'oem', 'system', 'build', |
| 106 |
'--description', 'desc', '--target-dir', 'target' |
| 107 |
], |
| 108 |
keep_open=True, |
| 109 |
kiwi_version='9.22.1', |
| 110 |
custom_shared_path='/var/tmp/repos' |
| 111 |
) |
| 112 |
|
| 113 |
@patch('os.path.isdir') |
| 114 |
@patch('os.path.abspath') |
| 115 |
@patch('os.path.exists') |
| 116 |
@patch('os.environ') |
| 117 |
@patch('os.system') |
| 118 |
@patch('kiwi.boxbuild.box_container_build.NamedTemporaryFile') |
| 119 |
@patch('kiwi.boxbuild.box_container_build.Command.run') |
| 120 |
def test_run( |
| 121 |
self, mock_Command_run, mock_NamedTemporaryFile, |
| 122 |
mock_os_system, mock_os_environ, mock_os_path_exists, |
| 123 |
mock_os_abspath, mock_os_path_isdir |
| 124 |
): |
| 125 |
def abspath(path): |
| 126 |
return path |
| 127 |
|
| 128 |
def exists(path): |
| 129 |
if path == 'target/result.code': |
| 130 |
return True |
| 131 |
if path == '/var/tmp/repos': |
| 132 |
return True |
| 133 |
if path == 'desc': |
| 134 |
return True |
| 135 |
return False |
| 136 |
|
| 137 |
mock_os_path_isdir.return_value = False |
| 138 |
mock_os_abspath.side_effect = abspath |
| 139 |
mock_os_path_exists.side_effect = exists |
| 140 |
tmpfile = Mock() |
| 141 |
tmpfile.name = 'tmpfile' |
| 142 |
mock_NamedTemporaryFile.return_value = tmpfile |
| 143 |
with patch('builtins.open', create=True) as mock_open: |
| 144 |
file_handle = mock_open.return_value.__enter__.return_value |
| 145 |
file_handle.readline.return_value = '0' |
| 146 |
self.build.run( |
| 147 |
[ |
| 148 |
'--debug', '--type', 'oem', 'system', 'build', |
| 149 |
'--description', 'desc', '--target-dir', 'target' |
| 150 |
], |
| 151 |
keep_open=True, |
| 152 |
kiwi_version='9.22.1', |
| 153 |
custom_shared_path='/var/tmp/repos' |
| 154 |
) |
| 155 |
mock_Command_run.assert_called_once_with( |
| 156 |
['sudo', 'mkdir', '-p', '/var/cache/kiwi'] |
| 157 |
) |
| 158 |
mock_os_system.assert_called_once_with( |
| 159 |
'sudo podman run --rm -ti ' |
| 160 |
'--privileged ' |
| 161 |
'--net host ' |
| 162 |
'--cap-add AUDIT_WRITE ' |
| 163 |
'--cap-add AUDIT_CONTROL ' |
| 164 |
'--cap-add CAP_MKNOD ' |
| 165 |
'--cap-add CAP_SYS_ADMIN ' |
| 166 |
'--volume /var/cache/kiwi:/var/cache/kiwi ' |
| 167 |
'--volume tmpfile:/container.cmdline ' |
| 168 |
'--volume target:/bundle ' |
| 169 |
'--volume desc:/description ' |
| 170 |
'--volume /dev:/dev ' |
| 171 |
'--volume /var/tmp/repos:/var/tmp/repos ' |
| 172 |
'some' |
| 173 |
) |