Commit | Line | Data |
---|---|---|
c5dbcd79 | 1 | #-*-python-*- |
aa896435 RK |
2 | # |
3 | # This file is part of DisOrder. | |
e663c61b | 4 | # Copyright (C) 2007-2009 Richard Kettlewell |
aa896435 | 5 | # |
e7eb3a27 | 6 | # This program is free software: you can redistribute it and/or modify |
aa896435 | 7 | # it under the terms of the GNU General Public License as published by |
e7eb3a27 | 8 | # the Free Software Foundation, either version 3 of the License, or |
aa896435 RK |
9 | # (at your option) any later version. |
10 | # | |
e7eb3a27 RK |
11 | # This program is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
15 | # | |
aa896435 | 16 | # You should have received a copy of the GNU General Public License |
e7eb3a27 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
aa896435 | 18 | # |
c5dbcd79 RK |
19 | |
20 | """Utility module used by tests""" | |
21 | ||
e663c61b | 22 | import os,os.path,subprocess,sys,re,time,unicodedata,random,socket,traceback |
3bfecfac RK |
23 | |
24 | def fatal(s): | |
25 | """Write an error message and exit""" | |
26 | sys.stderr.write("ERROR: %s\n" % s) | |
27 | sys.exit(1) | |
28 | ||
29 | # Identify the top build directory | |
30 | cwd = os.getcwd() | |
31 | if os.path.exists("config.h"): | |
32 | top_builddir = cwd | |
22cd3411 | 33 | elif os.path.exists("../config.h"): |
3bfecfac RK |
34 | top_builddir = os.path.dirname(cwd) |
35 | else: | |
36 | fatal("cannot identify build directory") | |
37 | ||
38 | # Make sure the Python build directory is on the module search path | |
39 | sys.path.insert(0, os.path.join(top_builddir, "python")) | |
40 | import disorder | |
41 | ||
d48eab4d | 42 | # Make sure the build directories are on the executable search path |
3bfecfac RK |
43 | ospath = os.environ["PATH"].split(os.pathsep) |
44 | ospath.insert(0, os.path.join(top_builddir, "server")) | |
d48eab4d | 45 | ospath.insert(0, os.path.join(top_builddir, "clients")) |
6ce6b5a9 | 46 | ospath.insert(0, os.path.join(top_builddir, "tests")) |
3bfecfac RK |
47 | os.environ["PATH"] = os.pathsep.join(ospath) |
48 | ||
49 | # Parse the makefile in the current directory to identify the source directory | |
50 | top_srcdir = None | |
51 | for l in file("Makefile"): | |
52 | r = re.match("top_srcdir *= *(.*)", l) | |
53 | if r: | |
54 | top_srcdir = r.group(1) | |
55 | break | |
56 | if not top_srcdir: | |
57 | fatal("cannot identify source directory") | |
58 | ||
59 | # The tests source directory must be on the module search path already since | |
60 | # we found dtest.py | |
61 | ||
62 | # ----------------------------------------------------------------------------- | |
c5dbcd79 RK |
63 | |
64 | def copyfile(a,b): | |
65 | """copyfile(A, B) | |
66 | Copy A to B.""" | |
67 | open(b,"w").write(open(a).read()) | |
68 | ||
de5ccb1a RK |
69 | def to_unicode(s): |
70 | """Convert UTF-8 to unicode. A no-op if already unicode.""" | |
71 | if type(s) == unicode: | |
72 | return s | |
73 | else: | |
74 | return unicode(s, "UTF-8") | |
75 | ||
76 | def nfc(s): | |
77 | """Convert UTF-8 string or unicode to NFC unicode.""" | |
78 | return unicodedata.normalize("NFC", to_unicode(s)) | |
79 | ||
c5dbcd79 RK |
80 | def maketrack(s): |
81 | """maketrack(S) | |
82 | ||
83 | Make track with relative path S exist""" | |
121e3654 | 84 | trackpath = "%s/%s" % (tracks, s) |
c5dbcd79 RK |
85 | trackdir = os.path.dirname(trackpath) |
86 | if not os.path.exists(trackdir): | |
87 | os.makedirs(trackdir) | |
f07e139b | 88 | copyfile("%s/sounds/long.ogg" % top_srcdir, trackpath) |
121e3654 | 89 | # We record the tracks we created so they can be tested against |
7bbe944b RK |
90 | # server responses. We put them into NFC since that's what the server |
91 | # uses internally. | |
de5ccb1a | 92 | bits = nfc(s).split('/') |
121e3654 RK |
93 | dp = tracks |
94 | for d in bits [0:-1]: | |
95 | dd = "%s/%s" % (dp, d) | |
96 | if dp not in dirs_by_dir: | |
97 | dirs_by_dir[dp] = [] | |
98 | if dd not in dirs_by_dir[dp]: | |
99 | dirs_by_dir[dp].append(dd) | |
100 | dp = "%s/%s" % (dp, d) | |
101 | if dp not in files_by_dir: | |
102 | files_by_dir[dp] = [] | |
103 | files_by_dir[dp].append("%s/%s" % (dp, bits[-1])) | |
c5dbcd79 RK |
104 | |
105 | def stdtracks(): | |
f9635e06 RK |
106 | # We create some tracks with non-ASCII characters in the name and |
107 | # we (currently) force UTF-8. | |
108 | # | |
109 | # On a traditional UNIX filesystem, that treats filenames as byte strings | |
110 | # with special significant for '/', this should just work, though the | |
111 | # names will look wrong to ls(1) in a non UTF-8 locale. | |
112 | # | |
113 | # On Apple HFS+ filenames normalized to a decomposed form that isn't quite | |
114 | # NFD, so our attempts to have both normalized and denormalized filenames | |
115 | # is frustrated. Provided we test on traditional filesytsems too this | |
116 | # shouldn't be a problem. | |
117 | # (See http://developer.apple.com/qa/qa2001/qa1173.html) | |
121e3654 RK |
118 | |
119 | global dirs_by_dir, files_by_dir | |
120 | dirs_by_dir={} | |
121 | files_by_dir={} | |
f9635e06 RK |
122 | |
123 | # C3 8C = 00CC LATIN CAPITAL LETTER I WITH GRAVE | |
124 | # (in NFC) | |
125 | maketrack("Joe Bloggs/First Album/01:F\xC3\x8Crst track.ogg") | |
126 | ||
c5dbcd79 | 127 | maketrack("Joe Bloggs/First Album/02:Second track.ogg") |
f9635e06 RK |
128 | |
129 | # CC 81 = 0301 COMBINING ACUTE ACCENT | |
130 | # (giving an NFD i-acute) | |
131 | maketrack("Joe Bloggs/First Album/03:ThI\xCC\x81rd track.ogg") | |
132 | # ...hopefuly giving C3 8D = 00CD LATIN CAPITAL LETTER I WITH ACUTE | |
c5dbcd79 RK |
133 | maketrack("Joe Bloggs/First Album/04:Fourth track.ogg") |
134 | maketrack("Joe Bloggs/First Album/05:Fifth track.ogg") | |
c5dbcd79 RK |
135 | maketrack("Joe Bloggs/Second Album/01:First track.ogg") |
136 | maketrack("Joe Bloggs/Second Album/02:Second track.ogg") | |
137 | maketrack("Joe Bloggs/Second Album/03:Third track.ogg") | |
138 | maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg") | |
139 | maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg") | |
de5ccb1a RK |
140 | maketrack("Joe Bloggs/Third Album/01:First_track.ogg") |
141 | maketrack("Joe Bloggs/Third Album/02:Second_track.ogg") | |
142 | maketrack("Joe Bloggs/Third Album/03:Third_track.ogg") | |
143 | maketrack("Joe Bloggs/Third Album/04:Fourth_track.ogg") | |
144 | maketrack("Joe Bloggs/Third Album/05:Fifth_track.ogg") | |
c5dbcd79 RK |
145 | maketrack("Fred Smith/Boring/01:Dull.ogg") |
146 | maketrack("Fred Smith/Boring/02:Tedious.ogg") | |
147 | maketrack("Fred Smith/Boring/03:Drum Solo.ogg") | |
148 | maketrack("Fred Smith/Boring/04:Yawn.ogg") | |
149 | maketrack("misc/blahblahblah.ogg") | |
150 | maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg") | |
151 | maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg") | |
31773020 RK |
152 | |
153 | def bindable(p): | |
154 | """bindable(P) | |
155 | ||
156 | Return True iff UDP port P is bindable, else False""" | |
157 | s = socket.socket(socket.AF_INET, | |
158 | socket.SOCK_DGRAM, | |
159 | socket.IPPROTO_UDP) | |
160 | try: | |
161 | s.bind(("127.0.0.1", p)) | |
162 | s.close() | |
163 | return True | |
164 | except: | |
165 | return False | |
166 | ||
031f8feb | 167 | def default_config(encoding="UTF-8"): |
f0feb22e | 168 | """Write the default config""" |
f9635e06 | 169 | open("%s/config" % testroot, "w").write( |
a0ead2ba | 170 | """home %s/home |
031f8feb | 171 | collection fs %s %s/tracks |
b6995afb RK |
172 | scratch %s/scratch.ogg |
173 | gap 0 | |
61192f93 | 174 | queue_pad 5 |
b6995afb RK |
175 | stopword 01 02 03 04 05 06 07 08 09 10 |
176 | stopword 1 2 3 4 5 6 7 8 9 | |
177 | stopword 11 12 13 14 15 16 17 18 19 20 | |
178 | stopword 21 22 23 24 25 26 27 28 29 30 | |
179 | stopword the a an and to too in on of we i am as im for is | |
180 | username fred | |
181 | password fredpass | |
40c30921 | 182 | plugins |
deaaa115 | 183 | plugins %s/plugins |
40c30921 | 184 | plugins %s/plugins/.libs |
b6995afb RK |
185 | player *.mp3 execraw disorder-decode |
186 | player *.ogg execraw disorder-decode | |
187 | player *.wav execraw disorder-decode | |
188 | player *.flac execraw disorder-decode | |
189 | tracklength *.mp3 disorder-tracklength | |
190 | tracklength *.ogg disorder-tracklength | |
191 | tracklength *.wav disorder-tracklength | |
192 | tracklength *.flac disorder-tracklength | |
655f7563 | 193 | api rtp |
213b4064 RK |
194 | broadcast 127.0.0.1 %d |
195 | broadcast_from 127.0.0.1 %d | |
f1592969 | 196 | mail_sender no.such.user.sorry@greenend.org.uk |
031f8feb | 197 | """ % (testroot, encoding, testroot, testroot, top_builddir, top_builddir, |
213b4064 | 198 | port, port + 1)) |
f0feb22e RK |
199 | |
200 | def common_setup(): | |
201 | remove_dir(testroot) | |
202 | os.mkdir(testroot) | |
203 | # Choose a port | |
204 | global port | |
263ed9c1 | 205 | port = random.randint(49152, 65530) |
f0feb22e RK |
206 | while not bindable(port + 1): |
207 | print "port %d is not bindable, trying another" % (port + 1) | |
263ed9c1 | 208 | port = random.randint(49152, 65530) |
f0feb22e RK |
209 | # Log anything sent to that port |
210 | packetlog = "%s/packetlog" % testroot | |
211 | subprocess.Popen(["disorder-udplog", | |
212 | "--output", packetlog, | |
213 | "127.0.0.1", "%d" % port]) | |
214 | # disorder-udplog will quit when its parent process terminates | |
fbcfb257 | 215 | copyfile("%s/sounds/scratch.ogg" % top_srcdir, |
f9635e06 | 216 | "%s/scratch.ogg" % testroot) |
f0feb22e | 217 | default_config() |
f9635e06 | 218 | |
1c8f3db8 RK |
219 | def start_daemon(): |
220 | """start_daemon() | |
c5dbcd79 | 221 | |
1c8f3db8 | 222 | Start the daemon.""" |
31773020 | 223 | global daemon, errs, port |
213b4064 | 224 | assert daemon == None, "no daemon running" |
6ce6b5a9 RK |
225 | if not bindable(port + 1): |
226 | print "waiting for port %d to become bindable again..." % (port + 1) | |
31773020 | 227 | time.sleep(1) |
6ce6b5a9 | 228 | while not bindable(port + 1): |
31773020 | 229 | time.sleep(1) |
c5dbcd79 | 230 | print " starting daemon" |
1a4a6350 | 231 | # remove the socket if it exists |
a0ead2ba | 232 | socket = "%s/home/socket" % testroot |
abf64ff8 | 233 | if os.path.exists(socket): |
1a4a6350 | 234 | os.remove(socket) |
c5dbcd79 RK |
235 | daemon = subprocess.Popen(["disorderd", |
236 | "--foreground", | |
237 | "--config", "%s/config" % testroot], | |
238 | stderr=errs) | |
1a4a6350 RK |
239 | # Wait for the socket to be created |
240 | waited = 0 | |
ae1c9228 | 241 | sleep_resolution = 0.125 |
1a4a6350 RK |
242 | while not os.path.exists(socket): |
243 | rc = daemon.poll() | |
244 | if rc is not None: | |
245 | print "FATAL: daemon failed to start up" | |
246 | sys.exit(1) | |
ae1c9228 RK |
247 | waited += sleep_resolution |
248 | if sleep_resolution < 1: | |
249 | sleep_resolution *= 2 | |
1a4a6350 RK |
250 | if waited == 1: |
251 | print " waiting for socket..." | |
252 | elif waited >= 60: | |
253 | print "FATAL: took too long for socket to appear" | |
254 | sys.exit(1) | |
ae1c9228 | 255 | time.sleep(sleep_resolution) |
1a4a6350 | 256 | if waited > 0: |
ae1c9228 | 257 | print " took about %ss for socket to appear" % waited |
abf64ff8 RK |
258 | # Wait for root user to be created |
259 | command(["disorder", | |
260 | "--config", disorder._configfile, "--no-per-user-config", | |
261 | "--wait-for-root"]) | |
c5dbcd79 | 262 | |
f0feb22e RK |
263 | def create_user(username="fred", password="fredpass"): |
264 | """create_user(USERNAME, PASSWORD) | |
265 | ||
eb5dc014 RK |
266 | Create a user, abusing direct database access to do so. Gives the |
267 | user rights 'all', allowing them to do anything.""" | |
f0feb22e RK |
268 | print " creating user %s" % username |
269 | command(["disorder", | |
270 | "--config", disorder._configfile, "--no-per-user-config", | |
271 | "--user", "root", "adduser", username, password]) | |
eb5dc014 RK |
272 | command(["disorder", |
273 | "--config", disorder._configfile, "--no-per-user-config", | |
274 | "--user", "root", "edituser", username, "rights", "all"]) | |
f0feb22e | 275 | |
d8055dc4 | 276 | def rescan(c=None): |
dd9af5cb | 277 | print " initiating rescan" |
d8055dc4 RK |
278 | if c is None: |
279 | c = disorder.client() | |
dd9af5cb | 280 | c.rescan('wait') |
d8055dc4 RK |
281 | print " rescan completed" |
282 | ||
f9635e06 RK |
283 | def stop_daemon(): |
284 | """stop_daemon() | |
c5dbcd79 RK |
285 | |
286 | Stop the daemon if it has not stopped already""" | |
287 | global daemon | |
5c07ba71 | 288 | if daemon == None: |
53e8f9c5 | 289 | print " (daemon not running)" |
5c07ba71 | 290 | return |
c5dbcd79 RK |
291 | rc = daemon.poll() |
292 | if rc == None: | |
eee9d4b3 | 293 | print " stopping daemon" |
12746062 | 294 | os.kill(daemon.pid, 15) |
1a4a6350 | 295 | print " waiting for daemon" |
c5dbcd79 | 296 | rc = daemon.wait() |
12746062 | 297 | print " daemon has stopped (rc=%d)" % rc |
1a4a6350 RK |
298 | else: |
299 | print " daemon already stopped" | |
c5dbcd79 RK |
300 | daemon = None |
301 | ||
5c07ba71 RK |
302 | def run(module=None, report=True): |
303 | """dtest.run(MODULE) | |
304 | ||
305 | Run the test in MODULE. This can be a string (in which case the module | |
306 | will be imported) or a module object.""" | |
e663c61b | 307 | global tests, failures |
c5dbcd79 | 308 | tests += 1 |
deaaa115 | 309 | # Locate the test module |
5c07ba71 RK |
310 | if module is None: |
311 | # We're running a test stand-alone | |
312 | import __main__ | |
313 | module = __main__ | |
314 | name = os.path.splitext(os.path.basename(sys.argv[0]))[0] | |
315 | else: | |
316 | # We've been passed a module or a module name | |
317 | if type(module) == str: | |
318 | module = __import__(module) | |
319 | name = module.__name__ | |
05438e89 | 320 | print "--- %s ---" % name |
deaaa115 | 321 | # Open the error log |
5c07ba71 | 322 | global errs |
7b32e917 RK |
323 | logfile = "%s.log" % name |
324 | try: | |
325 | os.remove(logfile) | |
326 | except: | |
327 | pass | |
328 | errs = open(logfile, "a") | |
deaaa115 | 329 | # Ensure that disorder.py uses the test installation |
1c8f3db8 RK |
330 | disorder._configfile = "%s/config" % testroot |
331 | disorder._userconf = False | |
3514766b | 332 | # Make config file etc |
f9635e06 | 333 | common_setup() |
deaaa115 RK |
334 | # Create some standard tracks |
335 | stdtracks() | |
c5dbcd79 | 336 | try: |
b12be54a | 337 | module.test() |
4b37a75e | 338 | except Exception, e: |
e663c61b RK |
339 | traceback.print_exc(None, sys.stderr) |
340 | failures += 1 | |
4692cf6a RK |
341 | finally: |
342 | stop_daemon() | |
53e8f9c5 | 343 | os.system("ps -ef | grep disorderd") |
c5dbcd79 RK |
344 | if report: |
345 | if failures: | |
346 | print " FAILED" | |
347 | sys.exit(1) | |
348 | else: | |
349 | print " OK" | |
350 | ||
351 | def remove_dir(d): | |
352 | """remove_dir(D) | |
353 | ||
354 | Recursively delete directory D""" | |
355 | if os.path.lexists(d): | |
356 | if os.path.isdir(d): | |
357 | for dd in os.listdir(d): | |
358 | remove_dir("%s/%s" % (d, dd)) | |
359 | os.rmdir(d) | |
360 | else: | |
361 | os.remove(d) | |
362 | ||
31773020 RK |
363 | def lists_have_same_contents(l1, l2): |
364 | """lists_have_same_contents(L1, L2) | |
365 | ||
366 | Return True if L1 and L2 have equal members, in any order; else False.""" | |
367 | s1 = [] | |
368 | s1.extend(l1) | |
369 | s1.sort() | |
370 | s2 = [] | |
371 | s2.extend(l2) | |
372 | s2.sort() | |
d8055dc4 | 373 | return map(nfc, s1) == map(nfc, s2) |
31773020 | 374 | |
d8055dc4 | 375 | def check_files(chatty=True): |
a4d8ba8f RK |
376 | c = disorder.client() |
377 | failures = 0 | |
378 | for d in dirs_by_dir: | |
379 | xdirs = dirs_by_dir[d] | |
380 | dirs = c.directories(d) | |
31773020 | 381 | if not lists_have_same_contents(xdirs, dirs): |
d8055dc4 RK |
382 | if chatty: |
383 | ||
384 | print "directory: %s" % d | |
385 | print "expected: %s" % xdirs | |
386 | print "got: %s" % dirs | |
a4d8ba8f RK |
387 | failures += 1 |
388 | for d in files_by_dir: | |
389 | xfiles = files_by_dir[d] | |
390 | files = c.files(d) | |
31773020 | 391 | if not lists_have_same_contents(xfiles, files): |
d8055dc4 RK |
392 | if chatty: |
393 | ||
394 | print "directory: %s" % d | |
395 | print "expected: %s" % xfiles | |
396 | print "got: %s" % files | |
a4d8ba8f RK |
397 | failures += 1 |
398 | return failures | |
399 | ||
d48eab4d RK |
400 | def command(args): |
401 | """Execute a command given as a list and return its stdout""" | |
402 | p = subprocess.Popen(args, stdout=subprocess.PIPE) | |
403 | lines = p.stdout.readlines() | |
404 | rc = p.wait() | |
405 | assert rc == 0, ("%s returned status %s" % (args, rc)) | |
406 | return lines | |
407 | ||
c5dbcd79 RK |
408 | # ----------------------------------------------------------------------------- |
409 | # Common setup | |
410 | ||
411 | tests = 0 | |
412 | failures = 0 | |
413 | daemon = None | |
3bfecfac | 414 | testroot = "%s/tests/testroot" % top_builddir |
121e3654 | 415 | tracks = "%s/tracks" % testroot |