Debian Codegraph @

test/unit/system/root_import/base_test.py

raw
1 import io
2 import os
3 from unittest.mock import (
4 patch, call, Mock, MagicMock
5 )
6 from pytest import raises
7
8 from kiwi.system.uri import Uri
9 from kiwi.system.root_import.base import RootImportBase
10
11 from kiwi.exceptions import KiwiRootImportError
12
13
14 class TestRootImportBase:
15 @patch('os.path.exists')
16 @patch('kiwi.system.uri.Defaults.is_buildservice_worker')
17 @patch('kiwi.system.uri.RuntimeConfig')
18 def test_init(self, mock_RuntimeConfig, mock_buildservice, mock_path):
19 mock_buildservice.return_value = False
20 mock_path.return_value = True
21 with patch.dict('os.environ', {'HOME': '../data'}):
22 RootImportBase('root_dir', [Uri('file:///image.tar.xz')])
23 assert call('/image.tar.xz') in mock_path.call_args_list
24
25 def test_init_remote_uri(self):
26 with raises(KiwiRootImportError):
27 RootImportBase('root_dir', [Uri('http://example.com/image.tar.xz')])
28
29 @patch('kiwi.system.root_import.base.log.warning')
30 def test_init_unknown_uri(self, mock_log_warn):
31 root = RootImportBase('root_dir', [Uri('docker://opensuse:leap')])
32 assert root.raw_urls == ['docker://opensuse:leap']
33 assert mock_log_warn.called
34
35 @patch('os.path.exists')
36 def test_init_non_existing(self, mock_path):
37 mock_path.return_value = False
38 with patch.dict('os.environ', {'HOME': '../data'}):
39 with raises(KiwiRootImportError):
40 RootImportBase('root_dir', [Uri('file:///image.tar.xz')])
41
42 @patch('os.path.exists')
43 def test_data_sync(self, mock_path):
44 mock_path.return_value = True
45 with patch.dict('os.environ', {'HOME': '../data'}):
46 root_import = RootImportBase(
47 'root_dir', [Uri('file:///image.tar.xz')]
48 )
49 with raises(NotImplementedError):
50 root_import.sync_data()
51
52 @patch('os.path.exists')
53 def test_overlay_data(self, mock_path):
54 mock_path.return_value = True
55 with patch.dict('os.environ', {'HOME': '../data'}):
56 root_import = RootImportBase(
57 'root_dir', [Uri('docker://opensuse:leap')]
58 )
59 with raises(NotImplementedError):
60 root_import.overlay_data()
61
62 @patch('os.path.exists')
63 @patch('kiwi.system.uri.Defaults.is_buildservice_worker')
64 @patch('kiwi.system.root_import.base.SystemSetup')
65 @patch('kiwi.system.root_import.base.Path')
66 @patch('kiwi.system.root_import.base.pathlib')
67 @patch('kiwi.system.root_import.base.Command.run')
68 @patch('kiwi.system.uri.RuntimeConfig')
69 def test_overlay_finalize(
70 self, mock_runtime_config, mock_Command_run, mock_pathlib, mock_Path,
71 mock_SystemSetup, mock_buildservice, mock_path_exists
72 ):
73 mock_path_exists.return_value = True
74 mock_buildservice.return_value = False
75 with patch('builtins.open', create=True) as mock_open:
76 mock_open.return_value = MagicMock(spec=io.IOBase)
77 file_handle = mock_open.return_value.__enter__.return_value
78 xml_state = Mock()
79 mock_Command_run.return_value.output = '/file_a\n/file_b'
80 with patch.dict('os.environ', {'HOME': '../data'}):
81 root = RootImportBase(
82 'root_dir', [Uri('docker://opensuse:leap')]
83 )
84 root.overlay = Mock()
85 mock_pathlib.Path = Mock()
86 root_overlay_path_mock = Mock()
87 mock_pathlib.Path.return_value = root_overlay_path_mock
88
89 root.overlay_finalize(xml_state)
90
91 # run config-overlay.sh
92 mock_SystemSetup.return_value.\
93 call_config_overlay_script.assert_called_once_with()
94
95 # run config-host-overlay.sh
96 mock_SystemSetup.return_value.\
97 call_config_host_overlay_script.assert_called_once_with(
98 working_directory='root_dir'
99 )
100
101 # umount and create the new upper
102 root.overlay.umount.assert_called_once_with()
103 assert mock_Path.wipe.call_args_list == [
104 call('root_dir'),
105 call('root_dir_cow_before_pinch'),
106 call(root.overlay.lower),
107 call(root.overlay.work)
108 ]
109
110 mock_pathlib.Path.assert_called_once_with(root.overlay.upper)
111 root_overlay_path_mock.replace.assert_called_once_with(
112 'root_dir'
113 )
114
115 # find files that got removed
116 assert mock_Command_run.call_args_list == [
117 call(
118 [
119 'rsync', '-av', '--dry-run', '--out-format=%n',
120 '--exclude', 'etc/hosts.kiwi',
121 '--exclude', 'etc/hosts.sha',
122 '--exclude', 'etc/resolv.conf.kiwi',
123 '--exclude', 'etc/resolv.conf.sha',
124 '--exclude', 'etc/sysconfig/proxy.kiwi',
125 '--exclude', 'etc/sysconfig/proxy.sha',
126 '--exclude', 'usr/lib/sysimage/rpm',
127 'root_dir_cow_before_pinch/', 'root_dir/'
128 ]
129 ),
130 call(
131 ['find', 'root_dir', '-type', 'c', '-delete']
132 )
133 ]
134 # create removed files metadata for later host provisioning
135 assert file_handle.write.call_args_list == [
136 call('\n'),
137 call('/file_a'), call(os.linesep),
138 call('/file_b'), call(os.linesep)
139 ]