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