• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3
4import unittest, os
5import common
6from autotest_lib.client.common_lib.test_utils import mock
7from autotest_lib.client.bin import package, os_dep, utils
8
9
10class TestPackage(unittest.TestCase):
11    def setUp(self):
12        self.god = mock.mock_god()
13        self.god.stub_function(os_dep, "command")
14
15    def tearDown(self):
16        self.god.unstub_all()
17
18
19    def info_common_setup(self, input_package, result):
20        self.god.stub_function(os.path, "isfile")
21        self.god.stub_function(utils, "system_output")
22        self.god.stub_function(utils, "system")
23
24        # record
25        os.path.isfile.expect_call(input_package).and_return(True)
26        utils.system_output.expect_call(
27            'file ' + input_package).and_return(result)
28        utils.system_output.expect_call(
29            'file ' + input_package).and_return(result)
30
31
32    def test_info_rpm(self):
33        # setup
34        input_package = "package.rpm"
35        file_result = "rpm"
36        ver = '1.0'
37
38        # common setup
39        self.info_common_setup(input_package, file_result)
40
41        # record
42        package_info = {}
43        package_info['type'] = 'rpm'
44        os_dep.command.expect_call('rpm')
45        s_cmd = 'rpm -qp --qf %{SOURCE} ' + input_package + ' 2>/dev/null'
46        a_cmd = 'rpm -qp --qf %{ARCH} ' + input_package + ' 2>/dev/null'
47        v_cmd = 'rpm -qp ' + input_package + ' 2>/dev/null'
48
49        utils.system_output.expect_call(v_cmd).and_return(ver)
50        i_cmd = 'rpm -q ' + ver + ' 2>&1 >/dev/null'
51
52        package_info['system_support'] = True
53        utils.system_output.expect_call(s_cmd).and_return('source')
54        package_info['source'] = True
55        utils.system_output.expect_call(v_cmd).and_return(ver)
56        package_info['version'] = ver
57        utils.system_output.expect_call(a_cmd).and_return('586')
58        package_info['arch'] = '586'
59        utils.system.expect_call(i_cmd)
60        package_info['installed'] = True
61
62        # run and check
63        info = package.info(input_package)
64        self.god.check_playback()
65        self.assertEquals(info, package_info)
66
67
68    def test_info_dpkg(self):
69        # setup
70        input_package = "package.deb"
71        file_result = "debian"
72        ver = '1.0'
73
74        # common setup
75        self.info_common_setup(input_package, file_result)
76
77        # record
78        package_info = {}
79        package_info['type'] = 'dpkg'
80        package_info['source'] = False
81        os_dep.command.expect_call('dpkg')
82        a_cmd = 'dpkg -f ' + input_package + ' Architecture 2>/dev/null'
83        v_cmd = 'dpkg -f ' + input_package + ' Package 2>/dev/null'
84        utils.system_output.expect_call(v_cmd).and_return(ver)
85        i_cmd = 'dpkg -s ' + ver + ' 2>/dev/null'
86        package_info['system_support'] = True
87        utils.system_output.expect_call(v_cmd).and_return(ver)
88        package_info['version'] = ver
89        utils.system_output.expect_call(a_cmd).and_return('586')
90        package_info['arch'] = '586'
91        utils.system_output.expect_call(i_cmd,
92            ignore_status=True).and_return('installed')
93        package_info['installed'] = True
94
95        # run and check
96        info = package.info(input_package)
97        self.god.check_playback()
98        self.assertEquals(info, package_info)
99
100
101    def test_install(self):
102        # setup
103        input_package = "package.rpm"
104        self.god.stub_function(package, "info")
105        self.god.stub_function(utils, "system")
106
107        # record
108        package_info = {}
109        package_info['type'] = 'rpm'
110        package_info['system_support'] = True
111        package_info['source'] = True
112        package_info['installed'] = True
113
114        package.info.expect_call(input_package).and_return(package_info)
115        install_command = 'rpm %s -U %s' % ('', input_package)
116        utils.system.expect_call(install_command)
117
118        # run and test
119        package.install(input_package)
120        self.god.check_playback()
121
122
123    def test_convert(self):
124        os_dep.command.expect_call('alien')
125        dest_format = 'dpkg'
126        input_package = "package.rpm"
127        output = "package_output.deb"
128
129        # record
130        self.god.stub_function(utils, "system_output")
131        utils.system_output.expect_call(
132            'alien --to-deb %s 2>/dev/null' % input_package).and_return(output)
133
134        # run test
135        package.convert(input_package, dest_format)
136        self.god.check_playback()
137
138
139    def test_os_support_full(self):
140        # recording
141        exp_support = {}
142        for package_manager in package.KNOWN_PACKAGE_MANAGERS:
143            os_dep.command.expect_call(package_manager)
144            exp_support[package_manager] = True
145
146        os_dep.command.expect_call('alien')
147        exp_support['conversion'] = True
148
149        # run and test
150        support = package.os_support()
151        self.god.check_playback()
152        self.assertEquals(support, exp_support)
153
154
155    def test_os_support_none(self):
156        # recording
157        exp_support = {}
158        for package_manager in package.KNOWN_PACKAGE_MANAGERS:
159            os_dep.command.expect_call(package_manager).and_raises(ValueError)
160            exp_support[package_manager] = False
161
162        os_dep.command.expect_call('alien').and_raises(ValueError)
163        exp_support['conversion'] = False
164
165        # run and test
166        support = package.os_support()
167        self.god.check_playback()
168        self.assertEquals(support, exp_support)
169
170
171if __name__ == "__main__":
172    unittest.main()
173