3 """Automated tests of the apt-p2p functionality.
5 This script runs several automatic tests of some of the functionality in
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}).
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).
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).
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.
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
36 from time import sleep, time
37 import sys, os, signal
38 from traceback import print_exc
39 from os.path import exists
41 tests = {'1': ('Start a single bootstrap and downloader, test updating and downloading ' +
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']),
55 '2': ('Start a single bootstrap and 2 downloaders to test downloading from a peer.',
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']),
75 '3': ('Start a single bootstrap and 6 downloaders, to test downloading' +
76 ' speeds from each other.',
85 (1, ['install', 'aboot-base']),
86 (1, ['install', 'ada-reference-manual']),
87 (1, ['install', 'fop-doc']),
88 (1, ['install', 'crash-whitepaper']),
90 (2, ['install', 'aboot-base']),
91 (2, ['install', 'ada-reference-manual']),
92 (2, ['install', 'fop-doc']),
93 (2, ['install', 'crash-whitepaper']),
95 (3, ['install', 'aboot-base']),
96 (3, ['install', 'ada-reference-manual']),
97 (3, ['install', 'fop-doc']),
98 (3, ['install', 'crash-whitepaper']),
100 (4, ['install', 'aboot-base']),
101 (4, ['install', 'ada-reference-manual']),
102 (4, ['install', 'fop-doc']),
103 (4, ['install', 'crash-whitepaper']),
105 (5, ['install', 'aboot-base']),
106 (5, ['install', 'ada-reference-manual']),
107 (5, ['install', 'fop-doc']),
108 (5, ['install', 'crash-whitepaper']),
110 (6, ['install', 'aboot-base']),
111 (6, ['install', 'ada-reference-manual']),
112 (6, ['install', 'fop-doc']),
113 (6, ['install', 'crash-whitepaper']),
116 '4': ('Start a single bootstrap and 1 downloader, requesting the same' +
117 ' packages multiple times to test caching.',
121 (1, ['install', 'aboot-base']),
122 (1, ['install', 'ada-reference-manual']),
123 (1, ['install', 'fop-doc']),
124 (1, ['install', 'crash-whitepaper']),
126 (1, ['install', 'aboot-base']),
127 (1, ['install', 'ada-reference-manual']),
128 (1, ['install', 'fop-doc']),
129 (1, ['install', 'crash-whitepaper']),
131 (1, ['install', 'aboot-base']),
132 (1, ['install', 'ada-reference-manual']),
133 (1, ['install', 'fop-doc']),
134 (1, ['install', 'crash-whitepaper']),
137 '5': ('Start a single bootstrap and 6 downloaders, update all to test' +
138 ' that they can all see each other.',
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'})},
154 '6': ('Test caching with multiple apt-get updates.',
163 '7': ('Test pipelining of multiple simultaneous downloads.',
167 (1, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
168 'aspectj-doc', 'fop-doc',
169 'bison-doc', 'crash-whitepaper',
171 'aptitude-doc-en', 'asr-manpages',
172 'atomix-data', 'alcovebook-sgml-doc',
173 'afbackup-common', 'airstrike-common',
177 '8': ('Test pipelining of multiple simultaneous downloads with many peers.',
186 (1, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
187 'aspectj-doc', 'fop-doc',
188 'bison-doc', 'crash-whitepaper',
190 'aptitude-doc-en', 'asr-manpages',
191 'atomix-data', 'alcovebook-sgml-doc',
192 'afbackup-common', 'airstrike-common',
195 (2, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
196 'aspectj-doc', 'fop-doc',
197 'bison-doc', 'crash-whitepaper',
199 'aptitude-doc-en', 'asr-manpages',
200 'atomix-data', 'alcovebook-sgml-doc',
201 'afbackup-common', 'airstrike-common',
204 (3, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
205 'aspectj-doc', 'fop-doc',
206 'bison-doc', 'crash-whitepaper',
208 'aptitude-doc-en', 'asr-manpages',
209 'atomix-data', 'alcovebook-sgml-doc',
210 'afbackup-common', 'airstrike-common',
213 (4, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
214 'aspectj-doc', 'fop-doc',
215 'bison-doc', 'crash-whitepaper',
217 'aptitude-doc-en', 'asr-manpages',
218 'atomix-data', 'alcovebook-sgml-doc',
219 'afbackup-common', 'airstrike-common',
222 (5, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
223 'aspectj-doc', 'fop-doc',
224 'bison-doc', 'crash-whitepaper',
226 'aptitude-doc-en', 'asr-manpages',
227 'atomix-data', 'alcovebook-sgml-doc',
228 'afbackup-common', 'airstrike-common',
231 (6, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
232 'aspectj-doc', 'fop-doc',
233 'bison-doc', 'crash-whitepaper',
235 'aptitude-doc-en', 'asr-manpages',
236 'atomix-data', 'alcovebook-sgml-doc',
237 'afbackup-common', 'airstrike-common',
241 '9': ('Start a single bootstrap and 6 downloaders and test downloading' +
242 ' a very large file.',
251 (1, ['install', 'kde-icons-oxygen']),
253 (2, ['install', 'kde-icons-oxygen']),
255 (3, ['install', 'kde-icons-oxygen']),
257 (4, ['install', 'kde-icons-oxygen']),
259 (5, ['install', 'kde-icons-oxygen']),
261 (6, ['install', 'kde-icons-oxygen']),
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',
272 'aptitude-doc-en', 'asr-manpages',
273 'atomix-data', 'alcovebook-sgml-doc',
274 'afbackup-common', 'airstrike-common',
278 (1, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
279 'aspectj-doc', 'fop-doc',
280 'bison-doc', 'crash-whitepaper',
282 'aptitude-doc-en', 'asr-manpages',
283 'atomix-data', 'alcovebook-sgml-doc',
284 'afbackup-common', 'airstrike-common',
286 (1, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
287 'aspectj-doc', 'fop-doc',
288 'bison-doc', 'crash-whitepaper',
290 'aptitude-doc-en', 'asr-manpages',
291 'atomix-data', 'alcovebook-sgml-doc',
292 'afbackup-common', 'airstrike-common',
294 (2, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
295 'aspectj-doc', 'fop-doc',
296 'bison-doc', 'crash-whitepaper',
298 'aptitude-doc-en', 'asr-manpages',
299 'atomix-data', 'alcovebook-sgml-doc',
300 'afbackup-common', 'airstrike-common',
304 (2, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
305 'aspectj-doc', 'fop-doc',
306 'bison-doc', 'crash-whitepaper',
308 'aptitude-doc-en', 'asr-manpages',
309 'atomix-data', 'alcovebook-sgml-doc',
310 'afbackup-common', 'airstrike-common',
312 (2, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
313 'aspectj-doc', 'fop-doc',
314 'bison-doc', 'crash-whitepaper',
316 'aptitude-doc-en', 'asr-manpages',
317 'atomix-data', 'alcovebook-sgml-doc',
318 'afbackup-common', 'airstrike-common',
322 'b': ('Start 2 downloaders and test source downloads.',
324 {1: {'types': ['deb-src']},
325 2: {'types': ['deb-src']}},
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']),
342 'c': ('Test downloading from peers and just a mirror.',
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',
353 (2, ['install', 'aboot-base', 'aap-doc', 'ada-reference-manual',
354 'aspectj-doc', 'fop-doc',
355 'bison-doc', 'crash-whitepaper',
357 'aptitude-doc-en', 'asr-manpages',
358 'atomix-data', 'alcovebook-sgml-doc',
359 'afbackup-common', 'airstrike-common',
365 assert 'all' not in tests
366 assert 'help' not in tests
369 apt_conf_template = """
371 // Location of the state dir
376 userstatus "status.user";
377 cdroms "cdroms.list";
380 // Location of the cache dir
381 Cache "var/cache/apt/" {
382 Archives "archives/";
383 srcpkgcache "srcpkgcache.bin";
384 pkgcache "pkgcache.bin";
389 SourceList "sources.list";
391 Preferences "preferences";
395 // Locations of binaries
397 methods "/usr/lib/apt/methods/";
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";
408 /* Options you can set to see some debugging text They correspond to names
409 of classes in the source code */
412 pkgProblemResolver "false";
413 pkgDepCache::AutoInstall "false"; // what packages apt install to satify dependencies
415 pkgAcquire::Worker "false";
416 pkgAcquire::Auth "false";
418 pkgDPkgProgressReporting "false";
419 pkgOrderList "false";
422 pkgInitialize "false"; // This one will dump the configuration space
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
432 apt_p2p_conf_template = """
435 # Port to listen on for all requests (TCP and UDP)
438 # The rate to limit sending data to peers to, in KBytes/sec.
439 # Set this to 0 to not limit the upload bandwidth.
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
446 MIN_DOWNLOAD_PEERS = 3
448 # Directory to store the downloaded files in
449 CACHE_DIR = %(CACHE_DIR)s
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
456 # Whether it's OK to use an IP addres from a known local/private range
459 # Whether a remote peer can access the statistics page
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
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.
471 # The user name to try and run as (leave blank to run as current user)
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
479 # Whether to only run the DHT (for providing only a bootstrap node)
480 DHT-ONLY = %(DHT-ONLY)s
483 # bootstrap nodes to contact to join the DHT
484 BOOTSTRAP = %(BOOTSTRAP)s
486 # whether this node is a bootstrap node
487 BOOTSTRAP_NODE = %(BOOTSTRAP_NODE)s
489 # checkpoint every this many seconds
490 CHECKPOINT_INTERVAL = 5m
492 # concurrent xmlrpc calls per find node/value request!
495 # how many hosts to post to
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
506 # how many times in a row a node can fail to respond before it's booted from the routing table
509 # never ping a node more often than this
510 MIN_PING_INTERVAL = 15m
512 # refresh buckets that haven't been touched in this long
513 BUCKET_STALENESS = 1h
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.
520 # expire entries older than this
523 # Timeout KRPC requests to nodes after this time.
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
532 KRPC_INITIAL_DELAY = 2s
534 # whether to spew info about the requests/responses in the protocol
539 """Remove all the files and directories below a top-level one.
542 @param top: the top-level directory to start at
546 for root, dirs, files in os.walk(top, topdown=False):
548 os.remove(os.path.join(root, name))
550 os.rmdir(os.path.join(root, name))
553 """Join together a list of directories into a path string.
555 @type dir: C{list} of C{string}
556 @param dir: the path to join together
558 @return: the joined together path
564 joined = os.path.join(joined, i)
568 """Create all the directories to make a path.
570 @type dir: C{list} of C{string}
571 @param dir: the path to create
574 if not os.path.exists(join(dir)):
575 os.makedirs(join(dir))
578 """Create an empty file.
580 @type path: C{list} of C{string}
581 @param path: the path to create
585 f = open(join(path), 'w')
588 def start(cmd, args, work_dir = None):
589 """Fork and start a background process running.
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)
599 @return: the PID of the forked process
603 new_cmd = [cmd] + args
606 pid = os.spawnvp(os.P_NOWAIT, new_cmd[0], new_cmd)
610 """Stop a forked background process that is running.
613 @param pid: the PID of the process to stop
615 @return: the return status code from the child
619 # First try a keyboard interrupt
620 os.kill(pid, signal.SIGINT)
623 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
627 # Try a keyboard interrupt again, just in case
628 os.kill(pid, signal.SIGINT)
631 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
636 os.kill(pid, signal.SIGTERM)
639 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
643 # Finally a kill, don't return until killed
644 os.kill(pid, signal.SIGKILL)
647 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
651 def apt_get(num_down, cmd):
652 """Start an apt-get process in the background.
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.
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
663 @return: the PID of the background process
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'])
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)
678 def bootstrap_address(num_boot):
679 """Determine the bootstrap address to use for a node.
681 @type num_boot: C{int}
682 @param num_boot: the number of the bootstrap node
684 @return: the bootstrap address to use
688 return 'localhost:1' + str(num_boot) + '969'
690 def down_dir(num_down):
691 """Determine the working directory to use for a downloader.
693 @type num_down: C{int}
694 @param num_down: the number of the downloader
696 @return: the downloader's directory
700 return os.path.join(CWD,'downloader' + str(num_down))
702 def boot_dir(num_boot):
703 """Determine the working directory to use for a bootstrap node.
705 @type num_boot: C{int}
706 @param num_boot: the number of the bootstrap node
708 @return: the bootstrap node's directory
712 return os.path.join(CWD,'bootstrap' + str(num_boot))
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.
719 The default arguments specified to the downloader invocation are
720 the configuration directory, apt port, minport, maxport and the
722 Any additional arguments needed should be specified by L{options}.
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)
743 @return: the PID of the downloader process
747 assert num_down < 100
749 print '************************** Starting Downloader ' + str(num_down) + ' **************************'
751 downloader_dir = down_dir(num_down)
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'])
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')
774 f.write('%s http://localhost:1%02d77/%s/ unstable %s\n' % (type, num_down, mirror, suites))
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)
783 defaults = {'PORT': '1%02d77' % num_down,
784 'CACHE_DIR': downloader_dir,
786 'BOOTSTRAP': bootstrap_addresses,
787 'BOOTSTRAP_NODE': 'no'}
790 defaults[k] = options[k]
791 f = open(join([downloader_dir, 'apt-p2p.conf']), 'w')
792 f.write(apt_p2p_conf_template % defaults)
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']),],
801 def start_bootstrap(bootstrap_addresses, num_boot, options = [], clean = True):
802 """Initialize a new bootstrap node process.
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}.
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)
817 @return: the PID of the downloader process
823 print '************************** Starting Bootstrap ' + str(num_boot) + ' **************************'
825 bootstrap_dir = boot_dir(num_boot)
833 makedirs([bootstrap_dir])
835 defaults = {'PORT': '1%d969' % num_boot,
836 'CACHE_DIR': bootstrap_dir,
838 'BOOTSTRAP': bootstrap_addresses,
839 'BOOTSTRAP_NODE': 'yes'}
842 defaults[k] = options[k]
843 f = open(join([bootstrap_dir, 'apt-p2p.conf']), 'w')
844 f.write(apt_p2p_conf_template % defaults)
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']),],
854 def run_test(bootstraps, downloaders, apt_get_queue):
855 """Run a single test.
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
870 running_bootstraps = {}
871 running_downloaders = {}
876 boot_keys = bootstraps.keys()
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])
882 for k, v in bootstraps.items():
883 running_bootstraps[k] = start_bootstrap(bootstrap_addresses, k, **v)
887 for k, v in downloaders.items():
888 running_downloaders[k] = start_downloader(bootstrap_addresses, k, **v)
892 for (num_down, cmd) in apt_get_queue:
893 running_apt_get[num_down] = apt_get(num_down, cmd)
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))
902 print '********** apt-get completed successfully in ' + str(elapsed) + ' sec. *****************'
904 print '********** apt-get finished with status ' + str(r_value) + ' in ' + str(elapsed) + ' sec. ************'
909 print '************************** Exception occurred **************************'
911 print '************************** will attempt to shut down *******************'
913 print '*********************** shutting down the apt-gets *******************'
914 for k, v in running_apt_get.items():
916 print 'apt-get', k, stop(v)
918 print '************************** Exception occurred **************************'
923 print '*********************** shutting down the downloaders *******************'
924 for k, v in running_downloaders.items():
926 print 'downloader', k, stop(v)
928 print '************************** Exception occurred **************************'
933 print '************************** shutting down the bootstraps *******************'
934 for k, v in running_bootstraps.items():
936 print 'bootstrap', k, stop(v)
938 print '************************** Exception occurred **************************'
941 print '************************** Test Results *******************'
943 for (num_down, cmd) in apt_get_queue:
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) + ')'
950 s += 'did not complete'
953 return apt_get_results
956 """Get the usage information to display to the user.
959 @return: the usage information to display
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'
971 s += 'test "' + str(k) + '" - ' + v[0] + '\n'
975 if __name__ == '__main__':
976 if len(sys.argv) != 2:
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':
987 print 'Unknown test to run:', sys.argv[1], '\n'