3 # This file is part of DisOrder.
4 # Copyright (C) 2007, 2008 Richard Kettlewell
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.
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.
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/>.
20 """Utility module used by tests"""
22 import os
,os
.path
,subprocess
,sys
,re
,time
,unicodedata
,random
,socket
25 """Write an error message and exit"""
26 sys
.stderr
.write("ERROR: %s\n" % s
)
29 # Identify the top build directory
31 if os
.path
.exists("config.h"):
33 elif os
.path
.exists("../config.h"):
34 top_builddir
= os
.path
.dirname(cwd
)
36 fatal("cannot identify build directory")
38 # Make sure the Python build directory is on the module search path
39 sys
.path
.insert(0, os
.path
.join(top_builddir
, "python"))
42 # Make sure the build directories are on the executable search path
43 ospath
= os
.environ
["PATH"].split(os
.pathsep
)
44 ospath
.insert(0, os
.path
.join(top_builddir
, "server"))
45 ospath
.insert(0, os
.path
.join(top_builddir
, "clients"))
46 ospath
.insert(0, os
.path
.join(top_builddir
, "tests"))
47 os
.environ
["PATH"] = os
.pathsep
.join(ospath
)
49 # Parse the makefile in the current directory to identify the source directory
51 for l
in file("Makefile"):
52 r
= re
.match("top_srcdir *= *(.*)", l
)
54 top_srcdir
= r
.group(1)
57 fatal("cannot identify source directory")
59 # The tests source directory must be on the module search path already since
62 # -----------------------------------------------------------------------------
67 open(b
,"w").write(open(a
).read())
70 """Convert UTF-8 to unicode. A no-op if already unicode."""
71 if type(s
) == unicode:
74 return unicode(s
, "UTF-8")
77 """Convert UTF-8 string or unicode to NFC unicode."""
78 return unicodedata
.normalize("NFC", to_unicode(s
))
83 Make track with relative path S exist"""
84 trackpath
= "%s/%s" %
(tracks
, s
)
85 trackdir
= os
.path
.dirname(trackpath
)
86 if not os
.path
.exists(trackdir
):
88 copyfile("%s/sounds/long.ogg" % top_builddir
, trackpath
)
89 # We record the tracks we created so they can be tested against
90 # server responses. We put them into NFC since that's what the server
92 bits
= nfc(s
).split('/')
95 dd
= "%s/%s" %
(dp
, d
)
96 if dp
not in dirs_by_dir
:
98 if dd
not in dirs_by_dir
[dp
]:
99 dirs_by_dir
[dp
].append(dd
)
100 dp
= "%s/%s" %
(dp
, d
)
101 if dp
not in files_by_dir
:
102 files_by_dir
[dp
] = []
103 files_by_dir
[dp
].append("%s/%s" %
(dp
, bits
[-1]))
106 # We create some tracks with non-ASCII characters in the name and
107 # we (currently) force UTF-8.
109 # On a traditional UNIX filesystem, that treats filenames as byte strings
110 # with special significant for '/', this should just work, though the
111 # names will look wrong to ls(1) in a non UTF-8 locale.
113 # On Apple HFS+ filenames normalized to a decomposed form that isn't quite
114 # NFD, so our attempts to have both normalized and denormalized filenames
115 # is frustrated. Provided we test on traditional filesytsems too this
116 # shouldn't be a problem.
117 # (See http://developer.apple.com/qa/qa2001/qa1173.html)
119 global dirs_by_dir
, files_by_dir
123 # C3 8C = 00CC LATIN CAPITAL LETTER I WITH GRAVE
125 maketrack("Joe Bloggs/First Album/01:F\xC3\x8Crst track.ogg")
127 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
129 # CC 81 = 0301 COMBINING ACUTE ACCENT
130 # (giving an NFD i-acute)
131 maketrack("Joe Bloggs/First Album/03:ThI\xCC\x81rd track.ogg")
132 # ...hopefuly giving C3 8D = 00CD LATIN CAPITAL LETTER I WITH ACUTE
133 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
134 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
135 maketrack("Joe Bloggs/Second Album/01:First track.ogg")
136 maketrack("Joe Bloggs/Second Album/02:Second track.ogg")
137 maketrack("Joe Bloggs/Second Album/03:Third track.ogg")
138 maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg")
139 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
140 maketrack("Joe Bloggs/Third Album/01:First_track.ogg")
141 maketrack("Joe Bloggs/Third Album/02:Second_track.ogg")
142 maketrack("Joe Bloggs/Third Album/03:Third_track.ogg")
143 maketrack("Joe Bloggs/Third Album/04:Fourth_track.ogg")
144 maketrack("Joe Bloggs/Third Album/05:Fifth_track.ogg")
145 maketrack("Fred Smith/Boring/01:Dull.ogg")
146 maketrack("Fred Smith/Boring/02:Tedious.ogg")
147 maketrack("Fred Smith/Boring/03:Drum Solo.ogg")
148 maketrack("Fred Smith/Boring/04:Yawn.ogg")
149 maketrack("misc/blahblahblah.ogg")
150 maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
151 maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
156 Return True iff UDP port P is bindable, else False"""
157 s
= socket
.socket(socket
.AF_INET
,
161 s
.bind(("127.0.0.1", p
))
167 def default_config(encoding
="UTF-8"):
168 """Write the default config"""
169 open("%s/config" % testroot
, "w").write(
171 collection fs %s %s/tracks
172 scratch %s/scratch.ogg
175 stopword 01 02 03 04 05 06 07 08 09 10
176 stopword 1 2 3 4 5 6 7 8 9
177 stopword 11 12 13 14 15 16 17 18 19 20
178 stopword 21 22 23 24 25 26 27 28 29 30
179 stopword the a an and to too in on of we i am as im for is
184 plugins %s/plugins/.libs
185 player *.mp3 execraw disorder-decode
186 player *.ogg execraw disorder-decode
187 player *.wav execraw disorder-decode
188 player *.flac execraw disorder-decode
189 tracklength *.mp3 disorder-tracklength
190 tracklength *.ogg disorder-tracklength
191 tracklength *.wav disorder-tracklength
192 tracklength *.flac disorder-tracklength
194 broadcast 127.0.0.1 %d
195 broadcast_from 127.0.0.1 %d
196 mail_sender no.such.user.sorry@greenend.org.uk
197 """ %
(testroot
, encoding
, testroot
, testroot
, top_builddir
, top_builddir
,
205 port
= random
.randint(49152, 65535)
206 while not bindable(port
+ 1):
207 print "port %d is not bindable, trying another" %
(port
+ 1)
208 port
= random
.randint(49152, 65535)
209 # Log anything sent to that port
210 packetlog
= "%s/packetlog" % testroot
211 subprocess
.Popen(["disorder-udplog",
212 "--output", packetlog
,
213 "127.0.0.1", "%d" % port
])
214 # disorder-udplog will quit when its parent process terminates
215 copyfile("%s/sounds/scratch.ogg" % top_srcdir
,
216 "%s/scratch.ogg" % testroot
)
223 global daemon
, errs
, port
224 assert daemon
== None, "no daemon running"
225 if not bindable(port
+ 1):
226 print "waiting for port %d to become bindable again..." %
(port
+ 1)
228 while not bindable(port
+ 1):
230 print " starting daemon"
231 # remove the socket if it exists
232 socket
= "%s/home/socket" % testroot
237 daemon
= subprocess
.Popen(["disorderd",
239 "--config", "%s/config" % testroot
],
241 # Wait for the socket to be created
243 while not os
.path
.exists(socket
):
246 print "FATAL: daemon failed to start up"
250 print " waiting for socket..."
252 print "FATAL: took too long for socket to appear"
256 print " took about %ds for socket to appear" % waited
258 def create_user(username
="fred", password
="fredpass"):
259 """create_user(USERNAME, PASSWORD)
261 Create a user, abusing direct database access to do so. Gives the
262 user rights 'all', allowing them to do anything."""
263 print " creating user %s" % username
265 "--config", disorder
._configfile
, "--no-per-user-config",
266 "--user", "root", "adduser", username
, password
])
268 "--config", disorder
._configfile
, "--no-per-user-config",
269 "--user", "root", "edituser", username
, "rights", "all"])
272 print " initiating rescan"
274 c
= disorder
.client()
276 print " rescan completed"
281 Stop the daemon if it has not stopped already"""
287 print " stopping daemon"
288 disorder
.client().shutdown()
289 print " waiting for daemon"
291 print " daemon has stopped"
293 print " daemon already stopped"
295 # Wait a bit for subprocess to finish too, to try to avoid stupid races
298 def run(module
=None, report
=True):
301 Run the test in MODULE. This can be a string (in which case the module
302 will be imported) or a module object."""
305 # Locate the test module
307 # We're running a test stand-alone
310 name
= os
.path
.splitext(os
.path
.basename(sys
.argv
[0]))[0]
312 # We've been passed a module or a module name
313 if type(module
) == str:
314 module
= __import__(module
)
315 name
= module
.__name__
316 print "--- %s ---" % name
319 logfile
= "%s.log" % name
324 errs
= open(logfile
, "a")
325 # Ensure that disorder.py uses the test installation
326 disorder
._configfile
= "%s/config" % testroot
327 disorder
._userconf
= False
328 # Make config file etc
330 # Create some standard tracks
346 Recursively delete directory D"""
347 if os
.path
.lexists(d
):
349 for dd
in os
.listdir(d
):
350 remove_dir("%s/%s" %
(d
, dd
))
355 def lists_have_same_contents(l1
, l2
):
356 """lists_have_same_contents(L1, L2)
358 Return True if L1 and L2 have equal members, in any order; else False."""
365 return map(nfc
, s1
) == map(nfc
, s2
)
367 def check_files(chatty
=True):
368 c
= disorder
.client()
370 for d
in dirs_by_dir
:
371 xdirs
= dirs_by_dir
[d
]
372 dirs
= c
.directories(d
)
373 if not lists_have_same_contents(xdirs
, dirs
):
376 print "directory: %s" % d
377 print "expected: %s" % xdirs
378 print "got: %s" % dirs
380 for d
in files_by_dir
:
381 xfiles
= files_by_dir
[d
]
383 if not lists_have_same_contents(xfiles
, files
):
386 print "directory: %s" % d
387 print "expected: %s" % xfiles
388 print "got: %s" % files
393 """Execute a command given as a list and return its stdout"""
394 p
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
)
395 lines
= p
.stdout
.readlines()
397 assert rc
== 0, ("%s returned status %s" %
(args
, rc
))
400 # -----------------------------------------------------------------------------
406 testroot
= "%s/tests/testroot" % top_builddir
407 tracks
= "%s/tracks" % testroot