Debian Codegraph @

test/unit/filesystem/base_test.py

raw
1 import logging
2 from unittest.mock import (
3 patch, Mock
4 )
5 from pytest import (
6 raises, fixture
7 )
8
9 import kiwi.defaults as defaults
10
11 from kiwi.filesystem.base import FileSystemBase
12
13 from kiwi.exceptions import KiwiFileSystemSyncError
14
15
16 class TestFileSystemBase:
17 @fixture(autouse=True)
18 def inject_fixtures(self, caplog):
19 self._caplog = caplog
20
21 def setup(self):
22 provider = Mock()
23 provider.get_device = Mock(
24 return_value='/dev/loop0'
25 )
26 provider.get_byte_size = Mock(
27 return_value=1073741824 # 1GB
28 )
29 custom_args = {
30 'fs_attributes': ['no-copy-on-write']
31 }
32 self.fsbase = FileSystemBase(provider, 'root_dir', custom_args)
33
34 def setup_method(self, cls):
35 self.setup()
36
37 def test_root_dir_does_not_exist(self):
38 fsbase = FileSystemBase(Mock(), 'root_dir_not_existing')
39 with raises(KiwiFileSystemSyncError):
40 fsbase.sync_data()
41
42 def test_root_dir_not_defined(self):
43 fsbase = FileSystemBase(Mock())
44 with raises(KiwiFileSystemSyncError):
45 fsbase.sync_data()
46
47 def test_create_on_device(self):
48 with raises(NotImplementedError):
49 self.fsbase.create_on_device('/dev/foo')
50
51 def test_create_on_file(self):
52 with raises(NotImplementedError):
53 self.fsbase.create_on_file('myimage')
54
55 def test_get_mountpoint(self):
56 assert self.fsbase.get_mountpoint() is None
57
58 @patch('kiwi.filesystem.base.MountManager')
59 @patch('kiwi.filesystem.base.DataSync')
60 @patch('kiwi.filesystem.base.Command.run')
61 @patch('os.path.exists')
62 def test_sync_data(
63 self, mock_exists, mock_Command_run, mock_sync, mock_mount
64 ):
65 mock_exists.return_value = True
66
67 filesystem_mount = Mock()
68 filesystem_mount.mountpoint = 'tmpdir'
69 mock_mount.return_value = filesystem_mount
70
71 data_sync = Mock()
72 mock_sync.return_value = data_sync
73
74 self.fsbase.sync_data(['exclude_me'])
75
76 mock_sync.assert_called_once_with('root_dir', 'tmpdir')
77 data_sync.sync_data.assert_called_once_with(
78 exclude=['exclude_me'],
79 options=[
80 '--archive', '--hard-links', '--xattrs',
81 '--acls', '--one-file-system', '--inplace'
82 ]
83 )
84 mock_mount.assert_called_once_with(
85 device='/dev/loop0'
86 )
87 mock_Command_run.assert_called_once_with(
88 ['chattr', '+C', 'tmpdir']
89 )
90 filesystem_mount.mount.assert_called_once_with([])
91 assert self.fsbase.get_mountpoint() == 'tmpdir'
92
93 @patch('kiwi.filesystem.base.VeritySetup')
94 def test_create_verity_layer(self, mock_VeritySetup):
95 self.fsbase.create_verity_layer()
96 mock_VeritySetup.return_value.format.assert_called_once_with()
97
98 def test_create_verification_metadata(self):
99 veritysetup = Mock()
100 self.fsbase.veritysetup = veritysetup
101 self.fsbase.create_verification_metadata('/some/device')
102 veritysetup.create_verity_verification_metadata.assert_called_once_with()
103 veritysetup.sign_verification_metadata.assert_called_once_with()
104 veritysetup.write_verification_metadata.assert_called_once_with(
105 '/some/device'
106 )
107
108 def test_umount(self):
109 mount = Mock()
110 self.fsbase.filesystem_mount = mount
111 self.fsbase.umount()
112 mount.umount.assert_called_once_with()
113
114 def test_umount_volumes(self):
115 mount = Mock()
116 self.fsbase.filesystem_mount = mount
117 self.fsbase.umount_volumes()
118 mount.umount.assert_called_once_with()
119
120 def test_mount(self):
121 mount = Mock()
122 self.fsbase.filesystem_mount = mount
123 self.fsbase.mount()
124 mount.mount.assert_called_once_with([])
125
126 def test_mount_volumes(self):
127 mount = Mock()
128 self.fsbase.filesystem_mount = mount
129 self.fsbase.mount_volumes()
130 mount.mount.assert_called_once_with([])
131
132 def test_get_volumes(self):
133 with raises(NotImplementedError):
134 self.fsbase.get_volumes()
135
136 def test_get_root_volume_name(self):
137 with raises(NotImplementedError):
138 self.fsbase.get_root_volume_name()
139
140 def test_get_fstab(self):
141 with raises(NotImplementedError):
142 self.fsbase.get_fstab()
143
144 def test_set_property_readonly_root(self):
145 with raises(NotImplementedError):
146 self.fsbase.set_property_readonly_root()
147
148 def test_fs_size(self):
149 # size of 100k must be 100k (default unit)
150 assert self.fsbase._fs_size(100) == '100'
151 # size of 1073741824b (see test init) - 100k must be 1048476k
152 assert self.fsbase._fs_size(-100) == '1048476'
153 # size of 1073741824b (see test init) - 102400b must be 1073639424b
154 assert self.fsbase._fs_size(
155 -102400, unit=defaults.UNIT.byte
156 ) == '1073639424'
157 # size of 1073741824b - 1024m must be zero
158 assert self.fsbase._fs_size(-1024, unit=defaults.UNIT.mb) == '0'
159 # size of 1073741824b - 1g must be zero
160 assert self.fsbase._fs_size(-1, unit=defaults.UNIT.gb) == '0'
161
162 def test_set_uuid(self):
163 with self._caplog.at_level(logging.WARNING):
164 self.fsbase.set_uuid()
165
166 def test_context_manager_exit(self):
167 mount_manager = Mock()
168 with FileSystemBase(Mock()) as fsbase:
169 fsbase.filesystem_mount = mount_manager
170 mount_manager.umount.assert_called_once_with()
171
172 def test_generate_seed_uuid(self):
173 with patch.dict('os.environ', {'SOURCE_DATE_EPOCH': '123456'}):
174 assert self.fsbase._generate_seed_uuid('label') == \
175 'c0cbf4e3-10a7-86ea-9472-87aaca0074f2'
176
177 @patch('kiwi.filesystem.base.uuid')
178 def test_generate_seed_uuid_random(self, mock_uuid):
179 mock_uuid.uuid4.return_value = 'some'
180 with patch.dict('os.environ', {'SOURCE_DATE_EPOCH': ''}):
181 assert self.fsbase._generate_seed_uuid('label') == 'some'