Merge "Create Dockerfile to create a yardstick-image of docker"
[yardstick.git] / yardstick / tests / functional / 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 os
16 from os import path
17 import re
18
19 from yardstick.common import packages
20 from yardstick.common import utils
21 from yardstick.tests.functional import base
22
23
24 class PipPackagesTestCase(base.BaseFunctionalTestCase):
25
26     TMP_FOLDER = '/tmp/pip_packages/'
27     PYTHONPATH = 'PYTHONPATH=%s' % TMP_FOLDER
28
29     def setUp(self):
30         super(PipPackagesTestCase, self).setUp()
31         privsep_helper = os.path.join(
32             os.getenv('VIRTUAL_ENV'), 'bin', 'privsep-helper')
33         self.config(
34             helper_command=' '.join(['sudo', '-EH', privsep_helper]),
35             group='yardstick_privileged')
36         self.addCleanup(self._cleanup)
37
38     def _cleanup(self):
39         utils.execute_command('sudo rm -rf %s' % self.TMP_FOLDER)
40
41     def _remove_package(self, package):
42         os.system('%s pip uninstall %s -y' % (self.PYTHONPATH, package))
43
44     def _list_packages(self):
45         pip_list_regex = re.compile(
46             r"(?P<name>[\w\.-]+) \((?P<version>[\w\d_\.\-]+),*.*\)")
47         pkg_dict = {}
48         pkgs = utils.execute_command('pip list',
49                                      env={'PYTHONPATH': self.TMP_FOLDER})
50         for line in pkgs:
51             match = pip_list_regex.match(line)
52             if match and match.group('name'):
53                 pkg_dict[match.group('name')] = match.group('version')
54         return pkg_dict
55
56     def test_install_from_folder(self):
57         dirname = path.dirname(__file__)
58         package_dir = dirname + '/fake_directory_package'
59         package_name = 'yardstick-new-plugin-2'
60         self.addCleanup(self._remove_package, package_name)
61         self._remove_package(package_name)
62         self.assertFalse(package_name in self._list_packages())
63
64         self.assertEqual(0, packages.pip_install(package_dir, self.TMP_FOLDER))
65         self.assertTrue(package_name in self._list_packages())
66
67     def test_install_from_pip_package(self):
68         dirname = path.dirname(__file__)
69         package_path = (dirname +
70                         '/fake_pip_package/yardstick_new_plugin-1.0.0.tar.gz')
71         package_name = 'yardstick-new-plugin'
72         self.addCleanup(self._remove_package, package_name)
73         self._remove_package(package_name)
74         self.assertFalse(package_name in self._list_packages())
75
76         self.assertEqual(0, packages.pip_install(package_path, self.TMP_FOLDER))
77         self.assertTrue(package_name in self._list_packages())
78
79     # NOTE(ralonsoh): an stable test plugin project is needed in OPNFV git
80     # server to execute this test.
81     # def test_install_from_url(self):
82
83     def test_pip_freeze(self):
84         # NOTE (ralonsoh): from requirements.txt file. The best way to test
85         # this function is to parse requirements.txt and test-requirements.txt
86         # and check all packages.
87         pkgs_ref = {'Babel': '2.3.4',
88                     'SQLAlchemy': '1.1.12',
89                     'influxdb': '4.1.1',
90                     'netifaces': '0.10.6',
91                     'unicodecsv': '0.14.1'}
92         pkgs = packages.pip_list()
93         for name, version in (pkgs_ref.items()):
94             self.assertEqual(version, pkgs[name])