Ejemplo n.º 1
0
def testinfra(inventory,
              testinfra_dir,
              debug=False,
              env=None,
              out=print_stdout,
              err=print_stderr):
    """
    Runs testinfra against specified ansible inventory file

    :param inventory: Path to ansible inventory file
    :param testinfra_dir: Path to the testinfra tests
    :param debug: Pass debug flag to testinfra
    :param env: Environment to pass to underlying sh call
    :param out: Function to process STDOUT for underlying sh call
    :param err: Function to process STDERR for underlying sh call
    :return: sh response object
    """
    kwargs = {
        '_env': env,
        '_out': out,
        '_err': err,
        'debug': debug,
        'ansible_inventory': inventory,
        'sudo': True,
        'connection': 'ansible'
    }

    if 'HOME' not in kwargs['_env']:
        kwargs['_env']['HOME'] = os.path.expanduser('~')

    return sh.testinfra(testinfra_dir, **kwargs)
Ejemplo n.º 2
0
def testinfra(tests,
              debug=False,
              env=os.environ.copy(),
              out=utilities.logger.warning,
              err=utilities.logger.error,
              **kwargs):
    """
    Runs testinfra against specified ansible inventory file

    :param inventory: Path to ansible inventory file
    :param tests: List of testinfra tests
    :param debug: Pass debug flag to testinfra
    :param env: Environment to pass to underlying sh call
    :param out: Function to process STDOUT for underlying sh call
    :param err: Function to process STDERR for underlying sh call
    :return: sh response object
    """
    kwargs['debug'] = debug
    kwargs['_env'] = env
    kwargs['_out'] = out
    kwargs['_err'] = err

    if 'HOME' not in kwargs['_env']:
        kwargs['_env']['HOME'] = os.path.expanduser('~')

    return sh.testinfra(tests, **kwargs)
Ejemplo n.º 3
0
def testinfra(inventory, debug=False, env=None, out=print_stdout, err=print_stderr):
    """
    Runs testinfra against specified ansible inventory file

    :param inventory: Path to ansible inventory file
    :param debug: Pass debug flag to testinfra
    :param env: Environment to pass to underlying sh call
    :param out: Function to process STDOUT for underlying sh call
    :param err: Function to process STDERR for underlying sh call
    :return: sh response object
    """
    kwargs = {
        '_env': env,
        '_out': out,
        '_err': err,
        'debug': debug,
        'ansible_inventory': inventory,
        'sudo': True,
        'connection': 'ansible'
    }

    if 'HOME' not in kwargs['_env']:
        kwargs['_env']['HOME'] = os.path.expanduser('~')

    return sh.testinfra(**kwargs)
Ejemplo n.º 4
0
    def _testinfra(self,
                   tests,
                   debug=False,
                   env=os.environ.copy(),
                   out=LOG.info,
                   err=LOG.error,
                   **kwargs):
        """
        Executes testinfra against specified tests, and returns a :func:`sh`
        response object.

        :param tests: A list of testinfra tests.
        :param debug: An optional bool to toggle debug output.
        :param pattern: A string containing the pattern of files to lint.
        :param env: An optional environment to pass to underlying :func:`sh`
         call.
        :param out: An optional function to process STDOUT for underlying
         :func:`sh` call.
        :param err: An optional function to process STDERR for underlying
         :func:`sh` call.
        :return: :func:`sh` response object.
        """
        kwargs['debug'] = debug
        kwargs['_env'] = env
        kwargs['_out'] = out
        kwargs['_err'] = err

        msg = 'Executing testinfra tests found in {}/.'.format(
            self._testinfra_dir)
        util.print_info(msg)

        return sh.testinfra(tests, **kwargs)
Ejemplo n.º 5
0
    def _testinfra(self,
                   tests,
                   debug=False,
                   env=os.environ.copy(),
                   out=LOG.info,
                   err=LOG.error,
                   **kwargs):
        """
        Runs testinfra and returns a sh response object.

        :param tests: List of testinfra tests
        :param debug: Pass debug flag to testinfra
        :param env: Environment to pass to underlying sh call
        :param out: Function to process STDOUT for underlying sh call
        :param err: Function to process STDERR for underlying sh call
        :return: sh response object
        """
        kwargs['debug'] = debug
        kwargs['_env'] = env
        kwargs['_out'] = out
        kwargs['_err'] = err

        if 'HOME' not in kwargs['_env']:
            kwargs['_env']['HOME'] = os.path.expanduser('~')

        msg = 'Executing testinfra tests found in {}/.'.format(
            self._testinfra_dir)
        util.print_info(msg)

        return sh.testinfra(tests, **kwargs)
Ejemplo n.º 6
0
    def _testinfra(self,
                   tests,
                   debug=False,
                   ansible_env={},
                   out=LOG.info,
                   err=LOG.error,
                   **kwargs):
        """
        Executes testinfra against specified tests, and returns a :func:`sh`
        response object.

        :param tests: A list of testinfra tests.
        :param debug: An optional bool to toggle debug output.
        :param pattern: A string containing the pattern of files to lint.
        :param ansible_env: An optional environment to pass to underlying
         :func:`sh` call.
        :param out: An optional function to process STDOUT for underlying
         :func:`sh` call.
        :param err: An optional function to process STDERR for underlying
         :func:`sh` call.
        :return: :func:`sh` response object.
        """
        kwargs['debug'] = debug
        kwargs['_env'] = ansible_env
        kwargs['_out'] = out
        kwargs['_err'] = err

        msg = 'Executing testinfra tests found in {}/.'.format(
            self._testinfra_dir)
        util.print_info(msg)

        return sh.testinfra(tests, **kwargs)
Ejemplo n.º 7
0
    def _testinfra(self,
                   tests,
                   debug=False,
                   env=os.environ.copy(),
                   out=LOG.info,
                   err=LOG.error,
                   **kwargs):
        """
        Runs testinfra and returns a sh response object.

        :param tests: List of testinfra tests
        :param debug: Pass debug flag to testinfra
        :param env: Environment to pass to underlying sh call
        :param out: Function to process STDOUT for underlying sh call
        :param err: Function to process STDERR for underlying sh call
        :return: sh response object
        """
        kwargs['debug'] = debug
        kwargs['_env'] = env
        kwargs['_out'] = out
        kwargs['_err'] = err

        if 'HOME' not in kwargs['_env']:
            kwargs['_env']['HOME'] = os.path.expanduser('~')

        msg = 'Executing testinfra tests found in {}/.'.format(
            self._testinfra_dir)
        util.print_info(msg)

        return sh.testinfra(tests, **kwargs)
Ejemplo n.º 8
0
    def execute(self):
        """
        Performs verification steps on running instances.
        Checks files for trailing whitespace and newlines.
        Runs testinfra against instances.
        Runs serverspec against instances (also calls rubocop on spec files).

        :return: None if no tests are found, otherwise return code of underlying command
        """
        if self.static:
            self.disabled('verify')

        validators.check_trailing_cruft(ignore_paths=self.molecule._config.config['molecule']['ignore_paths'])

        # no tests found
        if not os.path.isdir(self.molecule._config.config['molecule']['serverspec_dir']) and not os.path.isdir(
                self.molecule._config.config['molecule'][
                    'testinfra_dir']):
            msg = '{}Skipping tests, could not find {}/ or {}/.{}'
            print(msg.format(Fore.YELLOW, self.molecule._config.config['molecule']['serverspec_dir'],
                             self.molecule._config.config[
                                 'molecule']['testinfra_dir'], Fore.RESET))
            return

        self.molecule._write_ssh_config()
        kwargs = {'_env': self.molecule._env, '_out': utilities.print_stdout, '_err': utilities.print_stderr}
        kwargs['_env']['PYTHONDONTWRITEBYTECODE'] = '1'
        args = []

        # testinfra
        if os.path.isdir(self.molecule._config.config['molecule']['testinfra_dir']):
            try:
                ti_args = [
                    '--sudo', '--connection=ansible',
                    '--ansible-inventory=' + self.molecule._config.config['ansible']['inventory_file']
                ]
                output = sh.testinfra(*ti_args, **kwargs)
                return output.exit_code
            except sh.ErrorReturnCode as e:
                print('ERROR: {}'.format(e))
                sys.exit(e.exit_code)

        # serverspec
        if os.path.isdir(self.molecule._config.config['molecule']['serverspec_dir']):
            self.molecule._rubocop()
            if 'rakefile_file' in self.molecule._config.config['molecule']:
                kwargs['rakefile'] = self.molecule._config.config['molecule']['rakefile_file']
            if self.molecule._args['--debug']:
                args.append('--trace')
            try:
                rakecmd = sh.Command("rake")
                output = rakecmd(*args, **kwargs)
                return output.exit_code
            except sh.ErrorReturnCode as e:
                print('ERROR: {}'.format(e))
                sys.exit(e.exit_code)
Ejemplo n.º 9
0
def testinfra(testinfra_dir, env=None, debug=False, **kwargs):
    """
    Runs testinfra against specified ansible inventory file

    :param inventory: Path to ansible inventory file
    :param testinfra_dir: Path to the testinfra tests
    :param debug: Pass debug flag to testinfra
    :param env: Environment to pass to underlying sh call
    :param out: Function to process STDOUT for underlying sh call
    :param err: Function to process STDERR for underlying sh call
    :return: sh response object
    """
    kwargs['debug'] = debug
    kwargs['_env'] = env

    if 'HOME' not in kwargs['_env']:
        kwargs['_env']['HOME'] = os.path.expanduser('~')

    tests = '{}/test_*.py'.format(testinfra_dir)
    tests_glob = sh.glob(tests)

    return sh.testinfra(tests_glob, **kwargs)
Ejemplo n.º 10
0
def testinfra(testinfra_dir, env=None, debug=False, **kwargs):
    """
    Runs testinfra against specified ansible inventory file

    :param inventory: Path to ansible inventory file
    :param testinfra_dir: Path to the testinfra tests
    :param debug: Pass debug flag to testinfra
    :param env: Environment to pass to underlying sh call
    :param out: Function to process STDOUT for underlying sh call
    :param err: Function to process STDERR for underlying sh call
    :return: sh response object
    """
    kwargs['debug'] = debug
    kwargs['_env'] = env

    if 'HOME' not in kwargs['_env']:
        kwargs['_env']['HOME'] = os.path.expanduser('~')

    tests = '{}/test_*.py'.format(testinfra_dir)
    tests_glob = sh.glob(tests)

    return sh.testinfra(tests_glob, **kwargs)
Ejemplo n.º 11
0
    def _verify(self):
        validators.check_trailing_cruft(ignore_paths=self._config['ignore_paths'])

        # no tests found
        if not os.path.isdir(self._config['serverspec_dir']) and not os.path.isdir(self._config['testinfra_dir']):
            msg = '{}Skipping tests, could not find {}/ or {}/.{}'
            print(msg.format(Fore.YELLOW, self._config['serverspec_dir'], self._config['testinfra_dir'], Fore.RESET))
            return

        self._write_ssh_config()
        kwargs = {'_env': self._env, '_out': self._print_line, '_err': self._print_line}
        args = []

        # testinfra
        if os.path.isdir(self._config['testinfra_dir']):
            ssh_config = '--ssh-config={0}'.format(self._get_vagrant_ssh_config())
            try:
                output = sh.testinfra(ssh_config, '--sudo', self._config['testinfra_dir'], **kwargs)
                return output.exit_code
            except sh.ErrorReturnCode as e:
                print('ERROR: {}'.format(e))
                sys.exit(e.exit_code)

        # serverspec
        if os.path.isdir(self._config['serverspec_dir']):
            self._rubocop()
            if 'rakefile_file' in self._config:
                kwargs['rakefile'] = self._config['rakefile_file']
            if self._args['--debug']:
                args.append('--trace')
            try:
                rakecmd = sh.Command("rake")
                output = rakecmd(*args, **kwargs)
                return output.exit_code
            except sh.ErrorReturnCode as e:
                print('ERROR: {}'.format(e))
                sys.exit(e.exit_code)
Ejemplo n.º 12
0
        # no tests found
        if not os.path.isdir(self._config['serverspec_dir']) and not os.path.isdir(self._config['testinfra_dir']):
            msg = '{}Skipping tests, could not find {}/ or {}/.{}'
            print(msg.format(Fore.YELLOW, self._config['serverspec_dir'], self._config['testinfra_dir'], Fore.RESET))
            return

        self._write_ssh_config()
        kwargs = {'_env': self._env, '_out': self._print_line, '_err': self._print_line}
        args = []

        # testinfra
        if os.path.isdir(self._config['testinfra_dir']):
            ssh_config = '--ssh-config={0}'.format(self._get_vagrant_ssh_config())
            try:
                output = sh.testinfra(ssh_config, '--sudo', self._config['testinfra_dir'], **kwargs)
                return output.exit_code
            except sh.ErrorReturnCode as e:
                print('ERROR: {}'.format(e))
                sys.exit(e.exit_code)

        # serverspec
        if os.path.isdir(self._config['serverspec_dir']):
            self._rubocop()
            if 'rakefile_file' in self._config:
                kwargs['rakefile'] = self._config['rakefile_file']
            if self._args['--debug']:
                args.append('--trace')
            try:
                rakecmd = sh.Command("rake")
                output = rakecmd(*args, **kwargs)