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