Beispiel #1
0
def test_rubocop(mocker):
    mocked = mocker.patch('molecule.validators.rubocop')
    args = ['/tmp']
    kwargs = {'pattern': '**/**/**/*', 'out': '/dev/null', 'err': None}
    validators.rubocop(*args, **kwargs)

    mocked.assert_called_once_with(*args, **kwargs)
Beispiel #2
0
    def execute(self, exit=True):
        if self.static:
            self.disabled('verify')

        serverspec_dir = self.molecule._config.config['molecule']['serverspec_dir']
        testinfra_dir = self.molecule._config.config['molecule']['testinfra_dir']
        inventory_file = self.molecule._config.config['ansible']['inventory_file']
        rakefile = self.molecule._config.config['molecule']['rakefile_file']
        ignore_paths = self.molecule._config.config['molecule']['ignore_paths']

        # whitespace & trailing newline check
        validators.check_trailing_cruft(ignore_paths=ignore_paths, exit=exit)

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

        self.molecule._write_ssh_config()
        kwargs = {'env': self.molecule._env}
        kwargs['env']['PYTHONDONTWRITEBYTECODE'] = '1'
        kwargs['debug'] = True if self.molecule._args.get('--debug') else False

        try:
            # testinfra
            if os.path.isdir(testinfra_dir):
                msg = '\n{}Executing testinfra tests found in {}/.{}'
                print(msg.format(Fore.MAGENTA, testinfra_dir, Fore.RESET))
                validators.testinfra(inventory_file, testinfra_dir, **kwargs)
                print()
            else:
                msg = '{}No testinfra tests found in {}/.\n{}'
                print(msg.format(Fore.YELLOW, testinfra_dir, Fore.RESET))

            # serverspec / rubocop
            if os.path.isdir(serverspec_dir):
                msg = '{}Executing rubocop on *.rb files found in {}/.{}'
                print(msg.format(Fore.MAGENTA, serverspec_dir, Fore.RESET))
                validators.rubocop(serverspec_dir, **kwargs)
                print()

                msg = '{}Executing serverspec tests found in {}/.{}'
                print(msg.format(Fore.MAGENTA, serverspec_dir, Fore.RESET))
                validators.rake(rakefile, **kwargs)
                print()
            else:
                msg = '{}No serverspec tests found in {}/.\n{}'
                print(msg.format(Fore.YELLOW, serverspec_dir, Fore.RESET))
        except sh.ErrorReturnCode as e:
            print('ERROR: {}'.format(e))
            if exit:
                sys.exit(e.exit_code)
            return e.exit_code, None

        return None, None
Beispiel #3
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')

        serverspec_dir = self.molecule._config.config['molecule']['serverspec_dir']
        testinfra_dir = self.molecule._config.config['molecule']['testinfra_dir']
        inventory_file = self.molecule._config.config['ansible']['inventory_file']
        rakefile = self.molecule._config.config['molecule']['rakefile_file']
        ignore_paths = self.molecule._config.config['molecule']['ignore_paths']

        # whitespace & trailing newline check
        validators.check_trailing_cruft(ignore_paths=ignore_paths)

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

        self.molecule._write_ssh_config()
        kwargs = {'env': self.molecule._env}
        kwargs['env']['PYTHONDONTWRITEBYTECODE'] = '1'
        kwargs['debug'] = True if self.molecule._args['--debug'] else False

        try:
            # testinfra
            if os.path.isdir(testinfra_dir):
                validators.testinfra(inventory_file, **kwargs)

            # serverspec / rubocop
            if os.path.isdir(serverspec_dir):
                validators.rubocop(serverspec_dir, **kwargs)
                validators.rake(rakefile, **kwargs)
        except sh.ErrorReturnCode as e:
            print('ERROR: {}'.format(e))
            sys.exit(e.exit_code)
Beispiel #4
0
    def execute(self):
        if self.static:
            self.disabled('verify')

        serverspec_dir = self.molecule._config.config['molecule']['serverspec_dir']
        testinfra_dir = self.molecule._config.config['molecule']['testinfra_dir']
        inventory_file = self.molecule._config.config['ansible']['inventory_file']
        rakefile = self.molecule._config.config['molecule']['rakefile_file']
        ignore_paths = self.molecule._config.config['molecule']['ignore_paths']

        # whitespace & trailing newline check
        validators.check_trailing_cruft(ignore_paths=ignore_paths)

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

        self.molecule._write_ssh_config()
        kwargs = {'env': self.molecule._env}
        kwargs['env']['PYTHONDONTWRITEBYTECODE'] = '1'
        kwargs['debug'] = True if self.molecule._args.get('--debug') else False

        try:
            # testinfra
            if os.path.isdir(testinfra_dir):
                validators.testinfra(inventory_file, **kwargs)
            else:
                msg = '{}No testinfra tests found in {}/. {}'
                print(msg.format(Fore.YELLOW, testinfra_dir, Fore.RESET))

            # serverspec / rubocop
            if os.path.isdir(serverspec_dir):
                validators.rubocop(serverspec_dir, **kwargs)
                validators.rake(rakefile, **kwargs)
            else:
                msg = '{}No serverspec tests found in {}/. {}'
                print(msg.format(Fore.YELLOW, serverspec_dir, Fore.RESET))
        except sh.ErrorReturnCode as e:
            print('ERROR: {}'.format(e))
            sys.exit(e.exit_code)
Beispiel #5
0
    def execute(self, exit=True):
        if self.static:
            self.disabled('verify')

        serverspec_dir = self.molecule._config.config['molecule'][
            'serverspec_dir']
        testinfra_dir = self.molecule._config.config['molecule'][
            'testinfra_dir']
        rakefile = self.molecule._config.config['molecule']['rakefile_file']
        ignore_paths = self.molecule._config.config['molecule']['ignore_paths']

        # whitespace & trailing newline check
        validators.check_trailing_cruft(ignore_paths=ignore_paths, exit=exit)

        # no serverspec or testinfra
        if not os.path.isdir(serverspec_dir) and not os.path.isdir(
                testinfra_dir):
            msg = '{}Skipping tests, could not find {}/ or {}/.{}'
            utilities.logger.warning(msg.format(colorama.Fore.YELLOW,
                                                serverspec_dir, testinfra_dir,
                                                colorama.Fore.RESET))
            return None, None

        self.molecule._write_ssh_config()

        # testinfra's Ansible calls get same env vars as ansible-playbook
        ansible = AnsiblePlaybook(self.molecule._config.config['ansible'],
                                  _env=self.molecule._env)

        testinfra_kwargs = self.molecule._provisioner.testinfra_args
        serverspec_kwargs = self.molecule._provisioner.serverspec_args
        testinfra_kwargs['env'] = ansible.env
        testinfra_kwargs['env']['PYTHONDONTWRITEBYTECODE'] = '1'
        testinfra_kwargs['debug'] = True if self.molecule._args.get(
            '--debug') else False
        testinfra_kwargs['sudo'] = True if self.molecule._args.get(
            '--sudo') else False
        serverspec_kwargs['env'] = testinfra_kwargs['env']
        serverspec_kwargs['debug'] = testinfra_kwargs['debug']

        try:
            # testinfra
            if len(glob.glob1(testinfra_dir, "test_*.py")) > 0:
                msg = '\n{}Executing testinfra tests found in {}/.{}'
                print(msg.format(colorama.Fore.MAGENTA, testinfra_dir,
                                 colorama.Fore.RESET))
                validators.testinfra(testinfra_dir, **testinfra_kwargs)
                print()
            else:
                msg = '{}No testinfra tests found in {}/.\n{}'
                utilities.logger.warning(msg.format(
                    colorama.Fore.YELLOW, testinfra_dir, colorama.Fore.RESET))

            # serverspec / rubocop
            if os.path.isdir(serverspec_dir):
                msg = '{}Executing rubocop on *.rb files found in {}/.{}'
                print(msg.format(colorama.Fore.MAGENTA, serverspec_dir,
                                 colorama.Fore.RESET))
                validators.rubocop(serverspec_dir, **serverspec_kwargs)
                print()

                msg = '{}Executing serverspec tests found in {}/.{}'
                print(msg.format(colorama.Fore.MAGENTA, serverspec_dir,
                                 colorama.Fore.RESET))
                validators.rake(rakefile, **serverspec_kwargs)
                print()
            else:
                msg = '{}No serverspec tests found in {}/.\n{}'
                utilities.logger.warning(msg.format(
                    colorama.Fore.YELLOW, serverspec_dir, colorama.Fore.RESET))
        except sh.ErrorReturnCode as e:
            utilities.logger.error('ERROR: {}'.format(e))
            if exit:
                sys.exit(e.exit_code)
            return e.exit_code, e.stdout

        return None, None
Beispiel #6
0
    def execute(self, exit=True):
        if self.static:
            self.disabled('verify')

        serverspec_dir = self.molecule._config.config['molecule'][
            'serverspec_dir']
        testinfra_dir = self.molecule._config.config['molecule'][
            'testinfra_dir']
        inventory_file = self.molecule._config.config['ansible'][
            'inventory_file']
        rakefile = self.molecule._config.config['molecule']['rakefile_file']
        ignore_paths = self.molecule._config.config['molecule']['ignore_paths']

        # whitespace & trailing newline check
        validators.check_trailing_cruft(ignore_paths=ignore_paths, exit=exit)

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

        self.molecule._write_ssh_config()
        # testinfra's Ansible calls get same env vars as ansible-playbook
        ansible = AnsiblePlaybook(self.molecule._config.config['ansible'],
                                  _env=self.molecule._env)
        kwargs = {'env': ansible.env}
        kwargs['env']['PYTHONDONTWRITEBYTECODE'] = '1'
        kwargs['debug'] = True if self.molecule._args.get('--debug') else False

        try:
            # testinfra
            if os.path.isdir(testinfra_dir):
                msg = '\n{}Executing testinfra tests found in {}/.{}'
                print(msg.format(Fore.MAGENTA, testinfra_dir, Fore.RESET))
                validators.testinfra(inventory_file, testinfra_dir, **kwargs)
                print()
            else:
                msg = '{}No testinfra tests found in {}/.\n{}'
                print(msg.format(Fore.YELLOW, testinfra_dir, Fore.RESET))

            # serverspec / rubocop
            if os.path.isdir(serverspec_dir):
                msg = '{}Executing rubocop on *.rb files found in {}/.{}'
                print(msg.format(Fore.MAGENTA, serverspec_dir, Fore.RESET))
                validators.rubocop(serverspec_dir, **kwargs)
                print()

                msg = '{}Executing serverspec tests found in {}/.{}'
                print(msg.format(Fore.MAGENTA, serverspec_dir, Fore.RESET))
                validators.rake(rakefile, **kwargs)
                print()
            else:
                msg = '{}No serverspec tests found in {}/.\n{}'
                print(msg.format(Fore.YELLOW, serverspec_dir, Fore.RESET))
        except sh.ErrorReturnCode as e:
            print('ERROR: {}'.format(e))
            if exit:
                sys.exit(e.exit_code)
            return e.exit_code, None

        return None, None
Beispiel #7
0
    def execute(self, exit=True):
        if self.static:
            self.disabled('verify')

        serverspec_dir = self.molecule._config.config['molecule'][
            'serverspec_dir']
        testinfra_dir = self.molecule._config.config['molecule'][
            'testinfra_dir']
        rakefile = self.molecule._config.config['molecule']['rakefile_file']
        ignore_paths = self.molecule._config.config['molecule']['ignore_paths']

        # whitespace & trailing newline check
        validators.check_trailing_cruft(ignore_paths=ignore_paths, exit=exit)

        self.molecule._write_ssh_config()

        # testinfra's Ansible calls get same env vars as ansible-playbook
        ansible = ansible_playbook.AnsiblePlaybook(
            self.molecule._config.config['ansible'],
            _env=self.molecule._env)

        debug = self.molecule._args.get('--debug', False)

        testinfra_kwargs = utilities.merge_dicts(
            self.molecule._provisioner.testinfra_args,
            self.molecule._config.config['testinfra'])
        testinfra_kwargs['env'] = ansible.env
        testinfra_kwargs['env']['PYTHONDONTWRITEBYTECODE'] = '1'
        testinfra_kwargs['debug'] = debug
        testinfra_kwargs['sudo'] = self.molecule._args.get('--sudo', False)

        serverspec_kwargs = self.molecule._provisioner.serverspec_args
        serverspec_kwargs['debug'] = debug

        try:
            # testinfra
            tests = '{}/test_*.py'.format(testinfra_dir)
            tests_glob = glob.glob(tests)
            if len(tests_glob) > 0:
                msg = 'Executing testinfra tests found in {}/.'
                utilities.print_info(msg.format(testinfra_dir))
                validators.testinfra(tests_glob, **testinfra_kwargs)
            else:
                msg = 'No testinfra tests found in {}/.\n'
                utilities.logger.warning(msg.format(testinfra_dir))

            # serverspec / rubocop
            if os.path.isdir(serverspec_dir):
                msg = 'Executing rubocop on *.rb files found in {}/.'
                utilities.print_info(msg.format(serverspec_dir))
                validators.rubocop(serverspec_dir, **serverspec_kwargs)

                msg = 'Executing serverspec tests found in {}/.'
                utilities.print_info(msg.format(serverspec_dir))
                validators.rake(rakefile, **serverspec_kwargs)
            else:
                msg = 'No serverspec tests found in {}/.\n'
                utilities.logger.warning(msg.format(serverspec_dir))
        except sh.ErrorReturnCode as e:
            utilities.logger.error('ERROR: {}'.format(e))
            if exit:
                utilities.sysexit(e.exit_code)
            return e.exit_code, e.stdout

        return None, None
Beispiel #8
0
    def execute(self, exit=True):
        if self.static:
            self.disabled('verify')

        serverspec_dir = self.molecule._config.config['molecule'][
            'serverspec_dir']
        testinfra_dir = self.molecule._config.config['molecule'][
            'testinfra_dir']
        rakefile = self.molecule._config.config['molecule']['rakefile_file']
        ignore_paths = self.molecule._config.config['molecule']['ignore_paths']

        # whitespace & trailing newline check
        validators.check_trailing_cruft(ignore_paths=ignore_paths, exit=exit)

        # no serverspec or testinfra
        if not os.path.isdir(serverspec_dir) and not os.path.isdir(
                testinfra_dir):
            msg = '{}Skipping tests, could not find {}/ or {}/.{}'
            utilities.logger.warning(
                msg.format(colorama.Fore.YELLOW, serverspec_dir, testinfra_dir,
                           colorama.Fore.RESET))
            return None, None

        self.molecule._write_ssh_config()

        # testinfra's Ansible calls get same env vars as ansible-playbook
        ansible = AnsiblePlaybook(self.molecule._config.config['ansible'],
                                  _env=self.molecule._env)

        testinfra_kwargs = self.molecule._provisioner.testinfra_args
        serverspec_kwargs = self.molecule._provisioner.serverspec_args
        testinfra_kwargs['env'] = ansible.env
        testinfra_kwargs['env']['PYTHONDONTWRITEBYTECODE'] = '1'
        testinfra_kwargs['debug'] = True if self.molecule._args.get(
            '--debug') else False
        testinfra_kwargs['sudo'] = True if self.molecule._args.get(
            '--sudo') else False
        serverspec_kwargs['env'] = testinfra_kwargs['env']
        serverspec_kwargs['debug'] = testinfra_kwargs['debug']

        try:
            # testinfra
            if len(glob.glob1(testinfra_dir, "test_*.py")) > 0:
                msg = '\n{}Executing testinfra tests found in {}/.{}'
                print(
                    msg.format(colorama.Fore.MAGENTA, testinfra_dir,
                               colorama.Fore.RESET))
                validators.testinfra(testinfra_dir, **testinfra_kwargs)
                print()
            else:
                msg = '{}No testinfra tests found in {}/.\n{}'
                utilities.logger.warning(
                    msg.format(colorama.Fore.YELLOW, testinfra_dir,
                               colorama.Fore.RESET))

            # serverspec / rubocop
            if os.path.isdir(serverspec_dir):
                msg = '{}Executing rubocop on *.rb files found in {}/.{}'
                print(
                    msg.format(colorama.Fore.MAGENTA, serverspec_dir,
                               colorama.Fore.RESET))
                validators.rubocop(serverspec_dir, **serverspec_kwargs)
                print()

                msg = '{}Executing serverspec tests found in {}/.{}'
                print(
                    msg.format(colorama.Fore.MAGENTA, serverspec_dir,
                               colorama.Fore.RESET))
                validators.rake(rakefile, **serverspec_kwargs)
                print()
            else:
                msg = '{}No serverspec tests found in {}/.\n{}'
                utilities.logger.warning(
                    msg.format(colorama.Fore.YELLOW, serverspec_dir,
                               colorama.Fore.RESET))
        except sh.ErrorReturnCode as e:
            utilities.logger.error('ERROR: {}'.format(e))
            if exit:
                sys.exit(e.exit_code)
            return e.exit_code, e.stdout

        return None, None
Beispiel #9
0
 def test_rubocop(self, mocked):
     args = ['/tmp']
     kwargs = {'pattern': '**/**/**/*', 'out': '/dev/null', 'err': None}
     validators.rubocop(*args, **kwargs)
     mocked.assert_called_once_with(*args, **kwargs)