Debian Codegraph @

test/unit/partitioner/dasd_test.py

raw
1 import logging
2 from pytest import fixture
3 from unittest.mock import (
4 patch, mock_open
5 )
6
7 import unittest.mock as mock
8
9 from kiwi.partitioner.dasd import PartitionerDasd
10
11
12 class TestPartitionerDasd:
13 @fixture(autouse=True)
14 def inject_fixtures(self, caplog):
15 self._caplog = caplog
16
17 @patch('kiwi.partitioner.dasd.Command.run')
18 @patch('kiwi.partitioner.dasd.Temporary.new_file')
19 def setup(self, mock_temp, mock_command):
20 self.tempfile = mock.Mock()
21 self.tempfile.name = 'tempfile'
22
23 mock_temp.return_value = self.tempfile
24
25 disk_provider = mock.Mock()
26 disk_provider.get_device = mock.Mock(
27 return_value='/dev/loop0'
28 )
29
30 self.partitioner = PartitionerDasd(disk_provider)
31
32 @patch('kiwi.partitioner.dasd.Command.run')
33 @patch('kiwi.partitioner.dasd.Temporary.new_file')
34 def setup_method(self, cls, mock_temp, mock_command):
35 self.setup()
36
37 @patch('kiwi.partitioner.dasd.Command.run')
38 @patch('kiwi.partitioner.dasd.Temporary.new_file')
39 def test_create(self, mock_temp, mock_command):
40 mock_command.side_effect = Exception
41 mock_temp.return_value = self.tempfile
42
43 m_open = mock_open()
44 with patch('builtins.open', m_open, create=True):
45 with self._caplog.at_level(logging.DEBUG):
46 self.partitioner.create('name', '100', 't.linux', ['f.active'])
47
48 m_open.return_value.write.assert_called_once_with(
49 'n\np\n\n+100M\nw\nq\n'
50 )
51 mock_command.assert_called_once_with(
52 ['bash', '-c', 'cat tempfile | fdasd -f /dev/loop0']
53 )
54
55 @patch('kiwi.partitioner.dasd.Command.run')
56 @patch('kiwi.partitioner.dasd.Temporary.new_file')
57 def test_create_all_free(self, mock_temp, mock_command):
58 mock_temp.return_value = self.tempfile
59
60 m_open = mock_open()
61 with patch('builtins.open', m_open, create=True):
62 self.partitioner.create('name', 'all_free', 't.linux')
63
64 m_open.return_value.write.assert_called_once_with(
65 'n\np\n\n\nw\nq\n'
66 )
67
68 def test_resize_table(self):
69 self.partitioner.resize_table()