cfac85b572851815e030689792ed4611fbaabd10
[quix0rs-apt-p2p.git] / test.py
1 #!/usr/bin/env python
2
3 """Automated tests of the apt-dht functionality.
4
5 This script runs several automatic tests of some of the functionality in
6 the apt-dht program.
7
8 @type tests: C{dictionary}
9 @var tests: all of the tests that can be run.
10     The keys are the test names (strings) which are used on the command-line
11     to identify the tests (can not be 'all' or 'help'). The values are tuples
12     with four elements: a description of the test (C{string}), the bootstrap
13     nodes to start (C{dictionary}), the downloaders to start (C{dictionary},
14     and the apt-get commands to run (C{list}).
15     
16     The bootstrap nodes keys are integers, which must be in the range 1-9.
17     The values are the dictionary of string formatting values for creating
18     the apt-dht configuration file (see L{apt_dht_conf_template} below).
19     
20     The downloaders keys are also integers in the range 1-99. The values are
21     the dictionary of string formatting values for creating the apt-dht
22     configuration file (see L{apt_dht_conf_template} below).
23     
24     The apt-get commands' list elements are tuples with 2 elements: the
25     downloader to run the command on, and the list of command-line
26     arguments to specify to the apt-get program.
27     
28 @type CWD: C{string}
29 @var CWD: the working directory the script was run from
30 @type apt_conf_template: C{string}
31 @var apt_conf_template: the template to use for the apt.conf file
32
33 """
34
35 from time import sleep, time
36 import sys, os, signal
37 from traceback import print_exc
38 from os.path import exists
39
40 tests = {'1': ('Start a single bootstrap and downloader, test updating and downloading ' +
41              'using HTTP only.',
42              {1: {}},
43              {1: {}},
44              [(1, ['update']), 
45               (1, ['install', 'aboot-base']),
46               (1, ['install', 'aap-doc']),
47               (1, ['install', 'ada-reference-manual']),
48               (1, ['install', 'aspectj-doc']),
49               (1, ['install', 'fop-doc']),
50               (1, ['install', 'jswat-doc']),
51               (1, ['install', 'asis-doc']),
52               (1, ['install', 'bison-doc']),
53               (1, ['install', 'crash-whitepaper']),
54               (1, ['install', 'doc-iana']),
55               ]),
56
57          '2': ('Start a single bootstrap and 2 downloaders to test downloading from a peer.',
58                {1: {}},
59                {1: {},
60                 2: {}},
61                [(1, ['update']),
62                 (2, ['update']),
63                 (1, ['install', 'aboot-base']),
64                 (2, ['install', 'aboot-base']),
65                 (1, ['install', 'aap-doc']),
66                 (1, ['install', 'ada-reference-manual']),
67                 (1, ['install', 'fop-doc']),
68                 (1, ['install', 'jswat-doc']),
69                 (1, ['install', 'bison-doc']),
70                 (1, ['install', 'crash-whitepaper']),
71                 (2, ['install', 'aap-doc']),
72                 (2, ['install', 'ada-reference-manual']),
73                 (2, ['install', 'fop-doc']),
74                 (2, ['install', 'jswat-doc']),
75                 (2, ['install', 'bison-doc']),
76                 (2, ['install', 'crash-whitepaper']),
77                 ]),
78                 
79          '3': ('Start a single bootstrap and 6 downloaders, to test downloading' +
80                ' speeds from each other.',
81                {1: {}},
82                {1: {},
83                 2: {},
84                 3: {},
85                 4: {},
86                 5: {},
87                 6: {}},
88                [(1, ['update']),
89                 (1, ['install', 'aboot-base']),
90                 (1, ['install', 'ada-reference-manual']),
91                 (1, ['install', 'fop-doc']),
92                 (1, ['install', 'doc-iana']),
93                 (2, ['update']),
94                 (2, ['install', 'aboot-base']),
95                 (2, ['install', 'ada-reference-manual']),
96                 (2, ['install', 'fop-doc']),
97                 (2, ['install', 'doc-iana']),
98                 (3, ['update']),
99                 (3, ['install', 'aboot-base']),
100                 (3, ['install', 'ada-reference-manual']),
101                 (3, ['install', 'fop-doc']),
102                 (3, ['install', 'doc-iana']),
103                 (4, ['update']),
104                 (4, ['install', 'aboot-base']),
105                 (4, ['install', 'ada-reference-manual']),
106                 (4, ['install', 'fop-doc']),
107                 (4, ['install', 'doc-iana']),
108                 (5, ['update']),
109                 (5, ['install', 'aboot-base']),
110                 (5, ['install', 'ada-reference-manual']),
111                 (5, ['install', 'fop-doc']),
112                 (5, ['install', 'doc-iana']),
113                 (6, ['update']),
114                 (6, ['install', 'aboot-base']),
115                 (6, ['install', 'ada-reference-manual']),
116                 (6, ['install', 'fop-doc']),
117                 (6, ['install', 'doc-iana']),
118                 ]),
119
120          '4': ('Start a single bootstrap and 1 downloader, requesting the same' +
121                ' packages multiple times to test caching.',
122                {1: {}},
123                {1: {}},
124                [(1, ['update']),
125                 (1, ['install', 'aboot-base']),
126                 (1, ['install', 'ada-reference-manual']),
127                 (1, ['install', 'fop-doc']),
128                 (1, ['install', 'doc-iana']),
129                 (1, ['update']),
130                 (1, ['install', 'aboot-base']),
131                 (1, ['install', 'ada-reference-manual']),
132                 (1, ['install', 'fop-doc']),
133                 (1, ['install', 'doc-iana']),
134                 (1, ['update']),
135                 (1, ['install', 'aboot-base']),
136                 (1, ['install', 'ada-reference-manual']),
137                 (1, ['install', 'fop-doc']),
138                 (1, ['install', 'doc-iana']),
139                 ]),
140                 
141          '5': ('Start a single bootstrap and 6 downloaders, update all to test' +
142                ' that they can all see each other.',
143                {1: {}},
144                {1: ([], {'suites': 'contrib non-free'}),
145                 2: ([], {'suites': 'contrib non-free'}),
146                 3: ([], {'suites': 'contrib non-free'}),
147                 4: ([], {'suites': 'contrib non-free'}),
148                 5: ([], {'suites': 'contrib non-free'}),
149                 6: ([], {'suites': 'contrib non-free'})},
150                [(1, ['update']),
151                 (2, ['update']),
152                 (3, ['update']),
153                 (4, ['update']),
154                 (5, ['update']),
155                 (6, ['update']),
156                 ]),
157
158         '6': ('Test caching with multiple apt-get updates.',
159              {1: {}},
160              {1: {}},
161              [(1, ['update']), 
162               (1, ['update']),
163               (1, ['update']),
164               (1, ['update']),
165               ]),
166
167         '7': ('Test pipelining of multiple simultaneous downloads.',
168              {1: {}},
169              {1: {}},
170              [(1, ['update']), 
171               (1, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
172                    'aspectj-doc', 'fop-doc', 'jswat-doc', 'asis-doc',
173                    'bison-doc', 'crash-whitepaper', 'doc-iana',
174                    'bash-doc', 'apt-howto-common', 'autotools-dev',
175                    'aptitude-doc-en', 'armagetron-common', 'asr-manpages',
176                    'atomix-data', 'alcovebook-sgml-doc', 'alamin-doc',
177                    'aegis-doc', 'afbackup-common', 'airstrike-common',
178                    ]),
179               ]),
180
181         '8': ('Test pipelining of multiple simultaneous downloads with many peers.',
182              {1: {}},
183              {1: {},
184               2: {},
185               3: {},
186               4: {},
187               5: {},
188               6: {}},
189              [(1, ['update']), 
190               (1, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
191                    'aspectj-doc', 'fop-doc', 'jswat-doc', 'asis-doc',
192                    'bison-doc', 'crash-whitepaper', 'doc-iana',
193                    'bash-doc', 'apt-howto-common', 'autotools-dev',
194                    'aptitude-doc-en', 'armagetron-common', 'asr-manpages',
195                    'atomix-data', 'alcovebook-sgml-doc', 'alamin-doc',
196                    'aegis-doc', 'afbackup-common', 'airstrike-common',
197                    ]),
198               (2, ['update']), 
199               (2, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
200                    'aspectj-doc', 'fop-doc', 'jswat-doc', 'asis-doc',
201                    'bison-doc', 'crash-whitepaper', 'doc-iana',
202                    'bash-doc', 'apt-howto-common', 'autotools-dev',
203                    'aptitude-doc-en', 'armagetron-common', 'asr-manpages',
204                    'atomix-data', 'alcovebook-sgml-doc', 'alamin-doc',
205                    'aegis-doc', 'afbackup-common', 'airstrike-common',
206                    ]),
207               (3, ['update']), 
208               (3, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
209                    'aspectj-doc', 'fop-doc', 'jswat-doc', 'asis-doc',
210                    'bison-doc', 'crash-whitepaper', 'doc-iana',
211                    'bash-doc', 'apt-howto-common', 'autotools-dev',
212                    'aptitude-doc-en', 'armagetron-common', 'asr-manpages',
213                    'atomix-data', 'alcovebook-sgml-doc', 'alamin-doc',
214                    'aegis-doc', 'afbackup-common', 'airstrike-common',
215                    ]),
216               (4, ['update']), 
217               (4, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
218                    'aspectj-doc', 'fop-doc', 'jswat-doc', 'asis-doc',
219                    'bison-doc', 'crash-whitepaper', 'doc-iana',
220                    'bash-doc', 'apt-howto-common', 'autotools-dev',
221                    'aptitude-doc-en', 'armagetron-common', 'asr-manpages',
222                    'atomix-data', 'alcovebook-sgml-doc', 'alamin-doc',
223                    'aegis-doc', 'afbackup-common', 'airstrike-common',
224                    ]),
225               (5, ['update']), 
226               (5, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
227                    'aspectj-doc', 'fop-doc', 'jswat-doc', 'asis-doc',
228                    'bison-doc', 'crash-whitepaper', 'doc-iana',
229                    'bash-doc', 'apt-howto-common', 'autotools-dev',
230                    'aptitude-doc-en', 'armagetron-common', 'asr-manpages',
231                    'atomix-data', 'alcovebook-sgml-doc', 'alamin-doc',
232                    'aegis-doc', 'afbackup-common', 'airstrike-common',
233                    ]),
234               (6, ['update']), 
235               (6, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
236                    'aspectj-doc', 'fop-doc', 'jswat-doc', 'asis-doc',
237                    'bison-doc', 'crash-whitepaper', 'doc-iana',
238                    'bash-doc', 'apt-howto-common', 'autotools-dev',
239                    'aptitude-doc-en', 'armagetron-common', 'asr-manpages',
240                    'atomix-data', 'alcovebook-sgml-doc', 'alamin-doc',
241                    'aegis-doc', 'afbackup-common', 'airstrike-common',
242                    ]),
243               ]),
244
245          }
246
247 assert 'all' not in tests
248 assert 'help' not in tests
249
250 CWD = os.getcwd()
251 apt_conf_template = """
252 {
253   // Location of the state dir
254   State "var/lib/apt/"
255   {
256      Lists "lists/";
257      xstatus "xstatus";
258      userstatus "status.user";
259      cdroms "cdroms.list";
260   };
261
262   // Location of the cache dir
263   Cache "var/cache/apt/" {
264      Archives "archives/";
265      srcpkgcache "srcpkgcache.bin";
266      pkgcache "pkgcache.bin";
267   };
268
269   // Config files
270   Etc "etc/apt/" {
271      SourceList "sources.list";
272      Main "apt.conf";
273      Preferences "preferences";
274      Parts "apt.conf.d/";
275   };
276
277   // Locations of binaries
278   Bin {
279      methods "/usr/lib/apt/methods/";
280      gzip "/bin/gzip";
281      gpg  "/usr/bin/gpgv";
282      dpkg "/usr/bin/dpkg --simulate";
283      dpkg-source "/usr/bin/dpkg-source";
284      dpkg-buildpackage "/usr/bin/dpkg-buildpackage";
285      apt-get "/usr/bin/apt-get";
286      apt-cache "/usr/bin/apt-cache";
287   };
288 };
289
290 /* Options you can set to see some debugging text They correspond to names
291    of classes in the source code */
292 Debug
293 {
294   pkgProblemResolver "false";
295   pkgDepCache::AutoInstall "false"; // what packages apt install to satify dependencies
296   pkgAcquire "false";
297   pkgAcquire::Worker "false";
298   pkgAcquire::Auth "false";
299   pkgDPkgPM "false";
300   pkgDPkgProgressReporting "false";
301   pkgOrderList "false";
302   BuildDeps "false";
303
304   pkgInitialize "false";   // This one will dump the configuration space
305   NoLocking "false";
306   Acquire::Ftp "false";    // Show ftp command traffic
307   Acquire::Http "false";   // Show http command traffic
308   Acquire::Debtorrent "false";   // Show http command traffic
309   Acquire::gpgv "false";   // Show the gpgv traffic
310   aptcdrom "false";        // Show found package files
311   IdentCdrom "false";
312
313 }
314 """
315 apt_dht_conf_template = """
316 [DEFAULT]
317
318 # Port to listen on for all requests (TCP and UDP)
319 PORT = %(PORT)s
320     
321 # Directory to store the downloaded files in
322 CACHE_DIR = %(CACHE_DIR)s
323     
324 # Other directories containing packages to share with others
325 # WARNING: all files in these directories will be hashed and available
326 #          for everybody to download
327 # OTHER_DIRS = 
328     
329 # User name to try and run as
330 # USERNAME = 
331
332 # Whether it's OK to use an IP addres from a known local/private range
333 LOCAL_OK = yes
334
335 # Unload the packages cache after an interval of inactivity this long.
336 # The packages cache uses a lot of memory, and only takes a few seconds
337 # to reload when a new request arrives.
338 UNLOAD_PACKAGES_CACHE = 5m
339
340 # Which DHT implementation to use.
341 # It must be possile to do "from <DHT>.DHT import DHT" to get a class that
342 # implements the IDHT interface.
343 DHT = apt_dht_Khashmir
344
345 # Whether to only run the DHT (for providing only a bootstrap node)
346 DHT-ONLY = %(DHT-ONLY)s
347
348 [apt_dht_Khashmir]
349 # bootstrap nodes to contact to join the DHT
350 BOOTSTRAP = %(BOOTSTRAP)s
351
352 # whether this node is a bootstrap node
353 BOOTSTRAP_NODE = %(BOOTSTRAP_NODE)s
354
355 # Kademlia "K" constant, this should be an even number
356 K = 8
357
358 # SHA1 is 160 bits long
359 HASH_LENGTH = 160
360
361 # checkpoint every this many seconds
362 CHECKPOINT_INTERVAL = 5m
363
364 # concurrent xmlrpc calls per find node/value request!
365 CONCURRENT_REQS = 4
366
367 # how many hosts to post to
368 STORE_REDUNDANCY = 3
369
370 # how many times in a row a node can fail to respond before it's booted from the routing table
371 MAX_FAILURES = 3
372
373 # never ping a node more often than this
374 MIN_PING_INTERVAL = 15m
375
376 # refresh buckets that haven't been touched in this long
377 BUCKET_STALENESS = 1h
378
379 # time before expirer starts running
380 KEINITIAL_DELAY = 15s
381
382 # time between expirer runs
383 KE_DELAY = 20m
384
385 # expire entries older than this
386 KE_AGE = 1h
387
388 # whether to spew info about the requests/responses in the protocol
389 SPEW = yes
390 """
391
392 def rmrf(top):
393     """Remove all the files and directories below a top-level one.
394     
395     @type top: C{string}
396     @param top: the top-level directory to start at
397     
398     """
399     
400     for root, dirs, files in os.walk(top, topdown=False):
401         for name in files:
402             os.remove(os.path.join(root, name))
403         for name in dirs:
404             os.rmdir(os.path.join(root, name))
405
406 def join(dir):
407     """Join together a list of directories into a path string.
408     
409     @type dir: C{list} of C{string}
410     @param dir: the path to join together
411     @rtype: C{string}
412     @return: the joined together path
413     
414     """
415     
416     joined = ''
417     for i in dir:
418         joined = os.path.join(joined, i)
419     return joined
420
421 def makedirs(dir):
422     """Create all the directories to make a path.
423     
424     @type dir: C{list} of C{string}
425     @param dir: the path to create
426     
427     """
428     if not os.path.exists(join(dir)):
429         os.makedirs(join(dir))
430
431 def touch(path):
432     """Create an empty file.
433     
434     @type path: C{list} of C{string}
435     @param path: the path to create
436     
437     """
438     
439     f = open(join(path), 'w')
440     f.close()
441
442 def start(cmd, args, work_dir = None):
443     """Fork and start a background process running.
444     
445     @type cmd: C{string}
446     @param cmd: the name of the command to run
447     @type args: C{list} of C{string}
448     @param args: the argument to pass to the command
449     @type work_dir: C{string}
450     @param work_dir: the directory to change to to execute the child process in
451         (optional, defaults to the current directory)
452     @rtype: C{int}
453     @return: the PID of the forked process
454     
455     """
456     
457     new_cmd = [cmd] + args
458     pid = os.spawnvp(os.P_NOWAIT, new_cmd[0], new_cmd)
459     return pid
460
461 def stop(pid):
462     """Stop a forked background process that is running.
463     
464     @type pid: C{int}
465     @param pid: the PID of the process to stop
466     @rtype: C{int}
467     @return: the return status code from the child
468     
469     """
470
471     # First try a keyboard interrupt
472     os.kill(pid, signal.SIGINT)
473     for i in xrange(5):
474         sleep(1)
475         (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
476         if r_pid:
477             return r_value
478     
479     # Try a keyboard interrupt again, just in case
480     os.kill(pid, signal.SIGINT)
481     for i in xrange(5):
482         sleep(1)
483         (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
484         if r_pid:
485             return r_value
486
487     # Try a terminate
488     os.kill(pid, signal.SIGTERM)
489     for i in xrange(5):
490         sleep(1)
491         (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
492         if r_pid:
493             return r_value
494
495     # Finally a kill, don't return until killed
496     os.kill(pid, signal.SIGKILL)
497     while not r_pid:
498         sleep(1)
499         (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
500
501     return r_value
502
503 def apt_get(num_down, cmd):
504     """Start an apt-get process in the background.
505
506     The default argument specified to the apt-get invocation are
507     'apt-get -d -q -c <conf_file>'. Any additional arguments (including
508     the apt-get action to use) should be specified.
509     
510     @type num_down: C{int}
511     @param num_down: the number of the downloader to use
512     @type cmd: C{list} of C{string}
513     @param cmd: the arguments to pass to the apt-get process
514     @rtype: C{int}
515     @return: the PID of the background process
516     
517     """
518     
519     print '*************** apt-get (' + str(num_down) + ') ' + ' '.join(cmd) + ' ****************'
520     apt_conf = join([down_dir(num_down), 'etc', 'apt', 'apt.conf'])
521     dpkg_status = join([down_dir(num_down), 'var', 'lib', 'dpkg', 'status'])
522     args = ['-d', '-c', apt_conf, '-o', 'Dir::state::status='+dpkg_status] + cmd
523     pid = start('apt-get', args)
524     return pid
525
526 def bootstrap_address(num_boot):
527     """Determine the bootstrap address to use for a node.
528     
529     @type num_boot: C{int}
530     @param num_boot: the number of the bootstrap node
531     @rtype: C{string}
532     @return: the bootstrap address to use
533     
534     """
535     
536     return 'localhost:1' + str(num_boot) + '969'
537
538 def down_dir(num_down):
539     """Determine the working directory to use for a downloader.
540     
541     @type num_down: C{int}
542     @param num_down: the number of the downloader
543     @rtype: C{string}
544     @return: the downloader's directory
545     
546     """
547     
548     return os.path.join(CWD,'downloader' + str(num_down))
549
550 def boot_dir(num_boot):
551     """Determine the working directory to use for a bootstrap node.
552     
553     @type num_boot: C{int}
554     @param num_boot: the number of the bootstrap node
555     @rtype: C{string}
556     @return: the bootstrap node's directory
557     
558     """
559     
560     return os.path.join(CWD,'bootstrap' + str(num_boot))
561
562 def start_downloader(bootstrap_addresses, num_down, options = {},
563                      mirror = 'ftp.us.debian.org/debian', 
564                      suites = 'main contrib non-free', clean = True):
565     """Initialize a new downloader process.
566
567     The default arguments specified to the downloader invocation are
568     the configuration directory, apt port, minport, maxport and the
569     maximum upload rate. 
570     Any additional arguments needed should be specified by L{options}.
571     
572     @type num_down: C{int}
573     @param num_down: the number of the downloader to use
574     @type options: C{dictionary}
575     @param options: the dictionary of string formatting values for creating
576         the apt-dht configuration file (see L{apt_dht_conf_template} above).
577         (optional, defaults to only using the default arguments)
578     @type mirror: C{string}
579     @param mirror: the Debian mirror to use
580         (optional, defaults to 'ftp.us.debian.org/debian')
581     @type suites: C{string}
582     @param suites: space separated list of suites to download
583         (optional, defaults to 'main contrib non-free')
584     @type clean: C{boolean}
585     @param clean: whether to remove any previous downloader files
586         (optional, defaults to removing them)
587     @rtype: C{int}
588     @return: the PID of the downloader process
589     
590     """
591     
592     assert num_down < 100
593     
594     print '************************** Starting Downloader ' + str(num_down) + ' **************************'
595
596     downloader_dir = down_dir(num_down)
597     
598     if clean:
599         try:
600             rmrf(downloader_dir)
601         except:
602             pass
603     
604         # Create the directory structure needed by apt
605         makedirs([downloader_dir, 'etc', 'apt', 'apt.conf.d'])
606         makedirs([downloader_dir, 'var', 'lib', 'apt', 'lists', 'partial'])
607         makedirs([downloader_dir, 'var', 'lib', 'dpkg'])
608         makedirs([downloader_dir, 'var', 'cache', 'apt', 'archives', 'partial'])
609         touch([downloader_dir, 'var', 'lib', 'apt', 'lists', 'lock'])
610         touch([downloader_dir, 'var', 'lib', 'dpkg', 'lock'])
611         touch([downloader_dir, 'var', 'lib', 'dpkg', 'status'])
612         touch([downloader_dir, 'var', 'cache', 'apt', 'archives', 'lock'])
613
614         # Create apt's config files
615         f = open(join([downloader_dir, 'etc', 'apt', 'sources.list']), 'w')
616         f.write('deb http://localhost:1%02d77/%s/ stable %s\n' % (num_down, mirror, suites))
617         f.close()
618
619         f = open(join([downloader_dir, 'etc', 'apt', 'apt.conf']), 'w')
620         f.write('Dir "' + downloader_dir + '"')
621         f.write(apt_conf_template)
622         f.close()
623
624     defaults = {'PORT': '1%02d77' % num_down,
625                 'CACHE_DIR': downloader_dir,
626                 'DHT-ONLY': 'no',
627                 'BOOTSTRAP': bootstrap_addresses,
628                 'BOOTSTRAP_NODE': 'no'}
629
630     for k in options:
631         defaults[k] = options[k]
632     f = open(join([downloader_dir, 'apt-dht.conf']), 'w')
633     f.write(apt_dht_conf_template % defaults)
634     f.close()
635     
636     pid = start('python', [join([sys.path[0], 'apt-dht.py']),
637                            '--config-file=' + join([downloader_dir, 'apt-dht.conf']),
638                            '--log-file=' + join([downloader_dir, 'apt-dht.log']),],
639                 downloader_dir)
640     return pid
641
642 def start_bootstrap(bootstrap_addresses, num_boot, options = [], clean = True):
643     """Initialize a new bootstrap node process.
644
645     The default arguments specified to the apt-dht invocation are
646     the state file and port to use. Any additional arguments needed 
647     should be specified by L{options}.
648     
649     @type num_boot: C{int}
650     @param num_boot: the number of the bootstrap node to use
651     @type options: C{list} of C{string}
652     @param options: the arguments to pass to the bootstrap node
653         (optional, defaults to only using the default arguments)
654     @type clean: C{boolean}
655     @param clean: whether to remove any previous bootstrap node files
656         (optional, defaults to removing them)
657     @rtype: C{int}
658     @return: the PID of the downloader process
659     
660     """
661     
662     assert num_boot < 10
663
664     print '************************** Starting Bootstrap ' + str(num_boot) + ' **************************'
665
666     bootstrap_dir = boot_dir(num_boot)
667     
668     if clean:
669         try:
670             rmrf(bootstrap_dir)
671         except:
672             pass
673
674     makedirs([bootstrap_dir])
675
676     defaults = {'PORT': '1%d969' % num_boot,
677                 'CACHE_DIR': bootstrap_dir,
678                 'DHT-ONLY': 'yes',
679                 'BOOTSTRAP': bootstrap_addresses,
680                 'BOOTSTRAP_NODE': 'yes'}
681
682     for k in options:
683         defaults[k] = options[k]
684     f = open(join([bootstrap_dir, 'apt-dht.conf']), 'w')
685     f.write(apt_dht_conf_template % defaults)
686     f.close()
687     
688     pid = start('python', [join([sys.path[0], 'apt-dht.py']),
689                            '--config-file=' + join([bootstrap_dir, 'apt-dht.conf']),
690                            '--log-file=' + join([bootstrap_dir, 'apt-dht.log']),],
691                 bootstrap_dir)
692
693     return pid
694
695 def run_test(bootstraps, downloaders, apt_get_queue):
696     """Run a single test.
697     
698     @type bootstraps: C{dictionary} of {C{int}: C{list} of C{string}}
699     @param bootstraps: the bootstrap nodes to start, keys are the bootstrap numbers and
700         values are the list of options to invoke the bootstrap node with
701     @type downloaders: C{dictionary} of {C{int}: (C{int}, C{list} of C{string})}
702     @param downloaders: the downloaders to start, keys are the downloader numbers and
703         values are the list of options to invoke the downloader with
704     @type apt_get_queue: C{list} of (C{int}, C{list} of C{string})
705     @param apt_get_queue: the apt-get downloader to use and commands to execute
706     @rtype: C{list} of (C{float}, C{int})
707     @return: the execution time and returned status code for each element of apt_get_queue
708     
709     """
710     
711     running_bootstraps = {}
712     running_downloaders = {}
713     running_apt_get = {}
714     apt_get_results = []
715
716     try:
717         boot_keys = bootstraps.keys()
718         boot_keys.sort()
719         bootstrap_addresses = bootstrap_address(boot_keys[0])
720         for i in xrange(1, len(boot_keys)):
721             bootstrap_addresses += '\n      ' + bootstrap_address(boot_keys[i])
722             
723         for k, v in bootstraps.items():
724             running_bootstraps[k] = start_bootstrap(bootstrap_addresses, k, v)
725         
726         sleep(5)
727         
728         for k, v in downloaders.items():
729             running_downloaders[k] = start_downloader(bootstrap_addresses, k, v)
730     
731         sleep(5)
732         
733         for (num_down, cmd) in apt_get_queue:
734             running_apt_get[num_down] = apt_get(num_down, cmd)
735             start_time = time()
736             (pid, r_value) = os.waitpid(running_apt_get[num_down], 0)
737             elapsed = time() - start_time
738             del running_apt_get[num_down]
739             r_value = r_value / 256
740             apt_get_results.append((elapsed, r_value))
741
742             if r_value == 0:
743                 print '********** apt-get completed successfully in ' +  str(elapsed) + ' sec. *****************'
744             else:
745                 print '********** apt-get finished with status ' + str(r_value) + ' in ' +  str(elapsed) + ' sec. ************'
746         
747             sleep(5)
748             
749     except:
750         print '************************** Exception occurred **************************'
751         print_exc()
752         print '************************** will attempt to shut down *******************'
753         
754     print '*********************** shutting down the apt-gets *******************'
755     for k, v in running_apt_get.items():
756         try:
757             print 'apt-get', k, stop(v)
758         except:
759             print '************************** Exception occurred **************************'
760             print_exc()
761
762     sleep(5)
763
764     print '*********************** shutting down the downloaders *******************'
765     for k, v in running_downloaders.items():
766         try:
767             print 'downloader', k, stop(v)
768         except:
769             print '************************** Exception occurred **************************'
770             print_exc()
771
772     sleep(5)
773
774     print '************************** shutting down the bootstraps *******************'
775     for k, v in running_bootstraps.items():
776         try:
777             print 'bootstrap', k, stop(v)
778         except:
779             print '************************** Exception occurred **************************'
780             print_exc()
781
782     print '************************** Test Results *******************'
783     i = -1
784     for (num_down, cmd) in apt_get_queue:
785         i += 1
786         s = str(num_down) + ': "apt-get ' + ' '.join(cmd) + '" '
787         if len(apt_get_results) > i:
788             (elapsed, r_value) = apt_get_results[i]
789             s += 'took ' + str(elapsed) + ' secs (' + str(r_value) + ')'
790         else:
791             s += 'did not complete'
792         print s
793     
794     return apt_get_results
795
796 def get_usage():
797     """Get the usage information to display to the user.
798     
799     @rtype: C{string}
800     @return: the usage information to display
801     
802     """
803     
804     s = 'Usage: ' + sys.argv[0] + ' (all|<test>|help)\n\n'
805     s += '  all    - run all the tests\n'
806     s += '  help   - display this usage information\n'
807     s += '  <test> - run the <test> test (see list below for valid tests)\n\n'
808     
809     t = tests.items()
810     t.sort()
811     for k, v in t:
812         s += 'test "' + str(k) + '" - ' + v[0] + '\n'
813     
814     return s
815
816 if __name__ == '__main__':
817     if len(sys.argv) != 2:
818         print get_usage()
819     elif sys.argv[1] == 'all':
820         for k, v in tests.items():
821             run_test(v[1], v[2], v[3])
822     elif sys.argv[1] in tests:
823         v = tests[sys.argv[1]]
824         run_test(v[1], v[2], v[3])
825     elif sys.argv[1] == 'help':
826         print get_usage()
827     else:
828         print 'Unknown test to run:', sys.argv[1], '\n'
829         print get_usage()
830