| 1 |
import sys |
| 2 |
from pytest import raises |
| 3 |
from unittest.mock import ( |
| 4 |
Mock, patch, call |
| 5 |
) |
| 6 |
|
| 7 |
from ..test_helper import argv_kiwi_tests |
| 8 |
|
| 9 |
from kiwi.tasks.system_stackbuild import SystemStackbuildTask |
| 10 |
from kiwi.exceptions import ( |
| 11 |
KiwiStackBuildTargetDirExists, |
| 12 |
KiwiStackBuildRootSyncFailed |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
class TestSystemStackbuildTask: |
| 17 |
def setup(self): |
| 18 |
sys.argv = [ |
| 19 |
sys.argv[0], '--profile', 'a', '--profile', 'b', |
| 20 |
'--type', 'iso', 'system', 'stackbuild', |
| 21 |
'--stash', 'name', '--target-dir', 'some-target-dir', 'kiwi', |
| 22 |
'--signing-key', 'some-key' |
| 23 |
] |
| 24 |
self.task = SystemStackbuildTask() |
| 25 |
|
| 26 |
def setup_method(self, cls): |
| 27 |
self.setup() |
| 28 |
|
| 29 |
def teardown(self): |
| 30 |
sys.argv = argv_kiwi_tests |
| 31 |
|
| 32 |
def teardown_method(self, cls): |
| 33 |
self.teardown() |
| 34 |
|
| 35 |
def _init_command_args(self): |
| 36 |
self.task.command_args = {} |
| 37 |
self.task.command_args['help'] = False |
| 38 |
self.task.command_args['stackbuild'] = False |
| 39 |
self.task.command_args['--stash'] = [] |
| 40 |
self.task.command_args['--from-registry'] = None |
| 41 |
self.task.command_args['--target-dir'] = None |
| 42 |
self.task.command_args['--description'] = None |
| 43 |
self.task.command_args['system_build_or_create'] = [ |
| 44 |
'--signing-key', 'some-key' |
| 45 |
] |
| 46 |
|
| 47 |
@patch('kiwi.tasks.system_stackbuild.Help') |
| 48 |
def test_process_help(self, mock_Help): |
| 49 |
Help = Mock() |
| 50 |
mock_Help.return_value = Help |
| 51 |
self._init_command_args() |
| 52 |
self.task.command_args['help'] = True |
| 53 |
self.task.command_args['stackbuild'] = True |
| 54 |
self.task.process() |
| 55 |
Help.show.assert_called_once_with( |
| 56 |
'kiwi::system::stackbuild' |
| 57 |
) |
| 58 |
|
| 59 |
@patch('kiwi.tasks.system_stackbuild.Privileges') |
| 60 |
@patch('os.path.exists') |
| 61 |
def test_process_target_dir_exists( |
| 62 |
self, mock_os_path_exists, mock_Privileges |
| 63 |
): |
| 64 |
self._init_command_args() |
| 65 |
self.task.command_args['stackbuild'] = True |
| 66 |
self.task.command_args['--stash'] = ['name'] |
| 67 |
self.task.command_args['--target-dir'] = '/some/target-dir' |
| 68 |
mock_os_path_exists.return_value = True |
| 69 |
with raises(KiwiStackBuildTargetDirExists): |
| 70 |
self.task.process() |
| 71 |
|
| 72 |
@patch('kiwi.tasks.system_stackbuild.DataSync') |
| 73 |
@patch('kiwi.tasks.system_stackbuild.Privileges') |
| 74 |
@patch('kiwi.tasks.system_stackbuild.Path.create') |
| 75 |
@patch('kiwi.tasks.system_stackbuild.Command.run') |
| 76 |
@patch('kiwi.tasks.system_stackbuild.SystemCreateTask') |
| 77 |
@patch('os.path.exists') |
| 78 |
def test_process_root_sync_failed( |
| 79 |
self, mock_os_path_exists, mock_SystemCreateTask, mock_Command_run, |
| 80 |
mock_Path_create, mock_Privileges, mock_DataSync |
| 81 |
): |
| 82 |
self._init_command_args() |
| 83 |
self.task.command_args['stackbuild'] = True |
| 84 |
self.task.command_args['--stash'] = ['name'] |
| 85 |
self.task.command_args['--target-dir'] = '/some/target-dir' |
| 86 |
mock_os_path_exists.return_value = False |
| 87 |
|
| 88 |
mock_DataSync.side_effect = Exception |
| 89 |
|
| 90 |
kiwi_task = Mock() |
| 91 |
mock_SystemCreateTask.return_value = kiwi_task |
| 92 |
with raises(KiwiStackBuildRootSyncFailed): |
| 93 |
self.task.process() |
| 94 |
|
| 95 |
@patch('kiwi.tasks.system_stackbuild.Privileges') |
| 96 |
@patch('kiwi.tasks.system_stackbuild.Path.create') |
| 97 |
@patch('kiwi.tasks.system_stackbuild.Command.run') |
| 98 |
@patch('kiwi.tasks.system_stackbuild.SystemCreateTask') |
| 99 |
@patch('os.path.exists') |
| 100 |
@patch('kiwi.tasks.system_stackbuild.patch.object') |
| 101 |
def test_process_rebuild_typer_commandline( |
| 102 |
self, mock_patch_object, mock_os_path_exists, |
| 103 |
mock_SystemCreateTask, mock_Command_run, |
| 104 |
mock_Path_create, mock_Privileges |
| 105 |
): |
| 106 |
self._init_command_args() |
| 107 |
self.task.command_args['stackbuild'] = True |
| 108 |
self.task.command_args['--stash'] = ['name'] |
| 109 |
self.task.command_args['--target-dir'] = '/some/target-dir' |
| 110 |
self.task.command_args['--from-registry'] = 'registry.uri' |
| 111 |
mock_os_path_exists.return_value = False |
| 112 |
mock_Command_run.return_value.output = '/podman/mount/path' |
| 113 |
kiwi_task = Mock() |
| 114 |
mock_SystemCreateTask.return_value = kiwi_task |
| 115 |
self.task.process() |
| 116 |
assert mock_Command_run.call_args_list == [ |
| 117 |
call(['podman', 'pull', 'registry.uri/name']), |
| 118 |
call(['podman', 'image', 'mount', 'name']), |
| 119 |
call( |
| 120 |
[ |
| 121 |
'rsync', '--archive', '--hard-links', '--xattrs', |
| 122 |
'--acls', '--one-file-system', '--inplace', |
| 123 |
'/podman/mount/path/', |
| 124 |
'/some/target-dir/build/image-root' |
| 125 |
] |
| 126 |
), |
| 127 |
call( |
| 128 |
['podman', 'image', 'umount', '--force', 'name'], |
| 129 |
raise_on_error=False |
| 130 |
) |
| 131 |
] |
| 132 |
mock_SystemCreateTask.assert_called_once_with( |
| 133 |
should_perform_task_setup=False |
| 134 |
) |
| 135 |
kiwi_task.process.assert_called_once_with() |
| 136 |
mock_patch_object.assert_called_once_with( |
| 137 |
sys, 'argv', [ |
| 138 |
'kiwi-ng', '--type', 'iso', |
| 139 |
'--profile', 'a', '--profile', 'b', |
| 140 |
'system', 'create', |
| 141 |
'--root', '/some/target-dir/build/image-root', |
| 142 |
'--target-dir', '/some/target-dir', |
| 143 |
'--signing-key', 'some-key' |
| 144 |
] |
| 145 |
) |
| 146 |
|
| 147 |
@patch('kiwi.tasks.system_stackbuild.Privileges') |
| 148 |
@patch('kiwi.tasks.system_stackbuild.Path.create') |
| 149 |
@patch('kiwi.tasks.system_stackbuild.Command.run') |
| 150 |
@patch('kiwi.tasks.system_stackbuild.SystemBuildTask') |
| 151 |
@patch('os.path.exists') |
| 152 |
@patch('kiwi.tasks.system_stackbuild.patch.object') |
| 153 |
def test_process_new_build_typer_commandline( |
| 154 |
self, mock_patch_object, mock_os_path_exists, |
| 155 |
mock_SystemBuildTask, mock_Command_run, |
| 156 |
mock_Path_create, mock_Privileges |
| 157 |
): |
| 158 |
self._init_command_args() |
| 159 |
self.task.command_args['stackbuild'] = True |
| 160 |
self.task.command_args['--stash'] = ['name'] |
| 161 |
self.task.command_args['--target-dir'] = '/some/target-dir' |
| 162 |
self.task.command_args['--description'] = '/path/to/kiwi/description' |
| 163 |
self.task.command_args['--from-registry'] = 'registry.uri' |
| 164 |
mock_os_path_exists.return_value = False |
| 165 |
mock_Command_run.return_value.output = '/podman/mount/path' |
| 166 |
kiwi_task = Mock() |
| 167 |
mock_SystemBuildTask.return_value = kiwi_task |
| 168 |
self.task.process() |
| 169 |
assert mock_Command_run.call_args_list == [ |
| 170 |
call(['podman', 'pull', 'registry.uri/name']), |
| 171 |
call(['podman', 'image', 'mount', 'name']), |
| 172 |
call( |
| 173 |
[ |
| 174 |
'rsync', '--archive', '--hard-links', '--xattrs', |
| 175 |
'--acls', '--one-file-system', '--inplace', |
| 176 |
'/podman/mount/path/', |
| 177 |
'/some/target-dir/build/image-root' |
| 178 |
] |
| 179 |
), |
| 180 |
call( |
| 181 |
['podman', 'image', 'umount', '--force', 'name'], |
| 182 |
raise_on_error=False |
| 183 |
) |
| 184 |
] |
| 185 |
mock_SystemBuildTask.assert_called_once_with( |
| 186 |
should_perform_task_setup=False |
| 187 |
) |
| 188 |
kiwi_task.process.assert_called_once_with() |
| 189 |
mock_patch_object.assert_called_once_with( |
| 190 |
sys, 'argv', [ |
| 191 |
'kiwi-ng', '--type', 'iso', |
| 192 |
'--profile', 'a', '--profile', 'b', |
| 193 |
'system', 'build', |
| 194 |
'--description', '/path/to/kiwi/description', |
| 195 |
'--target-dir', '/some/target-dir', |
| 196 |
'--allow-existing-root', |
| 197 |
'--signing-key', 'some-key' |
| 198 |
] |
| 199 |
) |
| 200 |
|
| 201 |
@patch('kiwi.tasks.system_stackbuild.Privileges') |
| 202 |
@patch('kiwi.tasks.system_stackbuild.Path.create') |
| 203 |
@patch('kiwi.tasks.system_stackbuild.Command.run') |
| 204 |
@patch('kiwi.tasks.system_stackbuild.DataSync') |
| 205 |
@patch('kiwi.tasks.system_stackbuild.SystemBuildTask') |
| 206 |
@patch('os.path.exists') |
| 207 |
@patch('kiwi.tasks.system_stackbuild.patch.object') |
| 208 |
def test_process_new_build_no_kiwi_subcommand( |
| 209 |
self, mock_patch_object, mock_os_path_exists, |
| 210 |
mock_SystemBuildTask, mock_DataSync, mock_Command_run, |
| 211 |
mock_Path_create, mock_Privileges |
| 212 |
): |
| 213 |
self._init_command_args() |
| 214 |
self.task.global_args = { |
| 215 |
'--kiwi-file': 'appliance.kiwi' |
| 216 |
} |
| 217 |
self.task.command_args['system_build_or_create'] = None |
| 218 |
self.task.command_args['--stash'] = ['base', 'layer'] |
| 219 |
self.task.command_args['--target-dir'] = '/some/target-dir' |
| 220 |
self.task.command_args['--description'] = '/path/to/kiwi/description' |
| 221 |
mock_os_path_exists.return_value = False |
| 222 |
mock_Command_run.return_value.output = '/podman/mount/path' |
| 223 |
self.task.process() |
| 224 |
assert mock_Command_run.call_args_list == [ |
| 225 |
call(['podman', 'image', 'mount', 'base']), |
| 226 |
call( |
| 227 |
['podman', 'image', 'umount', '--force', 'base'], |
| 228 |
raise_on_error=False |
| 229 |
), |
| 230 |
call(['podman', 'image', 'mount', 'layer']), |
| 231 |
call( |
| 232 |
['podman', 'image', 'umount', '--force', 'layer'], |
| 233 |
raise_on_error=False |
| 234 |
) |
| 235 |
] |
| 236 |
assert mock_DataSync.return_value.sync_data.call_count == 2 |
| 237 |
mock_patch_object.assert_called_once_with( |
| 238 |
sys, 'argv', [ |
| 239 |
'kiwi-ng', '--kiwi-file', 'appliance.kiwi', |
| 240 |
'system', 'build', |
| 241 |
'--description', '/path/to/kiwi/description', |
| 242 |
'--target-dir', '/some/target-dir', |
| 243 |
'--allow-existing-root' |
| 244 |
] |
| 245 |
) |
| 246 |
mock_SystemBuildTask.return_value.process.assert_called_once_with() |