| 1 |
import sys |
| 2 |
import logging |
| 3 |
from unittest.mock import ( |
| 4 |
patch, call, Mock |
| 5 |
) |
| 6 |
from pytest import ( |
| 7 |
raises, fixture |
| 8 |
) |
| 9 |
|
| 10 |
from ..test_helper import argv_kiwi_tests |
| 11 |
|
| 12 |
from kiwi.system.root_bind import RootBind |
| 13 |
|
| 14 |
from kiwi.exceptions import ( |
| 15 |
KiwiMountKernelFileSystemsError, |
| 16 |
KiwiMountSharedDirectoryError, |
| 17 |
KiwiSetupIntermediateConfigError |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
class TestRootBind: |
| 22 |
@fixture(autouse=True) |
| 23 |
def inject_fixtures(self, caplog): |
| 24 |
self._caplog = caplog |
| 25 |
|
| 26 |
def setup(self): |
| 27 |
root = Mock() |
| 28 |
root.root_dir = 'root-dir' |
| 29 |
self.bind_root = RootBind(root) |
| 30 |
|
| 31 |
# test expected real bind mount locations |
| 32 |
assert self.bind_root.bind_locations == [ |
| 33 |
'/proc', |
| 34 |
'/dev', |
| 35 |
'/var/run/dbus', |
| 36 |
'/sys', |
| 37 |
'/sys/fs/cgroup' |
| 38 |
] |
| 39 |
|
| 40 |
# stub config files and bind locations |
| 41 |
self.bind_root.config_files = ['/etc/sysconfig/proxy'] |
| 42 |
self.bind_root.bind_locations = ['/proc'] |
| 43 |
|
| 44 |
# stub files/dirs and mountpoints to cleanup |
| 45 |
self.mount_manager = Mock() |
| 46 |
self.bind_root.cleanup_files = ['/etc/sysconfig/proxy.kiwi'] |
| 47 |
self.bind_root.mount_stack = [self.mount_manager] |
| 48 |
self.bind_root.dir_stack = ['/mountpoint'] |
| 49 |
|
| 50 |
self.runtime_config = Mock() |
| 51 |
self.shasum = self.runtime_config.get_checksum_handler.return_value |
| 52 |
self.bind_root.runtime_config = self.runtime_config |
| 53 |
|
| 54 |
def setup_method(self, cls): |
| 55 |
self.setup() |
| 56 |
|
| 57 |
def teardown(self): |
| 58 |
sys.argv = argv_kiwi_tests |
| 59 |
|
| 60 |
def teardown_method(self, cls): |
| 61 |
self.teardown() |
| 62 |
|
| 63 |
@patch('kiwi.system.root_bind.MountManager.bind_mount') |
| 64 |
@patch('kiwi.system.root_bind.RootBind.cleanup') |
| 65 |
@patch('os.path.exists') |
| 66 |
def test_kernel_file_systems_raises_error( |
| 67 |
self, mock_exists, mock_cleanup, mock_mount |
| 68 |
): |
| 69 |
mock_exists.return_value = True |
| 70 |
mock_mount.side_effect = KiwiMountKernelFileSystemsError( |
| 71 |
'mount-error' |
| 72 |
) |
| 73 |
with raises(KiwiMountKernelFileSystemsError): |
| 74 |
self.bind_root.mount_kernel_file_systems() |
| 75 |
mock_cleanup.assert_called_once_with() |
| 76 |
|
| 77 |
@patch('kiwi.system.root_bind.MountManager.bind_mount') |
| 78 |
@patch('kiwi.system.root_bind.Path.create') |
| 79 |
@patch('kiwi.system.root_bind.RootBind.cleanup') |
| 80 |
def test_shared_directory_raises_error( |
| 81 |
self, mock_cleanup, mock_path, mock_mount |
| 82 |
): |
| 83 |
mock_mount.side_effect = KiwiMountSharedDirectoryError( |
| 84 |
'mount-error' |
| 85 |
) |
| 86 |
with raises(KiwiMountSharedDirectoryError): |
| 87 |
self.bind_root.mount_shared_directory() |
| 88 |
mock_cleanup.assert_called_once_with() |
| 89 |
|
| 90 |
@patch('kiwi.command.Command.run') |
| 91 |
@patch('kiwi.system.root_bind.RootBind.cleanup') |
| 92 |
@patch('os.path.exists') |
| 93 |
def test_intermediate_config_raises_error( |
| 94 |
self, mock_exists, mock_cleanup, mock_command |
| 95 |
): |
| 96 |
mock_exists.return_value = True |
| 97 |
mock_command.side_effect = KiwiSetupIntermediateConfigError( |
| 98 |
'config-error' |
| 99 |
) |
| 100 |
with raises(KiwiSetupIntermediateConfigError): |
| 101 |
self.bind_root.setup_intermediate_config() |
| 102 |
mock_cleanup.assert_called_once_with() |
| 103 |
|
| 104 |
@patch('kiwi.system.root_bind.os.path.exists') |
| 105 |
@patch('kiwi.system.root_bind.MountManager') |
| 106 |
def test_mount_kernel_file_systems(self, mock_mount, mock_exists): |
| 107 |
mock_exists.return_value = True |
| 108 |
shared_mount = Mock() |
| 109 |
mock_mount.return_value = shared_mount |
| 110 |
assert self.bind_root.mount_kernel_file_systems() is None |
| 111 |
assert mock_mount.call_args_list == [ |
| 112 |
call(device='root-dir', mountpoint='root-dir'), |
| 113 |
call(device='/proc', mountpoint='root-dir/proc') |
| 114 |
] |
| 115 |
assert shared_mount.bind_mount.call_args_list == [ |
| 116 |
call(), call() |
| 117 |
] |
| 118 |
|
| 119 |
@patch('kiwi.system.root_bind.MountManager') |
| 120 |
def test_umount_kernel_file_systems(self, mock_mount): |
| 121 |
self.mount_manager.device = '/proc' |
| 122 |
self.mount_manager.is_mounted = Mock(return_value=True) |
| 123 |
assert self.bind_root.umount_kernel_file_systems() is None |
| 124 |
self.mount_manager.umount_lazy.assert_called_once_with() |
| 125 |
assert self.bind_root.mount_stack == [] |
| 126 |
|
| 127 |
@patch('kiwi.system.root_bind.MountManager') |
| 128 |
def test_umount_kernel_file_systems_raises_error(self, mock_mount): |
| 129 |
self.mount_manager.device = '/proc' |
| 130 |
self.mount_manager.is_mounted = Mock(return_value=True) |
| 131 |
self.mount_manager.umount_lazy = Mock(side_effect=Exception) |
| 132 |
assert self.bind_root.umount_kernel_file_systems() is None |
| 133 |
self.mount_manager.umount_lazy.assert_called_once_with() |
| 134 |
assert self.bind_root.mount_stack == [self.mount_manager] |
| 135 |
|
| 136 |
@patch('kiwi.system.root_bind.MountManager') |
| 137 |
@patch('kiwi.system.root_bind.Path.create') |
| 138 |
def test_mount_shared_directory(self, mock_path, mock_mount): |
| 139 |
shared_mount = Mock() |
| 140 |
mock_mount.return_value = shared_mount |
| 141 |
assert self.bind_root.mount_shared_directory() is None |
| 142 |
mock_path.call_args_list = [ |
| 143 |
call('root-dir/var/cache/kiwi'), |
| 144 |
call('/var/cache/kiwi') |
| 145 |
] |
| 146 |
mock_mount.assert_called_once_with( |
| 147 |
device='/var/cache/kiwi', mountpoint='root-dir/var/cache/kiwi' |
| 148 |
) |
| 149 |
shared_mount.bind_mount.assert_called_once_with() |
| 150 |
|
| 151 |
@patch('kiwi.command.Command.run') |
| 152 |
@patch('kiwi.system.root_bind.Checksum') |
| 153 |
@patch('os.path.exists') |
| 154 |
def test_intermediate_config( |
| 155 |
self, mock_exists, mock_Checksum, mock_command |
| 156 |
): |
| 157 |
checksum = Mock() |
| 158 |
mock_Checksum.return_value = checksum |
| 159 |
mock_exists.return_value = True |
| 160 |
|
| 161 |
with patch('builtins.open') as m_open: |
| 162 |
assert self.bind_root.setup_intermediate_config() is None |
| 163 |
m_open.assert_called_once_with( |
| 164 |
'root-dir/etc/sysconfig/proxy.sha', 'w' |
| 165 |
) |
| 166 |
|
| 167 |
assert mock_command.call_args_list == [ |
| 168 |
call([ |
| 169 |
'cp', '/etc/sysconfig/proxy', 'root-dir/etc/sysconfig/proxy.kiwi' |
| 170 |
]), |
| 171 |
call([ |
| 172 |
'ln', '-s', '-f', 'proxy.kiwi', 'root-dir/etc/sysconfig/proxy' |
| 173 |
]) |
| 174 |
] |
| 175 |
self.shasum.digest.assert_called_once_with() |
| 176 |
|
| 177 |
@patch('textwrap.dedent') |
| 178 |
@patch('kiwi.system.root_bind.Checksum') |
| 179 |
@patch('kiwi.system.root_bind.MountManager.is_mounted') |
| 180 |
@patch('kiwi.system.root_bind.Command.run') |
| 181 |
@patch('kiwi.system.root_bind.Path.remove_hierarchy') |
| 182 |
@patch('os.path.islink') |
| 183 |
@patch('os.path.exists') |
| 184 |
@patch('shutil.move') |
| 185 |
def test_cleanup( |
| 186 |
self, mock_move, mock_exists, mock_islink, |
| 187 |
mock_remove_hierarchy, mock_command, mock_is_mounted, |
| 188 |
mock_Checksum, mock_dedent |
| 189 |
): |
| 190 |
checksum = Mock() |
| 191 |
checksum.matches.return_value = False |
| 192 |
mock_Checksum.return_value = checksum |
| 193 |
os_exists_return_values = [False, True, True, False] |
| 194 |
|
| 195 |
def exists_side_effect(*args): |
| 196 |
return os_exists_return_values.pop() |
| 197 |
|
| 198 |
mock_is_mounted.return_value = False |
| 199 |
mock_exists.side_effect = exists_side_effect |
| 200 |
mock_islink.return_value = True |
| 201 |
with self._caplog.at_level(logging.WARNING): |
| 202 |
assert self.bind_root.cleanup() is None |
| 203 |
self.mount_manager.umount_lazy.assert_called_once_with() |
| 204 |
mock_remove_hierarchy.assert_called_once_with( |
| 205 |
root='root-dir', path='/mountpoint' |
| 206 |
) |
| 207 |
assert mock_command.call_args_list == [ |
| 208 |
call(['rm', '-f', 'root-dir/etc/sysconfig/proxy']), |
| 209 |
call( |
| 210 |
[ |
| 211 |
'cp', |
| 212 |
'root-dir/usr/share/fillup-templates/sysconfig.proxy', |
| 213 |
'root-dir/etc/sysconfig/proxy' |
| 214 |
] |
| 215 |
), |
| 216 |
call( |
| 217 |
[ |
| 218 |
'rm', '-f', 'root-dir/etc/sysconfig/proxy.kiwi', |
| 219 |
'root-dir/etc/sysconfig/proxy.sha' |
| 220 |
] |
| 221 |
) |
| 222 |
] |
| 223 |
mock_move.assert_called_once_with( |
| 224 |
'root-dir/etc/sysconfig/proxy.rpmnew', |
| 225 |
'root-dir/etc/sysconfig/proxy' |
| 226 |
) |
| 227 |
|
| 228 |
@patch('os.path.islink') |
| 229 |
@patch('kiwi.command.Command.run') |
| 230 |
@patch('kiwi.system.root_bind.Path.remove_hierarchy') |
| 231 |
@patch('kiwi.system.root_bind.Checksum') |
| 232 |
def test_cleanup_continue_on_error( |
| 233 |
self, mock_Checksum, mock_remove_hierarchy, |
| 234 |
mock_command, mock_islink |
| 235 |
): |
| 236 |
mock_islink.return_value = True |
| 237 |
mock_remove_hierarchy.side_effect = Exception('rm') |
| 238 |
mock_command.side_effect = Exception |
| 239 |
self.mount_manager.umount_lazy.side_effect = Exception |
| 240 |
with self._caplog.at_level(logging.WARNING): |
| 241 |
self.bind_root.cleanup() |
| 242 |
assert 'Image root directory root-dir not cleanly umounted:' in \ |
| 243 |
self._caplog.text |
| 244 |
assert 'Failed to remove directory hierarchy ' |
| 245 |
'root-dir/mountpoint: rm' in self._caplog.text |
| 246 |
assert 'Failed to cleanup intermediate config files' in \ |
| 247 |
self._caplog.text |
| 248 |
|
| 249 |
@patch('kiwi.command.Command.run') |
| 250 |
@patch('kiwi.system.root_bind.Path.remove_hierarchy') |
| 251 |
@patch('kiwi.system.root_bind.Checksum') |
| 252 |
def test_cleanup_nothing_mounted( |
| 253 |
self, mock_Checksum, mock_remove_hierarchy, mock_command |
| 254 |
): |
| 255 |
self.mount_manager.is_mounted.return_value = False |
| 256 |
self.mount_manager.mountpoint = '/mountpoint' |
| 257 |
with self._caplog.at_level(logging.WARNING): |
| 258 |
assert self.bind_root.cleanup() is None |
| 259 |
assert 'Path /mountpoint not a mountpoint' in self._caplog.text |