Debian Codegraph @

test/unit/partitioner/msdos_test.py

raw
1 import logging
2 from unittest.mock import (
3 patch, call, mock_open, Mock
4 )
5 from pytest import (
6 raises, fixture
7 )
8 from collections import namedtuple
9
10 from kiwi.partitioner.msdos import PartitionerMsDos
11
12 from kiwi.exceptions import KiwiPartitionerMsDosFlagError
13
14
15 class TestPartitionerMsDos:
16 @fixture(autouse=True)
17 def inject_fixtures(self, caplog):
18 self._caplog = caplog
19
20 def setup(self):
21 disk_provider = Mock()
22 disk_provider.get_device = Mock(
23 return_value='/dev/loop0'
24 )
25 self.partitioner = PartitionerMsDos(disk_provider, 4096)
26 self.partitioner_extended = PartitionerMsDos(
27 disk_provider, extended_layout=True
28 )
29
30 def setup_method(self, cls):
31 self.setup()
32
33 @patch('kiwi.partitioner.msdos.Command.run')
34 @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
35 @patch('kiwi.partitioner.msdos.Temporary.new_file')
36 def test_create_primary(self, mock_temp, mock_flag, mock_command):
37 mock_command.side_effect = Exception
38 temp_type = namedtuple(
39 'temp_type', ['name']
40 )
41 mock_temp.return_value = temp_type(
42 name='tempfile'
43 )
44 m_open = mock_open()
45
46 with patch('builtins.open', m_open, create=True):
47 self.partitioner._create_primary(
48 'name', 100, 't.linux', ['f.active']
49 )
50
51 m_open.return_value.write.assert_called_once_with(
52 'n\np\n1\n\n+101M\nw\nq\n'
53 )
54 call = mock_flag.call_args_list[0]
55 assert mock_flag.call_args_list[0] == \
56 call(1, 't.linux')
57 call = mock_flag.call_args_list[1]
58 assert mock_flag.call_args_list[1] == \
59 call(1, 'f.active')
60 mock_command.assert_called_once_with(
61 ['bash', '-c', 'cat tempfile | fdisk /dev/loop0']
62 )
63
64 @patch('kiwi.partitioner.msdos.Command.run')
65 @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
66 @patch('kiwi.partitioner.msdos.Temporary.new_file')
67 def test_create_extended(self, mock_temp, mock_flag, mock_command):
68 mock_command.side_effect = Exception
69 temp_type = namedtuple(
70 'temp_type', ['name']
71 )
72 mock_temp.return_value = temp_type(
73 name='tempfile'
74 )
75 m_open = mock_open()
76
77 with patch('builtins.open', m_open, create=True):
78 self.partitioner._create_extended('name')
79
80 m_open.return_value.write.assert_called_once_with(
81 'n\ne\n1\n\n\nw\nq\n'
82 )
83 mock_command.assert_called_once_with(
84 ['bash', '-c', 'cat tempfile | fdisk /dev/loop0']
85 )
86
87 @patch('kiwi.partitioner.msdos.Command.run')
88 @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
89 @patch('kiwi.partitioner.msdos.Temporary.new_file')
90 def test_create_logical(self, mock_temp, mock_flag, mock_command):
91 mock_command.side_effect = Exception
92 temp_type = namedtuple(
93 'temp_type', ['name']
94 )
95 mock_temp.return_value = temp_type(
96 name='tempfile'
97 )
98 m_open = mock_open()
99
100 with patch('builtins.open', m_open, create=True):
101 self.partitioner._create_logical(
102 'name', 100, 't.linux', ['f.active']
103 )
104
105 m_open.return_value.write.assert_called_once_with(
106 'n\n1\n\n+100M\nw\nq\n'
107 )
108 call = mock_flag.call_args_list[0]
109 assert mock_flag.call_args_list[0] == \
110 call(1, 't.linux')
111 call = mock_flag.call_args_list[1]
112 assert mock_flag.call_args_list[1] == \
113 call(1, 'f.active')
114 mock_command.assert_called_once_with(
115 ['bash', '-c', 'cat tempfile | fdisk /dev/loop0']
116 )
117
118 @patch.object(PartitionerMsDos, '_create_primary')
119 @patch.object(PartitionerMsDos, '_create_extended')
120 @patch.object(PartitionerMsDos, '_create_logical')
121 def test_create(
122 self, mock_create_logical, mock_create_extended, mock_create_primary
123 ):
124 self.partitioner.create('name', 100, 't.linux')
125 mock_create_primary.assert_called_once_with(
126 'name', 100, 't.linux', [], None
127 )
128 mock_create_primary.reset_mock()
129 self.partitioner_extended.create('name', 100, 't.linux')
130 mock_create_primary.assert_called_once_with(
131 'name', 100, 't.linux', [], None
132 )
133 self.partitioner_extended.partition_id = 3
134 self.partitioner_extended.partition_count = 3
135 self.partitioner_extended.create('name', 100, 't.linux')
136 mock_create_extended.assert_called_once_with('name')
137 mock_create_logical.assert_called_once_with(
138 'name', 100, 't.linux', [], None
139 )
140 mock_create_logical.reset_mock()
141 self.partitioner_extended.partition_id = 7
142 self.partitioner_extended.partition_count = 7
143 self.partitioner_extended.create('name', 100, 't.linux')
144 mock_create_logical.assert_called_once_with(
145 'name', 100, 't.linux', [], None
146 )
147
148 @patch('kiwi.partitioner.msdos.Command.run')
149 @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
150 @patch('kiwi.partitioner.msdos.Temporary.new_file')
151 def test_create_all_free(
152 self, mock_temp, mock_flag, mock_command
153 ):
154 mock_command.side_effect = Exception
155 temp_type = namedtuple(
156 'temp_type', ['name']
157 )
158 mock_temp.return_value = temp_type(
159 name='tempfile'
160 )
161
162 m_open = mock_open()
163 with patch('builtins.open', m_open, create=True):
164 self.partitioner.create('name', 'all_free', 't.linux')
165
166 m_open.return_value.write.assert_called_once_with(
167 'n\np\n1\n\n\nw\nq\n'
168 )
169
170 def test_set_flag_invalid(self):
171 with raises(KiwiPartitionerMsDosFlagError):
172 self.partitioner.set_flag(1, 'foo')
173
174 @patch('kiwi.partitioner.msdos.Command.run')
175 def test_set_flag(self, mock_command):
176 self.partitioner.set_flag(1, 't.linux')
177 mock_command.assert_called_once_with(
178 ['sfdisk', '-c', '/dev/loop0', '1', '83']
179 )
180
181 @patch('kiwi.partitioner.msdos.Command.run')
182 def test_set_active(self, mock_command):
183 self.partitioner.set_flag(1, 'f.active')
184 mock_command.assert_called_once_with(
185 ['sfdisk', '--activate', '/dev/loop0', '1']
186 )
187
188 def test_set_flag_ignored(self):
189 with self._caplog.at_level(logging.WARNING):
190 self.partitioner.set_flag(1, 't.csm')
191
192 def test_resize_table(self):
193 self.partitioner.resize_table()
194
195 @patch('kiwi.partitioner.msdos.Command.run')
196 @patch('kiwi.partitioner.msdos.Temporary.new_file')
197 @patch('kiwi.partitioner.msdos.BlockID')
198 def test_set_start_sector(self, mock_BlockID, mock_temp, mock_Command_run):
199 temp_type = namedtuple(
200 'temp_type', ['name']
201 )
202 mock_temp.return_value = temp_type(
203 name='tempfile'
204 )
205 m_open = mock_open()
206 mock_BlockID.return_value.get_partition_count.return_value = 4
207 with patch('builtins.open', m_open, create=True):
208 self.partitioner.set_start_sector(4096)
209 assert m_open.return_value.write.call_args_list == [
210 call('d\n1\nn\np\n4096\n\nw\nq\n')
211 ]
212 mock_Command_run.assert_called_once_with(
213 ['bash', '-c', 'cat tempfile | fdisk /dev/loop0']
214 )
215 mock_BlockID.return_value.get_partition_count.return_value = 3
216 m_open.reset_mock()
217 with patch('builtins.open', m_open, create=True):
218 self.partitioner.set_start_sector(4096)
219 assert m_open.return_value.write.call_args_list == [
220 call('d\n1\nn\np\n1\n4096\n\nw\nq\n')
221 ]