Debian Codegraph @

test/unit/storage/raid_device_test.py

raw
1 import logging
2 from unittest.mock import (
3 patch, Mock, mock_open
4 )
5 from pytest import (
6 raises, fixture
7 )
8
9 from kiwi.storage.raid_device import RaidDevice
10
11 from kiwi.exceptions import KiwiRaidSetupError
12
13
14 class TestRaidDevice:
15 @fixture(autouse=True)
16 def inject_fixtures(self, caplog):
17 self._caplog = caplog
18
19 def setup(self):
20 storage_device = Mock()
21 storage_device.get_device = Mock(
22 return_value='/dev/some-device'
23 )
24 storage_device.is_loop = Mock(
25 return_value=True
26 )
27 self.raid = RaidDevice(storage_device)
28
29 def setup_method(self, cls):
30 self.setup()
31
32 def test_create_degraded_raid_invalid_level(self):
33 with raises(KiwiRaidSetupError):
34 self.raid.create_degraded_raid('bogus-level')
35
36 @patch('os.path.exists')
37 def test_create_degraded_raid_no_free_device(self, mock_path):
38 mock_path.return_value = True
39 with raises(KiwiRaidSetupError):
40 self.raid.create_degraded_raid('mirroring')
41
42 @patch('os.path.exists')
43 def test_get_device(self, mock_path):
44 mock_path.return_value = True
45 self.raid.raid_device = '/dev/md0'
46 assert self.raid.get_device().get_device() == '/dev/md0'
47 self.raid.raid_device = None
48
49 def test_get_device_no_mapped_device(self):
50 assert self.raid.get_device() is None
51
52 @patch('kiwi.storage.raid_device.Command.run')
53 @patch('os.path.exists')
54 def test_create_degraded_raid(self, mock_path, mock_command):
55 mock_path.return_value = False
56 self.raid.create_degraded_raid('mirroring')
57 mock_command.assert_called_once_with(
58 [
59 'mdadm', '--create', '--run', '/dev/md0',
60 '--level', '1', '--raid-disks', '2',
61 '/dev/some-device', 'missing'
62 ]
63 )
64 self.raid.raid_device = None
65
66 @patch('kiwi.storage.raid_device.Command.run')
67 def test_create_raid_config(self, mock_command):
68 self.raid.raid_device = '/dev/md0'
69 command_call = Mock()
70 command_call.output = 'data'
71 mock_command.return_value = command_call
72
73 m_open = mock_open()
74 with patch('builtins.open', m_open, create=True):
75 self.raid.create_raid_config('mdadm.conf')
76
77 mock_command.assert_called_once_with(
78 ['mdadm', '-Db', '/dev/md0']
79 )
80 m_open.return_value.write.assert_called_once_with('data')
81 self.raid.raid_device = None
82
83 @patch('kiwi.storage.raid_device.Command.run')
84 def test_create_raid_config_without_raid_device(self, mock_command):
85 self.raid.raid_device = None
86
87 with raises(KiwiRaidSetupError) as raid_err_ctx:
88 self.raid.create_raid_config('mdadm.conf')
89
90 assert "No raid device" in str(raid_err_ctx.value)
91 mock_command.assert_not_called()
92
93 def test_is_loop(self):
94 assert self.raid.is_loop() is True
95
96 @patch('kiwi.storage.raid_device.Command.run')
97 @patch('kiwi.storage.raid_device.log.warning')
98 def test_context_manager_exit(self, mock_log_warn, mock_command):
99 mock_command.side_effect = Exception
100 with self._caplog.at_level(logging.ERROR):
101 with RaidDevice(Mock()) as raid:
102 raid.raid_device = '/dev/md0'
103 mock_command.assert_called_once_with(
104 ['mdadm', '--stop', '/dev/md0']
105 )