def test_trace_with_stuck_inprogress(self): output = io.BytesIO() stream = StreamResultToBytes(output) stream.startTestRun() stream.status(test_id='test_passes', test_status='inprogress', timestamp=dt.now(UTC)) stream.status(test_id='test_segfault', test_status='inprogress', timestamp=dt.now(UTC)) stream.status(test_id='test_passes', test_status='success', timestamp=dt.now(UTC)) stream.stopTestRun() output.seek(0) # capture stderr for test stderr = io.StringIO() sys_err = sys.stderr sys.stderr = stderr def restore_stderr(): sys.stderr = sys_err self.addCleanup(restore_stderr) stdin = io.TextIOWrapper(io.BufferedReader(output)) returncode = subunit_trace.trace(stdin, sys.stdout) self.assertEqual(1, returncode) stderr.seek(0) expected = """ The following tests exited without returning a status and likely segfaulted or crashed Python: \t* test_segfault """ self.assertEqual(stderr.read(), expected)
class SubunitContext(): """Context manager for writing subunit results.""" def __init__(self, output_path): self.output_path = output_path def __enter__(self): self.output_stream = open(self.output_path, "wb+") self.output = StreamResultToBytes(self.output_stream) self.output.startTestRun() return self.output def __exit__(self, *args, **kwargs): self.output.stopTestRun() self.output_stream.close()
def test_smoke(self): output = os.path.join(self.useFixture(TempDir()).path, 'output') stdin = io.BytesIO() stdout = io.StringIO() writer = StreamResultToBytes(stdin) writer.startTestRun() writer.status( 'foo', 'success', set(['tag']), file_name='fred', file_bytes=b'abcdefg', eof=True, mime_type='text/plain') writer.stopTestRun() stdin.seek(0) _to_disk.to_disk(['-d', output], stdin=stdin, stdout=stdout) self.expectThat( os.path.join(output, 'foo/test.json'), FileContains( '{"details": ["fred"], "id": "foo", "start": null, ' '"status": "success", "stop": null, "tags": ["tag"]}')) self.expectThat( os.path.join(output, 'foo/fred'), FileContains('abcdefg'))
def test_smoke(self): output = os.path.join(self.useFixture(TempDir()).path, 'output') stdin = io.BytesIO() stdout = io.StringIO() writer = StreamResultToBytes(stdin) writer.startTestRun() writer.status('foo', 'success', set(['tag']), file_name='fred', file_bytes=b'abcdefg', eof=True, mime_type='text/plain') writer.stopTestRun() stdin.seek(0) _to_disk.to_disk(['-d', output], stdin=stdin, stdout=stdout) self.expectThat( os.path.join(output, 'foo/test.json'), FileContains( '{"details": ["fred"], "id": "foo", "start": null, ' '"status": "success", "stop": null, "tags": ["tag"]}')) self.expectThat(os.path.join(output, 'foo/fred'), FileContains('abcdefg'))
browser.close() raise url = 'http://localhost:%i/test/test-runner.html' % port browser.get(url) def close_other_windows(browser, url): for win in browser.window_handles: browser.switch_to_window(win) if browser.current_url != url: browser.close() browser.switch_to_window(browser.window_handles[0]) import time while True: # Sometimes other windows are accidently opened (such as an extension # popup), close them. if len(browser.window_handles) > 1: close_other_windows(browser, url) if not browser.execute_script('return window.finished'): time.sleep(1) continue else: break output.stopTestRun() while args.dontexit: time.sleep(30)
def take_action(self, args): # Setup where the output stream must go if args.output_file == '-': output_stream = sys.stdout else: output_stream = open(args.output_file, 'wb') # Create the output stream output = StreamResultToBytes(output_stream) # Create the test run output.startTestRun() if args.playbook is not None: playbooks = args.playbook results = (models.TaskResult().query.join(models.Task).filter( models.TaskResult.task_id == models.Task.id).filter( models.Task.playbook_id.in_(playbooks))) else: results = models.TaskResult().query.all() for result in results: # Generate a fixed length identifier for the task test_id = utils.generate_identifier(result) # Assign the test_status value if result.status in ('failed', 'unreachable'): if result.ignore_errors is False: test_status = 'xfail' else: test_status = 'fail' elif result.status == 'skipped': test_status = 'skip' else: test_status = 'success' # Determine the play file path if result.task.playbook and result.task.playbook.path: playbook_path = result.task.playbook.path else: playbook_path = '' # Determine the task file path if result.task.file and result.task.file.path: task_path = result.task.file.path else: task_path = '' # Assign the file_bytes value test_data = { 'host': result.host.name, 'playbook_id': result.task.playbook.id, 'playbook_path': playbook_path, 'play_name': result.task.play.name, 'task_action': result.task.action, 'task_action_lineno': result.task.lineno, 'task_id': result.task.id, 'task_name': result.task.name, 'task_path': task_path } file_bytes = encodeutils.safe_encode(jsonutils.dumps(test_data)) # Assign the start_time and stop_time value # The timestamp needs to be an epoch, so we need # to convert it. start_time = datetime.datetime.fromtimestamp( float(result.time_start.strftime('%s'))).replace( tzinfo=iso8601.UTC) end_time = datetime.datetime.fromtimestamp( float(result.time_end.strftime('%s'))).replace( tzinfo=iso8601.UTC) # Output the start of the event output.status(test_id=test_id, timestamp=start_time) # Output the end of the event output.status(test_id=test_id, test_status=test_status, test_tags=None, runnable=False, file_name=test_id, file_bytes=file_bytes, timestamp=end_time, eof=True, mime_type='text/plain; charset=UTF8') output.stopTestRun()
def take_action(self, args): # Setup where the output stream must go if args.output_file == '-': output_stream = sys.stdout else: output_stream = open(args.output_file, 'wb') # Create the output stream output = StreamResultToBytes(output_stream) # Create the test run output.startTestRun() if args.playbook is not None: playbooks = args.playbook results = (models.TaskResult().query .join(models.Task) .filter(models.TaskResult.task_id == models.Task.id) .filter(models.Task.playbook_id.in_(playbooks))) else: results = models.TaskResult().query.all() for result in results: # Generate a fixed length identifier for the task test_id = utils.generate_identifier(result) # Assign the test_status value if result.status in ('failed', 'unreachable'): if result.ignore_errors is False: test_status = 'xfail' else: test_status = 'fail' elif result.status == 'skipped': test_status = 'skip' else: test_status = 'success' # Determine the play file path if result.task.playbook and result.task.playbook.path: playbook_path = result.task.playbook.path else: playbook_path = '' # Determine the task file path if result.task.file and result.task.file.path: task_path = result.task.file.path else: task_path = '' # Assign the file_bytes value test_data = { 'host': result.host.name, 'playbook_id': result.task.playbook.id, 'playbook_path': playbook_path, 'play_name': result.task.play.name, 'task_action': result.task.action, 'task_action_lineno': result.task.lineno, 'task_id': result.task.id, 'task_name': result.task.name, 'task_path': task_path } file_bytes = encodeutils.safe_encode(jsonutils.dumps(test_data)) # Assign the start_time and stop_time value # The timestamp needs to be an epoch, so we need # to convert it. start_time = datetime.datetime.fromtimestamp( float(result.time_start.strftime('%s')) ).replace(tzinfo=iso8601.UTC) end_time = datetime.datetime.fromtimestamp( float(result.time_end.strftime('%s')) ).replace(tzinfo=iso8601.UTC) # Output the start of the event output.status( test_id=test_id, timestamp=start_time ) # Output the end of the event output.status( test_id=test_id, test_status=test_status, test_tags=None, runnable=False, file_name=test_id, file_bytes=file_bytes, timestamp=end_time, eof=True, mime_type='text/plain; charset=UTF8' ) output.stopTestRun()