chiark / gitweb /
more tag normalization work
[disorder] / tests / dtest.py
1 #-*-python-*-
2 #
3 # This file is part of DisOrder.
4 # Copyright (C) 2007 Richard Kettlewell
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 #
21
22 """Utility module used by tests"""
23
24 import os,os.path,subprocess,sys,re,time,unicodedata,random,socket
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
44 # Make sure the build directories are on the executable search path
45 ospath = os.environ["PATH"].split(os.pathsep)
46 ospath.insert(0, os.path.join(top_builddir, "server"))
47 ospath.insert(0, os.path.join(top_builddir, "clients"))
48 ospath.insert(0, os.path.join(top_builddir, "tests"))
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 # -----------------------------------------------------------------------------
65
66 def copyfile(a,b):
67     """copyfile(A, B)
68 Copy A to B."""
69     open(b,"w").write(open(a).read())
70
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
82 def maketrack(s):
83     """maketrack(S)
84
85 Make track with relative path S exist"""
86     trackpath = "%s/%s" % (tracks, s)
87     trackdir = os.path.dirname(trackpath)
88     if not os.path.exists(trackdir):
89         os.makedirs(trackdir)
90     copyfile("%s/sounds/slap.ogg" % top_srcdir, trackpath)
91     # We record the tracks we created so they can be tested against
92     # server responses.  We put them into NFC since that's what the server
93     # uses internally.
94     bits = nfc(s).split('/')
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]))
106
107 def stdtracks():
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)
120
121     global dirs_by_dir, files_by_dir
122     dirs_by_dir={}
123     files_by_dir={}
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
129     maketrack("Joe Bloggs/First Album/02:Second track.ogg")
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
135     maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
136     maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
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")
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")
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")
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
169 def common_setup():
170     remove_dir(testroot)
171     os.mkdir(testroot)
172     # Choose a port
173     global port
174     port = random.randint(49152, 65535)
175     while not bindable(port + 1):
176         print "port %d is not bindable, trying another" % (port + 1)
177         port = random.randint(49152, 65535)
178     # Log anything sent to that port
179     packetlog = "%s/packetlog" % testroot
180     subprocess.Popen(["disorder-udplog",
181                       "--output", packetlog,
182                       "127.0.0.1", "%d" % port])
183     # disorder-udplog will quit when its parent process terminates
184     open("%s/config" % testroot, "w").write(
185     """home %s
186 collection fs UTF-8 %s/tracks
187 scratch %s/scratch.ogg
188 gap 0
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
196 allow fred fredpass
197 trust fred
198 plugins
199 plugins %s/plugins
200 plugins %s/plugins/.libs
201 player *.mp3 execraw disorder-decode
202 player *.ogg execraw disorder-decode
203 player *.wav execraw disorder-decode
204 player *.flac execraw disorder-decode
205 tracklength *.mp3 disorder-tracklength
206 tracklength *.ogg disorder-tracklength
207 tracklength *.wav disorder-tracklength
208 tracklength *.flac disorder-tracklength
209 speaker_backend network
210 broadcast 127.0.0.1 %d
211 broadcast_from 127.0.0.1 %d
212 """ % (testroot, testroot, testroot, top_builddir, top_builddir,
213        port, port + 1))
214     copyfile("%s/sounds/scratch.ogg" % top_srcdir,
215              "%s/scratch.ogg" % testroot)
216
217 def start_daemon():
218     """start_daemon()
219
220 Start the daemon."""
221     global daemon, errs, port
222     assert daemon == None, "no daemon running"
223     if not bindable(port + 1):
224         print "waiting for port %d to become bindable again..." % (port + 1)
225         time.sleep(1)
226         while not bindable(port + 1):
227             time.sleep(1)
228     print " starting daemon"
229     # remove the socket if it exists
230     socket = "%s/socket" % testroot
231     try:
232         os.remove(socket)
233     except:
234         pass
235     daemon = subprocess.Popen(["disorderd",
236                                "--foreground",
237                                "--config", "%s/config" % testroot],
238                               stderr=errs)
239     # Wait for the socket to be created
240     waited = 0
241     while not os.path.exists(socket):
242         rc = daemon.poll()
243         if rc is not None:
244             print "FATAL: daemon failed to start up"
245             sys.exit(1)
246         waited += 1
247         if waited == 1:
248             print "  waiting for socket..."
249         elif waited >= 60:
250             print "FATAL: took too long for socket to appear"
251             sys.exit(1)
252         time.sleep(1)
253     if waited > 0:
254         print "  took about %ds for socket to appear" % waited
255
256 def stop_daemon():
257     """stop_daemon()
258
259 Stop the daemon if it has not stopped already"""
260     global daemon
261     if daemon == None:
262         return
263     rc = daemon.poll()
264     if rc == None:
265         print " stopping daemon"
266         disorder.client().shutdown()
267         print "  waiting for daemon"
268         rc = daemon.wait()
269         print "  daemon has stopped"
270     else:
271         print "  daemon already stopped"
272     daemon = None
273
274 def run(module=None, report=True):
275     """dtest.run(MODULE)
276
277     Run the test in MODULE.  This can be a string (in which case the module
278     will be imported) or a module object."""
279     global tests
280     tests += 1
281     # Locate the test module
282     if module is None:
283         # We're running a test stand-alone
284         import __main__
285         module = __main__
286         name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
287     else:
288         # We've been passed a module or a module name
289         if type(module) == str:
290             module = __import__(module)
291         name = module.__name__
292     # Open the error log
293     global errs
294     errs = open("%s.log" % name, "w")
295     # Ensure that disorder.py uses the test installation
296     disorder._configfile = "%s/config" % testroot
297     disorder._userconf = False
298     # Make config file etc
299     common_setup()
300     # Create some standard tracks
301     stdtracks()
302     try:
303         try:
304             module.test()
305         except AssertionError, e:
306             global failures
307             failures += 1
308             print "assertion failed: %s" % e.message
309     finally:
310         stop_daemon()
311     if report:
312         if failures:
313             print " FAILED"
314             sys.exit(1)
315         else:
316             print " OK"
317
318 def remove_dir(d):
319     """remove_dir(D)
320
321 Recursively delete directory D"""
322     if os.path.lexists(d):
323         if os.path.isdir(d):
324             for dd in os.listdir(d):
325                 remove_dir("%s/%s" % (d, dd))
326             os.rmdir(d)
327         else:
328             os.remove(d)
329
330 def lists_have_same_contents(l1, l2):
331     """lists_have_same_contents(L1, L2)
332
333     Return True if L1 and L2 have equal members, in any order; else False."""
334     s1 = []
335     s1.extend(l1)
336     s1.sort()
337     s2 = []
338     s2.extend(l2)
339     s2.sort()
340     return s1 == s2
341
342 def check_files():
343     c = disorder.client()
344     failures = 0
345     for d in dirs_by_dir:
346         xdirs = dirs_by_dir[d]
347         dirs = c.directories(d)
348         if not lists_have_same_contents(xdirs, dirs):
349             print
350             print "directory: %s" % d
351             print "expected:  %s" % xdirs
352             print "got:       %s" % dirs
353             failures += 1
354     for d in files_by_dir:
355         xfiles = files_by_dir[d]
356         files = c.files(d)
357         if not lists_have_same_contents(xfiles, files):
358             print
359             print "directory: %s" % d
360             print "expected:  %s" % xfiles
361             print "got:       %s" % files
362             failures += 1
363     return failures
364
365 def command(args):
366     """Execute a command given as a list and return its stdout"""
367     p = subprocess.Popen(args, stdout=subprocess.PIPE)
368     lines = p.stdout.readlines()
369     rc = p.wait()
370     assert rc == 0, ("%s returned status %s" % (args, rc))
371     return lines
372
373 # -----------------------------------------------------------------------------
374 # Common setup
375
376 tests = 0
377 failures = 0
378 daemon = None
379 testroot = "%s/tests/testroot" % top_builddir
380 tracks = "%s/tracks" % testroot