Ejemplo n.º 1
0
 def __str__(self):
     s = 'Configuration result for node `%s` (package `%s`)' % (
         self.node_name, self.package_name)
     s += '\nThe configuration sequence was %s.' % list(
         self.config_sequence)
     s += '\nThe following is the list of parameters set and their origin:'
     s += '\n' + indent(
         config_summary(self.all_keys, self.values, self.origin), '  ')
     return s
Ejemplo n.º 2
0
def expect(data, query, result_keys, filters=None):
    result_keys = list(result_keys)
    check_isinstance(data, OrderedDict)

    if True:
        spec = parse_match_spec(query, filters=filters)
        print '-----'
        print 'Query: %s' % query
        print indent(spec, '', 'Spec: ')

    res = fuzzy_match(query, data, filters=filters)
    check_isinstance(res, OrderedDict)

    if list(res) != result_keys:
        msg = 'Error:'
        msg += '\n Data: %s' % data
        msg += '\n Query: %s' % query
        msg += '\n Result: %s' % list(res)
        msg += '\n Expect: %s' % list(result_keys)
        raise Exception(msg)
Ejemplo n.º 3
0
    def check(self):
        fn = expand_all(self.filename)

        if not os.path.exists(fn):
            msg = 'File does not exist: %s' % fn
            raise CheckError(msg)

        contents = open(fn).read()

        l = 'Entire contents of %s:\n' % fn + indent(contents, '  > ')

        if not self.string in contents:
            msg = 'String %r not contained in %s' % (self.string, fn)
            raise CheckFailed(msg, l)
Ejemplo n.º 4
0
    def check(self):
        hostname = socket.gethostname()

        initial = 'duckiebot-not-configured'
        if hostname == initial:
            msg = 'The hostname is not configured; it is still %r.' % initial
            raise CheckFailed(msg)

        fn = '/etc/hostname'
        contents = open(fn).read().strip()
        if contents != hostname:
            msg = 'The hostname is %r but it does not match %s.' % (hostname,
                                                                    fn)
            l = 'Entire contents of %s:\n' % fn + indent(contents, '  > ')
            raise CheckFailed(msg, l)
Ejemplo n.º 5
0
def print_results(analyzers, results_all, out):
    base = os.path.join(out, 'statistics')
    yaml_data = yaml_dump_pretty(results_all)
    write_data_to_file(yaml_data, os.path.join(base, 'statistics.yaml'))
    print(yaml_data)

    for a in analyzers:
        write_data_to_file(yaml_dump_pretty(results_all[a]),
                           os.path.join(base, '%s.table.yaml' % a))
        s = ""
        s += '\n' + '-' * 10 + ' Results for %s ' % a + '-' * 10
        table = table_for_analyzer(results_all[a])
        s += '\n' + indent(format_table_plus(table, colspacing=3), '  ')
        s += '\n'
        write_data_to_file(yaml_data, os.path.join(base, '%s.table.txt' % a))
Ejemplo n.º 6
0
    def check(self):
        # read hosts file
        fn = '/etc/hosts'
        contents = open(fn).read()

        l = 'Entire contents of %s:\n' % fn + indent(contents, '  > ')

        if '10.' in contents or '192.' in contents:
            msg = 'The %s file contains hard-wired IPs.' % fn
            raise CheckFailed(msg, l)

        if '.local' in contents:
            msg = 'The %s file contains hard-wired host names.' % fn
            raise CheckFailed(msg, l)

        hostname = socket.gethostname()

        if not hostname in contents:
            msg = 'The %s file does not contain an entry for your hostname %r.' % (
                fn, hostname)
            raise CheckFailed(msg, l)
Ejemplo n.º 7
0
    def query_results_one(self, branch, date, commit):
        possible = self.query_results(branch, date, commit)
        from easy_regression.conditions.eval import DataNotFound
        if len(possible) == 0:
            msg = 'Could not find any match for the query.'
            msg += '\n branch: %s' % branch
            msg += '\n   date: %s' % date
            msg += '\n commit: %s' % commit
            raise DataNotFound(msg)

        if len(possible) > 1:
            n = len(possible)
            msg = 'Found %d matches for this query.' % n
            msg += '\n   branch: %s' % branch
            msg += '\n     date: %s' % date
            msg += '\n   commit: %s' % commit
            msg += '\nThese are the matches:'
            for i, p in enumerate(possible):
                check_isinstance(p, ResultDBEntry)
                msg += '\n' + indent(str(p), ' %2d of %d: ' % (i + 1, n))
            raise AmbiguousQuery(msg)

        return possible[0]
Ejemplo n.º 8
0
 def __str__(self):
     s = 'CheckResult:'
     s += '\n' + indent(self.status, '   status: ')
     s += '\n' + indent(self.summary, '  summary: ')
     s += '\n' + indent(self.details, '', '  details: ')
     return s
Ejemplo n.º 9
0
 def __str__(self):
     s = "Binary operation"
     s += '\n' + indent(self.a, '', '   a: ')
     s += '\n' + indent(self.op, '', '  op: ')
     s += '\n' + indent(self.b, '', '   b: ')
     return s
Ejemplo n.º 10
0
def interpret_config_file(filename):
    """ 
        Returns a ConfigInfo.     
    """
    try:
        basename = os.path.basename(filename)
        base = basename.replace(SUFFIX, '')
        # now we have something like
        #   package-node.config_name.date
        # or
        #   package-node.config_name
        if not '.' in base:
            msg = 'Invalid filename %r.' % filename
            raise DTConfigException(msg)

        tokens = base.split('.')
        if len(tokens) > 3:
            msg = 'Too many periods/tokens (tokens=%s)' % tokens
            raise DTConfigException(msg)

        if len(tokens) <= 2:
            #  package-node.config_name
            package_node = tokens[0]
            if not '-' in package_node:
                msg = 'Expected a "-" in "%s".' % package_node
                raise DTConfigException(msg)
            i = package_node.index('-')
            package_name = package_node[:i]
            node_name = package_node[i + 1:]

        config_name = tokens[1]

        if len(tokens) == 3:
            # package-node.config_name.date
            date_effective = tokens[2]
        else:
            date_effective = '20170101'
        from dateutil.parser import parse

        try:
            date_effective = parse(date_effective)
        except:
            msg = 'Cannot interpret "%s" as a date.' % date_effective
            raise DTConfigException(msg)

        # now read file

        contents = open(filename).read()
        try:
            try:
                data = yaml.load(contents)
            except YAMLError as e:
                raise_wrapped(DTConfigException,
                              e,
                              'Invalid YAML',
                              compact=True)

            if not isinstance(data, dict):
                msg = 'Expected a dictionary inside.'
                raise DTConfigException(msg)

            for field in ['description', 'values']:
                if not field in data:
                    msg = 'Missing field "%s".' % field
                    raise DTConfigException(msg)

            description = data.pop('description')
            if not isinstance(description, str):
                msg = 'I expected that "description" is a string, obtained %r.' % description
                raise DTConfigException(msg)

            extends = data.pop('extends', [])
            if not isinstance(extends, list):
                msg = 'I expected that "extends" is a list, obtained %r.' % extends
                raise DTConfigException(msg)

            values = data.pop('values')
            if not isinstance(values, dict):
                msg = 'I expected that "values" is a dictionary, obtained %s.' % type(
                    values)
                raise DTConfigException(msg)

            # Freeze the data
            extends = tuple(extends)
            values = frozendict(values)

        except DTConfigException as e:
            msg = 'Could not interpret the contents of the file\n'
            msg += '   %s\n' % friendly_path(filename)
            msg += 'Contents:\n' + indent(contents, ' > ')
            raise_wrapped(DTConfigException, e, msg, compact=True)

        return ConfigInfo(
            filename=filename,
            package_name=package_name,
            node_name=node_name,
            config_name=config_name,
            date_effective=date_effective,
            extends=extends,
            description=description,
            values=values,
            # not decided
            valid=None,
            error_if_invalid=None)

    except DTConfigException as e:
        msg = 'Invalid file %s' % friendly_path(filename)
        raise_wrapped(DTConfigException, e, msg, compact=True)
Ejemplo n.º 11
0
def run_checks(entries):
    """ Returns the names of the failures  """
    results = [] 
    
    def record_result(r):
        results.append(r) 
    
    # raise NotRun if not previously run
    class NotRun(Exception): pass
    
    def get_previous_result_status(e):
        for r in results:
            if e == r.entry:
                return r.status
            
        logger.error('Could not find %s' % e)
        logger.error(results)
        raise NotRun()
    
    for entry in entries:
        
        # check dependencies
        only_run_if = entry.only_run_if
        if only_run_if is None:
            pass
        else:
            try:
                dep_status = get_previous_result_status(only_run_if)
            
                if dep_status in [FAIL, ERROR]:
                    msg = "Skipped because the previous test %r failed." % (only_run_if.desc)
                    r = Result(entry=entry, status=SKIP, out_short=msg, out_long='')
                    record_result(r)
                    continue
                
                elif dep_status in [SKIP]:
                    msg = "Skipped because the previous test %r skipped." % (only_run_if.desc)
                    r = Result(entry=entry, status=SKIP, out_short=msg, out_long='')
                    record_result(r)
                    continue

            except NotRun:
                msg = 'Dependency did not run yet.'
                r = Result(entry=entry, status=ERROR, out_short=msg, out_long='', )
                record_result(r)
                continue
        
        # at this point, either it's None or passed
        assert only_run_if is None or (get_previous_result_status(only_run_if) == OK)
    
        try:
            res = entry.check.check() or ''
            r = Result(entry=entry, status=OK, out_short=res, out_long='')
            record_result(r)
            
        except CheckError as e:
            r = Result(entry=entry, status=ERROR, 
                       out_short='Could not run test.',
                       out_long=e.long_explanation)
            record_result(r)
            
        except CheckFailed as e:
            r = Result(entry=entry, status=FAIL, 
                       out_short=e.compact,
                       out_long=e.long_explanation)
            record_result(r)
            
        except Exception as e:
            msg = 'Invalid test: it raised the exception %s.' % type(e).__name__
            l = 'I expect the tests to only raise CheckError or CheckFailed.'
            l += '\n\nEntire exception:\n\n'
            l += indent(traceback.format_exc(e), '  ')
            r = Result(entry=entry, status=ERROR, 
                       out_short=msg,
                       out_long=l)
            record_result(r)
            
    return results
Ejemplo n.º 12
0
def display_check_results(results, out):
    s = ""
    for i, r in enumerate(results):
        s += '\n' + indent(str(r), '', '%d of %d: ' % (i+1, len(results)))
    print(s)
Ejemplo n.º 13
0
def raise_error(rdb, t, res, s):
    msg = s
    msg += '\n' + indent(str(res), 'obtained: ')
    msg += '\n' + indent(str(t), '', 'test: ')
    msg += '\n' + indent(str(rdb), '', 'rdb: ')
    raise Exception(msg)