Debian Codegraph @

test/unit/partitioner/gpt_test.py

raw
1 import logging
2 from unittest.mock import (
3 patch, call, Mock
4 )
5 from pytest import (
6 raises, fixture
7 )
8
9 from kiwi.partitioner.gpt import PartitionerGpt
10
11 from kiwi.exceptions import KiwiPartitionerGptFlagError
12
13
14 class TestPartitionerGpt:
15 @fixture(autouse=True)
16 def inject_fixtures(self, caplog):
17 self._caplog = caplog
18
19 def setup(self):
20 disk_provider = Mock()
21 disk_provider.get_device = Mock(
22 return_value='/dev/loop0'
23 )
24 self.partitioner = PartitionerGpt(disk_provider)
25
26 def setup_method(self, cls):
27 self.setup()
28
29 @patch('kiwi.partitioner.gpt.Command.run')
30 @patch('kiwi.partitioner.gpt.PartitionerGpt.set_flag')
31 def test_create(self, mock_flag, mock_command):
32 self.partitioner.create('name', 100, 't.linux', ['t.csm'])
33 mock_command.assert_called_once_with(
34 ['sgdisk', '-n', '1:0:+100M', '-c', '1:name', '/dev/loop0']
35 )
36 call = mock_flag.call_args_list[0]
37 assert mock_flag.call_args_list[0] == \
38 call(1, 't.linux')
39 call = mock_flag.call_args_list[1]
40 assert mock_flag.call_args_list[1] == \
41 call(1, 't.csm')
42
43 @patch('kiwi.partitioner.gpt.Command.run')
44 @patch('kiwi.partitioner.gpt.PartitionerGpt.set_flag')
45 def test_create_custom_start_sector(self, mock_flag, mock_command):
46 disk_provider = Mock()
47 disk_provider.get_device = Mock(
48 return_value='/dev/loop0'
49 )
50 partitioner = PartitionerGpt(disk_provider, 4096)
51 partitioner.create('name', 100, 't.linux', ['t.csm'])
52 partitioner.create('name', 100, 't.linux', ['t.csm'])
53 mock_command.assert_has_calls([
54 call([
55 'sgdisk', '-n', '1:4096:+100M', '-c', '1:name', '/dev/loop0'
56 ]),
57 call([
58 'sgdisk', '-n', '2:0:+100M', '-c', '2:name', '/dev/loop0'
59 ])
60 ])
61 assert mock_flag.call_args_list[0] == \
62 call(1, 't.linux')
63 assert mock_flag.call_args_list[1] == \
64 call(1, 't.csm')
65
66 @patch('kiwi.partitioner.gpt.Command.run')
67 @patch('kiwi.partitioner.gpt.PartitionerGpt.set_flag')
68 def test_create_all_free(self, mock_flag, mock_command):
69 self.partitioner.create('name', 'all_free', 't.linux')
70 mock_command.assert_called_once_with(
71 ['sgdisk', '-n', '1:0:0', '-c', '1:name', '/dev/loop0']
72 )
73
74 def test_set_flag_invalid(self):
75 with raises(KiwiPartitionerGptFlagError):
76 self.partitioner.set_flag(1, 'foo')
77
78 @patch('kiwi.partitioner.gpt.Command.run')
79 def test_set_flag(self, mock_command):
80 self.partitioner.set_flag(1, 't.csm')
81 mock_command.assert_called_once_with(
82 ['sgdisk', '-t', '1:EF02', '/dev/loop0']
83 )
84
85 def test_set_flag_ignored(self):
86 with self._caplog.at_level(logging.WARNING):
87 self.partitioner.set_flag(1, 'f.active')
88
89 @patch('kiwi.partitioner.gpt.Command.run')
90 def test_set_hybrid_mbr(self, mock_command):
91 self.partitioner.partition_id = 5
92 self.partitioner.partition_count = 5
93 self.partitioner.partition_map = {
94 1: 1,
95 2: 2,
96 3: 3,
97 4: 4,
98 5: 5
99 }
100 self.partitioner.set_hybrid_mbr()
101 mock_command.assert_called_once_with(
102 ['sgdisk', '-h', '1:2:3', '/dev/loop0']
103 )
104
105 @patch('kiwi.partitioner.gpt.Command.run')
106 def test_set_mbr(self, mock_command):
107 command_output = Mock()
108 command_output.output = '...(EFI System)'
109 self.partitioner.partition_id = 4
110 self.partitioner.partition_count = 4
111 self.partitioner.partition_map = {
112 1: 1,
113 2: 2,
114 3: 3,
115 4: 4
116 }
117 mock_command.return_value = command_output
118 self.partitioner.set_mbr()
119 assert mock_command.call_args_list == [
120 call(['sgdisk', '-i=1', '/dev/loop0']),
121 call(['sgdisk', '-i=2', '/dev/loop0']),
122 call(['sgdisk', '-i=3', '/dev/loop0']),
123 call(['sgdisk', '-i=4', '/dev/loop0']),
124 call(['sgdisk', '-t', '4:8300', '/dev/loop0']),
125 call(['sgdisk', '-m', '1:2:3:4', '/dev/loop0'])
126 ]
127
128 @patch('kiwi.partitioner.gpt.Command.run')
129 def test_resize_table(self, mock_command):
130 self.partitioner.resize_table(42)
131 mock_command.assert_called_once_with(
132 ['sgdisk', '--resize-table', '42', '/dev/loop0']
133 )
134
135 @patch('kiwi.partitioner.gpt.Command.run')
136 def test_set_uuid(self, mock_Command_run):
137 self.partitioner.set_uuid(42, 'ID')
138 mock_Command_run.assert_called_once_with(
139 ['sgdisk', '--typecode', '42:ID', '/dev/loop0']
140 )