def test_run_execute_failure(mock_method, execute_task): mock_method.return_value = 'Traceback' mock_executor = mock.MagicMock(run=mock.MagicMock( side_effect=TefloExecuteError('e'))) execute_task.executor = mock_executor with pytest.raises(TefloExecuteError): execute_task.run()
def __git__(self): self.status = self.ans_service.run_git_playbook(self.git) if self.status != 0: raise TefloExecuteError('Failed to clone git repositories!')
def __artifacts__(self): """Archive artifacts produced by the tests. This method takes a string formatted playbook, writes it to disk, provides the test artifacts details to the playbook and runs it. The result is on the machine where teflo is run, all test artifacts will be archived inside the data folder. Example artifacts archive structure: artifacts/ host_01/ test_01_output.log results/ .. host_02/ test_01_output.log results/ .. """ # local path on disk to save artifacts destination = self.config['ARTIFACT_FOLDER'] self.logger.info('Fetching test artifacts @ %s' % destination) artifact_location = list() # settings required by synchronize module os.environ['ANSIBLE_LOCAL_TEMP'] = '$HOME/.ansible/tmp' os.environ['ANSIBLE_REMOTE_TEMP'] = '$HOME/.ansible/tmp' # setting variable so to no display any skipped tasks os.environ['DISPLAY_SKIPPED_HOSTS'] = 'False' results = self.ans_service.run_artifact_playbook( destination, self.artifacts) if results[0] != 0: self.logger.error(results[1]) raise TefloExecuteError('A failure occurred while trying to copy ' 'test artifacts.') # Get results from file try: with open('sync-results-' + self.ans_service.uid + '.txt') as fp: lines = fp.read().splitlines() except (IOError, OSError) as ex: self.logger.error(ex) raise TefloExecuteError( 'Failed to find the sync-results.txt file ' 'which means there was an uncaught failure running ' 'the synchronization playbook. Please enable verbose Ansible ' 'logging in the teflo.cfg file and try again.') # Build Results sync_results = [] for line in lines: host, artifact, dest, skipped, rc = ast.literal_eval( textwrap.dedent(line).strip()) sync_results.append({ 'host': host, 'artifact': artifact, 'destination': dest, 'skipped': skipped, 'rc': rc }) # remove Sync Results file os.remove('sync-results-' + self.ans_service.uid + '.txt') for r in sync_results: if r['rc'] != 0 and not r['skipped']: # checking if exit on error is set to true in teflo.cfg file if self.config.get('RUNNER_EXIT_ON_ERROR', 'False').lower() == 'true': raise TefloExecuteError( 'Failed to copy the artifact(s), %s, from %s' % (r['artifact'], r['host'])) else: self.logger.error( 'Failed to copy the artifact(s), %s, from %s' % (r['artifact'], r['host'])) if r['rc'] == 0 and not r['skipped']: temp_list = r['artifact'].replace('[', '').replace(']', '').replace( "'", "").split(',') res_folder_parts = self.config['RESULTS_FOLDER'].split('/') dest_path_parts = r['destination'].split('/') if not self.ans_service.ans_extra_vars['localhost']: art_list = [a[11:] for a in temp_list if 'cd+' not in a] path = '/'.join(r['destination'].split('/')[-3:]) else: path = '/'.join( r['destination'].split('/')[len(res_folder_parts):-1]) art_list = [ '/'.join( a.replace('’', "").split('->')[-1].split('/')[( len(dest_path_parts) - 1):]) for a in temp_list ] self.logger.info('Copied the artifact(s), %s, from %s' % (art_list, r['host'])) # Adding the only the artifacts which are not already present for artifact in art_list: art = os.path.join(path, artifact) if art not in artifact_location: artifact_location.append(art) if r['skipped']: self.logger.warning( 'Could not find artifact(s), %s, on %s. Make sure the file exists ' 'and defined properly in the definition file.' % (r['artifact'], r['host'])) # Update the execute resource with the location of artifacts if self.execute.artifact_locations: for item in artifact_location: if item not in self.execute.artifact_locations: self.execute.artifact_locations.append(item) else: self.execute.artifact_locations = artifact_location if self.config.get('RUNNER_TESTRUN_RESULTS') and self.config.get( 'RUNNER_TESTRUN_RESULTS').lower() == 'false': self.execute.testrun_results = {} else: self.execute.testrun_results = create_testrun_results( self.injector.inject_list(self.execute.artifact_locations), self.config) # printing out the testrun results on the console self._print_testrun_results()
def test_teflo_execute_error(): with pytest.raises(TefloExecuteError): raise TefloExecuteError('error message')