test_integration.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. # implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. try:
  14. import configparser
  15. except ImportError:
  16. import ConfigParser as configparser
  17. import os.path
  18. import pkg_resources
  19. import shlex
  20. import sys
  21. import fixtures
  22. import testtools
  23. import textwrap
  24. from pbr.tests import base
  25. from pbr.tests import test_packaging
  26. PIPFLAGS = shlex.split(os.environ.get('PIPFLAGS', ''))
  27. PIPVERSION = os.environ.get('PIPVERSION', 'pip')
  28. PBRVERSION = os.environ.get('PBRVERSION', 'pbr')
  29. REPODIR = os.environ.get('REPODIR', '')
  30. WHEELHOUSE = os.environ.get('WHEELHOUSE', '')
  31. PIP_CMD = ['-m', 'pip'] + PIPFLAGS + ['install', '-f', WHEELHOUSE]
  32. PROJECTS = shlex.split(os.environ.get('PROJECTS', ''))
  33. PBR_ROOT = os.path.abspath(os.path.join(__file__, '..', '..', '..'))
  34. def all_projects():
  35. if not REPODIR:
  36. return
  37. # Future: make this path parameterisable.
  38. excludes = set(['tempest', 'requirements'])
  39. for name in PROJECTS:
  40. name = name.strip()
  41. short_name = name.split('/')[-1]
  42. try:
  43. with open(os.path.join(
  44. REPODIR, short_name, 'setup.py'), 'rt') as f:
  45. if 'pbr' not in f.read():
  46. continue
  47. except IOError:
  48. continue
  49. if short_name in excludes:
  50. continue
  51. yield (short_name, dict(name=name, short_name=short_name))
  52. class TestIntegration(base.BaseTestCase):
  53. scenarios = list(all_projects())
  54. def setUp(self):
  55. # Integration tests need a higher default - big repos can be slow to
  56. # clone, particularly under guest load.
  57. env = fixtures.EnvironmentVariable(
  58. 'OS_TEST_TIMEOUT', os.environ.get('OS_TEST_TIMEOUT', '600'))
  59. with env:
  60. super(TestIntegration, self).setUp()
  61. base._config_git()
  62. @testtools.skipUnless(
  63. os.environ.get('PBR_INTEGRATION', None) == '1',
  64. 'integration tests not enabled')
  65. def test_integration(self):
  66. # Test that we can:
  67. # - run sdist from the repo in a venv
  68. # - install the resulting tarball in a new venv
  69. # - pip install the repo
  70. # - pip install -e the repo
  71. # We don't break these into separate tests because we'd need separate
  72. # source dirs to isolate from side effects of running pip, and the
  73. # overheads of setup would start to beat the benefits of parallelism.
  74. path = os.path.join(REPODIR, self.short_name)
  75. setup_cfg = os.path.join(path, 'setup.cfg')
  76. project_name = pkg_resources.safe_name(self.short_name).lower()
  77. # These projects should all have setup.cfg files but we'll be careful
  78. if os.path.exists(setup_cfg):
  79. config = configparser.ConfigParser()
  80. config.read(setup_cfg)
  81. if config.has_section('metadata'):
  82. raw_name = config.get('metadata', 'name',
  83. fallback='notapackagename')
  84. # Technically we should really only need to use the raw
  85. # name because all our projects should be good and use
  86. # normalized names but they don't...
  87. project_name = pkg_resources.safe_name(raw_name).lower()
  88. constraints = os.path.join(REPODIR, 'requirements',
  89. 'upper-constraints.txt')
  90. tmp_constraints = os.path.join(
  91. self.useFixture(fixtures.TempDir()).path,
  92. 'upper-constraints.txt')
  93. # We need to filter out the package we are installing to avoid
  94. # conflicts with the constraints.
  95. with open(constraints, 'r') as src:
  96. with open(tmp_constraints, 'w') as dest:
  97. for line in src:
  98. constraint = line.split('===')[0]
  99. if project_name != constraint:
  100. dest.write(line)
  101. pip_cmd = PIP_CMD + ['-c', tmp_constraints]
  102. venv = self.useFixture(
  103. test_packaging.Venv('sdist',
  104. modules=['pip', 'wheel', PBRVERSION],
  105. pip_cmd=PIP_CMD))
  106. python = venv.python
  107. self.useFixture(base.CapturedSubprocess(
  108. 'sdist', [python, 'setup.py', 'sdist'], cwd=path))
  109. venv = self.useFixture(
  110. test_packaging.Venv('tarball',
  111. modules=['pip', 'wheel', PBRVERSION],
  112. pip_cmd=PIP_CMD))
  113. python = venv.python
  114. filename = os.path.join(
  115. path, 'dist', os.listdir(os.path.join(path, 'dist'))[0])
  116. self.useFixture(base.CapturedSubprocess(
  117. 'tarball', [python] + pip_cmd + [filename]))
  118. venv = self.useFixture(
  119. test_packaging.Venv('install-git',
  120. modules=['pip', 'wheel', PBRVERSION],
  121. pip_cmd=PIP_CMD))
  122. root = venv.path
  123. python = venv.python
  124. self.useFixture(base.CapturedSubprocess(
  125. 'install-git', [python] + pip_cmd + ['git+file://' + path]))
  126. if self.short_name == 'nova':
  127. found = False
  128. for _, _, filenames in os.walk(root):
  129. if 'alembic.ini' in filenames:
  130. found = True
  131. self.assertTrue(found)
  132. venv = self.useFixture(
  133. test_packaging.Venv('install-e',
  134. modules=['pip', 'wheel', PBRVERSION],
  135. pip_cmd=PIP_CMD))
  136. root = venv.path
  137. python = venv.python
  138. self.useFixture(base.CapturedSubprocess(
  139. 'install-e', [python] + pip_cmd + ['-e', path]))
  140. class TestInstallWithoutPbr(base.BaseTestCase):
  141. @testtools.skipUnless(
  142. os.environ.get('PBR_INTEGRATION', None) == '1',
  143. 'integration tests not enabled')
  144. def test_install_without_pbr(self):
  145. # Test easy-install of a thing that depends on a thing using pbr
  146. tempdir = self.useFixture(fixtures.TempDir()).path
  147. # A directory containing sdists of the things we're going to depend on
  148. # in using-package.
  149. dist_dir = os.path.join(tempdir, 'distdir')
  150. os.mkdir(dist_dir)
  151. self._run_cmd(sys.executable, ('setup.py', 'sdist', '-d', dist_dir),
  152. allow_fail=False, cwd=PBR_ROOT)
  153. # testpkg - this requires a pbr-using package
  154. test_pkg_dir = os.path.join(tempdir, 'testpkg')
  155. os.mkdir(test_pkg_dir)
  156. pkgs = {
  157. 'pkgTest': {
  158. 'setup.py': textwrap.dedent("""\
  159. #!/usr/bin/env python
  160. import setuptools
  161. setuptools.setup(
  162. name = 'pkgTest',
  163. tests_require = ['pkgReq'],
  164. test_suite='pkgReq'
  165. )
  166. """),
  167. 'setup.cfg': textwrap.dedent("""\
  168. [easy_install]
  169. find_links = %s
  170. """ % dist_dir)},
  171. 'pkgReq': {
  172. 'requirements.txt': textwrap.dedent("""\
  173. pbr
  174. """),
  175. 'pkgReq/__init__.py': textwrap.dedent("""\
  176. print("FakeTest loaded and ran")
  177. """)},
  178. }
  179. pkg_dirs = self.useFixture(
  180. test_packaging.CreatePackages(pkgs)).package_dirs
  181. test_pkg_dir = pkg_dirs['pkgTest']
  182. req_pkg_dir = pkg_dirs['pkgReq']
  183. self._run_cmd(sys.executable, ('setup.py', 'sdist', '-d', dist_dir),
  184. allow_fail=False, cwd=req_pkg_dir)
  185. # A venv to test within
  186. venv = self.useFixture(test_packaging.Venv('nopbr', ['pip', 'wheel']))
  187. python = venv.python
  188. # Run the depending script
  189. self.useFixture(base.CapturedSubprocess(
  190. 'nopbr', [python] + ['setup.py', 'test'], cwd=test_pkg_dir))
  191. class TestMarkersPip(base.BaseTestCase):
  192. scenarios = [
  193. ('pip-latest', {'modules': ['pip']}),
  194. (
  195. 'setuptools-Bullseye',
  196. {'modules': ['pip==20.3.4', 'setuptools==52.0.0']},
  197. ),
  198. (
  199. 'setuptools-Focal',
  200. {'modules': ['pip==20.0.2', 'setuptools==45.2.0']},
  201. ),
  202. (
  203. 'setuptools-Jammy',
  204. {'modules': ['pip==22.0.2', 'setuptools==59.6.0']},
  205. ),
  206. ]
  207. @testtools.skipUnless(
  208. os.environ.get('PBR_INTEGRATION', None) == '1',
  209. 'integration tests not enabled',
  210. )
  211. def test_pip_versions(self):
  212. pkgs = {
  213. 'test_markers':
  214. {'requirements.txt': textwrap.dedent("""\
  215. pkg_a; python_version=='1.2'
  216. pkg_b; python_version!='1.2'
  217. """)},
  218. 'pkg_a': {},
  219. 'pkg_b': {},
  220. }
  221. pkg_dirs = self.useFixture(
  222. test_packaging.CreatePackages(pkgs)).package_dirs
  223. temp_dir = self.useFixture(fixtures.TempDir()).path
  224. repo_dir = os.path.join(temp_dir, 'repo')
  225. venv = self.useFixture(test_packaging.Venv('markers'))
  226. bin_python = venv.python
  227. os.mkdir(repo_dir)
  228. for module in self.modules:
  229. self._run_cmd(
  230. bin_python,
  231. ['-m', 'pip', 'install', '--upgrade', module],
  232. cwd=venv.path, allow_fail=False)
  233. for pkg in pkg_dirs:
  234. self._run_cmd(
  235. bin_python, ['setup.py', 'sdist', '-d', repo_dir],
  236. cwd=pkg_dirs[pkg], allow_fail=False)
  237. self._run_cmd(
  238. bin_python,
  239. ['-m', 'pip', 'install', '--no-index', '-f', repo_dir,
  240. 'test_markers'],
  241. cwd=venv.path, allow_fail=False)
  242. self.assertIn('pkg-b', self._run_cmd(
  243. bin_python, ['-m', 'pip', 'freeze'], cwd=venv.path,
  244. allow_fail=False)[0])
  245. # Handle collections.abc moves in python breaking old pip
  246. # These versions come from the versions installed from the 'virtualenv'
  247. # command from the 'python-virtualenv' package.
  248. if sys.version_info[0:3] < (3, 10, 0):
  249. lts_scenarios = [
  250. ('Bionic', {'modules': ['pip==9.0.1', 'setuptools==39.0.1']}),
  251. ('Stretch', {'modules': ['pip==9.0.1', 'setuptools==33.1.1']}),
  252. ('EL8', {'modules': ['pip==9.0.3', 'setuptools==39.2.0']}),
  253. ('Buster', {'modules': ['pip==18.1', 'setuptools==40.8.0']}),
  254. ('Focal', {'modules': ['pip==20.0.2', 'setuptools==45.2.0']}),
  255. ]
  256. else:
  257. lts_scenarios = [
  258. ('Bullseye', {'modules': ['pip==20.3.4', 'setuptools==52.0.0']}),
  259. ('Focal', {'modules': ['pip==20.0.2', 'setuptools==45.2.0']}),
  260. ('Jammy', {'modules': ['pip==22.0.2', 'setuptools==59.6.0']}),
  261. ]
  262. class TestLTSSupport(base.BaseTestCase):
  263. scenarios = lts_scenarios
  264. @testtools.skipUnless(
  265. os.environ.get('PBR_INTEGRATION', None) == '1',
  266. 'integration tests not enabled',
  267. )
  268. def test_lts_venv_default_versions(self):
  269. venv = self.useFixture(
  270. test_packaging.Venv('setuptools', modules=self.modules))
  271. bin_python = venv.python
  272. pbr = 'file://%s#egg=pbr' % PBR_ROOT
  273. # Installing PBR is a reasonable indication that we are not broken on
  274. # this particular combination of setuptools and pip.
  275. self._run_cmd(bin_python, ['-m', 'pip', 'install', pbr],
  276. cwd=venv.path, allow_fail=False)