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