##// END OF EJS Templates
fixed test hg operations script
marcink -
r988:7f4943c9 beta
parent child Browse files
Show More
@@ -1,271 +1,276 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 :copyright: (c) 2010 by marcink.
9 :copyright: (c) 2010 by marcink.
10 :license: LICENSE_NAME, see LICENSE_FILE for more details.
10 :license: LICENSE_NAME, see LICENSE_FILE for more details.
11 """
11 """
12
12
13 import os
13 import os
14 import shutil
14 import shutil
15 import logging
15 import logging
16 from os.path import join as jn
16 from os.path import join as jn
17
17
18 from tempfile import _RandomNameSequence
18 from tempfile import _RandomNameSequence
19 from subprocess import Popen, PIPE
19 from subprocess import Popen, PIPE
20
20
21 from paste.deploy import appconfig
21 from paste.deploy import appconfig
22 from pylons import config
22 from pylons import config
23 from sqlalchemy import engine_from_config
23 from sqlalchemy import engine_from_config
24
24
25 from rhodecode.lib.utils import add_cache
25 from rhodecode.lib.utils import add_cache
26 from rhodecode.model import init_model
26 from rhodecode.model import init_model
27 from rhodecode.model import meta
27 from rhodecode.model import meta
28 from rhodecode.model.db import User
28 from rhodecode.model.db import User
29 from rhodecode.lib.auth import get_crypt_password
29 from rhodecode.lib.auth import get_crypt_password
30
30
31 from rhodecode.tests import TESTS_TMP_PATH, NEW_HG_REPO, HG_REPO
31 from rhodecode.tests import TESTS_TMP_PATH, NEW_HG_REPO, HG_REPO
32 from rhodecode.config.environment import load_environment
32 from rhodecode.config.environment import load_environment
33
33
34 conf = appconfig('config:development.ini', relative_to='./../../')
34 conf = appconfig('config:development.ini', relative_to='./../../')
35 load_environment(conf.global_conf, conf.local_conf)
35 load_environment(conf.global_conf, conf.local_conf)
36
36
37 add_cache(conf)
37 add_cache(conf)
38
38
39 USER = 'test_admin'
39 USER = 'test_admin'
40 PASS = 'test12'
40 PASS = 'test12'
41 HOST = '127.0.0.1:5000'
41 HOST = '127.0.0.1:5000'
42 DEBUG = True
42 DEBUG = True
43 log = logging.getLogger(__name__)
43 log = logging.getLogger(__name__)
44
44
45
45
46 class Command(object):
46 class Command(object):
47
47
48 def __init__(self, cwd):
48 def __init__(self, cwd):
49 self.cwd = cwd
49 self.cwd = cwd
50
50
51 def execute(self, cmd, *args):
51 def execute(self, cmd, *args):
52 """Runs command on the system with given ``args``.
52 """Runs command on the system with given ``args``.
53 """
53 """
54
54
55 command = cmd + ' ' + ' '.join(args)
55 command = cmd + ' ' + ' '.join(args)
56 log.debug('Executing %s' % command)
56 log.debug('Executing %s' % command)
57 if DEBUG:
57 if DEBUG:
58 print command
58 print command
59 p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, cwd=self.cwd)
59 p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, cwd=self.cwd)
60 stdout, stderr = p.communicate()
60 stdout, stderr = p.communicate()
61 if DEBUG:
61 if DEBUG:
62 print stdout, stderr
62 print stdout, stderr
63 return stdout, stderr
63 return stdout, stderr
64
64
65 def get_session():
65 def get_session():
66 engine = engine_from_config(conf, 'sqlalchemy.db1.')
66 engine = engine_from_config(conf, 'sqlalchemy.db1.')
67 init_model(engine)
67 init_model(engine)
68 sa = meta.Session()
68 sa = meta.Session()
69 return sa
69 return sa
70
70
71
71
72 def create_test_user(force=True):
72 def create_test_user(force=True):
73 sa = get_session()
73 sa = get_session()
74
74
75 user = sa.query(User).filter(User.username == USER).scalar()
75 user = sa.query(User).filter(User.username == USER).scalar()
76 if force:
76
77 if force and user:
77 sa.delete(user)
78 sa.delete(user)
78 sa.commit()
79 sa.commit()
79
80
80 if user is None or force:
81 if user is None or force:
81 new_usr = User()
82 new_usr = User()
82 new_usr.username = USER
83 new_usr.username = USER
83 new_usr.password = get_crypt_password(PASS)
84 new_usr.password = get_crypt_password(PASS)
85 new_usr.email = 'mail@mail.com'
86 new_usr.name = 'test'
87 new_usr.lastname = 'lasttestname'
84 new_usr.active = True
88 new_usr.active = True
85
89
86 sa.add(new_usr)
90 sa.add(new_usr)
87 sa.commit()
91 sa.commit()
88
92
89
93
90
94
91
95
92 #==============================================================================
96 #==============================================================================
93 # TESTS
97 # TESTS
94 #==============================================================================
98 #==============================================================================
95 def test_clone():
99 def test_clone():
96 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
100 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
97
101
98 try:
102 try:
99 shutil.rmtree(path, ignore_errors=True)
103 shutil.rmtree(path, ignore_errors=True)
100 os.makedirs(path)
104 os.makedirs(path)
101 #print 'made dirs %s' % jn(path)
105 #print 'made dirs %s' % jn(path)
102 except OSError:
106 except OSError:
103 raise
107 raise
104
108
105
109
106 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s %(dest)s' % \
110 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s %(dest)s' % \
107 {'user':USER,
111 {'user':USER,
108 'pass':PASS,
112 'pass':PASS,
109 'host':HOST,
113 'host':HOST,
110 'cloned_repo':HG_REPO,
114 'cloned_repo':HG_REPO,
111 'dest':path}
115 'dest':path}
112
116
113 stdout, stderr = Command(cwd).execute('hg clone', clone_url)
117 stdout, stderr = Command(cwd).execute('hg clone', clone_url)
114
118
115 assert """adding file changes""" in stdout, 'no messages about cloning'
119 assert """adding file changes""" in stdout, 'no messages about cloning'
116 assert """abort""" not in stderr , 'got error from clone'
120 assert """abort""" not in stderr , 'got error from clone'
117
121
118
122
119
123
120 def test_clone_anonymous_ok():
124 def test_clone_anonymous_ok():
121 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
125 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
122
126
123 try:
127 try:
124 shutil.rmtree(path, ignore_errors=True)
128 shutil.rmtree(path, ignore_errors=True)
125 os.makedirs(path)
129 os.makedirs(path)
126 #print 'made dirs %s' % jn(path)
130 #print 'made dirs %s' % jn(path)
127 except OSError:
131 except OSError:
128 raise
132 raise
129
133
130
134
131 clone_url = 'http://%(host)s/%(cloned_repo)s %(dest)s' % \
135 clone_url = 'http://%(host)s/%(cloned_repo)s %(dest)s' % \
132 {'user':USER,
136 {'user':USER,
133 'pass':PASS,
137 'pass':PASS,
134 'host':HOST,
138 'host':HOST,
135 'cloned_repo':HG_REPO,
139 'cloned_repo':HG_REPO,
136 'dest':path}
140 'dest':path}
137
141
138 stdout, stderr = Command(cwd).execute('hg clone', clone_url)
142 stdout, stderr = Command(cwd).execute('hg clone', clone_url)
139 print stdout, stderr
143 print stdout, stderr
140 assert """adding file changes""" in stdout, 'no messages about cloning'
144 assert """adding file changes""" in stdout, 'no messages about cloning'
141 assert """abort""" not in stderr , 'got error from clone'
145 assert """abort""" not in stderr , 'got error from clone'
142
146
143 def test_clone_wrong_credentials():
147 def test_clone_wrong_credentials():
144 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
148 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
145
149
146 try:
150 try:
147 shutil.rmtree(path, ignore_errors=True)
151 shutil.rmtree(path, ignore_errors=True)
148 os.makedirs(path)
152 os.makedirs(path)
149 #print 'made dirs %s' % jn(path)
153 #print 'made dirs %s' % jn(path)
150 except OSError:
154 except OSError:
151 raise
155 raise
152
156
153
157
154 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s %(dest)s' % \
158 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s %(dest)s' % \
155 {'user':USER + 'error',
159 {'user':USER + 'error',
156 'pass':PASS,
160 'pass':PASS,
157 'host':HOST,
161 'host':HOST,
158 'cloned_repo':HG_REPO,
162 'cloned_repo':HG_REPO,
159 'dest':path}
163 'dest':path}
160
164
161 stdout, stderr = Command(cwd).execute('hg clone', clone_url)
165 stdout, stderr = Command(cwd).execute('hg clone', clone_url)
162
166
163 assert """abort: authorization failed""" in stderr , 'no error from wrong credentials'
167 assert """abort: authorization failed""" in stderr , 'no error from wrong credentials'
164
168
165
169
166 def test_pull():
170 def test_pull():
167 pass
171 pass
168
172
169 def test_push():
173 def test_push():
170
174
171 modified_file = jn(TESTS_TMP_PATH, HG_REPO, 'setup.py')
175 modified_file = jn(TESTS_TMP_PATH, HG_REPO, 'setup.py')
172 for i in xrange(5):
176 for i in xrange(5):
173 cmd = """echo 'added_line%s' >> %s""" % (i, modified_file)
177 cmd = """echo 'added_line%s' >> %s""" % (i, modified_file)
174 Command(cwd).execute(cmd)
178 Command(cwd).execute(cmd)
175
179
176 cmd = """hg ci -m 'changed file %s' %s """ % (i, modified_file)
180 cmd = """hg ci -m 'changed file %s' %s """ % (i, modified_file)
177 Command(cwd).execute(cmd)
181 Command(cwd).execute(cmd)
178
182
179 Command(cwd).execute('hg push %s' % jn(TESTS_TMP_PATH, HG_REPO))
183 Command(cwd).execute('hg push %s' % jn(TESTS_TMP_PATH, HG_REPO))
180
184
181 def test_push_new_file(commits=15):
185 def test_push_new_file(commits=15):
182
186
183 test_clone()
187 test_clone()
184
188
185 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
189 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
186 added_file = jn(path, '%ssetup.py' % _RandomNameSequence().next())
190 added_file = jn(path, '%ssetup.py' % _RandomNameSequence().next())
187
191
188 Command(cwd).execute('touch %s' % added_file)
192 Command(cwd).execute('touch %s' % added_file)
189
193
190 Command(cwd).execute('hg add %s' % added_file)
194 Command(cwd).execute('hg add %s' % added_file)
191
195
192 for i in xrange(commits):
196 for i in xrange(commits):
193 cmd = """echo 'added_line%s' >> %s""" % (i, added_file)
197 cmd = """echo 'added_line%s' >> %s""" % (i, added_file)
194 Command(cwd).execute(cmd)
198 Command(cwd).execute(cmd)
195
199
196 cmd = """hg ci -m 'commited new %s' %s """ % (i, added_file)
200 cmd = """hg ci -m 'commited new %s' %s """ % (i, added_file)
197 Command(cwd).execute(cmd)
201 Command(cwd).execute(cmd)
198
202
199 push_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
203 push_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
200 {'user':USER,
204 {'user':USER,
201 'pass':PASS,
205 'pass':PASS,
202 'host':HOST,
206 'host':HOST,
203 'cloned_repo':HG_REPO,
207 'cloned_repo':HG_REPO,
204 'dest':jn(TESTS_TMP_PATH, HG_REPO)}
208 'dest':jn(TESTS_TMP_PATH, HG_REPO)}
205
209
206 Command(cwd).execute('hg push --verbose --debug %s' % push_url)
210 Command(cwd).execute('hg push --verbose --debug %s' % push_url)
207
211
208 def test_push_wrong_credentials():
212 def test_push_wrong_credentials():
209
213
210 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
214 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
211 {'user':USER + 'xxx',
215 {'user':USER + 'xxx',
212 'pass':PASS,
216 'pass':PASS,
213 'host':HOST,
217 'host':HOST,
214 'cloned_repo':HG_REPO,
218 'cloned_repo':HG_REPO,
215 'dest':jn(TESTS_TMP_PATH, HG_REPO)}
219 'dest':jn(TESTS_TMP_PATH, HG_REPO)}
216
220
217 modified_file = jn(TESTS_TMP_PATH, HG_REPO, 'setup.py')
221 modified_file = jn(TESTS_TMP_PATH, HG_REPO, 'setup.py')
218 for i in xrange(5):
222 for i in xrange(5):
219 cmd = """echo 'added_line%s' >> %s""" % (i, modified_file)
223 cmd = """echo 'added_line%s' >> %s""" % (i, modified_file)
220 Command(cwd).execute(cmd)
224 Command(cwd).execute(cmd)
221
225
222 cmd = """hg ci -m 'commited %s' %s """ % (i, modified_file)
226 cmd = """hg ci -m 'commited %s' %s """ % (i, modified_file)
223 Command(cwd).execute(cmd)
227 Command(cwd).execute(cmd)
224
228
225 Command(cwd).execute('hg push %s' % clone_url)
229 Command(cwd).execute('hg push %s' % clone_url)
226
230
227 def test_push_wrong_path():
231 def test_push_wrong_path():
228 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
232 cwd = path = jn(TESTS_TMP_PATH, HG_REPO)
229 added_file = jn(path, 'somefile.py')
233 added_file = jn(path, 'somefile.py')
230
234
231 try:
235 try:
232 shutil.rmtree(path, ignore_errors=True)
236 shutil.rmtree(path, ignore_errors=True)
233 os.makedirs(path)
237 os.makedirs(path)
234 print 'made dirs %s' % jn(path)
238 print 'made dirs %s' % jn(path)
235 except OSError:
239 except OSError:
236 raise
240 raise
237
241
238 Command(cwd).execute("""echo '' > %s""" % added_file)
242 Command(cwd).execute("""echo '' > %s""" % added_file)
239 Command(cwd).execute("""hg init %s""" % path)
243 Command(cwd).execute("""hg init %s""" % path)
240 Command(cwd).execute("""hg add %s""" % added_file)
244 Command(cwd).execute("""hg add %s""" % added_file)
241
245
242 for i in xrange(2):
246 for i in xrange(2):
243 cmd = """echo 'added_line%s' >> %s""" % (i, added_file)
247 cmd = """echo 'added_line%s' >> %s""" % (i, added_file)
244 Command(cwd).execute(cmd)
248 Command(cwd).execute(cmd)
245
249
246 cmd = """hg ci -m 'commited new %s' %s """ % (i, added_file)
250 cmd = """hg ci -m 'commited new %s' %s """ % (i, added_file)
247 Command(cwd).execute(cmd)
251 Command(cwd).execute(cmd)
248
252
249 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
253 clone_url = 'http://%(user)s:%(pass)s@%(host)s/%(cloned_repo)s' % \
250 {'user':USER,
254 {'user':USER,
251 'pass':PASS,
255 'pass':PASS,
252 'host':HOST,
256 'host':HOST,
253 'cloned_repo':HG_REPO + '_error',
257 'cloned_repo':HG_REPO + '_error',
254 'dest':jn(TESTS_TMP_PATH, HG_REPO)}
258 'dest':jn(TESTS_TMP_PATH, HG_REPO)}
255
259
256 stdout, stderr = Command(cwd).execute('hg push %s' % clone_url)
260 stdout, stderr = Command(cwd).execute('hg push %s' % clone_url)
257 assert """abort: HTTP Error 403: Forbidden""" in stderr
261 assert """abort: HTTP Error 403: Forbidden""" in stderr
258
262
259
263
260 if __name__ == '__main__':
264 if __name__ == '__main__':
261 create_test_user()
265 #create_test_user()
262 #test_clone()
266 test_clone()
267 test_clone_anonymous_ok()
263
268
264 #test_clone_wrong_credentials()
269 #test_clone_wrong_credentials()
265 ##test_clone_anonymous_ok()
270
266 #test_pull()
271 #test_pull()
267 test_push_new_file(3)
272 #test_push_new_file(3)
268 #test_push_wrong_path()
273 #test_push_wrong_path()
269 #test_push_wrong_credentials()
274 #test_push_wrong_credentials()
270
275
271
276
General Comments 0
You need to be logged in to leave comments. Login now