##// END OF EJS Templates
added new suite of tests for VCS operations...
marcink -
r2752:6d904a0c beta
parent child Browse files
Show More
@@ -78,7 +78,10 b' def load_environment(global_conf, app_co'
78
78
79 from rhodecode.lib.utils import create_test_env, create_test_index
79 from rhodecode.lib.utils import create_test_env, create_test_index
80 from rhodecode.tests import TESTS_TMP_PATH
80 from rhodecode.tests import TESTS_TMP_PATH
81 create_test_env(TESTS_TMP_PATH, config)
81 # set RC_NO_TMP_PATH=1 to disable re-creating the database and
82 # test repos
83 if not int(os.environ.get('RC_NO_TMP_PATH', 0)):
84 create_test_env(TESTS_TMP_PATH, config)
82 # set RC_WHOOSH_TEST_DISABLE=1 to disable whoosh index during tests
85 # set RC_WHOOSH_TEST_DISABLE=1 to disable whoosh index during tests
83 if not int(os.environ.get('RC_WHOOSH_TEST_DISABLE', 0)):
86 if not int(os.environ.get('RC_WHOOSH_TEST_DISABLE', 0)):
84 create_test_index(TESTS_TMP_PATH, config, True)
87 create_test_index(TESTS_TMP_PATH, config, True)
@@ -171,7 +171,7 b' class BaseVCSController(object):'
171 do the locking. Think about this as signals passed to hooks what to do.
171 do the locking. Think about this as signals passed to hooks what to do.
172
172
173 """
173 """
174 locked = False
174 locked = False # defines that locked error should be thrown to user
175 make_lock = None
175 make_lock = None
176 repo = Repository.get_by_repo_name(repo)
176 repo = Repository.get_by_repo_name(repo)
177 user = User.get(user_id)
177 user = User.get(user_id)
@@ -187,7 +187,7 b' class BaseVCSController(object):'
187 #check if it's already locked !, if it is compare users
187 #check if it's already locked !, if it is compare users
188 user_id, _date = repo.locked
188 user_id, _date = repo.locked
189 if user.user_id == user_id:
189 if user.user_id == user_id:
190 log.debug('Got push from user, now unlocking' % (user))
190 log.debug('Got push from user %s, now unlocking' % (user))
191 # unlock if we have push from user who locked
191 # unlock if we have push from user who locked
192 make_lock = False
192 make_lock = False
193 else:
193 else:
@@ -202,7 +202,8 b' class BaseVCSController(object):'
202
202
203 else:
203 else:
204 log.debug('Repository %s do not have locking enabled' % (repo))
204 log.debug('Repository %s do not have locking enabled' % (repo))
205
205 log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s'
206 % (make_lock, locked, locked_by))
206 return make_lock, locked, locked_by
207 return make_lock, locked, locked_by
207
208
208 def __call__(self, environ, start_response):
209 def __call__(self, environ, start_response):
@@ -6,7 +6,7 b''
6 Test suite for making push/pull operations.
6 Test suite for making push/pull operations.
7 Run using::
7 Run using::
8
8
9 RC_WHOOSH_TEST_DISABLE=1 nosetests rhodecode/tests/scripts/test_scm_operations.py
9 RC_WHOOSH_TEST_DISABLE=1 RC_NO_TMP_PATH=1 nosetests rhodecode/tests/scripts/test_vcs_operations.py
10
10
11 :created_on: Dec 30, 2010
11 :created_on: Dec 30, 2010
12 :author: marcink
12 :author: marcink
@@ -28,6 +28,7 b''
28
28
29 import os
29 import os
30 import tempfile
30 import tempfile
31 import unittest
31 from os.path import join as jn
32 from os.path import join as jn
32 from os.path import dirname as dn
33 from os.path import dirname as dn
33
34
@@ -37,6 +38,7 b' from subprocess import Popen, PIPE'
37 from rhodecode.tests import *
38 from rhodecode.tests import *
38 from rhodecode.model.db import User, Repository, UserLog
39 from rhodecode.model.db import User, Repository, UserLog
39 from rhodecode.model.meta import Session
40 from rhodecode.model.meta import Session
41 from rhodecode.model.repo import RepoModel
40
42
41 DEBUG = True
43 DEBUG = True
42 HOST = '127.0.0.1:5000' # test host
44 HOST = '127.0.0.1:5000' # test host
@@ -85,6 +87,51 b' def _construct_url(repo, dest=None, **kw'
85 return _url
87 return _url
86
88
87
89
90 def _add_files_and_push(vcs, DEST, **kwargs):
91 """
92 Generate some files, add it to DEST repo and push back
93 vcs is git or hg and defines what VCS we want to make those files for
94
95 :param vcs:
96 :param DEST:
97 """
98 # commit some stuff into this repo
99 cwd = path = jn(DEST)
100 #added_file = jn(path, '%ssetupΔ…ΕΌΕΊΔ‡.py' % _RandomNameSequence().next())
101 added_file = jn(path, '%ssetup.py' % _RandomNameSequence().next())
102 Command(cwd).execute('touch %s' % added_file)
103 Command(cwd).execute('%s add %s' % (vcs, added_file))
104
105 for i in xrange(3):
106 cmd = """echo 'added_line%s' >> %s""" % (i, added_file)
107 Command(cwd).execute(cmd)
108 if vcs == 'hg':
109 cmd = """hg commit -m 'commited new %s' -u '%s' %s """ % (
110 i, 'Marcin KuΕΊminski <marcin@python-blog.com>', added_file
111 )
112 elif vcs == 'git':
113 cmd = """git ci -m 'commited new %s' --author '%s' %s """ % (
114 i, 'Marcin KuΕΊminski <marcin@python-blog.com>', added_file
115 )
116 Command(cwd).execute(cmd)
117 # PUSH it back
118 if vcs == 'hg':
119 _REPO = HG_REPO
120 elif vcs == 'git':
121 _REPO = GIT_REPO
122
123 kwargs['dest'] = ''
124 clone_url = _construct_url(_REPO, **kwargs)
125 if 'clone_url' in kwargs:
126 clone_url = kwargs['clone_url']
127 if vcs == 'hg':
128 stdout, stderr = Command(cwd).execute('hg push --verbose', clone_url)
129 elif vcs == 'git':
130 stdout, stderr = Command(cwd).execute('git push', clone_url + " master")
131
132 return stdout, stderr
133
134
88 def set_anonymous_access(enable=True):
135 def set_anonymous_access(enable=True):
89 user = User.get_by_username(User.DEFAULT_USER)
136 user = User.get_by_username(User.DEFAULT_USER)
90 user.active = enable
137 user.active = enable
@@ -95,148 +142,284 b' def set_anonymous_access(enable=True):'
95 raise Exception('Cannot set anonymous access')
142 raise Exception('Cannot set anonymous access')
96
143
97
144
98 def setup_module():
145 #==============================================================================
99 #DISABLE ANONYMOUS ACCESS
146 # TESTS
100 set_anonymous_access(False)
147 #==============================================================================
101
148
149 class TestVCSOperations(unittest.TestCase):
150
151 @classmethod
152 def setup_class(cls):
153 #DISABLE ANONYMOUS ACCESS
154 set_anonymous_access(False)
102
155
103 def test_clone_hg_repo_by_admin():
156 def setUp(self):
104 clone_url = _construct_url(HG_REPO)
157 r = Repository.get_by_repo_name(GIT_REPO)
105 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
158 Repository.unlock(r)
159 r.enable_locking = False
160 Session().add(r)
161 Session().commit()
106
162
107 assert 'requesting all changes' in stdout
163 r = Repository.get_by_repo_name(HG_REPO)
108 assert 'adding changesets' in stdout
164 Repository.unlock(r)
109 assert 'adding manifests' in stdout
165 r.enable_locking = False
110 assert 'adding file changes' in stdout
166 Session().add(r)
167 Session().commit()
111
168
112 assert stderr == ''
169 def test_clone_hg_repo_by_admin(self):
170 clone_url = _construct_url(HG_REPO)
171 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
113
172
173 assert 'requesting all changes' in stdout
174 assert 'adding changesets' in stdout
175 assert 'adding manifests' in stdout
176 assert 'adding file changes' in stdout
114
177
115 def test_clone_git_repo_by_admin():
178 assert stderr == ''
116 clone_url = _construct_url(GIT_REPO)
179
117 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
180 def test_clone_git_repo_by_admin(self):
181 clone_url = _construct_url(GIT_REPO)
182 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
118
183
119 assert 'Cloning into' in stdout
184 assert 'Cloning into' in stdout
120 assert stderr == ''
185 assert stderr == ''
121
186
187 def test_clone_wrong_credentials_hg(self):
188 clone_url = _construct_url(HG_REPO, passwd='bad!')
189 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
190 assert 'abort: authorization failed' in stderr
122
191
123 def test_clone_wrong_credentials_hg():
192 def test_clone_wrong_credentials_git(self):
124 clone_url = _construct_url(HG_REPO, passwd='bad!')
193 clone_url = _construct_url(GIT_REPO, passwd='bad!')
125 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
194 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
126 assert 'abort: authorization failed' in stderr
195 assert 'fatal: Authentication failed' in stderr
196
197 def test_clone_git_dir_as_hg(self):
198 clone_url = _construct_url(GIT_REPO)
199 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
200 assert 'HTTP Error 404: Not Found' in stderr
127
201
202 def test_clone_hg_repo_as_git(self):
203 clone_url = _construct_url(HG_REPO)
204 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
205 assert 'not found:' in stderr
128
206
129 def test_clone_wrong_credentials_git():
207 def test_clone_non_existing_path_hg(self):
130 clone_url = _construct_url(GIT_REPO, passwd='bad!')
208 clone_url = _construct_url('trololo')
131 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
209 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
132 assert 'fatal: Authentication failed' in stderr
210 assert 'HTTP Error 404: Not Found' in stderr
133
211
212 def test_clone_non_existing_path_git(self):
213 clone_url = _construct_url('trololo')
214 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
215 assert 'not found:' in stderr
134
216
135 def test_clone_git_dir_as_hg():
217 def test_push_new_file_hg(self):
136 clone_url = _construct_url(GIT_REPO)
218 DEST = _get_tmp_dir()
137 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
219 clone_url = _construct_url(HG_REPO, dest=DEST)
138 assert 'HTTP Error 404: Not Found' in stderr
220 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
139
221
222 stdout, stderr = _add_files_and_push('hg', DEST)
223
224 assert 'pushing to' in stdout
225 assert 'Repository size' in stdout
226 assert 'Last revision is now' in stdout
140
227
141 def test_clone_hg_repo_as_git():
228 def test_push_new_file_git(self):
142 clone_url = _construct_url(HG_REPO)
229 DEST = _get_tmp_dir()
143 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
230 clone_url = _construct_url(GIT_REPO, dest=DEST)
144 assert 'not found: did you run git update-server-info on the server' in stderr
231 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
232
233 # commit some stuff into this repo
234 stdout, stderr = _add_files_and_push('git', DEST)
145
235
236 #WTF git stderr ?!
237 assert 'master -> master' in stderr
146
238
147 def test_clone_non_existing_path_hg():
239 def test_push_wrong_credentials_hg(self):
148 clone_url = _construct_url('trololo')
240 DEST = _get_tmp_dir()
149 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
241 clone_url = _construct_url(HG_REPO, dest=DEST)
150 assert 'HTTP Error 404: Not Found' in stderr
242 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
151
243
244 stdout, stderr = _add_files_and_push('hg', DEST, user='bad',
245 passwd='name')
152
246
153 def test_clone_non_existing_path_git():
247 assert 'abort: authorization failed' in stderr
154 clone_url = _construct_url('trololo')
155 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
156 assert 'not found: did you run git update-server-info on the server' in stderr
157
248
249 def test_push_wrong_credentials_git(self):
250 DEST = _get_tmp_dir()
251 clone_url = _construct_url(GIT_REPO, dest=DEST)
252 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
253
254 stdout, stderr = _add_files_and_push('git', DEST, user='bad',
255 passwd='name')
256
257 assert 'fatal: Authentication failed' in stderr
158
258
159 def test_push_new_file_hg():
259 def test_push_back_to_wrong_url_hg(self):
160 DEST = _get_tmp_dir()
260 DEST = _get_tmp_dir()
161 clone_url = _construct_url(HG_REPO, dest=DEST)
261 clone_url = _construct_url(HG_REPO, dest=DEST)
162 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
262 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
263
264 stdout, stderr = _add_files_and_push('hg', DEST,
265 clone_url='http://127.0.0.1:5000/tmp',)
266
267 assert 'HTTP Error 404: Not Found' in stderr
163
268
164 # commit some stuff into this repo
269 def test_push_back_to_wrong_url_git(self):
165 cwd = path = jn(DEST)
270 DEST = _get_tmp_dir()
166 added_file = jn(path, '%ssetupΔ…ΕΌΕΊΔ‡.py' % _RandomNameSequence().next())
271 clone_url = _construct_url(GIT_REPO, dest=DEST)
167 Command(cwd).execute('touch %s' % added_file)
272 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
168 Command(cwd).execute('hg add %s' % added_file)
273
274 stdout, stderr = _add_files_and_push('git', DEST,
275 clone_url='http://127.0.0.1:5000/tmp',)
276
277 assert 'not found:' in stderr
169
278
170 for i in xrange(3):
279 def test_clone_and_create_lock_hg(self):
171 cmd = """echo 'added_line%s' >> %s""" % (i, added_file)
280 # enable locking
172 Command(cwd).execute(cmd)
281 r = Repository.get_by_repo_name(HG_REPO)
282 r.enable_locking = True
283 Session().add(r)
284 Session().commit()
285 # clone
286 clone_url = _construct_url(HG_REPO)
287 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
288
289 #check if lock was made
290 r = Repository.get_by_repo_name(HG_REPO)
291 assert r.locked[0] == User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id
173
292
174 cmd = """hg ci -m 'commited new %s' -u '%s' %s """ % (
293 def test_clone_and_create_lock_git(self):
175 i,
294 # enable locking
176 'Marcin KuΕΊminski <marcin@python-blog.com>',
295 r = Repository.get_by_repo_name(GIT_REPO)
177 added_file
296 r.enable_locking = True
178 )
297 Session().add(r)
179 Command(cwd).execute(cmd)
298 Session().commit()
180 # PUSH it back
299 # clone
181 clone_url = _construct_url(HG_REPO, dest='')
300 clone_url = _construct_url(GIT_REPO)
182 stdout, stderr = Command(cwd).execute('hg push --verbose', clone_url)
301 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
302
303 #check if lock was made
304 r = Repository.get_by_repo_name(GIT_REPO)
305 assert r.locked[0] == User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id
183
306
184 assert 'pushing to' in stdout
307 def test_clone_after_repo_was_locked_hg(self):
185 assert 'Repository size' in stdout
308 #lock repo
186 assert 'Last revision is now' in stdout
309 r = Repository.get_by_repo_name(HG_REPO)
187
310 Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
188
311 #pull fails since repo is locked
189 def test_push_new_file_git():
312 clone_url = _construct_url(HG_REPO)
190 DEST = _get_tmp_dir()
313 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
191 clone_url = _construct_url(GIT_REPO, dest=DEST)
314 msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
192 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
315 % (HG_REPO, TEST_USER_ADMIN_LOGIN))
316 assert msg in stderr
193
317
194 # commit some stuff into this repo
318 def test_clone_after_repo_was_locked_git(self):
195 cwd = path = jn(DEST)
319 #lock repo
196 added_file = jn(path, '%ssetupΔ…ΕΌΕΊΔ‡.py' % _RandomNameSequence().next())
320 r = Repository.get_by_repo_name(GIT_REPO)
197 Command(cwd).execute('touch %s' % added_file)
321 Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
198 Command(cwd).execute('git add %s' % added_file)
322 #pull fails since repo is locked
323 clone_url = _construct_url(GIT_REPO)
324 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
325 msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
326 % (GIT_REPO, TEST_USER_ADMIN_LOGIN))
327 #TODO: fix this somehow later on GIT, GIT is stupid and even if we throw
328 # back 423 to it, it makes ANOTHER request and we fail there with 405 :/
329 msg = "405 Method Not Allowed"
330 assert msg in stderr
199
331
200 for i in xrange(3):
332 def test_push_on_locked_repo_by_other_user_hg(self):
201 cmd = """echo 'added_line%s' >> %s""" % (i, added_file)
333 #clone some temp
202 Command(cwd).execute(cmd)
334 DEST = _get_tmp_dir()
335 clone_url = _construct_url(HG_REPO, dest=DEST)
336 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
337
338 #lock repo
339 r = Repository.get_by_repo_name(HG_REPO)
340 # let this user actually push !
341 RepoModel().grant_user_permission(repo=r, user=TEST_USER_REGULAR_LOGIN,
342 perm='repository.write')
343 Session().commit()
344 Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
203
345
204 cmd = """git ci -m 'commited new %s' --author '%s' %s """ % (
346 #push fails repo is locked by other user !
205 i,
347 stdout, stderr = _add_files_and_push('hg', DEST,
206 'Marcin KuΕΊminski <marcin@python-blog.com>',
348 user=TEST_USER_REGULAR_LOGIN,
207 added_file
349 passwd=TEST_USER_REGULAR_PASS)
208 )
350 msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
209 Command(cwd).execute(cmd)
351 % (HG_REPO, TEST_USER_ADMIN_LOGIN))
210 # PUSH it back
352 assert msg in stderr
211 clone_url = _construct_url(GIT_REPO, dest='')
212 stdout, stderr = Command(cwd).execute('git push --verbose', clone_url)
213
353
214 #WTF git stderr ?!
354 #TODO: fix me ! somehow during tests hooks don't get called on GIT
215 assert 'master -> master' in stderr
355 # def test_push_on_locked_repo_by_other_user_git(self):
216
356 # #clone some temp
217
357 # DEST = _get_tmp_dir()
218 def test_push_modify_existing_file_hg():
358 # clone_url = _construct_url(GIT_REPO, dest=DEST)
219 assert 0
359 # stdout, stderr = Command('/tmp').execute('git clone', clone_url)
220
360 #
361 # #lock repo
362 # r = Repository.get_by_repo_name(GIT_REPO)
363 # # let this user actually push !
364 # RepoModel().grant_user_permission(repo=r, user=TEST_USER_REGULAR_LOGIN,
365 # perm='repository.write')
366 # Session().commit()
367 # Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
368 #
369 # #push fails repo is locked by other user !
370 # stdout, stderr = _add_files_and_push('git', DEST,
371 # user=TEST_USER_REGULAR_LOGIN,
372 # passwd=TEST_USER_REGULAR_PASS)
373 # msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
374 # % (GIT_REPO, TEST_USER_ADMIN_LOGIN))
375 # #TODO: fix this somehow later on GIT, GIT is stupid and even if we throw
376 # # back 423 to it, it makes ANOTHER request and we fail there with 405 :/
377 # msg = "405 Method Not Allowed"
378 # assert msg in stderr
221
379
222 def test_push_modify_existing_file_git():
380 def test_push_unlocks_repository_hg(self):
223 assert 0
381 # enable locking
224
382 r = Repository.get_by_repo_name(HG_REPO)
383 r.enable_locking = True
384 Session().add(r)
385 Session().commit()
386 #clone some temp
387 DEST = _get_tmp_dir()
388 clone_url = _construct_url(HG_REPO, dest=DEST)
389 stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
225
390
226 def test_push_wrong_credentials_hg():
391 #check for lock repo after clone
227 assert 0
392 r = Repository.get_by_repo_name(HG_REPO)
393 assert r.locked[0] == User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id
228
394
229
395 #push is ok and repo is now unlocked
230 def test_push_wrong_credentials_git():
396 stdout, stderr = _add_files_and_push('hg', DEST)
231 assert 0
397 assert ('remote: Released lock on repo `%s`' % HG_REPO) in stdout
398 #we need to cleanup the Session Here !
399 Session.remove()
400 r = Repository.get_by_repo_name(HG_REPO)
401 assert r.locked == [None, None]
232
402
233
403 #TODO: fix me ! somehow during tests hooks don't get called on GIT
234 def test_push_back_to_wrong_url_hg():
404 # def test_push_unlocks_repository_git(self):
235 assert 0
405 # # enable locking
236
406 # r = Repository.get_by_repo_name(GIT_REPO)
237
407 # r.enable_locking = True
238 def test_push_back_to_wrong_url_git():
408 # Session().add(r)
239 assert 0
409 # Session().commit()
240
410 # #clone some temp
241
411 # DEST = _get_tmp_dir()
242 #TODO: write all locking tests
412 # clone_url = _construct_url(GIT_REPO, dest=DEST)
413 # stdout, stderr = Command('/tmp').execute('git clone', clone_url)
414 #
415 # #check for lock repo after clone
416 # r = Repository.get_by_repo_name(GIT_REPO)
417 # assert r.locked[0] == User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id
418 #
419 # #push is ok and repo is now unlocked
420 # stdout, stderr = _add_files_and_push('git', DEST)
421 # #assert ('remote: Released lock on repo `%s`' % GIT_REPO) in stdout
422 # #we need to cleanup the Session Here !
423 # Session.remove()
424 # r = Repository.get_by_repo_name(GIT_REPO)
425 # assert r.locked == [None, None]
General Comments 0
You need to be logged in to leave comments. Login now