chiark / gitweb /
tests/dtest.py: Force a sensble locale if we don't have one.
[disorder] / tests / dtest.py
1 #-*-python-*-
2 #
3 # This file is part of DisOrder.
4 # Copyright (C) 2007-2012 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 3 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,
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
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """Utility module used by tests"""
21
22 import os,os.path,subprocess,sys,re,time,unicodedata,random,socket,traceback
23 import atexit,base64,errno
24
25 homelink = None
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
32 @atexit.register
33 def cleanup():
34     if homelink is not None:
35         os.unlink(homelink)
36
37 # Identify the top build directory
38 cwd = os.getcwd()
39 if os.path.exists("config.h"):
40     top_builddir = cwd
41 elif os.path.exists("../config.h"):
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
50 # Make sure the build directories are on the executable search path
51 ospath = os.environ["PATH"].split(os.pathsep)
52 ospath.insert(0, os.path.join(top_builddir, "server"))
53 ospath.insert(0, os.path.join(top_builddir, "clients"))
54 ospath.insert(0, os.path.join(top_builddir, "tests"))
55 os.environ["PATH"] = os.pathsep.join(ospath)
56
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
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 # -----------------------------------------------------------------------------
78
79 def copyfile(a,b):
80     """copyfile(A, B)
81 Copy A to B."""
82     open(b,"w").write(open(a).read())
83
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
95 def maketrack(s):
96     """maketrack(S)
97
98 Make track with relative path S exist"""
99     trackpath = "%s/%s" % (tracks, s)
100     trackdir = os.path.dirname(trackpath)
101     if not os.path.exists(trackdir):
102         os.makedirs(trackdir)
103     copyfile("%s/sounds/long.ogg" % top_srcdir, trackpath)
104     # We record the tracks we created so they can be tested against
105     # server responses.  We put them into NFC since that's what the server
106     # uses internally.
107     bits = nfc(s).split('/')
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]))
119
120 def stdtracks():
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)
133
134     global dirs_by_dir, files_by_dir
135     dirs_by_dir={}
136     files_by_dir={}
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
142     maketrack("Joe Bloggs/First Album/02:Second track.ogg")
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
148     maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
149     maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
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")
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")
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")
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
182 def default_config(encoding="UTF-8"):
183     """Write the default config"""
184     open("%s/config" % testroot, "w").write(
185     """home %s
186 collection fs %s %s/tracks
187 scratch %s/scratch.ogg
188 queue_pad 5
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 plugins
197 plugins %s/plugins
198 plugins %s/plugins/.libs
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
207 api rtp
208 broadcast 127.0.0.1 %d
209 broadcast_from 127.0.0.1 %d
210 mail_sender no.such.user.sorry@greenend.org.uk
211 """ % (homelink, encoding, testroot, testroot, top_builddir, top_builddir,
212        port, port + 1))
213
214 def common_setup():
215     global homelink
216     remove_dir(testroot)
217     os.makedirs(testroot)
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")
238     # Choose a port
239     global port
240     port = random.randint(49152, 65530)
241     while not bindable(port + 1):
242         print "port %d is not bindable, trying another" % (port + 1)
243         port = random.randint(49152, 65530)
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
250     copyfile("%s/sounds/scratch.ogg" % top_srcdir,
251              "%s/scratch.ogg" % testroot)
252     default_config()
253
254 def start_daemon():
255     """start_daemon()
256
257 Start the daemon."""
258     global daemon, errs, port
259     assert daemon == None, "no daemon running"
260     if not bindable(port + 1):
261         print "waiting for port %d to become bindable again..." % (port + 1)
262         time.sleep(1)
263         while not bindable(port + 1):
264             time.sleep(1)
265     print " starting daemon"
266     # remove the socket if it exists
267     socket = "%s/socket" % homelink
268     if os.path.exists(socket):
269         os.remove(socket)
270     daemon = subprocess.Popen(["disorderd",
271                                "--foreground",
272                                "--config", "%s/config" % testroot],
273                               stderr=errs)
274     # Wait for the socket to be created
275     waited = 0
276     sleep_resolution = 0.125
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)
282         waited += sleep_resolution
283         if sleep_resolution < 1:
284             sleep_resolution *= 2
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)
290         time.sleep(sleep_resolution)
291     if waited > 0:
292         print "  took about %ss for socket to appear" % waited
293     # Wait for root user to be created
294     command(["disorderd",
295              "--config", disorder._configfile,
296              "--wait-for-root"])
297
298 def create_user(username="fred", password="fredpass"):
299     """create_user(USERNAME, PASSWORD)
300
301     Create a user, abusing direct database access to do so.  Gives the
302     user rights 'all', allowing them to do anything."""
303     print " creating user %s" % username
304     command(["disorder",
305              "--config", disorder._configfile, "--no-per-user-config",
306              "--user", "root", "adduser", username, password])
307     command(["disorder",
308              "--config", disorder._configfile, "--no-per-user-config",
309              "--user", "root", "edituser", username, "rights", "all"])
310
311 def rescan(c=None):
312     print " initiating rescan"
313     if c is None:
314         c = disorder.client()
315     c.rescan('wait')
316     print " rescan completed"
317
318 def stop_daemon():
319     """stop_daemon()
320
321 Stop the daemon if it has not stopped already"""
322     global daemon
323     if daemon == None:
324         print " (daemon not running)"
325         return
326     rc = daemon.poll()
327     if rc == None:
328         print " stopping daemon"
329         os.kill(daemon.pid, 15)
330         print "  waiting for daemon"
331         rc = daemon.wait()
332         print "  daemon has stopped (rc=%d)" % rc
333     else:
334         print "  daemon already stopped"
335     daemon = None
336
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."""
342     global tests, failures
343     tests += 1
344     # Locate the test module
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__
355     print "--- %s ---" % name
356     # Open the error log
357     global errs
358     logfile = "%s.log" % name
359     try:
360         os.remove(logfile)
361     except:
362         pass
363     errs = open(logfile, "a")
364     # Ensure that disorder.py uses the test installation
365     disorder._configfile = "%s/config" % testroot
366     disorder._userconf = False
367     # Make config file etc
368     common_setup()
369     # Create some standard tracks
370     stdtracks()
371     try:
372         module.test()
373     except Exception, e:
374         traceback.print_exc(None, sys.stderr)
375         failures += 1
376     finally:
377         stop_daemon()
378         os.system("ps -ef | grep disorderd")
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
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()
408     return map(nfc, s1) == map(nfc, s2)
409
410 def check_files(chatty=True):
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)
416         if not lists_have_same_contents(xdirs, dirs):
417             if chatty:
418                 print
419                 print "directory: %s" % d
420                 print "expected:  %s" % xdirs
421                 print "got:       %s" % dirs
422             failures += 1
423     for d in files_by_dir:
424         xfiles = files_by_dir[d]
425         files = c.files(d)
426         if not lists_have_same_contents(xfiles, files):
427             if chatty:
428                 print
429                 print "directory: %s" % d
430                 print "expected:  %s" % xfiles
431                 print "got:       %s" % files
432             failures += 1
433     return failures
434
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
443 # -----------------------------------------------------------------------------
444 # Common setup
445
446 tests = 0
447 failures = 0
448 daemon = None
449 testroot = "%s/tests/testroot/%s" % \
450            (top_builddir, os.path.basename(sys.argv[0]))
451 tracks = "%s/tracks" % testroot