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)
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
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)
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)
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)
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
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
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
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
""" # remove blank lines to make regex matches easier output = re.sub("\n\s*\n*", "\n", output) # look for any non-zero changed lines changed = re.search(r'(changed=[1-9][0-9]*)', output) if changed: return False return True def _verify(self): <<<<<<< HEAD validators.check_trailing_cruft(ignore_paths=self._config['ignore_paths']) ======= self._trailing_validators() <<<<<<< HEAD <<<<<<< HEAD >>>>>>> ac8bbd23cccd6487b2a9b73ced5182ae22376c7d ======= >>>>>>> initial commit >>>>>>> initialized ======= >>>>>>> ac8bbd23cccd6487b2a9b73ced5182ae22376c7d >>>>>>> master # 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 {}/.{}'