| 1 |
import sys |
| 2 |
import os |
| 3 |
from pytest import raises |
| 4 |
from unittest.mock import ( |
| 5 |
Mock, patch |
| 6 |
) |
| 7 |
|
| 8 |
from ..test_helper import argv_kiwi_tests |
| 9 |
|
| 10 |
from kiwi.exceptions import KiwiBoxBuildError |
| 11 |
from kiwi.tasks.system_boxbuild import SystemBoxbuildTask |
| 12 |
|
| 13 |
|
| 14 |
class TestSystemBoxbuildTask: |
| 15 |
def setup(self): |
| 16 |
sys.argv = [ |
| 17 |
sys.argv[0], |
| 18 |
'--debug', |
| 19 |
'--profile', 'foo', |
| 20 |
'--type', 'oem', |
| 21 |
'--kiwi-file', 'appliance.kiwi', |
| 22 |
'system', 'boxbuild', |
| 23 |
'--box', 'universal', |
| 24 |
'--box-memory', '4', |
| 25 |
'--box-console', 'ttyAMA0', |
| 26 |
'--box-smp-cpus', '4', |
| 27 |
'kiwi', |
| 28 |
'--description', '../data/description', |
| 29 |
'--target-dir', '../data/target_dir' |
| 30 |
] |
| 31 |
self.task = SystemBoxbuildTask() |
| 32 |
|
| 33 |
def setup_method(self, cls): |
| 34 |
self.setup() |
| 35 |
|
| 36 |
def teardown(self): |
| 37 |
sys.argv = argv_kiwi_tests |
| 38 |
|
| 39 |
def teardown_method(self, cls): |
| 40 |
self.teardown() |
| 41 |
|
| 42 |
def _init_command_args(self): |
| 43 |
self.task.command_args = {} |
| 44 |
self.task.command_args['help'] = False |
| 45 |
self.task.command_args['boxbuild'] = False |
| 46 |
self.task.command_args['--list-boxes'] = False |
| 47 |
self.task.command_args['--container'] = False |
| 48 |
self.task.command_args['--box'] = None |
| 49 |
self.task.command_args['--box-debug'] = False |
| 50 |
self.task.command_args['--kiwi-version'] = None |
| 51 |
self.task.command_args['--shared-path'] = None |
| 52 |
self.task.command_args['--9p-sharing'] = None |
| 53 |
self.task.command_args['--sshfs-sharing'] = None |
| 54 |
self.task.command_args['--ssh-port'] = '22' |
| 55 |
self.task.command_args['--virtiofs-sharing'] = None |
| 56 |
self.task.command_args['--cpu'] = None |
| 57 |
self.task.command_args['--machine'] = None |
| 58 |
self.task.command_args['system_build'] = [ |
| 59 |
'--description', 'foo', |
| 60 |
'--target-dir', 'xxx', |
| 61 |
'--add-package', 'a', '--add-package', 'b', |
| 62 |
'--allow-existing-root' |
| 63 |
] |
| 64 |
|
| 65 |
@patch('kiwi.tasks.system_boxbuild.Help') |
| 66 |
def test_process_system_boxbuild_help(self, mock_kiwi_Help): |
| 67 |
Help = Mock() |
| 68 |
mock_kiwi_Help.return_value = Help |
| 69 |
self._init_command_args() |
| 70 |
self.task.command_args['help'] = True |
| 71 |
self.task.command_args['boxbuild'] = True |
| 72 |
self.task.process() |
| 73 |
Help.show.assert_called_once_with( |
| 74 |
'kiwi::system::boxbuild' |
| 75 |
) |
| 76 |
|
| 77 |
@patch('kiwi.tasks.system_boxbuild.BoxesConfig') |
| 78 |
def test_process_system_boxbuild_list_boxes(self, mock_BoxesConfig): |
| 79 |
plugin = Mock() |
| 80 |
mock_BoxesConfig.return_value = plugin |
| 81 |
self._init_command_args() |
| 82 |
self.task.command_args['boxbuild'] = True |
| 83 |
self.task.command_args['--list-boxes'] = True |
| 84 |
self.task.process() |
| 85 |
plugin.dump_config.assert_called_once_with() |
| 86 |
|
| 87 |
@patch('kiwi.tasks.system_boxbuild.BoxContainerBuild') |
| 88 |
def test_process_system_boxbuild_container(self, mock_BoxContainerBuild): |
| 89 |
self._init_command_args() |
| 90 |
self.task.command_args['boxbuild'] = True |
| 91 |
self.task.command_args['--box'] = 'universal' |
| 92 |
self.task.command_args['--container'] = True |
| 93 |
box_containerbuild = Mock() |
| 94 |
mock_BoxContainerBuild.return_value = box_containerbuild |
| 95 |
self.task.process() |
| 96 |
mock_BoxContainerBuild.assert_called_once_with( |
| 97 |
boxname='universal', arch='' |
| 98 |
) |
| 99 |
box_containerbuild.run.assert_called_once_with( |
| 100 |
[ |
| 101 |
'--debug', |
| 102 |
'--type', 'oem', |
| 103 |
'--profile', 'foo', |
| 104 |
'--kiwi-file', 'appliance.kiwi', |
| 105 |
'system', 'build', |
| 106 |
'--description', os.path.abspath(os.path.normpath('foo')), |
| 107 |
'--target-dir', os.path.abspath(os.path.normpath('xxx')), |
| 108 |
'--add-package', 'a', '--add-package', 'b', |
| 109 |
'--allow-existing-root' |
| 110 |
], False, None, None |
| 111 |
) |
| 112 |
|
| 113 |
@patch('kiwi.tasks.system_boxbuild.BoxBuild') |
| 114 |
def test_process_system_boxbuild_from_commandline(self, mock_BoxBuild): |
| 115 |
box_build = Mock() |
| 116 |
mock_BoxBuild.return_value = box_build |
| 117 |
self.task.process() |
| 118 |
mock_BoxBuild.assert_called_once_with( |
| 119 |
boxname='universal', ram='4', console='ttyAMA0', smp='4', |
| 120 |
arch='', machine=None, cpu='host', sharing_backend='9p', |
| 121 |
ssh_key='id_rsa', ssh_port='10022', accel=True |
| 122 |
) |
| 123 |
box_build.run.assert_called_once_with( |
| 124 |
[ |
| 125 |
'--debug', |
| 126 |
'--type', 'oem', |
| 127 |
'--profile', 'foo', |
| 128 |
'--kiwi-file', 'appliance.kiwi', |
| 129 |
'system', 'build', |
| 130 |
'--description', |
| 131 |
os.path.abspath(os.path.normpath('../data/description')), |
| 132 |
'--target-dir', |
| 133 |
os.path.abspath(os.path.normpath('../data/target_dir')) |
| 134 |
], True, True, False, None, None |
| 135 |
) |
| 136 |
|
| 137 |
def test_process_system_boxbuild_no_kiwi_build_command(self): |
| 138 |
sys.argv = [ |
| 139 |
sys.argv[0], 'system', 'boxbuild', '--box', 'universal' |
| 140 |
] |
| 141 |
task = SystemBoxbuildTask() |
| 142 |
with raises(KiwiBoxBuildError): |
| 143 |
task.process() |
| 144 |
|
| 145 |
@patch('kiwi.tasks.system_boxbuild.BoxBuild') |
| 146 |
def test_process_system_boxbuild(self, mock_BoxBuild): |
| 147 |
self._init_command_args() |
| 148 |
self.task.command_args['boxbuild'] = True |
| 149 |
self.task.command_args['--box'] = 'universal' |
| 150 |
box_build = Mock() |
| 151 |
mock_BoxBuild.return_value = box_build |
| 152 |
self.task.process() |
| 153 |
mock_BoxBuild.assert_called_once_with( |
| 154 |
boxname='universal', ram=None, console=None, smp=None, arch='', |
| 155 |
machine=None, cpu='host', sharing_backend='9p', |
| 156 |
ssh_key='id_rsa', ssh_port='22', accel=True |
| 157 |
) |
| 158 |
box_build.run.assert_called_once_with( |
| 159 |
[ |
| 160 |
'--debug', |
| 161 |
'--type', 'oem', |
| 162 |
'--profile', 'foo', |
| 163 |
'--kiwi-file', 'appliance.kiwi', |
| 164 |
'system', 'build', |
| 165 |
'--description', os.path.abspath(os.path.normpath('foo')), |
| 166 |
'--target-dir', os.path.abspath(os.path.normpath('xxx')), |
| 167 |
'--add-package', 'a', '--add-package', 'b', |
| 168 |
'--allow-existing-root' |
| 169 |
], True, True, False, None, None |
| 170 |
) |
| 171 |
|
| 172 |
@patch('kiwi.tasks.system_boxbuild.BoxBuild') |
| 173 |
def test_process_system_boxbuild_for_x86_64(self, mock_BoxBuild): |
| 174 |
self._init_command_args() |
| 175 |
self.task.command_args['boxbuild'] = True |
| 176 |
self.task.command_args['--box'] = 'universal' |
| 177 |
self.task.command_args['--x86_64'] = True |
| 178 |
box_build = Mock() |
| 179 |
mock_BoxBuild.return_value = box_build |
| 180 |
self.task.process() |
| 181 |
mock_BoxBuild.assert_called_once_with( |
| 182 |
boxname='universal', |
| 183 |
ram=None, console=None, smp=None, arch='x86_64', |
| 184 |
machine=None, cpu='host', sharing_backend='9p', |
| 185 |
ssh_key='id_rsa', ssh_port='22', accel=True |
| 186 |
) |
| 187 |
|
| 188 |
@patch('kiwi.tasks.system_boxbuild.BoxBuild') |
| 189 |
def test_process_system_boxbuild_for_aarch64(self, mock_BoxBuild): |
| 190 |
self._init_command_args() |
| 191 |
self.task.command_args['boxbuild'] = True |
| 192 |
self.task.command_args['--box'] = 'universal' |
| 193 |
self.task.command_args['--aarch64'] = True |
| 194 |
box_build = Mock() |
| 195 |
mock_BoxBuild.return_value = box_build |
| 196 |
self.task.process() |
| 197 |
mock_BoxBuild.assert_called_once_with( |
| 198 |
boxname='universal', |
| 199 |
ram=None, console=None, smp=None, arch='aarch64', |
| 200 |
machine=None, cpu='host', sharing_backend='9p', |
| 201 |
ssh_key='id_rsa', ssh_port='22', accel=True |
| 202 |
) |
| 203 |
|
| 204 |
@patch('kiwi.tasks.system_boxbuild.BoxBuild') |
| 205 |
def test_process_system_boxbuild_with_sharing_backend(self, mock_BoxBuild): |
| 206 |
self._init_command_args() |
| 207 |
self.task.command_args['boxbuild'] = True |
| 208 |
self.task.command_args['--box'] = 'universal' |
| 209 |
self.task.command_args['--9p-sharing'] = True |
| 210 |
box_build = Mock() |
| 211 |
mock_BoxBuild.return_value = box_build |
| 212 |
self.task.process() |
| 213 |
mock_BoxBuild.assert_called_once_with( |
| 214 |
boxname='universal', ram=None, console=None, smp=None, arch='', |
| 215 |
machine=None, cpu='host', sharing_backend='9p', |
| 216 |
ssh_key='id_rsa', ssh_port='22', accel=True |
| 217 |
) |
| 218 |
self.task.command_args['--9p-sharing'] = False |
| 219 |
self.task.command_args['--virtiofs-sharing'] = True |
| 220 |
mock_BoxBuild.reset_mock() |
| 221 |
self.task.process() |
| 222 |
mock_BoxBuild.assert_called_once_with( |
| 223 |
boxname='universal', ram=None, console=None, smp=None, arch='', |
| 224 |
machine=None, cpu='host', sharing_backend='virtiofs', |
| 225 |
ssh_key='id_rsa', ssh_port='22', accel=True |
| 226 |
) |
| 227 |
self.task.command_args['--9p-sharing'] = False |
| 228 |
self.task.command_args['--virtiofs-sharing'] = False |
| 229 |
self.task.command_args['--sshfs-sharing'] = True |
| 230 |
mock_BoxBuild.reset_mock() |
| 231 |
self.task.process() |
| 232 |
mock_BoxBuild.assert_called_once_with( |
| 233 |
boxname='universal', ram=None, console=None, smp=None, arch='', |
| 234 |
machine=None, cpu='host', sharing_backend='sshfs', |
| 235 |
ssh_key='id_rsa', ssh_port='22', accel=True |
| 236 |
) |
| 237 |
|
| 238 |
@patch('kiwi.tasks.system_boxbuild.BoxBuild') |
| 239 |
def test_validate_kiwi_build_command_raises_if_description_missing( |
| 240 |
self, mock_BoxBuild |
| 241 |
): |
| 242 |
self._init_command_args() |
| 243 |
self.task.command_args['--box'] = 'leap' |
| 244 |
self.task.command_args['system_build'] = [ |
| 245 |
'--target-dir', 'xxx', |
| 246 |
'--add-package', 'a' |
| 247 |
] |
| 248 |
box_build = Mock() |
| 249 |
mock_BoxBuild.return_value = box_build |
| 250 |
with raises(KiwiBoxBuildError): |
| 251 |
self.task.process() |
| 252 |
|
| 253 |
@patch('kiwi.tasks.system_boxbuild.BoxBuild') |
| 254 |
def test_validate_kiwi_build_command_raises_if_target_dir_missing( |
| 255 |
self, mock_BoxBuild |
| 256 |
): |
| 257 |
self._init_command_args() |
| 258 |
self.task.command_args['--box'] = 'leap' |
| 259 |
self.task.command_args['system_build'] = [ |
| 260 |
'--description', 'xxx', |
| 261 |
'--add-package', 'a' |
| 262 |
] |
| 263 |
box_build = Mock() |
| 264 |
mock_BoxBuild.return_value = box_build |
| 265 |
with raises(KiwiBoxBuildError): |
| 266 |
self.task.process() |