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.
20 from yardstick.common import packages
21 from yardstick.common import utils
22 from yardstick.tests.functional import base
25 class PipPackagesTestCase(base.BaseFunctionalTestCase):
27 TMP_FOLDER = '/tmp/pip_packages/'
28 PYTHONPATH = 'PYTHONPATH=%s' % TMP_FOLDER
31 super(PipPackagesTestCase, self).setUp()
32 privsep_helper = os.path.join(
33 os.getenv('VIRTUAL_ENV'), 'bin', 'privsep-helper')
35 helper_command=' '.join(['sudo', '-EH', privsep_helper]),
36 group='yardstick_privileged')
37 self.addCleanup(self._cleanup)
40 utils.execute_command('sudo rm -rf %s' % self.TMP_FOLDER)
42 def _remove_package(self, package):
43 os.system('%s python -m pip uninstall %s -y' %
44 (self.PYTHONPATH, package))
46 def _list_packages(self):
47 pip_list_regex = re.compile(
48 r"(?P<name>[\w\.-]+) \((?P<version>[\w\d_\.\-]+),*.*\)")
49 pip_list_regex_18 = re.compile(
50 r"(?P<name>[\w\.-]+)[\s]+(?P<version>[\w\d_\.\-]+),*.*")
52 pkgs = utils.execute_command('python -m pip list',
53 env={'PYTHONPATH': self.TMP_FOLDER})
55 match = pip_list_regex.match(line)
57 match = pip_list_regex_18.match(line)
58 if match and match.group('name'):
59 pkg_dict[match.group('name')] = match.group('version')
62 def test_install_from_folder(self):
63 dirname = path.dirname(__file__)
64 package_dir = dirname + '/fake_directory_package'
65 package_name = 'yardstick-new-plugin-2'
66 self.addCleanup(self._remove_package, package_name)
67 self._remove_package(package_name)
68 self.assertFalse(package_name in self._list_packages())
70 self.assertEqual(0, packages.pip_install(package_dir, self.TMP_FOLDER))
71 self.assertTrue(package_name in self._list_packages())
73 @unittest.skip("see https://github.com/pypa/pip/issues/3889")
74 def test_install_from_pip_package(self):
75 dirname = path.dirname(__file__)
76 package_path = (dirname +
77 '/fake_pip_package/yardstick_new_plugin-1.0.0.tar.gz')
78 package_name = 'yardstick-new-plugin'
79 self.addCleanup(self._remove_package, package_name)
80 self._remove_package(package_name)
81 self.assertFalse(package_name in self._list_packages())
83 self.assertEqual(0, packages.pip_install(package_path, self.TMP_FOLDER))
84 self.assertTrue(package_name in self._list_packages())
86 # NOTE(ralonsoh): an stable test plugin project is needed in OPNFV git
87 # server to execute this test.
88 # def test_install_from_url(self):
90 def test_pip_freeze(self):
91 # NOTE (ralonsoh): from requirements.txt file. The best way to test
92 # this function is to parse requirements.txt and test-requirements.txt
93 # and check all packages.
94 pkgs_ref = {'Babel': '2.6.0',
95 'SQLAlchemy': '1.2.18',
97 'netifaces': '0.10.9'}
98 pkgs = packages.pip_list()
99 for name, version in (pkgs_ref.items()):
100 self.assertEqual(version, pkgs[name])