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