Debian Codegraph @

test/unit/container/setup/base_test.py

raw
1 from unittest.mock import (
2 patch, call, mock_open
3 )
4 from pytest import raises
5
6 from kiwi.container.setup.base import ContainerSetupBase
7
8 from kiwi.exceptions import KiwiContainerSetupError
9
10
11 class TestContainerSetupBase:
12 @patch('os.path.exists')
13 def setup(self, mock_exists):
14 mock_exists.return_value = True
15
16 self.container = ContainerSetupBase('root_dir')
17
18 @patch('os.path.exists')
19 def setup_method(self, cls, mock_exists):
20 self.setup()
21
22 @patch('os.path.exists')
23 def test_container_root_dir_does_not_exist(self, mock_exists):
24 mock_exists.return_value = False
25 with raises(KiwiContainerSetupError):
26 ContainerSetupBase('root_dir')
27
28 def test_setup(self):
29 with raises(NotImplementedError):
30 self.container.setup()
31
32 def test_post_init(self):
33 self.container.custom_args == {}
34
35 def test_get_container_name(self):
36 assert self.container.get_container_name() == 'system-container'
37
38 @patch('os.path.exists')
39 def test_deactivate_bootloader_setup(self, mock_exists):
40 mock_exists.return_value = True
41
42 m_open = mock_open(read_data='LOADER_LOCATION="mylocation"')
43 with patch('builtins.open', m_open, create=True):
44 self.container.deactivate_bootloader_setup()
45
46 assert m_open.call_args_list[0] == call(
47 'root_dir/etc/sysconfig/bootloader', 'r'
48 )
49 assert m_open.return_value.write.call_args_list == [
50 call('LOADER_LOCATION="none"\nLOADER_TYPE="none"\n')
51 ]
52
53 @patch('os.path.exists')
54 def test_deactivate_root_filesystem_check(self, mock_exists):
55 mock_exists.return_value = True
56
57 m_open = mock_open(read_data=None)
58 with patch('builtins.open', m_open, create=True):
59 self.container.deactivate_root_filesystem_check()
60
61 assert m_open.call_args_list[0] == call(
62 'root_dir/etc/sysconfig/boot', 'r'
63 )
64 assert m_open.return_value.write.call_args_list == [
65 call('\nROOTFS_BLKDEV="/dev/null"\n')
66 ]
67
68 @patch('os.path.exists')
69 @patch('kiwi.container.setup.base.Command.run')
70 def test_deactivate_systemd_service(self, mock_command, mock_exists):
71 mock_exists.return_value = True
72 self.container.deactivate_systemd_service('my.service')
73 mock_command.assert_called_once_with(
74 [
75 'ln', '-s', '-f', '/dev/null',
76 'root_dir/usr/lib/systemd/system/my.service'
77 ]
78 )
79
80 @patch('os.path.exists')
81 @patch('kiwi.container.setup.base.Command.run')
82 def test_deactivate_systemd_service_failed(self, mock_command, mock_exists):
83 mock_exists.return_value = True
84 mock_command.side_effect = Exception
85 with raises(KiwiContainerSetupError):
86 self.container.deactivate_systemd_service('my.service')
87
88 @patch('os.path.exists')
89 def test_setup_root_console(self, mock_exists):
90 mock_exists.return_value = False
91
92 m_open = mock_open(read_data=None)
93 with patch('builtins.open', m_open, create=True):
94 self.container.setup_root_console()
95
96 assert m_open.call_args_list == [
97 call('root_dir/etc/securetty', 'w'),
98 call('root_dir/etc/securetty', 'r'),
99 call('root_dir/etc/securetty', 'w')
100 ]
101 assert m_open.return_value.write.call_args_list == [
102 call('\nconsole\n')
103 ]