From 22cccd2107867db2b7c0dbf5abc4e4065385ff40 Mon Sep 17 00:00:00 2001 From: ch3ll Date: Mon, 16 Mar 2020 15:33:43 -0400 Subject: [PATCH] Use virtualenv test helper that already exists and fix setup.py --- setup.py | 18 +++++-- tests/integration/setup/__init__.py | 1 + tests/integration/setup/test_bdist.py | 48 +++++++++++++++++ tests/integration/test_setup.py | 48 ----------------- tests/runtests.py | 10 ++++ tests/support/virtualenv.py | 77 --------------------------- tests/unit/test_module_names.py | 1 + 7 files changed, 75 insertions(+), 128 deletions(-) create mode 100644 tests/integration/setup/__init__.py create mode 100644 tests/integration/setup/test_bdist.py delete mode 100644 tests/integration/test_setup.py delete mode 100644 tests/support/virtualenv.py diff --git a/setup.py b/setup.py index 396f85dca5a..48b779997df 100755 --- a/setup.py +++ b/setup.py @@ -38,6 +38,11 @@ from setuptools.command.develop import develop from setuptools.command.install import install from setuptools.command.sdist import sdist from setuptools.command.egg_info import egg_info +try: + from wheel.bdist_wheel import bdist_wheel + HAS_BDIST_WHEEL = True +except ImportError: + HAS_BDIST_WHEEL = False # pylint: enable=E0611 try: @@ -642,6 +647,14 @@ class Clean(clean): os.remove(to_remove_filename) +if HAS_BDIST_WHEEL: + class BDistWheel(bdist_wheel): + + def finalize_options(self): + bdist_wheel.finalize_options(self) + self.distribution.build_wheel = True + + INSTALL_VERSION_TEMPLATE = '''\ # This file was auto-generated by salt's setup @@ -913,6 +926,8 @@ class SaltDistribution(distutils.dist.Distribution): 'install_lib': InstallLib}) if IS_WINDOWS_PLATFORM: self.cmdclass.update({'download-windows-dlls': DownloadWindowsDlls}) + if HAS_BDIST_WHEEL: + self.cmdclass["bdist_wheel"] = BDistWheel self.license = 'Apache Software License 2.0' self.packages = self.discover_packages() @@ -1229,9 +1244,6 @@ class SaltDistribution(distutils.dist.Distribution): if not self.ssh_packaging and PACKAGED_FOR_SALT_SSH: self.ssh_packaging = 1 - if 'bdist_wheel' in sys.argv: - self.build_wheel = True - if self.ssh_packaging: self.metadata.name = 'salt-ssh' self.salt_transport = 'ssh' diff --git a/tests/integration/setup/__init__.py b/tests/integration/setup/__init__.py new file mode 100644 index 00000000000..40a96afc6ff --- /dev/null +++ b/tests/integration/setup/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/integration/setup/test_bdist.py b/tests/integration/setup/test_bdist.py new file mode 100644 index 00000000000..ce2446b5c79 --- /dev/null +++ b/tests/integration/setup/test_bdist.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +''' +tests.integration.setup.test_bdist +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +''' + +# Import python libs +from __future__ import absolute_import, print_function, unicode_literals +import os +import re + +# Import Salt Testing libs +from tests.support.runtests import RUNTIME_VARS +from tests.support.unit import skipIf +from tests.support.helpers import skip_if_not_root, VirtualEnv +from tests.support.case import ModuleCase + +# Import salt libs +import salt.utils.path +import salt.utils.platform +from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES + + +@skip_if_not_root +@skipIf(salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed') +class BdistSetupTest(ModuleCase): + ''' + Tests for building and installing bdist_wheel packages + ''' + def test_wheel_build(self): + ''' + test building a bdist_wheel package + ''' + # Let's create the testing virtualenv + with VirtualEnv() as venv: + ret = self.run_function('cmd.run', ['{0} setup.py bdist_wheel --dist-dir={1}'.format(venv.venv_python, venv.venv_dir)], cwd=RUNTIME_VARS.CODE_DIR) + + for _file in os.listdir(venv.venv_dir): + if _file.endswith('whl'): + whl = os.path.join(venv.venv_dir, _file) + break + + ret = self.run_function('pip.install', pkgs=whl, bin_env=venv.venv_dir) + + # Let's ensure the version is correct + pip_ver = self.run_function('pip.list', bin_env=venv.venv_dir).get('salt') + whl_ver = [x for x in whl.split('/')[-1:][0].split('-') if re.search(r'^\d.\d*', x)][0] + assert pip_ver == whl_ver.replace('_', '-') diff --git a/tests/integration/test_setup.py b/tests/integration/test_setup.py deleted file mode 100644 index 47df24add18..00000000000 --- a/tests/integration/test_setup.py +++ /dev/null @@ -1,48 +0,0 @@ -# -*- coding: utf-8 -*- -''' -tests.integration.test_setup -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -''' - -# Import python libs -from __future__ import absolute_import, print_function, unicode_literals -import os -import re - -# Import Salt Testing libs -from tests.support.runtests import RUNTIME_VARS -from tests.support.unit import skipIf -from tests.support.helpers import skip_if_not_root -from tests.support.virtualenv import VirtualEnvHelper - -# Import salt libs -import salt.utils.path -import salt.utils.platform -from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES - - -@skip_if_not_root -@skipIf(salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed') -class SetupTest(VirtualEnvHelper): - ''' - Tests for building and installing packages using setup.py - ''' - def test_wheel_build(self): - ''' - test building a bdist_wheel package and ensure - ''' - # Let's create the testing virtualenv - self._create_virtualenv(self.venv_dir) - ret = self.run_function('cmd.run', ['python setup.py bdist_wheel --dist-dir={0}'.format(self.venv_dir)], cwd=RUNTIME_VARS.CODE_DIR) - - for _file in os.listdir(self.venv_dir): - if _file.endswith('whl'): - whl = os.path.join(self.venv_dir, _file) - break - - ret = self.run_function('pip.install', pkgs=whl, bin_env=self.venv_dir) - - # Let's ensure the version is correct - pip_ver = self.run_function('pip.list', bin_env=self.venv_dir).get('salt') - whl_ver = [x for x in whl.split('/')[-1:][0].split('-') if re.search(r'^\d.\d*', x)][0] - assert pip_ver == whl_ver diff --git a/tests/runtests.py b/tests/runtests.py index 5481383834e..4b706d2aa02 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -143,6 +143,9 @@ TEST_SUITES_UNORDERED = { 'returners': {'display_name': 'Returners', 'path': 'integration/returners'}, + 'setup': + {'display_name': 'Setup', + 'path': 'integration/setup'}, 'ssh-int': {'display_name': 'SSH Integration', 'path': 'integration/ssh'}, @@ -399,6 +402,13 @@ class SaltTestsuiteParser(SaltCoverageTestingParser): action='store_true', help='Run spm integration tests' ) + self.test_selection_group.add_option( + '--setup', + dest='setup', + default=False, + action='store_true', + help='Run setup integration tests' + ) self.test_selection_group.add_option( '-l', '--loader', diff --git a/tests/support/virtualenv.py b/tests/support/virtualenv.py deleted file mode 100644 index 51b34f31146..00000000000 --- a/tests/support/virtualenv.py +++ /dev/null @@ -1,77 +0,0 @@ -# -*- coding: utf-8 -*- -''' -helper for creating virtualenv's in tests -''' - -# Import python libs -from __future__ import absolute_import, print_function, unicode_literals -import os -import sys -import shutil -import tempfile - -# Import salt libs -import salt.utils.path -import salt.utils.platform - -# Import Salt Testing libs -from tests.support.runtests import RUNTIME_VARS -from tests.support.case import ModuleCase -from tests.support.helpers import patched_environ - - -class VirtualEnvHelper(ModuleCase): - - def setUp(self): - self.venv_test_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP) - # Remove the venv test directory - self.addCleanup(shutil.rmtree, self.venv_test_dir, ignore_errors=True) - self.venv_dir = os.path.join(self.venv_test_dir, 'venv') - self.pip_temp = os.path.join(self.venv_test_dir, '.pip-temp') - if not os.path.isdir(self.pip_temp): - os.makedirs(self.pip_temp) - self.patched_environ = patched_environ( - PIP_SOURCE_DIR='', - PIP_BUILD_DIR='', - __cleanup__=[k for k in os.environ if k.startswith('PIP_')] - ) - self.patched_environ.__enter__() - self.addCleanup(self.patched_environ.__exit__) - - def _create_virtualenv(self, path): - ''' - The reason why the virtualenv creation is proxied by this function is mostly - because under windows, we can't seem to properly create a virtualenv off of - another virtualenv(we can on linux) and also because, we really don't want to - test virtualenv creation off of another virtualenv, we want a virtualenv created - from the original python. - Also, one windows, we must also point to the virtualenv binary outside the existing - virtualenv because it will fail otherwise - ''' - try: - if salt.utils.platform.is_windows(): - python = os.path.join(sys.real_prefix, os.path.basename(sys.executable)) - else: - python_binary_names = [ - 'python{}.{}'.format(*sys.version_info), - 'python{}'.format(*sys.version_info), - 'python' - ] - for binary_name in python_binary_names: - python = os.path.join(sys.real_prefix, 'bin', binary_name) - if os.path.exists(python): - break - else: - self.fail( - 'Couldn\'t find a python binary name under \'{}\' matching: {}'.format( - os.path.join(sys.real_prefix, 'bin'), - python_binary_names - ) - ) - # We're running off a virtualenv, and we don't want to create a virtualenv off of - # a virtualenv - kwargs = {'python': python} - except AttributeError: - # We're running off of the system python - kwargs = {} - self.run_function('virtualenv.create', [path], **kwargs) diff --git a/tests/unit/test_module_names.py b/tests/unit/test_module_names.py index 8528285c282..5d91dfd383b 100644 --- a/tests/unit/test_module_names.py +++ b/tests/unit/test_module_names.py @@ -159,6 +159,7 @@ class BadTestModuleNamesTestCase(TestCase): 'integration.scheduler.test_maxrunning', 'integration.scheduler.test_helpers', 'integration.scheduler.test_run_job', + 'integration.setup.test_bdist', 'integration.shell.test_spm', 'integration.shell.test_cp', 'integration.shell.test_syndic',