ba59a3015cc44f8964380a527998330bd98c7201
[yardstick.git] / yardstick / tests / unit / common / test_packages.py
1 # Copyright (c) 2018 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import mock
16 from pip import exceptions as pip_exceptions
17 from pip.operations import freeze
18 import unittest
19
20 from yardstick.common import packages
21
22
23 class PipExecuteActionTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self._mock_pip_main = mock.patch.object(packages, '_pip_main')
27         self.mock_pip_main = self._mock_pip_main.start()
28         self.mock_pip_main.return_value = 0
29         self._mock_freeze = mock.patch.object(freeze, 'freeze')
30         self.mock_freeze = self._mock_freeze.start()
31         self.addCleanup(self._cleanup)
32
33     def _cleanup(self):
34         self._mock_pip_main.stop()
35         self._mock_freeze.stop()
36
37     def test_pip_execute_action(self):
38         self.assertEqual(0, packages._pip_execute_action('test_package'))
39
40     def test_remove(self):
41         self.assertEqual(0, packages._pip_execute_action('test_package',
42                                                          action='uninstall'))
43
44     def test_install(self):
45         self.assertEqual(0, packages._pip_execute_action(
46             'test_package', action='install', target='temp_dir'))
47
48     def test_pip_execute_action_error(self):
49         self.mock_pip_main.return_value = 1
50         self.assertEqual(1, packages._pip_execute_action('test_package'))
51
52     def test_pip_execute_action_exception(self):
53         self.mock_pip_main.side_effect = pip_exceptions.PipError
54         self.assertEqual(1, packages._pip_execute_action('test_package'))
55
56     def test_pip_list(self):
57         pkg_input = [
58             'XStatic-Rickshaw==1.5.0.0',
59             'xvfbwrapper==0.2.9',
60             '-e git+https://git.opnfv.org/yardstick@50773a24afc02c9652b662ecca'
61             '2fc5621ea6097a#egg=yardstick',
62             'zope.interface==4.4.3'
63         ]
64         pkg_dict = {
65             'XStatic-Rickshaw': '1.5.0.0',
66             'xvfbwrapper': '0.2.9',
67             'yardstick': '50773a24afc02c9652b662ecca2fc5621ea6097a',
68             'zope.interface': '4.4.3'
69         }
70         self.mock_freeze.return_value = pkg_input
71
72         pkg_output = packages.pip_list()
73         for pkg_name, pkg_version in pkg_output.items():
74             self.assertEqual(pkg_dict.get(pkg_name), pkg_version)
75
76     def test_pip_list_single_package(self):
77         pkg_input = [
78             'XStatic-Rickshaw==1.5.0.0',
79             'xvfbwrapper==0.2.9',
80             '-e git+https://git.opnfv.org/yardstick@50773a24afc02c9652b662ecca'
81             '2fc5621ea6097a#egg=yardstick',
82             'zope.interface==4.4.3'
83         ]
84         self.mock_freeze.return_value = pkg_input
85
86         pkg_output = packages.pip_list(pkg_name='xvfbwrapper')
87         self.assertEqual(1, len(pkg_output))
88         self.assertEqual(pkg_output.get('xvfbwrapper'), '0.2.9')