Debian Codegraph @

test/unit/utils/rpm_test.py

raw
1 import os
2 from unittest.mock import (
3 patch, Mock, call, mock_open
4 )
5
6 from kiwi.utils.rpm import Rpm
7 from kiwi.defaults import Defaults
8
9
10 class TestRpm:
11 def setup(self):
12 self.rpm_host = Rpm()
13 self.rpm_image = Rpm('root_dir')
14 self.macro_file = os.sep.join(
15 [
16 Defaults.get_custom_rpm_macros_path(),
17 Defaults.get_custom_rpm_bootstrap_macro_name()
18 ]
19 )
20
21 def setup_method(self, cls):
22 self.setup()
23
24 @patch('kiwi.utils.rpm.Command.run')
25 def test_expand_query(self, mock_Command_run):
26 self.rpm_host.expand_query('foo')
27 mock_Command_run.assert_called_once_with(
28 ['rpm', '-E', 'foo']
29 )
30 mock_Command_run.reset_mock()
31 self.rpm_image.expand_query('foo')
32 mock_Command_run.assert_called_once_with(
33 ['chroot', 'root_dir', 'rpm', '-E', 'foo']
34 )
35
36 @patch('kiwi.utils.rpm.Command.run')
37 def test_get_query(self, mock_Command_run):
38 rpmdb_call = Mock()
39 rpmdb_call.output = '%_dbpath %{_usr}/lib/sysimage/rpm'
40 mock_Command_run.return_value = rpmdb_call
41 assert self.rpm_host.get_query('_dbpath') == \
42 '%{_usr}/lib/sysimage/rpm'
43 mock_Command_run.assert_called_once_with(
44 ['rpmdb', '--showrc']
45 )
46 mock_Command_run.reset_mock()
47 assert self.rpm_image.get_query('_dbpath') == \
48 '%{_usr}/lib/sysimage/rpm'
49 mock_Command_run.assert_called_once_with(
50 ['chroot', 'root_dir', 'rpmdb', '--showrc']
51 )
52
53 def test_set_config_value(self):
54 self.rpm_host.set_config_value('key', 'value')
55 assert self.rpm_host.custom_config[0] == '%key\tvalue'
56
57 @patch('kiwi.utils.rpm.Path.wipe')
58 def test_wipe_config(self, mock_Path_wipe):
59 self.rpm_host.wipe_config()
60 mock_Path_wipe.assert_called_once_with(
61 os.sep + self.macro_file
62 )
63 mock_Path_wipe.reset_mock()
64 self.rpm_image.wipe_config()
65 mock_Path_wipe.assert_called_once_with(
66 os.sep.join(['root_dir', self.macro_file])
67 )
68
69 @patch('kiwi.utils.rpm.Path.create')
70 def test_write_config(self, mock_Path_create):
71 self.rpm_host.set_config_value('key', 'value')
72
73 m_open = mock_open()
74 with patch('builtins.open', m_open, create=True):
75 self.rpm_host.write_config()
76
77 m_open.assert_called_once_with(
78 os.sep + self.macro_file, 'w'
79 )
80 assert m_open.return_value.write.call_args_list == [
81 call('%key\tvalue\n')
82 ]
83
84 self.rpm_image.set_config_value('foo', 'bar')
85
86 m_open.reset_mock()
87 with patch('builtins.open', m_open, create=True):
88 self.rpm_image.write_config()
89
90 m_open.assert_called_once_with(
91 os.sep.join(['root_dir', self.macro_file]), 'w'
92 )
93 assert m_open.return_value.write.call_args_list == [
94 call('%foo\tbar\n')
95 ]