예제 #1
0
def run_tests(flavors=None, use_vt=True, echo=False, logcapture=True):
    if not flavors:
        flavors = []
    if isinstance(flavors, basestring):
        flavors = flavors.split(',')  # pylint: disable=E1101
    success = OrderedDict()
    failures = OrderedDict()
    # for step in ['lint', 'unit']:
    if echo:
        inq = Queue.Queue()
        outq = Queue.Queue()
        pr = threading.Thread(target=_echo, args=(inq, outq))
        pr.start()
    for step in ['unit']:
        try:
            utils.test_setup()
            success[step] = __salt__['mc_test.{0}_tests'.format(step)](
                use_vt=use_vt, logcapture=logcapture)
        except (TestError, ) as exc:
            failures[step] = exc
        except (Exception, KeyboardInterrupt):
            failures[step] = traceback.format_exc()
            break
        finally:
            utils.test_teardown()
    if echo:
        inq.put('STOP')
        pr.join()
    # for now, lint is not a failure
    acceptables = ['lint']
    for i in acceptables:
        failures.pop(i, None)
    if failures:
        _failures = dict([(a, "{0}".format(failures[a])) for a in failures])
        salt.output.display_output(_failures, opts=__opts__)
        raise TestError('test failure => non 0 exit code')
    # if no failure, be sure not to mark retcode as a failure
    __context__['retcode'] = 0
    return success
예제 #2
0
def unit_tests(tests=None,
               coverage=True,
               doctests=True,
               use_vt=True,
               logcapture=True):
    in_args = '--exe -e mc_test -v -s'
    if not logcapture:
        in_args += ' --nologcapture'
    if isinstance(tests, basestring):
        tests = tests.split(',')
    if not tests:
        tests = ['mc_states']
    if coverage:
        in_args += (' --with-xcoverage'
                    ' --xcoverage-file=.coverage.xml')
    if doctests:
        in_args += ' --with-doctest'
    failed = OrderedDict()
    success = OrderedDict()
    for test in tests:
        try:
            cmd = 'bin/nosetests {0} {1}'.format(
                in_args, test)
            result = __salt__['cmd.run_all'](
                cmd,
                output_loglevel='debug',
                use_vt=use_vt, cwd=mroot())
            if result['retcode']:
                failed[test] = result
            else:
                success[test] = result
        except salt.exceptions.CommandExecutionError:
            trace = traceback.format_exc()
            raise _error('Problem with nose install:\n {0}'.format(
                api.magicstring(trace)))
    if failed:
        fail = failed.pop([a for a in failed][0])
        for ffail in failed:
            fail = saltapi.concat_res_or_rets(fail, ffail)
        raise _error('Doctest tests failed', fail)
    return success
예제 #3
0
def get_local_registry(name,
                       cached=True,
                       cachetime=60,
                       registry_format='yaml'):
    '''Get local registry
    Masteralt & Salt share the local registries
    unless for the main ones:

        - controllers
        - services
        - nodetypes
        - localsettings
        - cloud

    For backward compatibility, we take care to load and merge
    shared registries in mastersalt & salt prefix if any is found.
    '''
    not_shared = ['controllers', 'services', 'nodetypes',
                  'localsettings', 'cloud']
    mastersalt_registryf = '{0}/makina-states/{1}.{2}'.format(
        '/etc/mastersalt',  name, registry_format)
    salt_registryf = '{0}/makina-states/{1}.{2}'.format(
        '/etc/salt',  name, registry_format)
    shared_registryf = os.path.join(
        '/etc/makina-states/{0}.{1}'.format(name, registry_format))
    registry = OrderedDict()
    # cache local registries one minute
    pkey = '{0}____'.format(name)
    key = '{0}{1}'.format(pkey, time.time() // cachetime)
    if name not in not_shared:
        to_load = [mastersalt_registryf,
                   salt_registryf,
                   shared_registryf]
    else:
        to_load = [
            '{0}/makina-states/{1}.{2}'.format(
                __opts__['config_dir'], name, registry_format)
        ]
    if (key not in _LOCAL_REG_CACHE) or (not cached):
        invalidate_cached_registry(name)
        for registryf in to_load:
            dregistry = os.path.dirname(registryf)
            if not os.path.exists(dregistry):
                os.makedirs(dregistry)
            if os.path.exists(registryf):
                _LOCAL_REG_CACHE[key] = registry = __salt__[
                    'mc_utils.dictupdate'](
                        registry,
                        __salt__[
                            'mc_macros.{0}_load_local_registry'.format(
                                registry_format)](name, registryf))
                # unprefix local simple registries
                loc_k = DEFAULT_LOCAL_REG_NAME.format(name)
                for k in [t for t in registry if t.startswith(loc_k)]:
                    spl = loc_k + '.'
                    nk = spl.join(k.split(spl)[1:])
                    registry[nk] = registry[k]
                    registry.pop(k)
    elif cached:
        registry = _LOCAL_REG_CACHE[key]
    return registry