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