r128 - /debtorrent/trunk/test.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Tue Jun 26 03:10:55 UTC 2007


Author: camrdale-guest
Date: Tue Jun 26 03:10:55 2007
New Revision: 128

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=128
Log:
Added automated testing suite.

Added:
    debtorrent/trunk/test.py   (with props)

Added: debtorrent/trunk/test.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/test.py?rev=128&op=file
==============================================================================
--- debtorrent/trunk/test.py (added)
+++ debtorrent/trunk/test.py Tue Jun 26 03:10:55 2007
@@ -1,0 +1,500 @@
+#!/usr/bin/env python
+#
+# see LICENSE.txt for license information
+#
+# $Id$
+
+"""Automated tests of the DebTorrent functionality.
+
+This script runs several automatic tests of some of the functionality in
+the DebTorrent programs.
+
+ at type CWD: C{string}
+ at var CWD: the working directory the script was run from
+ at type apt_conf_template: C{string}
+ at var apt_conf_template: the template to use for the apt.conf file
+
+"""
+
+from time import sleep, time
+import btlaunchmany
+from DebTorrent.BT1.track import track
+from DebTorrent.__init__ import resetPeerIDs
+import sys, os, signal
+from traceback import print_exc
+
+CWD = os.getcwd()
+apt_conf_template = """
+{
+  // Location of the state dir
+  State "var/lib/apt/"
+  {
+     Lists "lists/";
+     xstatus "xstatus";
+     userstatus "status.user";
+     status "/var/lib/dpkg/status";
+     cdroms "cdroms.list";
+  };
+
+  // Location of the cache dir
+  Cache "var/cache/apt/" {
+     Archives "archives/";
+     srcpkgcache "srcpkgcache.bin";
+     pkgcache "pkgcache.bin";
+  };
+
+  // Config files
+  Etc "etc/apt/" {
+     SourceList "sources.list";
+     Main "apt.conf";
+     Preferences "preferences";
+     Parts "apt.conf.d/";
+  };
+
+  // Locations of binaries
+  Bin {
+     methods "/usr/lib/apt/methods/";
+     gzip "/bin/gzip";
+     gpg  "/usr/bin/gpgv";
+     dpkg "/usr/bin/dpkg --simulate";
+     dpkg-source "/usr/bin/dpkg-source";
+     dpkg-buildpackage "/usr/bin/dpkg-buildpackage";
+     apt-get "/usr/bin/apt-get";
+     apt-cache "/usr/bin/apt-cache";
+  };
+};
+
+/* Options you can set to see some debugging text They correspond to names
+   of classes in the source code */
+Debug
+{
+  pkgProblemResolver "false";
+  pkgDepCache::AutoInstall "false"; // what packages apt install to satify dependencies
+  pkgAcquire "false";
+  pkgAcquire::Worker "false";
+  pkgAcquire::Auth "false";
+  pkgDPkgPM "false";
+  pkgDPkgProgressReporting "false";
+  pkgOrderList "false";
+  BuildDeps "false";
+
+  pkgInitialize "false";   // This one will dump the configuration space
+  NoLocking "false";
+  Acquire::Ftp "false";    // Show ftp command traffic
+  Acquire::Http "false";   // Show http command traffic
+  Acquire::gpgv "false";   // Show the gpgv traffic
+  aptcdrom "false";        // Show found package files
+  IdentCdrom "false";
+
+}
+"""
+
+def rmrf(top):
+    """Remove all the files and directories below a top-level one.
+    
+    @type top: C{string}
+    @param top: the top-level directory to start at
+    
+    """
+    
+    for root, dirs, files in os.walk(top, topdown=False):
+        for name in files:
+            os.remove(os.path.join(root, name))
+        for name in dirs:
+            os.rmdir(os.path.join(root, name))
+
+def join(dir):
+    """Join together a list of directories into a path string.
+    
+    @type dir: C{list} of C{string}
+    @param dir: the path to join together
+    @rtype: C{string}
+    @return: the joined together path
+    
+    """
+    
+    joined = ''
+    for i in dir:
+        joined = os.path.join(joined, i)
+    return joined
+
+def makedirs(dir):
+    """Create all the directories to make a path.
+    
+    @type dir: C{list} of C{string}
+    @param dir: the path to create
+    
+    """
+    
+    os.makedirs(join(dir))
+
+def touch(path):
+    """Create an empty file.
+    
+    @type dir: C{list} of C{string}
+    @param dir: the path to create
+    
+    """
+    
+    f = open(join(path), 'w')
+    f.close()
+
+def start(func, args, work_dir = None):
+    """Fork and start a background process running.
+    
+    @type func: C{method}
+    @param func: the function to call in the child
+    @type args: unknown
+    @param args: the argument to pass to the function
+    @type work_dir: C{string}
+    @param work_dir: the directory to change to to execute the child process in
+        (optional, defaults to the current directory)
+    @rtype: C{int}
+    @return: the PID of the forked child (only returned to the parent)
+    
+    """
+    
+    pid = os.fork()
+    if pid != 0:
+        return pid
+    if work_dir:
+        os.chdir(work_dir)
+    func(args)
+    os._exit(0)
+
+def stop(pid):
+    """Stop a forked background process that is running.
+    
+    @type pid: C{int}
+    @param pid: the PID of the process to stop
+    @rtype: C{int}
+    @return: the return status code from the child
+    
+    """
+
+    # First try a keyboard interrupt
+    os.kill(pid, signal.SIGINT)
+    for i in xrange(5):
+        sleep(1)
+        (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
+        if r_pid:
+            return r_value
+    
+    # Try a keyboard interrupt again, just in case
+    os.kill(pid, signal.SIGINT)
+    for i in xrange(5):
+        sleep(1)
+        (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
+        if r_pid:
+            return r_value
+
+    # Try a terminate
+    os.kill(pid, signal.SIGTERM)
+    for i in xrange(5):
+        sleep(1)
+        (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
+        if r_pid:
+            return r_value
+
+    # Finally a kill, don't return until killed
+    os.kill(pid, signal.SIGKILL)
+    while not r_pid:
+        sleep(1)
+        (r_pid, r_value) = os.waitpid(pid, os.WNOHANG)
+
+    return r_value
+
+def apt_get(num_down, cmd):
+    """Start an apt-get process in the background.
+
+    The default argument specified to the apt-get invocation are
+    'apt-get -d -q -c <conf_file>'. Any additional arguments (including
+    the apt-get action to use) should be specified.
+    
+    @type num_down: C{int}
+    @param num_down: the number of the downloader to use
+    @type func: C{list} of C{string}
+    @param func: the arguments to pass to the apt-get process
+    @rtype: C{int}
+    @return: the PID of the background process
+    
+    """
+    
+    print '************************** apt-get (' + str(num_down) + ') ' + ' '.join(cmd) + ' **************************'
+    apt_conf = join([down_dir(num_down), 'etc', 'apt', 'apt.conf'])
+    new_cmd = ['apt-get', '-d', '-q', '-c', apt_conf] + cmd
+    pid = os.spawnvp(os.P_NOWAIT, new_cmd[0], new_cmd)
+    return pid
+
+def tracker_address(num_track):
+    """Determine the announce address to use for a tracker.
+    
+    @type num_track: C{int}
+    @param num_track: the number of the tracker
+    @rtype: C{string}
+    @return: the tracker address to use
+    
+    """
+    
+    return 'http://localhost:' + str(num_track) + '969/announce'
+
+def down_dir(num_down):
+    """Determine the working directory to use for a downloader.
+    
+    @type num_down: C{int}
+    @param num_down: the number of the downloader
+    @rtype: C{string}
+    @return: the downloader's directory
+    
+    """
+    
+    return os.path.join(CWD,'downloader' + str(num_down))
+
+def start_downloader(num_down, options = [], clean = True):
+    """Initialize a new downloader process.
+
+    The default arguments specified to the downloader invocation are
+    the configuration directory, apt port, minport and maxport. 
+    Any additional arguments needed should be specified by L{options}.
+    
+    @type num_down: C{int}
+    @param num_down: the number of the downloader to use
+    @type options: C{list} of C{string}
+    @param options: the arguments to pass to the downloader
+        (optional, defaults to only using the default arguments)
+    @type clean: C{boolean}
+    @param clean: whether to remove any previous downloader files
+        (optional, defaults to removing them)
+    @rtype: C{int}
+    @return: the PID of the downloader process
+    
+    """
+    
+    assert num_down < 10
+    
+    print '************************** Starting Downloader ' + str(num_down) + ' **************************'
+
+    downloader_dir = down_dir(num_down)
+    
+    if clean:
+        try:
+            rmrf(downloader_dir)
+        except:
+            pass
+    
+        # Create the directory structure needed by apt
+        makedirs([downloader_dir, 'etc', 'apt', 'apt.conf.d'])
+        makedirs([downloader_dir, 'var', 'lib', 'apt', 'lists', 'partial'])
+        makedirs([downloader_dir, 'var', 'cache', 'apt', 'archives', 'partial'])
+        touch([downloader_dir, 'var', 'lib', 'apt', 'lists', 'lock'])
+        touch([downloader_dir, 'var', 'cache', 'apt', 'archives', 'lock'])
+
+        # Create apt's config files
+        f = open(join([downloader_dir, 'etc', 'apt', 'sources.list']), 'w')
+        f.write('deb http://localhost:' + str(num_down) + '988/ftp.us.debian.org/debian/ stable main contrib non-free\n')
+        f.close()
+
+        f = open(join([downloader_dir, 'etc', 'apt', 'apt.conf']), 'w')
+        f.write('Dir "' + downloader_dir + '"')
+        f.write(apt_conf_template)
+        f.close()
+
+    # Reset the peer ID so it will be different
+    resetPeerIDs()
+
+    pid = start(btlaunchmany.run, ['--config_dir', downloader_dir,
+                                   '--port', str(num_down) + '988', 
+                                   '--minport', '1' + str(num_down) + '000', 
+                                   '--maxport', '1' + str(num_down) + '999'] + options,
+                downloader_dir)
+    return pid
+
+def start_tracker(num_track, options = [], clean = True):
+    """Initialize a new tracker process.
+
+    The default arguments specified to the tracker invocation are
+    the state file and port to use. Any additional arguments needed 
+    should be specified by L{options}.
+    
+    @type num_track: C{int}
+    @param num_track: the number of the tracker to use
+    @type options: C{list} of C{string}
+    @param options: the arguments to pass to the tracker
+        (optional, defaults to only using the default arguments)
+    @type clean: C{boolean}
+    @param clean: whether to remove any previous tracker state files
+        (optional, defaults to removing them)
+    @rtype: C{int}
+    @return: the PID of the downloader process
+    
+    """
+    
+    assert num_track < 10
+
+    print '************************** Starting Tracker ' + str(num_track) + ' **************************'
+
+    if clean:
+        try:
+            os.remove('tracker' + str(num_track) + '.state')
+        except:
+            pass
+
+    pid = start(track, ['--dfile', 'tracker' + str(num_track) + '.state',
+                        '--port', str(num_track) + '969'] + options)
+    return pid
+
+def run_test(trackers, downloaders, apt_get_queue):
+    """Run a single test.
+    
+    @type trackers: C{dictionary} of {C{int}: C{list} of C{string}}
+    @param trackers: the trackers to start, keys are the tracker numbers and
+        values are the list of options to invoke the tracker with
+    @type downloaders: C{dictionary} of {C{int}: (C{int}, C{list} of C{string})}
+    @param downloaders: the downloaders to start, keys are the downloader numbers and
+        values are the tracker to ascossiate with and the list of options to invoke 
+        the downloader with
+    @type apt_get_queue: C{list} of (C{int}, C{list} of C{string})
+    @param apt_get_queue: the apt-get downloader to use and commands to execute
+    @rtype: C{list} of (C{float}, C{int})
+    @return: the execution time and returned status code for each element of apt_get_queue
+    
+    """
+    
+    running_trackers = {}
+    running_downloaders = {}
+    running_apt_get = {}
+    apt_get_results = []
+
+    try:
+        for k, v in trackers.items():
+            running_trackers[k] = start_tracker(k, v)
+        
+        sleep(5)
+        
+        for k, v in downloaders.items():
+            running_downloaders[k] = start_downloader(k, v[1] + ['--default_tracker', tracker_address(v[0])])
+    
+        sleep(10)
+        
+        for (num_down, cmd) in apt_get_queue:
+            running_apt_get[num_down] = apt_get(num_down, cmd)
+            start_time = time()
+            (pid, r_value) = os.waitpid(running_apt_get[num_down], 0)
+            elapsed = time() - start_time
+            del running_apt_get[num_down]
+            r_value = r_value / 256
+            apt_get_results.append((elapsed, r_value))
+
+            if r_value == 0:
+                print '********************** apt-get completed successfully in ' +  str(elapsed) + ' sec. **************************'
+            else:
+                print '********************** apt-get finished with status ' + str(r_value) + ' in ' +  str(elapsed) + ' sec. **************************'
+        
+            sleep(5)
+            
+    except:
+        print '************************** Exception occurred **************************'
+        print_exc()
+        print '************************** will attempt to shut down *******************'
+        
+    print '*********************** shutting down the apt-gets *******************'
+    for k, v in running_apt_get.items():
+        try:
+            print 'apt-get', k, stop(v)
+        except:
+            print '************************** Exception occurred **************************'
+            print_exc()
+
+    sleep(5)
+
+    print '*********************** shutting down the downloaders *******************'
+    for k, v in running_downloaders.items():
+        try:
+            print 'downloader', k, stop(v)
+        except:
+            print '************************** Exception occurred **************************'
+            print_exc()
+
+    sleep(5)
+
+    print '************************** shutting down the trackers *******************'
+    for k, v in running_trackers.items():
+        try:
+            print 'tracker', k, stop(v)
+        except:
+            print '************************** Exception occurred **************************'
+            print_exc()
+
+    print '************************** Test Results *******************'
+    i = -1
+    for (num_down, cmd) in apt_get_queue:
+        i += 1
+        s = str(num_down) + ': "apt-get ' + ' '.join(cmd) + '" '
+        if len(apt_get_results) > i:
+            (elapsed, r_value) = apt_get_results[i]
+            s += 'took ' + str(elapsed) + ' secs (' + str(r_value) + ')\n'
+        else:
+            s += 'did not complete\n'
+        print s
+    
+    return apt_get_results
+
+def get_usage():
+    """Get the usage information to display to the user.
+    
+    @rtype: C{string}
+    @return: the usage information to display
+    
+    """
+    
+    s = 'Usage: ' + sys.argv[0] + ' (all|<test>|help)\n\n'
+    s += '  all    - run all the tests\n'
+    s += '  help   - display this usage information\n'
+    s += '  <test> - run the <test> test (see list below for valid tests)\n\n'
+    
+    for k, v in tests.items():
+        s += 'test "' + str(k) + '" - ' + v[0] + '\n'
+    
+    return s
+
+tests = {'1': ('Start a single tracker and downloader, test updating and downloading ' +
+             'using the HTTPDownloader.',
+             {1: []},
+             {1: (1, [])},
+             [(1, ['update']), 
+              (1, ['install', 'aboot-base']),
+              (1, ['install', 'aap-doc']),
+              (1, ['install', 'ada-reference-manual']),
+              (1, ['install', 'aspectj-doc']),
+              (1, ['install', 'fop-doc']),
+              (1, ['install', 'jswat-doc']),
+              (1, ['install', 'asis-doc']),
+              (1, ['install', 'bison-doc']),
+              (1, ['install', 'crash-whitepaper']),
+              (1, ['install', 'doc-iana']),
+              ]),
+
+         '2': ('Unconfigured test.',
+               {},
+               {},
+               []),
+         }
+
+assert 'all' not in tests
+assert 'help' not in tests
+
+if __name__ == '__main__':
+    if len(sys.argv) != 2:
+        print get_usage()
+    elif sys.argv[1] == 'all':
+        for k, v in tests.items():
+            run_test(v[1], v[2], v[3])
+    elif sys.argv[1] in tests:
+        v = tests[sys.argv[1]]
+        run_test(v[1], v[2], v[3])
+    elif sys.argv[1] == 'help':
+        print get_usage()
+    else:
+        print 'Unknown test to run:', sys.argv[1], '\n'
+        print get_usage()
+        

Propchange: debtorrent/trunk/test.py
------------------------------------------------------------------------------
    svn:executable = *

Propchange: debtorrent/trunk/test.py
------------------------------------------------------------------------------
    svn:keywords = Id




More information about the Debtorrent-commits mailing list