home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / checkbox / test.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  9.0 KB  |  262 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. The purpose of this module is to encapsulate the concept of a test
  6. which might be presented in several ways. For example, a test might
  7. require manual intervention whereas another test might be completely
  8. automatic. Either way, this module provides the base common to each type
  9. of test.
  10. '''
  11. import re
  12. import logging
  13. from gettext import gettext as _
  14. from checkbox.lib.environ import add_variable, remove_variable
  15. from checkbox.lib.signal import signal_to_name, signal_to_description
  16. from checkbox.command import Command
  17. from checkbox.frontend import frontend
  18. from checkbox.requires import Requires
  19. DESKTOP = 'desktop'
  20. LAPTOP = 'laptop'
  21. SERVER = 'server'
  22. ALL_CATEGORIES = [
  23.     DESKTOP,
  24.     LAPTOP,
  25.     SERVER]
  26. I386 = 'i386'
  27. AMD64 = 'amd64'
  28. LPIA = 'lpia'
  29. SPARC = 'sparc'
  30. ALL_ARCHITECTURES = [
  31.     I386,
  32.     AMD64,
  33.     LPIA,
  34.     SPARC]
  35. FAIL = 'fail'
  36. PASS = 'pass'
  37. SKIP = 'skip'
  38. ALL_STATUS = [
  39.     PASS,
  40.     FAIL,
  41.     SKIP]
  42.  
  43. class TestResult(object):
  44.     
  45.     def __init__(self, test, status, data, duration = None):
  46.         self.test = test
  47.         self.status = status
  48.         self.data = data
  49.         self.duration = duration
  50.  
  51.     
  52.     def attributes(self):
  53.         return {
  54.             'status': self.status,
  55.             'data': self.data,
  56.             'duration': self.duration }
  57.  
  58.     attributes = property(attributes)
  59.     
  60.     def devices(self):
  61.         return self.test.requires.get_devices()
  62.  
  63.     devices = property(devices)
  64.     
  65.     def packages(self):
  66.         return self.test.requires.get_packages()
  67.  
  68.     packages = property(packages)
  69.     
  70.     def _get_status(self):
  71.         return self._status
  72.  
  73.     
  74.     def _set_status(self, status):
  75.         if status not in ALL_STATUS:
  76.             raise Exception, 'Invalid status: %s' % status
  77.         status not in ALL_STATUS
  78.         self._status = status
  79.  
  80.     status = property(_get_status, _set_status)
  81.  
  82.  
  83. class TestCommand(Command):
  84.     
  85.     def __init__(self, test, *args, **kwargs):
  86.         super(TestCommand, self).__init__(test.command, test.timeout, *args, **kwargs)
  87.         self.test = test
  88.  
  89.     
  90.     def execute(self, *args, **kwargs):
  91.         return super(TestCommand, self).execute(*args, **kwargs)
  92.  
  93.     execute = frontend('get_test_result')(execute)
  94.     
  95.     def post_execute(self, result):
  96.         result = super(TestCommand, self).post_execute(result)
  97.         if result.if_exited:
  98.             exit_status = result.exit_status
  99.             if exit_status == 0:
  100.                 status = PASS
  101.                 data = result.stdout
  102.                 if not data:
  103.                     data = result.stderr
  104.                 
  105.             elif exit_status == 127:
  106.                 status = SKIP
  107.                 data = _('Command failed, skipping.')
  108.             else:
  109.                 status = FAIL
  110.                 data = result.stderr
  111.         elif result.if_signaled:
  112.             status = SKIP
  113.             term_signal = result.term_signal
  114.             data = _('Received terminate signal %s: %s') % (signal_to_name(term_signal), signal_to_description(term_signal))
  115.         else:
  116.             raise Exception, 'Command not terminated: %s' % self.get_command()
  117.         duration = result.if_exited.duration
  118.         return TestResult(self.test, status, data, duration)
  119.  
  120.  
  121.  
  122. class TestDescription(Command):
  123.     
  124.     def __init__(self, test):
  125.         super(TestDescription, self).__init__(test.description, test.timeout)
  126.         self.test = test
  127.         self.output = None
  128.  
  129.     
  130.     def get_command(self):
  131.         command = super(TestDescription, self).get_command()
  132.         return 'cat <<EOF\n%s\nEOF\n' % command
  133.  
  134.     
  135.     def pre_execute(self, result = None):
  136.         super(TestDescription, self).pre_execute()
  137.         if re.search('\\$output', self.get_command()):
  138.             if not (self.output) and not result:
  139.                 result = self.test.command()
  140.             
  141.             if result:
  142.                 self.output = result.data.strip()
  143.                 result.data = ''
  144.             
  145.             add_variable('output', self.output)
  146.         
  147.  
  148.     
  149.     def execute(self, *args, **kwargs):
  150.         return super(TestDescription, self).execute(*args, **kwargs)
  151.  
  152.     execute = frontend('get_test_description')(execute)
  153.     
  154.     def post_execute(self, result):
  155.         result = super(TestDescription, self).post_execute(result)
  156.         remove_variable('output')
  157.         if not (result.if_exited) or result.exit_status != 0:
  158.             raise Exception, 'Description failed: %s' % self.get_command()
  159.         result.exit_status != 0
  160.         return result.stdout
  161.  
  162.  
  163.  
  164. class Test(object):
  165.     """
  166.     Test base class which should be inherited by each test
  167.     implementation. A test instance contains the following required
  168.     fields:
  169.  
  170.     name:          Unique name for a test.
  171.     plugin:        Plugin name to handle this test.
  172.     description:   Long description of what the test does.
  173.     suite:         Name of the suite containing this test.
  174.  
  175.     An instance also contains the following optional fields:
  176.  
  177.     architectures: List of architectures for which this test is relevant:
  178.                    amd64, i386, powerpc and/or sparc
  179.     categories:    List of categories for which this test is relevant:
  180.                    desktop, laptop and/or server
  181.     command:       Command to run for the test.
  182.     depends:       List of names on which this test depends. So, if
  183.                    the other test fails, this test will be skipped.
  184.     requires:      Registry expressions which are requirements for
  185.                    this test: 'input.mouse' in info.capabilities
  186.     timeout:       Timeout for running the command.
  187.     user:          User to run the command.
  188.     optional:      Boolean expression set to True if this test is optional
  189.                    or False if this test is required.
  190.     """
  191.     required_fields = [
  192.         'name',
  193.         'plugin',
  194.         'description',
  195.         'suite']
  196.     optional_fields = {
  197.         'architectures': [],
  198.         'categories': [],
  199.         'command': None,
  200.         'depends': [],
  201.         'requires': None,
  202.         'timeout': None,
  203.         'user': None,
  204.         'optional': False }
  205.     
  206.     def __init__(self, registry, **attributes):
  207.         super(Test, self).__setattr__('attributes', attributes)
  208.         for field in [
  209.             'architectures',
  210.             'categories',
  211.             'depends']:
  212.             if attributes.has_key(field):
  213.                 attributes[field] = re.split('\\s*,\\s*', attributes[field])
  214.                 continue
  215.         
  216.         for field in [
  217.             'timeout']:
  218.             if attributes.has_key(field):
  219.                 attributes[field] = int(attributes[field])
  220.                 continue
  221.         
  222.         for field in self.optional_fields.keys():
  223.             if not attributes.has_key(field):
  224.                 attributes[field] = self.optional_fields[field]
  225.                 continue
  226.         
  227.         attributes['requires'] = Requires(registry, attributes['requires'])
  228.         attributes['command'] = TestCommand(self)
  229.         attributes['description'] = TestDescription(self)
  230.         self._validate()
  231.  
  232.     
  233.     def _validate(self):
  234.         for field in self.attributes.keys():
  235.             if field not in self.required_fields + self.optional_fields.keys():
  236.                 logging.info('Test attributes contains unknown field: %s', field)
  237.                 del self.attributes[field]
  238.                 continue
  239.         
  240.         for field in self.required_fields:
  241.             if not self.attributes.has_key(field):
  242.                 raise Exception, "Test attributes does not contain '%s': %s" % (field, self.attributes)
  243.             self.attributes.has_key(field)
  244.         
  245.  
  246.     
  247.     def __getattr__(self, name):
  248.         if name not in self.attributes:
  249.             raise AttributeError, name
  250.         name not in self.attributes
  251.         return self.attributes[name]
  252.  
  253.     
  254.     def __setattr__(self, name, value):
  255.         if name not in self.attributes:
  256.             raise AttributeError, name
  257.         name not in self.attributes
  258.         self.attributes[name] = value
  259.         self._validate()
  260.  
  261.  
  262.