Debian Codegraph @

test/unit/utils/rpm_database_test.py

raw
1 from unittest.mock import (
2 patch, Mock, call
3 )
4
5 from kiwi.utils.rpm_database import RpmDataBase
6
7
8 class TestRpmDataBase:
9 def setup(self):
10 self.rpmdb = RpmDataBase('root_dir')
11 self.rpmdb.rpmdb_host = Mock()
12 self.rpmdb.rpmdb_image = Mock()
13
14 def setup_method(self, cls):
15 self.setup()
16
17 @patch('kiwi.utils.rpm_database.Path.which')
18 def test_has_rpm(self, mock_Path_which):
19 mock_Path_which.return_value = None
20 assert self.rpmdb.has_rpm() is False
21 mock_Path_which.assert_called_once_with(
22 'rpmdb', access_mode=1, root_dir='root_dir'
23 )
24 mock_Path_which.return_value = 'rpm'
25 assert self.rpmdb.has_rpm() is True
26
27 @patch('kiwi.command.Command.run')
28 def test_rebuild_database(self, mock_Command_run):
29 self.rpmdb.rebuild_database()
30 mock_Command_run.assert_called_once_with(
31 ['chroot', 'root_dir', 'rpmdb', '--rebuilddb']
32 )
33
34 @patch('kiwi.command.Command.run')
35 def test_init_database(self, mock_Command_run):
36 self.rpmdb.init_database()
37 mock_Command_run.assert_called_once_with(
38 ['rpm', '--root', 'root_dir', '--initdb']
39 )
40
41 def test_set_database_to_host_path(self):
42 self.rpmdb.set_database_to_host_path()
43 self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
44 '_dbpath', self.rpmdb.rpmdb_host.get_query.return_value
45 )
46 self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
47 self.rpmdb.rpmdb_host.get_query.assert_called_once_with('_dbpath')
48
49 @patch('kiwi.command.Command.run')
50 @patch('os.path.exists')
51 def test_link_database_to_host_path(self, mock_exists, mock_Command_run):
52 self.rpmdb.rpmdb_image.expand_query.return_value = \
53 '/var/lib/rpm'
54 self.rpmdb.rpmdb_host.expand_query.return_value = \
55 '/usr/lib/sysimage/rpm'
56 mock_exists.return_value = False
57 self.rpmdb.link_database_to_host_path()
58 assert mock_Command_run.call_args_list == [
59 call(
60 [
61 'ln', '-s', '../../../var/lib/rpm',
62 'root_dir/usr/lib/sysimage/rpm'
63 ]
64 )
65 ]
66
67 @patch('kiwi.utils.rpm_database.Path.wipe')
68 @patch('kiwi.command.Command.run')
69 @patch('os.path.islink')
70 @patch('os.path.exists')
71 @patch('os.unlink')
72 def test_set_database_to_image_path(
73 self, mock_os_unlink, mock_os_exists, mock_os_islink,
74 mock_Command_run, mock_Path_wipe
75 ):
76 mock_os_exists.return_value = True
77
78 link = [False, False]
79
80 def mocked_islink(path):
81 return link.pop()
82
83 mock_os_islink.side_effect = mocked_islink
84 self.rpmdb.rpmdb_image.expand_query.return_value = \
85 '/var/lib/rpm'
86 self.rpmdb.rpmdb_host.expand_query.return_value = \
87 '/usr/lib/sysimage/rpm'
88 self.rpmdb.set_database_to_image_path()
89 self.rpmdb.rpmdb_image.expand_query.assert_called_once_with('%_dbpath')
90 assert self.rpmdb.rpmdb_image.set_config_value.call_args_list == [
91 call(
92 '_dbpath', self.rpmdb.rpmdb_host.expand_query.return_value
93 ),
94 call(
95 '_dbpath_rebuild', self.rpmdb.rpmdb_image.get_query.return_value
96 )
97 ]
98 assert mock_Path_wipe.call_args_list == [
99 call('root_dir/var/lib/rpm'),
100 call('root_dir/usr/lib/sysimage/rpm')
101 ]
102 self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
103 assert mock_Command_run.call_args_list == [
104 call(
105 [
106 'chroot', 'root_dir', 'rpmdb', '--rebuilddb'
107 ]
108 ),
109 call(
110 [
111 'mv', 'root_dir/usr/lib/sysimage/rpm/alternatives',
112 'root_dir/var/lib/rpm'
113 ]
114 )
115 ]
116 assert self.rpmdb.rpmdb_image.wipe_config.call_count == 2
117 link = [True, False]
118 mock_os_islink.side_effect = mocked_islink
119 self.rpmdb.set_database_to_image_path()
120 mock_os_unlink.assert_called_once_with(
121 'root_dir/var/lib/rpm'
122 )
123 link = [True, True]
124 mock_os_islink.side_effect = mocked_islink
125 self.rpmdb.set_database_to_image_path()
126 assert mock_Command_run.call_args_list[-1] == call(
127 ['chroot', 'root_dir', 'rpmdb', '--rebuilddb']
128 )
129
130 def test_set_macro_from_string(self):
131 self.rpmdb.set_macro_from_string('_install_langs%en_US:de_DE')
132 self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
133 '_install_langs', 'en_US:de_DE'
134 )
135
136 def test_write_config(self):
137 self.rpmdb.write_config()
138 self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
139
140 @patch('kiwi.command.Command.run')
141 def test_import_signing_key_to_image(self, mock_Command_run):
142 self.rpmdb.import_signing_key_to_image('key')
143 mock_Command_run.assert_called_once_with(
144 [
145 'rpm', '--root', 'root_dir', '--import', 'key',
146 '--dbpath', self.rpmdb.rpmdb_host.expand_query.return_value
147 ]
148 )