e3adce3636ba573f55f08e21405a2d1ca982f794
[sdc/sdc-distribution-client.git] /
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
14 import os.path
15 import shlex
16
17 import fixtures
18 import testscenarios
19 import testtools
20 import virtualenv
21
22 from pbr.tests import base
23
24 PIPFLAGS = shlex.split(os.environ.get('PIPFLAGS', ''))
25 PIPVERSION = os.environ.get('PIPVERSION', 'pip')
26 PBRVERSION = os.environ.get('PBRVERSION', 'pbr')
27 REPODIR = os.environ.get('REPODIR', '')
28 WHEELHOUSE = os.environ.get('WHEELHOUSE', '')
29 PIP_CMD = ['-m', 'pip'] + PIPFLAGS + ['install', '-f', WHEELHOUSE]
30 PROJECTS = shlex.split(os.environ.get('PROJECTS', ''))
31
32
33 def all_projects():
34     if not REPODIR:
35         return
36     # Future: make this path parameterisable.
37     excludes = set(['pypi-mirror', 'jeepyb', 'tempest', 'requirements'])
38     for name in PROJECTS:
39         name = name.strip()
40         short_name = name.split('/')[-1]
41         try:
42             with open(os.path.join(
43                     REPODIR, short_name, 'setup.py'), 'rt') as f:
44                 if 'pbr' not in f.read():
45                     continue
46         except IOError:
47             continue
48         if short_name in excludes:
49             continue
50         yield (short_name, dict(name=name, short_name=short_name))
51
52
53 class TestIntegration(base.BaseTestCase):
54
55     scenarios = list(all_projects())
56
57     def setUp(self):
58         # Integration tests need a higher default - big repos can be slow to
59         # clone, particularly under guest load.
60         os.environ['OS_TEST_TIMEOUT'] = os.environ.get('OS_TEST_TIMEOUT',
61                                                        '600')
62         super(TestIntegration, self).setUp()
63         base._config_git()
64
65     def venv(self, reason):
66         path = self.useFixture(fixtures.TempDir()).path
67         virtualenv.create_environment(path, clear=True)
68         python = os.path.join(path, 'bin', 'python')
69         self.useFixture(base.CapturedSubprocess(
70             'mkvenv-' + reason, [python] + PIP_CMD + [
71                 '-U', PIPVERSION, 'wheel', PBRVERSION]))
72         return path, python
73
74     @testtools.skipUnless(
75         os.environ.get('PBR_INTEGRATION', None) == '1',
76         'integration tests not enabled')
77     def test_integration(self):
78         # Test that we can:
79         # - run sdist from the repo in a venv
80         # - install the resulting tarball in a new venv
81         # - pip install the repo
82         # - pip install -e the repo
83         # We don't break these into separate tests because we'd need separate
84         # source dirs to isolate from side effects of running pip, and the
85         # overheads of setup would start to beat the benefits of parallelism.
86         self.useFixture(base.CapturedSubprocess(
87             'sync-req',
88             ['python', 'update.py', os.path.join(REPODIR, self.short_name)],
89             cwd=os.path.join(REPODIR, 'requirements')))
90         self.useFixture(base.CapturedSubprocess(
91             'commit-requirements',
92             'git diff --quiet || git commit -amrequirements',
93             cwd=os.path.join(REPODIR, self.short_name), shell=True))
94         path = os.path.join(
95             self.useFixture(fixtures.TempDir()).path, 'project')
96         self.useFixture(base.CapturedSubprocess(
97             'clone',
98             ['git', 'clone', os.path.join(REPODIR, self.short_name), path]))
99         _, python = self.venv('sdist')
100         self.useFixture(base.CapturedSubprocess(
101             'sdist', [python, 'setup.py', 'sdist'], cwd=path))
102         _, python = self.venv('tarball')
103         filename = os.path.join(
104             path, 'dist', os.listdir(os.path.join(path, 'dist'))[0])
105         self.useFixture(base.CapturedSubprocess(
106             'tarball', [python] + PIP_CMD + [filename]))
107         root, python = self.venv('install-git')
108         self.useFixture(base.CapturedSubprocess(
109             'install-git', [python] + PIP_CMD + ['git+file://' + path]))
110         if self.short_name == 'nova':
111             found = False
112             for _, _, filenames in os.walk(root):
113                 if 'migrate.cfg' in filenames:
114                     found = True
115             self.assertTrue(found)
116         _, python = self.venv('install-e')
117         self.useFixture(base.CapturedSubprocess(
118             'install-e', [python] + PIP_CMD + ['-e', path]))
119
120
121 def load_tests(loader, in_tests, pattern):
122     return testscenarios.load_tests_apply_scenarios(loader, in_tests, pattern)