##// END OF EJS Templates
made concurrency test also for git
marcink -
r2895:f5dc0417 beta
parent child Browse files
Show More
@@ -1,213 +1,221 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.tests.test_hg_operations
3 rhodecode.tests.test_hg_operations
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
5
6 Test suite for making push/pull operations
6 Test suite for making push/pull operations
7
7
8 :created_on: Dec 30, 2010
8 :created_on: Dec 30, 2010
9 :author: marcink
9 :author: marcink
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software: you can redistribute it and/or modify
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
16 # (at your option) any later version.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25
25
26 import os
26 import os
27 import sys
27 import sys
28 import shutil
28 import shutil
29 import logging
29 import logging
30 from os.path import join as jn
30 from os.path import join as jn
31 from os.path import dirname as dn
31 from os.path import dirname as dn
32
32
33 from tempfile import _RandomNameSequence
33 from tempfile import _RandomNameSequence
34 from subprocess import Popen, PIPE
34 from subprocess import Popen, PIPE
35
35
36 from paste.deploy import appconfig
36 from paste.deploy import appconfig
37 from pylons import config
37 from pylons import config
38 from sqlalchemy import engine_from_config
38 from sqlalchemy import engine_from_config
39
39
40 from rhodecode.lib.utils import add_cache
40 from rhodecode.lib.utils import add_cache
41 from rhodecode.model import init_model
41 from rhodecode.model import init_model
42 from rhodecode.model import meta
42 from rhodecode.model import meta
43 from rhodecode.model.db import User, Repository
43 from rhodecode.model.db import User, Repository
44 from rhodecode.lib.auth import get_crypt_password
44 from rhodecode.lib.auth import get_crypt_password
45
45
46 from rhodecode.tests import TESTS_TMP_PATH, NEW_HG_REPO, HG_REPO
46 from rhodecode.tests import TESTS_TMP_PATH, NEW_HG_REPO, HG_REPO
47 from rhodecode.config.environment import load_environment
47 from rhodecode.config.environment import load_environment
48
48
49 rel_path = dn(dn(dn(os.path.abspath(__file__))))
49 rel_path = dn(dn(dn(dn(os.path.abspath(__file__)))))
50 conf = appconfig('config:development.ini', relative_to=rel_path)
50 conf = appconfig('config:rc.ini', relative_to=rel_path)
51 load_environment(conf.global_conf, conf.local_conf)
51 load_environment(conf.global_conf, conf.local_conf)
52
52
53 add_cache(conf)
53 add_cache(conf)
54
54
55 USER = 'test_admin'
55 USER = 'test_admin'
56 PASS = 'test12'
56 PASS = 'test12'
57 HOST = 'hg.local'
57 HOST = 'rc.local'
58 METHOD = 'pull'
58 METHOD = 'pull'
59 DEBUG = True
59 DEBUG = True
60 log = logging.getLogger(__name__)
60 log = logging.getLogger(__name__)
61
61
62
62
63 class Command(object):
63 class Command(object):
64
64
65 def __init__(self, cwd):
65 def __init__(self, cwd):
66 self.cwd = cwd
66 self.cwd = cwd
67
67
68 def execute(self, cmd, *args):
68 def execute(self, cmd, *args):
69 """Runs command on the system with given ``args``.
69 """Runs command on the system with given ``args``.
70 """
70 """
71
71
72 command = cmd + ' ' + ' '.join(args)
72 command = cmd + ' ' + ' '.join(args)
73 log.debug('Executing %s' % command)
73 log.debug('Executing %s' % command)
74 if DEBUG:
74 if DEBUG:
75 print command
75 print command
76 p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, cwd=self.cwd)
76 p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, cwd=self.cwd)
77 stdout, stderr = p.communicate()
77 stdout, stderr = p.communicate()
78 if DEBUG:
78 if DEBUG:
79 print stdout, stderr
79 print stdout, stderr
80 return stdout, stderr
80 return stdout, stderr
81
81
82
82
83 def get_session():
83 def get_session():
84 engine = engine_from_config(conf, 'sqlalchemy.db1.')
84 engine = engine_from_config(conf, 'sqlalchemy.db1.')
85 init_model(engine)
85 init_model(engine)
86 sa = meta.Session
86 sa = meta.Session
87 return sa
87 return sa
88
88
89
89
90 def create_test_user(force=True):
90 def create_test_user(force=True):
91 print 'creating test user'
91 print 'creating test user'
92 sa = get_session()
92 sa = get_session()
93
93
94 user = sa.query(User).filter(User.username == USER).scalar()
94 user = sa.query(User).filter(User.username == USER).scalar()
95
95
96 if force and user is not None:
96 if force and user is not None:
97 print 'removing current user'
97 print 'removing current user'
98 for repo in sa.query(Repository).filter(Repository.user == user).all():
98 for repo in sa.query(Repository).filter(Repository.user == user).all():
99 sa.delete(repo)
99 sa.delete(repo)
100 sa.delete(user)
100 sa.delete(user)
101 sa.commit()
101 sa.commit()
102
102
103 if user is None or force:
103 if user is None or force:
104 print 'creating new one'
104 print 'creating new one'
105 new_usr = User()
105 new_usr = User()
106 new_usr.username = USER
106 new_usr.username = USER
107 new_usr.password = get_crypt_password(PASS)
107 new_usr.password = get_crypt_password(PASS)
108 new_usr.email = 'mail@mail.com'
108 new_usr.email = 'mail@mail.com'
109 new_usr.name = 'test'
109 new_usr.name = 'test'
110 new_usr.lastname = 'lasttestname'
110 new_usr.lastname = 'lasttestname'
111 new_usr.active = True
111 new_usr.active = True
112 new_usr.admin = True
112 new_usr.admin = True
113 sa.add(new_usr)
113 sa.add(new_usr)
114 sa.commit()
114 sa.commit()
115
115
116 print 'done'
116 print 'done'
117
117
118
118
119 def create_test_repo(force=True):
119 def create_test_repo(force=True):
120 print 'creating test repo'
120 print 'creating test repo'
121 from rhodecode.model.repo import RepoModel
121 from rhodecode.model.repo import RepoModel
122 sa = get_session()
122 sa = get_session()
123
123
124 user = sa.query(User).filter(User.username == USER).scalar()
124 user = sa.query(User).filter(User.username == USER).scalar()
125 if user is None:
125 if user is None:
126 raise Exception('user not found')
126 raise Exception('user not found')
127
127
128 repo = sa.query(Repository).filter(Repository.repo_name == HG_REPO).scalar()
128 repo = sa.query(Repository).filter(Repository.repo_name == HG_REPO).scalar()
129
129
130 if repo is None:
130 if repo is None:
131 print 'repo not found creating'
131 print 'repo not found creating'
132
132
133 form_data = {'repo_name':HG_REPO,
133 form_data = {'repo_name': HG_REPO,
134 'repo_type':'hg',
134 'repo_type': 'hg',
135 'private':False,
135 'private':False,
136 'clone_uri':'' }
136 'clone_uri': '' }
137 rm = RepoModel(sa)
137 rm = RepoModel(sa)
138 rm.base_path = '/home/hg'
138 rm.base_path = '/home/hg'
139 rm.create(form_data, user)
139 rm.create(form_data, user)
140
140
141 print 'done'
141 print 'done'
142
142
143
143
144 def set_anonymous_access(enable=True):
144 def set_anonymous_access(enable=True):
145 sa = get_session()
145 sa = get_session()
146 user = sa.query(User).filter(User.username == 'default').one()
146 user = sa.query(User).filter(User.username == 'default').one()
147 user.active = enable
147 user.active = enable
148 sa.add(user)
148 sa.add(user)
149 sa.commit()
149 sa.commit()
150
150
151
151
152 def get_anonymous_access():
152 def get_anonymous_access():
153 sa = get_session()
153 sa = get_session()
154 return sa.query(User).filter(User.username == 'default').one().active
154 return sa.query(User).filter(User.username == 'default').one().active
155
155
156
156
157 #==============================================================================
157 #==============================================================================
158 # TESTS
158 # TESTS
159 #==============================================================================
159 #==============================================================================
160 def test_clone_with_credentials(no_errors=False, repo=HG_REPO, method=METHOD,
160 def test_clone_with_credentials(no_errors=False, repo=HG_REPO, method=METHOD,
161 seq=None):
161 seq=None, backend='hg'):
162 cwd = path = jn(TESTS_TMP_PATH, repo)
162 cwd = path = jn(TESTS_TMP_PATH, repo)
163
163
164 if seq == None:
164 if seq == None:
165 seq = _RandomNameSequence().next()
165 seq = _RandomNameSequence().next()
166
166
167 try:
167 try:
168 shutil.rmtree(path, ignore_errors=True)
168 shutil.rmtree(path, ignore_errors=True)
169 os.makedirs(path)
169 os.makedirs(path)
170 #print 'made dirs %s' % jn(path)
170 #print 'made dirs %s' % jn(path)
171 except OSError:
171 except OSError:
172 raise
172 raise
173
173
174 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
174 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
175 {'user':USER,
175 {'user': USER,
176 'pass':PASS,
176 'pass': PASS,
177 'host':HOST,
177 'host': HOST,
178 'cloned_repo':repo, }
178 'cloned_repo': repo, }
179
179
180 dest = path + seq
180 dest = path + seq
181 if method == 'pull':
181 if method == 'pull':
182 stdout, stderr = Command(cwd).execute('hg', method, '--cwd', dest, clone_url)
182 stdout, stderr = Command(cwd).execute(backend, method, '--cwd', dest, clone_url)
183 else:
183 else:
184 stdout, stderr = Command(cwd).execute('hg', method, clone_url, dest)
184 stdout, stderr = Command(cwd).execute(backend, method, clone_url, dest)
185
185 print stdout,'sdasdsadsa'
186 if no_errors is False:
186 if no_errors is False:
187 assert """adding file changes""" in stdout, 'no messages about cloning'
187 if backend == 'hg':
188 assert """abort""" not in stderr , 'got error from clone'
188 assert """adding file changes""" in stdout, 'no messages about cloning'
189 assert """abort""" not in stderr , 'got error from clone'
190 elif backend == 'git':
191 assert """Cloning into""" in stdout, 'no messages about cloning'
189
192
190 if __name__ == '__main__':
193 if __name__ == '__main__':
191 try:
194 try:
192 create_test_user(force=False)
195 create_test_user(force=False)
193 seq = None
196 seq = None
194 import time
197 import time
195
198
196 try:
199 try:
197 METHOD = sys.argv[3]
200 METHOD = sys.argv[3]
198 except:
201 except:
199 pass
202 pass
200
203
204 try:
205 backend = sys.argv[4]
206 except:
207 backend = 'hg'
208
201 if METHOD == 'pull':
209 if METHOD == 'pull':
202 seq = _RandomNameSequence().next()
210 seq = _RandomNameSequence().next()
203 test_clone_with_credentials(repo=sys.argv[1], method='clone',
211 test_clone_with_credentials(repo=sys.argv[1], method='clone',
204 seq=seq)
212 seq=seq, backend=backend)
205 s = time.time()
213 s = time.time()
206 for i in range(1, int(sys.argv[2]) + 1):
214 for i in range(1, int(sys.argv[2]) + 1):
207 print 'take', i
215 print 'take', i
208 test_clone_with_credentials(repo=sys.argv[1], method=METHOD,
216 test_clone_with_credentials(repo=sys.argv[1], method=METHOD,
209 seq=seq)
217 seq=seq, backend=backend)
210 print 'time taken %.3f' % (time.time() - s)
218 print 'time taken %.3f' % (time.time() - s)
211 except Exception, e:
219 except Exception, e:
212 raise
220 raise
213 sys.exit('stop on %s' % e)
221 sys.exit('stop on %s' % e)
General Comments 0
You need to be logged in to leave comments. Login now