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