test_integration.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """Run some integration tests.
  2. Try to install a few packages.
  3. """
  4. import glob
  5. import os
  6. import sys
  7. import urllib.request
  8. import pytest
  9. from setuptools.command.easy_install import easy_install
  10. from setuptools.command import easy_install as easy_install_pkg
  11. from setuptools.dist import Distribution
  12. pytestmark = pytest.mark.skipif(
  13. 'platform.python_implementation() == "PyPy" and '
  14. 'platform.system() == "Windows"',
  15. reason="pypa/setuptools#2496",
  16. )
  17. def setup_module(module):
  18. packages = 'stevedore', 'virtualenvwrapper', 'pbr', 'novaclient'
  19. for pkg in packages:
  20. try:
  21. __import__(pkg)
  22. tmpl = "Integration tests cannot run when {pkg} is installed"
  23. pytest.skip(tmpl.format(**locals()))
  24. except ImportError:
  25. pass
  26. try:
  27. urllib.request.urlopen('https://pypi.python.org/pypi')
  28. except Exception as exc:
  29. pytest.skip(str(exc))
  30. @pytest.fixture
  31. def install_context(request, tmpdir, monkeypatch):
  32. """Fixture to set up temporary installation directory.
  33. """
  34. # Save old values so we can restore them.
  35. new_cwd = tmpdir.mkdir('cwd')
  36. user_base = tmpdir.mkdir('user_base')
  37. user_site = tmpdir.mkdir('user_site')
  38. install_dir = tmpdir.mkdir('install_dir')
  39. def fin():
  40. # undo the monkeypatch, particularly needed under
  41. # windows because of kept handle on cwd
  42. monkeypatch.undo()
  43. new_cwd.remove()
  44. user_base.remove()
  45. user_site.remove()
  46. install_dir.remove()
  47. request.addfinalizer(fin)
  48. # Change the environment and site settings to control where the
  49. # files are installed and ensure we do not overwrite anything.
  50. monkeypatch.chdir(new_cwd)
  51. monkeypatch.setattr(easy_install_pkg, '__file__', user_site.strpath)
  52. monkeypatch.setattr('site.USER_BASE', user_base.strpath)
  53. monkeypatch.setattr('site.USER_SITE', user_site.strpath)
  54. monkeypatch.setattr('sys.path', sys.path + [install_dir.strpath])
  55. monkeypatch.setenv(str('PYTHONPATH'), str(os.path.pathsep.join(sys.path)))
  56. # Set up the command for performing the installation.
  57. dist = Distribution()
  58. cmd = easy_install(dist)
  59. cmd.install_dir = install_dir.strpath
  60. return cmd
  61. def _install_one(requirement, cmd, pkgname, modulename):
  62. cmd.args = [requirement]
  63. cmd.ensure_finalized()
  64. cmd.run()
  65. target = cmd.install_dir
  66. dest_path = glob.glob(os.path.join(target, pkgname + '*.egg'))
  67. assert dest_path
  68. assert os.path.exists(os.path.join(dest_path[0], pkgname, modulename))
  69. def test_stevedore(install_context):
  70. _install_one('stevedore', install_context,
  71. 'stevedore', 'extension.py')
  72. @pytest.mark.xfail
  73. def test_virtualenvwrapper(install_context):
  74. _install_one('virtualenvwrapper', install_context,
  75. 'virtualenvwrapper', 'hook_loader.py')
  76. def test_pbr(install_context):
  77. _install_one('pbr', install_context,
  78. 'pbr', 'core.py')
  79. @pytest.mark.xfail
  80. def test_python_novaclient(install_context):
  81. _install_one('python-novaclient', install_context,
  82. 'novaclient', 'base.py')
  83. def test_pyuri(install_context):
  84. """
  85. Install the pyuri package (version 0.3.1 at the time of writing).
  86. This is also a regression test for issue #1016.
  87. """
  88. _install_one('pyuri', install_context, 'pyuri', 'uri.py')
  89. pyuri = install_context.installed_projects['pyuri']
  90. # The package data should be installed.
  91. assert os.path.exists(os.path.join(pyuri.location, 'pyuri', 'uri.regex'))