| 1 |
import sys |
| 2 |
from pytest import raises |
| 3 |
from unittest.mock import ( |
| 4 |
Mock, patch |
| 5 |
) |
| 6 |
from tempfile import NamedTemporaryFile |
| 7 |
from ..test_helper import argv_kiwi_tests |
| 8 |
|
| 9 |
from kiwi.tasks.system_stash import SystemStashTask |
| 10 |
from kiwi.exceptions import ( |
| 11 |
KiwiStackBuildContainerNameInvalid |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
class TestSystemStashTask: |
| 16 |
def setup(self): |
| 17 |
sys.argv = [ |
| 18 |
sys.argv[0], 'system', 'stash', |
| 19 |
'--root', 'some-root-dir' |
| 20 |
] |
| 21 |
self.task = SystemStashTask() |
| 22 |
self.tmpfile = NamedTemporaryFile() |
| 23 |
|
| 24 |
def setup_method(self, cls): |
| 25 |
self.setup() |
| 26 |
|
| 27 |
def teardown(self): |
| 28 |
sys.argv = argv_kiwi_tests |
| 29 |
|
| 30 |
def teardown_method(self, cls): |
| 31 |
self.teardown() |
| 32 |
|
| 33 |
def _init_command_args(self): |
| 34 |
self.task.command_args = {} |
| 35 |
self.task.command_args['help'] = False |
| 36 |
self.task.command_args['stash'] = False |
| 37 |
self.task.command_args['--root'] = None |
| 38 |
self.task.command_args['--tag'] = None |
| 39 |
self.task.command_args['--container-name'] = None |
| 40 |
|
| 41 |
@patch('kiwi.tasks.system_stash.Help') |
| 42 |
def test_process_help(self, mock_Help): |
| 43 |
Help = Mock() |
| 44 |
mock_Help.return_value = Help |
| 45 |
self._init_command_args() |
| 46 |
self.task.command_args['help'] = True |
| 47 |
self.task.command_args['stash'] = True |
| 48 |
self.task.process() |
| 49 |
Help.show.assert_called_once_with( |
| 50 |
'kiwi::system::stash' |
| 51 |
) |
| 52 |
|
| 53 |
@patch('kiwi.tasks.system_stash.DataOutput') |
| 54 |
@patch('kiwi.tasks.system_stash.Privileges') |
| 55 |
@patch('os.listdir') |
| 56 |
def test_process_stash_list( |
| 57 |
self, mock_os_listdir, mock_Privileges, mock_DataOutput |
| 58 |
): |
| 59 |
mock_os_listdir.return_value = ['name'] |
| 60 |
stashes = Mock() |
| 61 |
mock_DataOutput.return_value = stashes |
| 62 |
self._init_command_args() |
| 63 |
self.task.command_args['stash'] = True |
| 64 |
self.task.command_args['--list'] = True |
| 65 |
self.task.process() |
| 66 |
stashes.display.assert_called_once_with() |
| 67 |
|
| 68 |
@patch('kiwi.tasks.system_stash.Privileges') |
| 69 |
def test_process_invalid_container_name(self, mock_Privileges): |
| 70 |
self._init_command_args() |
| 71 |
self.task.command_args['--root'] = '../data/stackbuild/image-root-invalid-name' |
| 72 |
with raises(KiwiStackBuildContainerNameInvalid): |
| 73 |
self.task.process() |
| 74 |
|
| 75 |
@patch('kiwi.tasks.system_stash.Command.run') |
| 76 |
@patch('kiwi.tasks.system_stash.OCI.new') |
| 77 |
@patch('kiwi.tasks.system_stash.Privileges') |
| 78 |
@patch('kiwi.tasks.system_stash.Path') |
| 79 |
@patch('os.path.isfile') |
| 80 |
def test_process_build_initial_layer( |
| 81 |
self, mock_os_path_isfile, |
| 82 |
mock_Path, mock_Privileges, mock_OCI_new, mock_Command_run |
| 83 |
): |
| 84 |
self._init_command_args() |
| 85 |
self.task.command_args['--root'] = '../data/stackbuild/image-root' |
| 86 |
oci = Mock() |
| 87 |
mock_OCI_new.return_value = oci |
| 88 |
mock_os_path_isfile.return_value = False |
| 89 |
container_config = { |
| 90 |
'container_name': 'tumbleweed', |
| 91 |
'container_tag': 'latest', |
| 92 |
'entry_command': ['/bin/sh'], |
| 93 |
'labels': { |
| 94 |
'io.osinside.kiwi.maintainer': 'Marcus Schaefer', |
| 95 |
'io.osinside.kiwi.meta': |
| 96 |
'KIWI - https://github.com/OSInside/kiwi' |
| 97 |
}, |
| 98 |
'history': { |
| 99 |
'created_by': |
| 100 |
'KIWI - https://github.com/OSInside/kiwi', |
| 101 |
'comment': 'KIWI Root Tree Stash', |
| 102 |
'author': 'Marcus Schaefer' |
| 103 |
} |
| 104 |
} |
| 105 |
self.task.process() |
| 106 |
mock_Privileges.check_for_root_permissions.assert_called_once_with() |
| 107 |
oci.init_container.assert_called_once_with() |
| 108 |
oci.unpack.assert_called_once_with() |
| 109 |
oci.sync_rootfs.assert_called_once_with( |
| 110 |
'../data/stackbuild/image-root', ['dev/*', 'sys/*', 'proc/*'] |
| 111 |
) |
| 112 |
oci.repack.assert_called_once_with(container_config) |
| 113 |
oci.set_config.assert_called_once_with(container_config) |
| 114 |
oci.post_process.assert_called_once_with() |
| 115 |
oci.export_container_image.assert_called_once_with( |
| 116 |
'/var/tmp/kiwi-stash/tumbleweed/tumbleweed.tar', |
| 117 |
'oci-archive', 'tumbleweed:latest' |
| 118 |
) |
| 119 |
mock_Command_run.assert_called_once_with( |
| 120 |
[ |
| 121 |
'podman', 'load', '-i', |
| 122 |
'/var/tmp/kiwi-stash/tumbleweed/tumbleweed.tar' |
| 123 |
] |
| 124 |
) |
| 125 |
|
| 126 |
@patch('kiwi.tasks.system_stash.Command.run') |
| 127 |
@patch('kiwi.tasks.system_stash.OCI.new') |
| 128 |
@patch('kiwi.tasks.system_stash.Privileges') |
| 129 |
@patch('kiwi.tasks.system_stash.Path') |
| 130 |
@patch('os.path.isfile') |
| 131 |
@patch('os.path.abspath') |
| 132 |
@patch('kiwi.utils.temporary.NamedTemporaryFile') |
| 133 |
def test_process_build_additional_layer( |
| 134 |
self, mock_NamedTemporaryFile, mock_os_path_abspath, |
| 135 |
mock_os_path_isfile, mock_Path, mock_Privileges, mock_OCI_new, |
| 136 |
mock_Command_run |
| 137 |
): |
| 138 |
tempfile = Mock() |
| 139 |
tempfile.name = self.tmpfile.name |
| 140 |
mock_NamedTemporaryFile.return_value = tempfile |
| 141 |
self._init_command_args() |
| 142 |
self.task.command_args['--root'] = '../data/stackbuild/image-root' |
| 143 |
oci = Mock() |
| 144 |
mock_OCI_new.return_value = oci |
| 145 |
mock_os_path_isfile.return_value = True |
| 146 |
mock_os_path_abspath.return_value = 'absolute_root_dir_path' |
| 147 |
self.task.process() |
| 148 |
oci.import_container_image.assert_called_once_with( |
| 149 |
'oci-archive:/var/tmp/kiwi-stash/' |
| 150 |
'tumbleweed/tumbleweed.tar:tumbleweed:latest' |
| 151 |
) |
| 152 |
mock_Command_run.assert_called_once_with( |
| 153 |
[ |
| 154 |
'podman', 'load', '-i', |
| 155 |
'/var/tmp/kiwi-stash/tumbleweed/tumbleweed.tar' |
| 156 |
] |
| 157 |
) |