aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/common/test_packages.py
blob: 09d76fe44b5c175fed4464c979e4f086eb146730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (c) 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
from pip._internal import exceptions as pip_exceptions
from pip._internal.operations import freeze
import unittest

from yardstick.common import packages


class PipExecuteActionTestCase(unittest.TestCase):

    def setUp(self):
        self._mock_pip_main = mock.patch.object(packages, '_pip_main')
        self.mock_pip_main = self._mock_pip_main.start()
        self.mock_pip_main.return_value = 0
        self._mock_freeze = mock.patch.object(freeze, 'freeze')
        self.mock_freeze = self._mock_freeze.start()
        self.addCleanup(self._cleanup)

    def _cleanup(self):
        self._mock_pip_main.stop()
        self._mock_freeze.stop()

    def test_pip_execute_action(self):
        self.assertEqual(0, packages._pip_execute_action('test_package'))

    def test_remove(self):
        self.assertEqual(0, packages._pip_execute_action('test_package',
                                                         action='uninstall'))

    def test_install(self):
        self.assertEqual(0, packages._pip_execute_action(
            'test_package', action='install', target='temp_dir'))

    def test_pip_execute_action_error(self):
        self.mock_pip_main.return_value = 1
        self.assertEqual(1, packages._pip_execute_action('test_package'))

    def test_pip_execute_action_exception(self):
        self.mock_pip_main.side_effect = pip_exceptions.PipError
        self.assertEqual(1, packages._pip_execute_action('test_package'))

    def test_pip_list(self):
        pkg_input = [
            'XStatic-Rickshaw==1.5.0.0',
            'xvfbwrapper==0.2.9',
            '-e git+https://git.opnfv.org/yardstick@50773a24afc02c9652b662ecca'
            '2fc5621ea6097a#egg=yardstick',
            'zope.interface==4.4.3'
        ]
        pkg_dict = {
            'XStatic-Rickshaw': '1.5.0.0',
            'xvfbwrapper': '0.2.9',
            'yardstick': '50773a24afc02c9652b662ecca2fc5621ea6097a',
            'zope.interface': '4.4.3'
        }
        self.mock_freeze.return_value = pkg_input

        pkg_output = packages.pip_list()
        for pkg_name, pkg_version in pkg_output.items():
            self.assertEqual(pkg_dict.get(pkg_name), pkg_version)

    def test_pip_list_single_package(self):
        pkg_input = [
            'XStatic-Rickshaw==1.5.0.0',
            'xvfbwrapper==0.2.9',
            '-e git+https://git.opnfv.org/yardstick@50773a24afc02c9652b662ecca'
            '2fc5621ea6097a#egg=yardstick',
            'zope.interface==4.4.3'
        ]
        self.mock_freeze.return_value = pkg_input

        pkg_output = packages.pip_list(pkg_name='xvfbwrapper')
        self.assertEqual(1, len(pkg_output))
        self.assertEqual(pkg_output.get('xvfbwrapper'), '0.2.9')