Show More
@@ -1,265 +1,269 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2018 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | py.test config for test suite for making push/pull operations. |
|
23 | 23 | |
|
24 | 24 | .. important:: |
|
25 | 25 | |
|
26 | 26 | You must have git >= 1.8.5 for tests to work fine. With 68b939b git started |
|
27 | 27 | to redirect things to stderr instead of stdout. |
|
28 | 28 | """ |
|
29 | 29 | |
|
30 | 30 | import os |
|
31 | 31 | import tempfile |
|
32 | 32 | import textwrap |
|
33 | 33 | import pytest |
|
34 | 34 | |
|
35 | 35 | from rhodecode import events |
|
36 | 36 | from rhodecode.model.db import Integration |
|
37 | 37 | from rhodecode.model.integration import IntegrationModel |
|
38 | 38 | from rhodecode.model.db import Repository |
|
39 | 39 | from rhodecode.model.meta import Session |
|
40 | 40 | from rhodecode.model.settings import SettingsModel |
|
41 | 41 | from rhodecode.integrations.types.webhook import WebhookIntegrationType |
|
42 | 42 | |
|
43 | 43 | from rhodecode.tests import GIT_REPO, HG_REPO |
|
44 | 44 | from rhodecode.tests.fixture import Fixture |
|
45 | 45 | from rhodecode.tests.server_utils import RcWebServer |
|
46 | 46 | |
|
47 | 47 | REPO_GROUP = 'a_repo_group' |
|
48 | 48 | HG_REPO_WITH_GROUP = '%s/%s' % (REPO_GROUP, HG_REPO) |
|
49 | 49 | GIT_REPO_WITH_GROUP = '%s/%s' % (REPO_GROUP, GIT_REPO) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | @pytest.fixture(scope="module") |
|
53 | 53 | def rcextensions(request, db_connection, tmpdir_factory): |
|
54 | 54 | """ |
|
55 | 55 | Installs a testing rcextensions pack to ensure they work as expected. |
|
56 | 56 | """ |
|
57 | 57 | init_content = textwrap.dedent(""" |
|
58 | 58 | # Forward import the example rcextensions to make it |
|
59 | 59 | # active for our tests. |
|
60 | 60 | from rhodecode.tests.other.example_rcextensions import * |
|
61 | 61 | """) |
|
62 | 62 | |
|
63 | 63 | # Note: rcextensions are looked up based on the path of the ini file |
|
64 | 64 | root_path = tmpdir_factory.getbasetemp() |
|
65 | 65 | rcextensions_path = root_path.join('rcextensions') |
|
66 | 66 | init_path = rcextensions_path.join('__init__.py') |
|
67 | 67 | |
|
68 | 68 | if rcextensions_path.check(): |
|
69 | 69 | pytest.fail( |
|
70 | 70 | "Path for rcextensions already exists, please clean up before " |
|
71 | 71 | "test run this path: %s" % (rcextensions_path, )) |
|
72 | 72 | return |
|
73 | 73 | |
|
74 | 74 | request.addfinalizer(rcextensions_path.remove) |
|
75 | 75 | init_path.write_binary(init_content, ensure=True) |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | @pytest.fixture(scope="module") |
|
79 | 79 | def repos(request, db_connection): |
|
80 | 80 | """Create a copy of each test repo in a repo group.""" |
|
81 | 81 | fixture = Fixture() |
|
82 | 82 | repo_group = fixture.create_repo_group(REPO_GROUP) |
|
83 | 83 | repo_group_id = repo_group.group_id |
|
84 | 84 | fixture.create_fork(HG_REPO, HG_REPO, |
|
85 | 85 | repo_name_full=HG_REPO_WITH_GROUP, |
|
86 | 86 | repo_group=repo_group_id) |
|
87 | 87 | fixture.create_fork(GIT_REPO, GIT_REPO, |
|
88 | 88 | repo_name_full=GIT_REPO_WITH_GROUP, |
|
89 | 89 | repo_group=repo_group_id) |
|
90 | 90 | |
|
91 | 91 | @request.addfinalizer |
|
92 | 92 | def cleanup(): |
|
93 | 93 | fixture.destroy_repo(HG_REPO_WITH_GROUP) |
|
94 | 94 | fixture.destroy_repo(GIT_REPO_WITH_GROUP) |
|
95 | 95 | fixture.destroy_repo_group(repo_group_id) |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | @pytest.fixture(scope="module") |
|
99 | 99 | def rc_web_server_config_modification(): |
|
100 | 100 | return [] |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | @pytest.fixture(scope="module") |
|
104 | 104 | def rc_web_server_config_factory(testini_factory, rc_web_server_config_modification): |
|
105 | 105 | """ |
|
106 | 106 | Configuration file used for the fixture `rc_web_server`. |
|
107 | 107 | """ |
|
108 | 108 | |
|
109 | def factory(vcsserver_port): | |
|
109 | def factory(rcweb_port, vcsserver_port): | |
|
110 | 110 | custom_params = [ |
|
111 | 111 | {'handler_console': {'level': 'DEBUG'}}, |
|
112 | {'server:main': {'port': rcweb_port}}, | |
|
112 | 113 | {'app:main': {'vcs.server': 'localhost:%s' % vcsserver_port}} |
|
113 | 114 | ] |
|
114 | 115 | custom_params.extend(rc_web_server_config_modification) |
|
115 | 116 | return testini_factory(custom_params) |
|
116 | 117 | return factory |
|
117 | 118 | |
|
118 | 119 | |
|
119 | 120 | @pytest.fixture(scope="module") |
|
120 | 121 | def rc_web_server( |
|
121 | 122 | request, vcsserver_factory, available_port_factory, |
|
122 | 123 | rc_web_server_config_factory, repos, rcextensions): |
|
123 | 124 | """ |
|
124 | 125 | Run the web server as a subprocess. with it's own instance of vcsserver |
|
125 | 126 | """ |
|
127 | rcweb_port = available_port_factory() | |
|
128 | print('Using rcweb ops test port {}'.format(rcweb_port)) | |
|
126 | 129 | |
|
127 | 130 | vcsserver_port = available_port_factory() |
|
128 | 131 | print('Using vcsserver ops test port {}'.format(vcsserver_port)) |
|
129 | 132 | |
|
130 | 133 | vcs_log = os.path.join(tempfile.gettempdir(), 'rc_op_vcs.log') |
|
131 | 134 | vcsserver_factory( |
|
132 | 135 | request, vcsserver_port=vcsserver_port, |
|
133 | 136 | log_file=vcs_log, |
|
134 | 137 | overrides=( |
|
135 | 138 | {'server:main': {'workers': 2}}, |
|
136 | 139 | {'server:main': {'graceful_timeout': 10}}, |
|
137 | 140 | )) |
|
138 | 141 | |
|
139 | 142 | rc_log = os.path.join(tempfile.gettempdir(), 'rc_op_web.log') |
|
140 | 143 | rc_web_server_config = rc_web_server_config_factory( |
|
144 | rcweb_port=rcweb_port, | |
|
141 | 145 | vcsserver_port=vcsserver_port) |
|
142 | 146 | server = RcWebServer(rc_web_server_config, log_file=rc_log) |
|
143 | 147 | server.start() |
|
144 | 148 | |
|
145 | 149 | @request.addfinalizer |
|
146 | 150 | def cleanup(): |
|
147 | 151 | server.shutdown() |
|
148 | 152 | |
|
149 | 153 | server.wait_until_ready() |
|
150 | 154 | return server |
|
151 | 155 | |
|
152 | 156 | |
|
153 | 157 | @pytest.fixture |
|
154 | 158 | def disable_locking(baseapp): |
|
155 | 159 | r = Repository.get_by_repo_name(GIT_REPO) |
|
156 | 160 | Repository.unlock(r) |
|
157 | 161 | r.enable_locking = False |
|
158 | 162 | Session().add(r) |
|
159 | 163 | Session().commit() |
|
160 | 164 | |
|
161 | 165 | r = Repository.get_by_repo_name(HG_REPO) |
|
162 | 166 | Repository.unlock(r) |
|
163 | 167 | r.enable_locking = False |
|
164 | 168 | Session().add(r) |
|
165 | 169 | Session().commit() |
|
166 | 170 | |
|
167 | 171 | |
|
168 | 172 | @pytest.fixture |
|
169 | 173 | def enable_auth_plugins(request, baseapp, csrf_token): |
|
170 | 174 | """ |
|
171 | 175 | Return a factory object that when called, allows to control which |
|
172 | 176 | authentication plugins are enabled. |
|
173 | 177 | """ |
|
174 | 178 | def _enable_plugins(plugins_list, override=None): |
|
175 | 179 | override = override or {} |
|
176 | 180 | params = { |
|
177 | 181 | 'auth_plugins': ','.join(plugins_list), |
|
178 | 182 | } |
|
179 | 183 | |
|
180 | 184 | # helper translate some names to others |
|
181 | 185 | name_map = { |
|
182 | 186 | 'token': 'authtoken' |
|
183 | 187 | } |
|
184 | 188 | |
|
185 | 189 | for module in plugins_list: |
|
186 | 190 | plugin_name = module.partition('#')[-1] |
|
187 | 191 | if plugin_name in name_map: |
|
188 | 192 | plugin_name = name_map[plugin_name] |
|
189 | 193 | enabled_plugin = 'auth_%s_enabled' % plugin_name |
|
190 | 194 | cache_ttl = 'auth_%s_cache_ttl' % plugin_name |
|
191 | 195 | |
|
192 | 196 | # default params that are needed for each plugin, |
|
193 | 197 | # `enabled` and `cache_ttl` |
|
194 | 198 | params.update({ |
|
195 | 199 | enabled_plugin: True, |
|
196 | 200 | cache_ttl: 0 |
|
197 | 201 | }) |
|
198 | 202 | if override.get: |
|
199 | 203 | params.update(override.get(module, {})) |
|
200 | 204 | |
|
201 | 205 | validated_params = params |
|
202 | 206 | for k, v in validated_params.items(): |
|
203 | 207 | setting = SettingsModel().create_or_update_setting(k, v) |
|
204 | 208 | Session().add(setting) |
|
205 | 209 | Session().commit() |
|
206 | 210 | |
|
207 | 211 | def cleanup(): |
|
208 | 212 | _enable_plugins(['egg:rhodecode-enterprise-ce#rhodecode']) |
|
209 | 213 | |
|
210 | 214 | request.addfinalizer(cleanup) |
|
211 | 215 | |
|
212 | 216 | return _enable_plugins |
|
213 | 217 | |
|
214 | 218 | |
|
215 | 219 | @pytest.fixture |
|
216 | 220 | def fs_repo_only(request, rhodecode_fixtures): |
|
217 | 221 | def fs_repo_fabric(repo_name, repo_type): |
|
218 | 222 | rhodecode_fixtures.create_repo(repo_name, repo_type=repo_type) |
|
219 | 223 | rhodecode_fixtures.destroy_repo(repo_name, fs_remove=False) |
|
220 | 224 | |
|
221 | 225 | def cleanup(): |
|
222 | 226 | rhodecode_fixtures.destroy_repo(repo_name, fs_remove=True) |
|
223 | 227 | rhodecode_fixtures.destroy_repo_on_filesystem(repo_name) |
|
224 | 228 | |
|
225 | 229 | request.addfinalizer(cleanup) |
|
226 | 230 | |
|
227 | 231 | return fs_repo_fabric |
|
228 | 232 | |
|
229 | 233 | |
|
230 | 234 | @pytest.fixture |
|
231 | 235 | def enable_webhook_push_integration(request): |
|
232 | 236 | integration = Integration() |
|
233 | 237 | integration.integration_type = WebhookIntegrationType.key |
|
234 | 238 | Session().add(integration) |
|
235 | 239 | |
|
236 | 240 | settings = dict( |
|
237 | 241 | url='http://httpbin.org', |
|
238 | 242 | secret_token='secret', |
|
239 | 243 | username=None, |
|
240 | 244 | password=None, |
|
241 | 245 | custom_header_key=None, |
|
242 | 246 | custom_header_val=None, |
|
243 | 247 | method_type='get', |
|
244 | 248 | events=[events.RepoPushEvent.name], |
|
245 | 249 | log_data=True |
|
246 | 250 | ) |
|
247 | 251 | |
|
248 | 252 | IntegrationModel().update_integration( |
|
249 | 253 | integration, |
|
250 | 254 | name='IntegrationWebhookTest', |
|
251 | 255 | enabled=True, |
|
252 | 256 | settings=settings, |
|
253 | 257 | repo=None, |
|
254 | 258 | repo_group=None, |
|
255 | 259 | child_repos_only=False, |
|
256 | 260 | ) |
|
257 | 261 | Session().commit() |
|
258 | 262 | integration_id = integration.integration_id |
|
259 | 263 | |
|
260 | 264 | @request.addfinalizer |
|
261 | 265 | def cleanup(): |
|
262 | 266 | integration = Integration.get(integration_id) |
|
263 | 267 | Session().delete(integration) |
|
264 | 268 | Session().commit() |
|
265 | 269 |
General Comments 0
You need to be logged in to leave comments.
Login now