1 # Copyright (c) 2018 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from pip import exceptions as pip_exceptions
17 from pip.operations import freeze
20 from yardstick.common import packages
23 class PipExecuteActionTestCase(unittest.TestCase):
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)
34 self._mock_pip_main.stop()
35 self._mock_freeze.stop()
37 def test_pip_execute_action(self):
38 self.assertEqual(0, packages._pip_execute_action('test_package'))
40 def test_remove(self):
41 self.assertEqual(0, packages._pip_execute_action('test_package',
44 def test_install(self):
45 self.assertEqual(0, packages._pip_execute_action(
46 'test_package', action='install', target='temp_dir'))
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'))
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'))
56 def test_pip_list(self):
58 'XStatic-Rickshaw==1.5.0.0',
60 '-e git+https://git.opnfv.org/yardstick@50773a24afc02c9652b662ecca'
61 '2fc5621ea6097a#egg=yardstick',
62 'zope.interface==4.4.3'
65 'XStatic-Rickshaw': '1.5.0.0',
66 'xvfbwrapper': '0.2.9',
67 'yardstick': '50773a24afc02c9652b662ecca2fc5621ea6097a',
68 'zope.interface': '4.4.3'
70 self.mock_freeze.return_value = pkg_input
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)
76 def test_pip_list_single_package(self):
78 'XStatic-Rickshaw==1.5.0.0',
80 '-e git+https://git.opnfv.org/yardstick@50773a24afc02c9652b662ecca'
81 '2fc5621ea6097a#egg=yardstick',
82 'zope.interface==4.4.3'
84 self.mock_freeze.return_value = pkg_input
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')