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', 'asis-doc']),
52 (1, ['install', 'bison-doc']),
53 (1, ['install', 'crash-whitepaper']),
56 '2': ('Start a single bootstrap and 2 downloaders to test downloading from a peer.',
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']),
76 '3': ('Start a single bootstrap and 6 downloaders, to test downloading' +
77 ' speeds from each other.',
86 (1, ['install', 'aboot-base']),
87 (1, ['install', 'ada-reference-manual']),
88 (1, ['install', 'fop-doc']),
89 (1, ['install', 'crash-whitepaper']),
91 (2, ['install', 'aboot-base']),
92 (2, ['install', 'ada-reference-manual']),
93 (2, ['install', 'fop-doc']),
94 (2, ['install', 'crash-whitepaper']),
96 (3, ['install', 'aboot-base']),
97 (3, ['install', 'ada-reference-manual']),
98 (3, ['install', 'fop-doc']),
99 (3, ['install', 'crash-whitepaper']),
101 (4, ['install', 'aboot-base']),
102 (4, ['install', 'ada-reference-manual']),
103 (4, ['install', 'fop-doc']),
104 (4, ['install', 'crash-whitepaper']),
106 (5, ['install', 'aboot-base']),
107 (5, ['install', 'ada-reference-manual']),
108 (5, ['install', 'fop-doc']),
109 (5, ['install', 'crash-whitepaper']),
111 (6, ['install', 'aboot-base']),
112 (6, ['install', 'ada-reference-manual']),
113 (6, ['install', 'fop-doc']),
114 (6, ['install', 'crash-whitepaper']),
117 '4': ('Start a single bootstrap and 1 downloader, requesting the same' +
118 ' packages multiple times to test caching.',
122 (1, ['install', 'aboot-base']),
123 (1, ['install', 'ada-reference-manual']),
124 (1, ['install', 'fop-doc']),
125 (1, ['install', 'crash-whitepaper']),
127 (1, ['install', 'aboot-base']),
128 (1, ['install', 'ada-reference-manual']),
129 (1, ['install', 'fop-doc']),
130 (1, ['install', 'crash-whitepaper']),
132 (1, ['install', 'aboot-base']),
133 (1, ['install', 'ada-reference-manual']),
134 (1, ['install', 'fop-doc']),
135 (1, ['install', 'crash-whitepaper']),
138 '5': ('Start a single bootstrap and 6 downloaders, update all to test' +
139 ' that they can all see each other.',
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'})},
155 '6': ('Test caching with multiple apt-get updates.',
164 '7': ('Test pipelining of multiple simultaneous downloads.',
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',
178 '8': ('Test pipelining of multiple simultaneous downloads with many peers.',
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',
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',
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',
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',
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',
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',
242 '9': ('Start a single bootstrap and 6 downloaders and test downloading' +
243 ' a very large file.',
252 (1, ['install', 'kde-icons-oxygen']),
254 (2, ['install', 'kde-icons-oxygen']),
256 (3, ['install', 'kde-icons-oxygen']),
258 (4, ['install', 'kde-icons-oxygen']),
260 (5, ['install', 'kde-icons-oxygen']),
262 (6, ['install', 'kde-icons-oxygen']),
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',
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',
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',
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',
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',
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',
323 'b': ('Start 2 downloaders and test source downloads.',
325 {1: {'types': ['deb-src']},
326 2: {'types': ['deb-src']}},
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']),
343 'c': ('Test downloading from peers and just a mirror.',
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',
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',
366 assert 'all' not in tests
367 assert 'help' not in tests
370 apt_conf_template = """
372 // Location of the state dir
377 userstatus "status.user";
378 cdroms "cdroms.list";
381 // Location of the cache dir
382 Cache "var/cache/apt/" {
383 Archives "archives/";
384 srcpkgcache "srcpkgcache.bin";
385 pkgcache "pkgcache.bin";
390 SourceList "sources.list";
392 Preferences "preferences";
396 // Locations of binaries
398 methods "/usr/lib/apt/methods/";
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";
409 /* Options you can set to see some debugging text They correspond to names
410 of classes in the source code */
413 pkgProblemResolver "false";
414 pkgDepCache::AutoInstall "false"; // what packages apt install to satify dependencies
416 pkgAcquire::Worker "false";
417 pkgAcquire::Auth "false";
419 pkgDPkgProgressReporting "false";
420 pkgOrderList "false";
423 pkgInitialize "false"; // This one will dump the configuration space
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
433 apt_p2p_conf_template = """
436 # Port to listen on for all requests (TCP and UDP)
439 # The rate to limit sending data to peers to, in KBytes/sec.
440 # Set this to 0 to not limit the upload bandwidth.
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
447 MIN_DOWNLOAD_PEERS = 3
449 # Directory to store the downloaded files in
450 CACHE_DIR = %(CACHE_DIR)s
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
457 # Whether it's OK to use an IP addres from a known local/private range
460 # Whether a remote peer can access the statistics page
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
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.
472 # The user name to try and run as (leave blank to run as current user)
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
480 # Whether to only run the DHT (for providing only a bootstrap node)
481 DHT-ONLY = %(DHT-ONLY)s
484 # bootstrap nodes to contact to join the DHT
485 BOOTSTRAP = %(BOOTSTRAP)s
487 # whether this node is a bootstrap node
488 BOOTSTRAP_NODE = %(BOOTSTRAP_NODE)s
490 # checkpoint every this many seconds
491 CHECKPOINT_INTERVAL = 5m
493 # concurrent xmlrpc calls per find node/value request!
496 # how many hosts to post to
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
507 # how many times in a row a node can fail to respond before it's booted from the routing table
510 # never ping a node more often than this
511 MIN_PING_INTERVAL = 15m
513 # refresh buckets that haven't been touched in this long
514 BUCKET_STALENESS = 1h
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.
521 # expire entries older than this
524 # Timeout KRPC requests to nodes after this time.
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
533 KRPC_INITIAL_DELAY = 2s
535 # whether to spew info about the requests/responses in the protocol
540 """Remove all the files and directories below a top-level one.
543 @param top: the top-level directory to start at
547 for root, dirs, files in os.walk(top, topdown=False):
549 os.remove(os.path.join(root, name))
551 os.rmdir(os.path.join(root, name))
554 """Join together a list of directories into a path string.
556 @type dir: C{list} of C{string}
557 @param dir: the path to join together
559 @return: the joined together path
565 joined = os.path.join(joined, i)
569 """Create all the directories to make a path.
571 @type dir: C{list} of C{string}
572 @param dir: the path to create
575 if not os.path.exists(join(dir)):
576 os.makedirs(join(dir))
579 """Create an empty file.
581 @type path: C{list} of C{string}
582 @param path: the path to create
586 f = open(join(path), 'w')
589 def start(cmd, args, work_dir = None):
590 """Fork and start a background process running.
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)
600 @return: the PID of the forked process
604 new_cmd = [cmd] + args
607 pid = os.spawnvp(os.P_NOWAIT, new_cmd[0], new_cmd)
611 """Stop a forked background process that is running.
614 @param pid: the PID of the process to stop
616 @return: the return status code from the child
620 # First try a keyboard interrupt
621 os.kill(pid, signal.SIGINT)
624 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
628 # Try a keyboard interrupt again, just in case
629 os.kill(pid, signal.SIGINT)
632 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
637 os.kill(pid, signal.SIGTERM)
640 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
644 # Finally a kill, don't return until killed
645 os.kill(pid, signal.SIGKILL)
648 (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
652 def apt_get(num_down, cmd):
653 """Start an apt-get process in the background.
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.
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
664 @return: the PID of the background process
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'])
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)
679 def bootstrap_address(num_boot):
680 """Determine the bootstrap address to use for a node.
682 @type num_boot: C{int}
683 @param num_boot: the number of the bootstrap node
685 @return: the bootstrap address to use
689 return 'localhost:1' + str(num_boot) + '969'
691 def down_dir(num_down):
692 """Determine the working directory to use for a downloader.
694 @type num_down: C{int}
695 @param num_down: the number of the downloader
697 @return: the downloader's directory
701 return os.path.join(CWD,'downloader' + str(num_down))
703 def boot_dir(num_boot):
704 """Determine the working directory to use for a bootstrap node.
706 @type num_boot: C{int}
707 @param num_boot: the number of the bootstrap node
709 @return: the bootstrap node's directory
713 return os.path.join(CWD,'bootstrap' + str(num_boot))
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.
720 The default arguments specified to the downloader invocation are
721 the configuration directory, apt port, minport, maxport and the
723 Any additional arguments needed should be specified by L{options}.
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)
744 @return: the PID of the downloader process
748 assert num_down < 100
750 print '************************** Starting Downloader ' + str(num_down) + ' **************************'
752 downloader_dir = down_dir(num_down)
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'])
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')
775 f.write('%s http://localhost:1%02d77/%s/ unstable %s\n' % (type, num_down, mirror, suites))
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)
784 defaults = {'PORT': '1%02d77' % num_down,
785 'CACHE_DIR': downloader_dir,
787 'BOOTSTRAP': bootstrap_addresses,
788 'BOOTSTRAP_NODE': 'no'}
791 defaults[k] = options[k]
792 f = open(join([downloader_dir, 'apt-p2p.conf']), 'w')
793 f.write(apt_p2p_conf_template % defaults)
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']),],
802 def start_bootstrap(bootstrap_addresses, num_boot, options = [], clean = True):
803 """Initialize a new bootstrap node process.
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}.
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)
818 @return: the PID of the downloader process
824 print '************************** Starting Bootstrap ' + str(num_boot) + ' **************************'
826 bootstrap_dir = boot_dir(num_boot)
834 makedirs([bootstrap_dir])
836 defaults = {'PORT': '1%d969' % num_boot,
837 'CACHE_DIR': bootstrap_dir,
839 'BOOTSTRAP': bootstrap_addresses,
840 'BOOTSTRAP_NODE': 'yes'}
843 defaults[k] = options[k]
844 f = open(join([bootstrap_dir, 'apt-p2p.conf']), 'w')
845 f.write(apt_p2p_conf_template % defaults)
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']),],
855 def run_test(bootstraps, downloaders, apt_get_queue):
856 """Run a single test.
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
871 running_bootstraps = {}
872 running_downloaders = {}
877 boot_keys = bootstraps.keys()
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])
883 for k, v in bootstraps.items():
884 running_bootstraps[k] = start_bootstrap(bootstrap_addresses, k, **v)
888 for k, v in downloaders.items():
889 running_downloaders[k] = start_downloader(bootstrap_addresses, k, **v)
893 for (num_down, cmd) in apt_get_queue:
894 running_apt_get[num_down] = apt_get(num_down, cmd)
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))
903 print '********** apt-get completed successfully in ' + str(elapsed) + ' sec. *****************'
905 print '********** apt-get finished with status ' + str(r_value) + ' in ' + str(elapsed) + ' sec. ************'
910 print '************************** Exception occurred **************************'
912 print '************************** will attempt to shut down *******************'
914 print '*********************** shutting down the apt-gets *******************'
915 for k, v in running_apt_get.items():
917 print 'apt-get', k, stop(v)
919 print '************************** Exception occurred **************************'
924 print '*********************** shutting down the downloaders *******************'
925 for k, v in running_downloaders.items():
927 print 'downloader', k, stop(v)
929 print '************************** Exception occurred **************************'
934 print '************************** shutting down the bootstraps *******************'
935 for k, v in running_bootstraps.items():
937 print 'bootstrap', k, stop(v)
939 print '************************** Exception occurred **************************'
942 print '************************** Test Results *******************'
944 for (num_down, cmd) in apt_get_queue:
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) + ')'
951 s += 'did not complete'
954 return apt_get_results
957 """Get the usage information to display to the user.
960 @return: the usage information to display
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'
972 s += 'test "' + str(k) + '" - ' + v[0] + '\n'
976 if __name__ == '__main__':
977 if len(sys.argv) != 2:
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':
988 print 'Unknown test to run:', sys.argv[1], '\n'