chiark / gitweb /
The initial rescan now blocks any client connections. This means that
[disorder] / tests / dtest.py
... / ...
CommitLineData
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
24import os,os.path,subprocess,sys,re,time
25
26def 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
32cwd = os.getcwd()
33if os.path.exists("config.h"):
34 top_builddir = cwd
35elif os.path.exists("alltests"):
36 top_builddir = os.path.dirname(cwd)
37else:
38 fatal("cannot identify build directory")
39
40# Make sure the Python build directory is on the module search path
41sys.path.insert(0, os.path.join(top_builddir, "python"))
42import disorder
43
44# Make sure the server build directory is on the executable search path
45ospath = os.environ["PATH"].split(os.pathsep)
46ospath.insert(0, os.path.join(top_builddir, "server"))
47os.environ["PATH"] = os.pathsep.join(ospath)
48
49# Parse the makefile in the current directory to identify the source directory
50top_srcdir = None
51for l in file("Makefile"):
52 r = re.match("top_srcdir *= *(.*)", l)
53 if r:
54 top_srcdir = r.group(1)
55 break
56if not top_srcdir:
57 fatal("cannot identify source directory")
58
59# The tests source directory must be on the module search path already since
60# we found dtest.py
61
62# -----------------------------------------------------------------------------
63
64def copyfile(a,b):
65 """copyfile(A, B)
66Copy A to B."""
67 open(b,"w").write(open(a).read())
68
69def maketrack(s):
70 """maketrack(S)
71
72Make track with relative path S exist"""
73 trackpath = "%s/%s" % (tracks, s)
74 trackdir = os.path.dirname(trackpath)
75 if not os.path.exists(trackdir):
76 os.makedirs(trackdir)
77 copyfile("%s/sounds/slap.ogg" % top_srcdir, trackpath)
78
79def stdtracks():
80 maketrack("Joe Bloggs/First Album/01:First track.ogg")
81 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
82 maketrack("Joe Bloggs/First Album/03:Third track.ogg")
83 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
84 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
85 maketrack("Joe Bloggs/Second Album/01:First track.ogg")
86 maketrack("Joe Bloggs/Second Album/02:Second track.ogg")
87 maketrack("Joe Bloggs/Second Album/03:Third track.ogg")
88 maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg")
89 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
90 maketrack("Joe Bloggs/Third Album/01:First track.ogg")
91 maketrack("Joe Bloggs/Third Album/02:Second track.ogg")
92 maketrack("Joe Bloggs/Third Album/03:Third track.ogg")
93 maketrack("Joe Bloggs/Third Album/04:Fourth track.ogg")
94 maketrack("Joe Bloggs/Third Album/05:Fifth track.ogg")
95 maketrack("Fred Smith/Boring/01:Dull.ogg")
96 maketrack("Fred Smith/Boring/02:Tedious.ogg")
97 maketrack("Fred Smith/Boring/03:Drum Solo.ogg")
98 maketrack("Fred Smith/Boring/04:Yawn.ogg")
99 maketrack("misc/blahblahblah.ogg")
100 maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
101 maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
102
103def common_setup():
104 remove_dir(testroot)
105 os.mkdir(testroot)
106 open("%s/config" % testroot, "w").write(
107 """player *.ogg shell 'echo "$TRACK" >> %s/played.log'
108home %s
109collection fs UTF-8 %s/tracks
110scratch %s/scratch.ogg
111gap 0
112stopword 01 02 03 04 05 06 07 08 09 10
113stopword 1 2 3 4 5 6 7 8 9
114stopword 11 12 13 14 15 16 17 18 19 20
115stopword 21 22 23 24 25 26 27 28 29 30
116stopword the a an and to too in on of we i am as im for is
117username fred
118password fredpass
119allow fred fredpass
120plugins %s/plugins
121player *.mp3 execraw disorder-decode
122player *.ogg execraw disorder-decode
123player *.wav execraw disorder-decode
124player *.flac execraw disorder-decode
125tracklength *.mp3 disorder-tracklength
126tracklength *.ogg disorder-tracklength
127tracklength *.wav disorder-tracklength
128tracklength *.flac disorder-tracklength
129""" % (testroot, testroot, testroot, testroot, top_builddir))
130 copyfile("%s/sounds/scratch.ogg" % top_srcdir,
131 "%s/scratch.ogg" % testroot)
132
133def start_daemon():
134 """start_daemon()
135
136Start the daemon."""
137 global daemon, errs
138 assert daemon == None
139 print " starting daemon"
140 # remove the socket if it exists
141 socket = "%s/socket" % testroot
142 try:
143 os.remove(socket)
144 except:
145 pass
146 daemon = subprocess.Popen(["disorderd",
147 "--foreground",
148 "--config", "%s/config" % testroot],
149 stderr=errs)
150 disorder._configfile = "%s/config" % testroot
151 disorder._userconf = False
152 # Wait for the socket to be created
153 waited = 0
154 while not os.path.exists(socket):
155 rc = daemon.poll()
156 if rc is not None:
157 print "FATAL: daemon failed to start up"
158 sys.exit(1)
159 waited += 1
160 if waited == 1:
161 print " waiting for socket..."
162 elif waited >= 60:
163 print "FATAL: took too long for socket to appear"
164 sys.exit(1)
165 time.sleep(1)
166 if waited > 0:
167 print " took about %ds for socket to appear" % waited
168
169def stop_daemon():
170 """stop_daemon()
171
172Stop the daemon if it has not stopped already"""
173 global daemon
174 if daemon == None:
175 return
176 rc = daemon.poll()
177 if rc == None:
178 print " stopping daemon"
179 os.kill(daemon.pid, 15)
180 print " waiting for daemon"
181 rc = daemon.wait()
182 print " daemon has stopped"
183 else:
184 print " daemon already stopped"
185 daemon = None
186
187def run(module=None, report=True):
188 """dtest.run(MODULE)
189
190 Run the test in MODULE. This can be a string (in which case the module
191 will be imported) or a module object."""
192 global tests
193 tests += 1
194 # Locate the test module
195 if module is None:
196 # We're running a test stand-alone
197 import __main__
198 module = __main__
199 name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
200 else:
201 # We've been passed a module or a module name
202 if type(module) == str:
203 module = __import__(module)
204 name = module.__name__
205 # Open the error log
206 global errs
207 errs = open("%s.log" % name, "w")
208 # Ensure that disorder.py uses the test installation
209 disorder._configfile = "%s/config" % testroot
210 disorder._userconf = False
211 # Make config file etc
212 common_setup()
213 # Create some standard tracks
214 stdtracks()
215 try:
216 try:
217 module.test()
218 except AssertionError, e:
219 global failures
220 failures += 1
221 print e
222 finally:
223 stop_daemon()
224 if report:
225 if failures:
226 print " FAILED"
227 sys.exit(1)
228 else:
229 print " OK"
230
231def remove_dir(d):
232 """remove_dir(D)
233
234Recursively delete directory D"""
235 if os.path.lexists(d):
236 if os.path.isdir(d):
237 for dd in os.listdir(d):
238 remove_dir("%s/%s" % (d, dd))
239 os.rmdir(d)
240 else:
241 os.remove(d)
242
243# -----------------------------------------------------------------------------
244# Common setup
245
246tests = 0
247failures = 0
248daemon = None
249testroot = "%s/tests/testroot" % top_builddir
250tracks = "%s/tracks" % testroot