Ejemplo n.º 1
0
def lint_tests(use_vt=True, logcapture=True):
    try:
        result = __salt__['cmd.run_all'](
            '_scripts/pylint.sh -f colorized mc_states',
            use_vt=use_vt, cwd=mroot())
        if result['retcode']:
            raise _error('Pylint tests failed', result)
    except salt.exceptions.CommandExecutionError:
        trace = traceback.format_exc()
        raise _error('Problem with pylint install:\n {0}'.format(
            api.magicstring(trace)))
Ejemplo n.º 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
Ejemplo n.º 3
0
    def _load_content(self, data, show_content=True, filename='<string>'):
        results = dict()
        failed = False
        old_data = None
        err_msg = ''
        try:
            data = magicstring(data)
            if (('{{' in data) or ('{%' in data)):
                while old_data != data:
                    old_data = data
                    data = self._templar.template(data)
                data = self._loader.load(data, show_content)
            else:
                data = self._loader.load(data, show_content)
        except (Exception, ) as exc:
            trace = traceback.format_exc()
            failed = True
            print('\nFile: {0}'.format(filename))
            err_msg = ('{0} does not render correctly: \n{1}\n{2}\n'.format(
                filename, exc, trace))
            if old_data:
                print('\nOriginal data')
                print(old_data)
            if data and (data != old_data):
                print('\nCurrently Rendered data')
                print(data)
            print('\nError')
            print(err_msg)
            return failed, err_msg, results

        if not data:
            data = dict()
        if not isinstance(data, dict):
            failed = True
            err_msg = (
                '{0} must be stored as a dictionary/hash'.format(filename))
        else:
            results.update(data)
        return failed, err_msg, results
Ejemplo n.º 4
0
def magicstring(thestr):
    """
    Convert any string to UTF-8 ENCODED one
    """
    return api.magicstring(thestr)
Ejemplo n.º 5
0
def concat_res_or_rets(ret,
                       cret=None,
                       result_keys=None,
                       output_keys=None,
                       dict_keys=None,
                       omit=None):
    '''
    Convenient and magical way to merge 2 structures
    or strings for usage in salt functions.

    concatenate string with string
        join them (separated with a newline)
    concatenate string with dict:
        append all output keys from dict in the
        string separated by a new line and prefixed
        by the output key identfier
    concatenate dict with string:
        concatenate (with newlineà)
        the string in all output keys
    concatenate dict with dict:
        merge corresponding keys in an intelligent way:

        - result from ret is setted to false
          if cret's one is setted to False
        - merge output keys (separate with newline)
        - merge dict keys by updating or creating
          the corresponding key in ret from cret

    .. doctest:: example

    >>> from collections import OrderedDict
    >>> from mc_states.saltapi import concat_res_or_rets
    >>> concat_res_or_rets({}, {'result': False})
    {'result': False}
    >>> concat_res_or_rets({'result': True}, {'result': False})
    {'result': False}
    >>> concat_res_or_rets('oo', {'stdout': 'a', 'stderr': 'b'})
    'oo\\nSTDOUT: a\\nSTDERR: b'
    >>> concat_res_or_rets('a', 'b')
    'a\\nb'
    >>> concat_res_or_rets(OrderedDict([('stdout', 'a'), ('stderr', 'b')]),
    ...                    'de')
    OrderedDict([('stdout', 'a\\nde'), ('stderr', 'b'), ('output', 'de')])
    >>> concat_res_or_rets(OrderedDict([('stdout', 'a'), ('stderr', 'b')]),
    ...                    {'stdout': 'c', 'stderr': 'd'})
    OrderedDict([('stdout', 'a\\nc'), ('stderr', 'b\\nd')])
    >>> concat_res_or_rets({'changes': {1: 2, 3: 4, 5: 6}},
    ...                    {'changes': {1: 3, 3: 4}})
    {'changes': {1: 3, 3: 4, 5: 6}}

    '''
    if not result_keys:
        result_keys = ['result']
    if not output_keys:
        output_keys = ['stdout', 'stderr',
                       'comment' 'trace', 'output']
    if not dict_keys:
        dict_keys = ['changes']
    if not omit:
        omit = []
    if not isinstance(ret, (six.string_types, dict)):
        ret = api.asstring(ret)
    if not isinstance(cret, (six.string_types, dict)):
        cret = api.asstring(cret)
    if isinstance(cret, six.string_types) and isinstance(ret, dict):
        cret = {'stdout': cret, 'comment': cret,
                'trace': cret, 'output': cret}
    if isinstance(ret, six.string_types):
        ret = api.magicstring(ret)
    elif isinstance(ret, dict):
        for k in output_keys:
            if k in omit:
                continue
            val = ret.get(k, None)
            if isinstance(val, six.string_types):
                ret[k] = api.magicstring(val)
    for k in result_keys:
        if (
            isinstance(cret, dict) and
            isinstance(ret, dict) and
            (k in cret)
        ):
            if not cret.get(k):
                ret[k] = False
    if isinstance(ret, dict):
        for k in output_keys:
            if not cret:
                break
            elif k in omit:
                continue
            val = api.asstring(ret.get(k, None))
            oval = api.asstring(cret.get(k, None))
            if oval:
                formatting = "{0}\n{1}"
                if not val:
                    formatting = "{1}"
                if val != oval:
                    ret[k] = formatting.format(
                        api.magicstring(val),
                        api.magicstring(oval))
    if isinstance(ret, six.string_types) and cret:
        if isinstance(cret, dict):
            for k in output_keys:
                if k not in cret:
                    continue
                if k in omit:
                    continue
                val = cret[k]
                if not val:
                    continue
                sval = api.magicstring(val)
                ret = '{0}\n{1}: {2}'.format(
                    ret, k.upper(), sval)
        elif isinstance(cret, six.string_types) and cret:
            ret = '{0}\n{1}'.format(
                api.magicstring(ret),
                api.magicstring(cret))
    if isinstance(ret, dict) and isinstance(cret, dict):
        for k in dict_keys:
            if k in omit:
                continue
            if k in cret or k in ret:
                d1 = ret.get(k, OrderedDict())
                d2 = cret.get(k, OrderedDict())
                if isinstance(d1, dict) and isinstance(d2, dict):
                    ret[k] = dictupdate(d1, d2)
    return ret