def create_run(): try: url = request.args.get('build_url') user = request.args.get('user') password = request.args.get('password') except KeyError as err: return 'Wrong url parameters! \nExpecting %s from jenkins.' % err if url.endswith('/'): url = url[:-1] # make configurable try: json_resp = requests_get('%s/api/json' % url, auth=(user, password)) subunit_data = requests_get('%s/artifact/subunit.stream' % url, auth=(user, password)) except ValueError as err: return 'Failed getting build info... %s' % err try: url = get_url(json_resp) except ValueError as err: return 'Looks like your test json data is bad: %s' % err try: subunit_file = io.BytesIO(subunit_data.content) except ValueError as err: return 'Looks like your subunit stream is bad: %s' % err set_artifacts_link(url) set_metadata(url) stream = read_subunit.ReadSubunit(subunit_file) shell.process_results(stream.get_results()) return "success for %s" % url
def test_process_results_no_results(self): fake_run_time = 'run time' fake_results = dict(run_time=fake_run_time) fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Setup Configuration fake_run_at = '2016-08-17 10:58:00.000' cfg.CONF.set_override(name='run_at', override=fake_run_at) fake_artifacts = 'artifacts' cfg.CONF.set_override(name='artifacts', override=fake_artifacts) fake_run_id = 'run_id' cfg.CONF.set_override(name='run_id', override=fake_run_id) fake_run_meta = {'run_meta': 'value'} cfg.CONF.set_override(name='run_meta', override=fake_run_meta) # Run process_results shell.process_results(fake_results) self.db_api_mock.get_session.assert_called_once() expected_run_at = date_parser.parse(fake_run_at) self.db_api_mock.create_run.assert_called_once_with( self.fake_totals['skips'], self.fake_totals['fails'], self.fake_totals['success'], fake_run_time, fake_artifacts, id=fake_run_id, run_at=expected_run_at, session=self.fake_session) self.db_api_mock.add_run_metadata.assert_called_once_with( fake_run_meta, fake_db_run_id, self.fake_session) self.fake_session.close.assert_called_once()
def test_process_results_existing_tests_invalid_status(self): fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.run_count = 3 # Setup results fake_results = dict(test1={'status': 'invalid', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': None}) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test with testtools.ExpectedException(exceptions.UnknownStatus, '^.*\n.*%s$' % 'invalid'): # Run process_results shell.process_results(fake_results_all) # Check that we lookup all tests in the DB expected_test_by_id_calls = [mock.call(x, self.fake_session) for x in fake_results] self.db_api_mock.get_test_by_test_id.assert_has_calls( expected_test_by_id_calls, any_order=True) # Check that a run is created in the DB self.db_api_mock.create_run.assert_called_once_with( self.fake_totals['skips'], self.fake_totals['fails'], self.fake_totals['success'], fake_run_time, None, id=None, run_at=None, session=self.fake_session) # Check no test run was added to the DB self.db_api_mock.create_test_run.assert_not_called()
def test_process_results_no_results(self): fake_run_time = 'run time' fake_results = dict(run_time=fake_run_time) fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Setup Configuration fake_run_at = '2016-08-17 10:58:00.000' cfg.CONF.set_override(name='run_at', override=fake_run_at) fake_artifacts = 'artifacts' cfg.CONF.set_override(name='artifacts', override=fake_artifacts) fake_run_id = 'run_id' cfg.CONF.set_override(name='run_id', override=fake_run_id) fake_run_meta = {'run_meta': 'value'} cfg.CONF.set_override(name='run_meta', override=fake_run_meta) # Run process_results shell.process_results(fake_results) self.db_api_mock.get_session.assert_called_once() expected_run_at = date_parser.parse(fake_run_at) self.db_api_mock.create_run.assert_called_once_with( self.fake_totals['skips'], self.fake_totals['fails'], self.fake_totals['success'], fake_run_time, fake_artifacts, id=fake_run_id, run_at=expected_run_at, session=self.fake_session) self.db_api_mock.add_run_metadata.assert_called_once_with( fake_run_meta, fake_db_run_id, self.fake_session) self.fake_session.close.assert_called_once()
def _write_to_db(self, subunit): subunit_v2 = subunit.pop('subunit') # Set run metadata from gearman log_url = subunit.pop('log_url', None) subunit = self._uniquify_name(subunit) if log_url: log_dir = os.path.dirname(log_url) # log_dir should be the top-level directory containing a job run, # but the subunit file may be nested in 0 - 2 subdirectories (top, # logs/, or logs/old/), so we need to safely correct the path here log_base = os.path.basename(log_dir) if log_base == 'logs': log_dir = os.path.dirname(log_dir) elif log_base == 'old': log_dir = os.path.dirname(os.path.dirname(log_dir)) shell.CONF.set_override('artifacts', log_dir) shell.CONF.set_override('run_meta', subunit) # Parse subunit stream and store in DB if subunit_v2.closed: logging.debug('Trying to convert closed subunit v2 stream: %s to ' 'SQL' % subunit_v2) else: logging.debug('Converting Subunit V2 stream: %s to SQL' % subunit_v2) stream = read_subunit.ReadSubunit(subunit_v2, targets=self.extra_targets, use_wall_time=True) results = stream.get_results() start_time = sorted( [results[x]['start_time'] for x in results if x != 'run_time'])[0] shell.CONF.set_override('run_at', start_time.isoformat()) shell.process_results(results) subunit_v2.close()
def test_process_results_test_metadata_new_remove_prefix(self): # Setup the metadata prefix configuration fake_meta_prefix = '_meta_' cfg.CONF.set_override(name='test_attr_prefix', override=fake_meta_prefix) cfg.CONF.set_override(name='remove_test_attr_prefix', override=True) fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results expected_metadata_list = ['a', 'b'] extra_metadata_list = ['c', 'd'] full_metadata_list = [ fake_meta_prefix + x for x in expected_metadata_list ] + extra_metadata_list fake_results = dict( test1={ 'status': 'fail', 'start_time': 0, 'end_time': 1, 'metadata': { 'attrs': ','.join(full_metadata_list) }, 'attachments': None }) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Mock create test run fake_db_test_run_id = 'test run id' fake_db_test_run = mock.Mock(name='db test run') fake_db_test_run.id = fake_db_test_run_id self.db_api_mock.create_test_run.return_value = fake_db_test_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Test metadata is not found in the DB self.db_api_mock.get_test_metadata.return_value = [] # Run process_results shell.process_results(fake_results_all) # Check only matching metadata is added to test_metadata expected_add_test_metadata_calls = [ mock.call({'attr': x}, fake_db_test.id, session=self.fake_session) for x in expected_metadata_list ] self.db_api_mock.add_test_metadata.assert_has_calls( expected_add_test_metadata_calls, any_order=True) # Check all metadata is added to test_run_metadata self.db_api_mock.add_test_run_metadata.assert_has_calls([ mock.call(fake_results['test1']['metadata'], fake_db_test_run_id, self.fake_session) ]) self.fake_session.close.assert_called_once()
def test_process_results_test_metadata_existing_remove_prefix(self): # Setup the metadata prefix configuration fake_meta_prefix = '_meta_' cfg.CONF.set_override(name='test_attr_prefix', override=fake_meta_prefix) cfg.CONF.set_override(name='remove_test_attr_prefix', override=True) fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results expected_metadata_list = ['a', 'b'] existing_metadata_list = ['c', 'd'] full_metadata_list = [fake_meta_prefix + x for x in ( expected_metadata_list + existing_metadata_list)] fake_results = dict( test1={'status': 'fail', 'start_time': 0, 'end_time': 1, 'metadata': {'attrs': ','.join(full_metadata_list)}, 'attachments': None}) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Mock create test run fake_db_test_run_id = 'test run id' fake_db_test_run = mock.Mock(name='db test run') fake_db_test_run.id = fake_db_test_run_id self.db_api_mock.create_test_run.return_value = fake_db_test_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Test metadata is found in the DB test_metadata_list = [] for value in existing_metadata_list: test_metadata = mock.Mock() test_metadata.key = 'attr' test_metadata.value = value test_metadata_list.append(test_metadata) self.db_api_mock.get_test_metadata.return_value = test_metadata_list # Run process_results shell.process_results(fake_results_all) # Check only matching metadata is added to test_metadata expected_add_test_metadata_calls = [ mock.call({'attr': x}, fake_db_test.id, session=self.fake_session) for x in expected_metadata_list] self.db_api_mock.add_test_metadata.assert_has_calls( expected_add_test_metadata_calls, any_order=True) # Check all metadata is added to test_run_metadata self.db_api_mock.add_test_run_metadata.assert_has_calls([ mock.call(fake_results['test1']['metadata'], fake_db_test_run_id, self.fake_session)]) self.fake_session.close.assert_called_once()
def test_process_results_no_conf(self): fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results fake_results = dict(test1={'status': 'success', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': None}, test2={'status': 'fail', 'start_time': 0, 'end_time': 2, 'metadata': None, 'attachments': None}, test3={'status': 'skip', 'start_time': 0, 'end_time': 3, 'metadata': None, 'attachments': None}) fake_results_cpy = copy.deepcopy(fake_results) fake_results_cpy['run_time'] = fake_run_time # create mocked resources to use in testing fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # define config objects to use in our function call fake_run_at = '2018-04-03 00:01:00.000' fake_artifacts = 'artifacts' fake_run_id = 'run_id' fake_run_meta = {'run_meta': 'value'} # Run process_results shell.process_results(fake_results_cpy, run_at=fake_run_at, artifacts=fake_artifacts, run_id=fake_run_id, run_meta=fake_run_meta) # Check that we lookup all tests in the DB expected_test_by_id_calls = [mock.call(x, self.fake_session) for x in fake_results] self.db_api_mock.get_test_by_test_id.assert_has_calls( expected_test_by_id_calls, any_order=True) # Check that a test_run for each test is created in the DB expected_create_test_run_calls = [ mock.call(fake_db_test.id, fake_db_run_id, fake_results[x]['status'], fake_results[x]['start_time'], fake_results[x]['end_time'], self.fake_session) for x in fake_results] self.db_api_mock.create_test_run.assert_has_calls( expected_create_test_run_calls, any_order=True) self.fake_session.close.assert_called_once()
def handle_subunit_event(self): # Pull subunit event from queue and separate stream from metadata subunit = self.subunitq.get() subunit_v2 = subunit.pop('subunit') # Set run metadata from gearman log_url = subunit.pop('log_url', None) if log_url: log_dir = os.path.dirname(os.path.dirname(log_url)) shell.CONF.set_override('artifacts', log_dir) shell.CONF.set_override('run_meta', subunit) # Parse subunit stream and store in DB logging.debug('Converting Subunit V2 stream to SQL') stream = read_subunit.ReadSubunit(subunit_v2) shell.process_results(stream.get_results())
def test_process_results_new_tests(self, get_duration_mock): fake_run_time = 'run time' fake_results = dict(test1={'status': 'success', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': None}, test2={'status': 'fail', 'start_time': 0, 'end_time': 2, 'metadata': None, 'attachments': None}, test3={'status': 'skip', 'start_time': 0, 'end_time': 3, 'metadata': None, 'attachments': None}) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create_run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Tests are not found in the DB self.db_api_mock.get_test_by_test_id.return_value = None # Mock create test get_duration_mock.return_value = 'fake_duration' fake_db_test_id = 'test_db_id' fake_test_create = mock.Mock(name='db test') fake_test_create.id = fake_db_test_id self.db_api_mock.create_test.return_value = fake_test_create # Run process_results shell.process_results(fake_results_all) # Check that we lookup all tests in the DB expected_test_by_id_calls = [mock.call(x, self.fake_session) for x in fake_results] self.db_api_mock.get_test_by_test_id.assert_has_calls( expected_test_by_id_calls, any_order=True) # Check that all tests are created in the DB expected_create_test_calls = [ mock.call('test1', 1, 1, 0, 'fake_duration', self.fake_session), mock.call('test2', 1, 0, 1, 'fake_duration', self.fake_session), mock.call('test3', 0, 0, 0, 'fake_duration', self.fake_session)] self.db_api_mock.create_test.assert_has_calls( expected_create_test_calls, any_order=True) # Check that a test_run for each test is created in the DB expected_create_test_run_calls = [ mock.call(fake_db_test_id, fake_db_run_id, fake_results[x]['status'], fake_results[x]['start_time'], fake_results[x]['end_time'], self.fake_session) for x in fake_results] self.db_api_mock.create_test_run.assert_has_calls( expected_create_test_run_calls, any_order=True) self.fake_session.close.assert_called_once()
def handle_subunit_event(self): # Pull subunit event from queue and separate stream from metadata subunit = self.subunitq.get() subunit_v2 = subunit.pop('subunit') # Set run metadata from gearman log_url = subunit.pop('log_url', None) if log_url: log_dir = os.path.dirname(os.path.dirname(log_url)) shell.CONF.set_override('artifacts', log_dir) run_id = subunit.get('build_uuid', None) if run_id: shell.CONF.set_override('run_id', run_id) shell.CONF.set_override('run_meta', subunit) # Parse subunit stream and store in DB logging.debug('Converting Subunit V2 stream to SQL') stream = read_subunit.ReadSubunit(subunit_v2) shell.process_results(stream.get_results())
def test_process_result_test_metadata_no_prefix(self): # Setup the metadata prefix configuration cfg.CONF.set_override(name='test_attr_prefix', override='') fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results expected_metadata_list = ['a', 'b', 'c'] fake_results = dict( test1={ 'status': 'fail', 'start_time': 0, 'end_time': 1, 'metadata': { 'attrs': ','.join(expected_metadata_list) }, 'attachments': None }) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Mock create test run fake_db_test_run_id = 'test run id' fake_db_test_run = mock.Mock(name='db test run') fake_db_test_run.id = fake_db_test_run_id self.db_api_mock.create_test_run.return_value = fake_db_test_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Run process_results shell.process_results(fake_results_all) # Check test metadata is not added self.db_api_mock.add_test_metadata.assert_not_called() # Check all metadata is added to test_run_metadata self.db_api_mock.add_test_run_metadata.assert_has_calls([ mock.call(fake_results['test1']['metadata'], fake_db_test_run_id, self.fake_session) ]) self.fake_session.close.assert_called_once()
def test_process_result_test_metadata_no_prefix(self): # Setup the metadata prefix configuration cfg.CONF.set_override(name='test_attr_prefix', override='') fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results expected_metadata_list = ['a', 'b', 'c'] fake_results = dict( test1={'status': 'fail', 'start_time': 0, 'end_time': 1, 'metadata': {'attrs': ','.join(expected_metadata_list)}, 'attachments': None}) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Mock create test run fake_db_test_run_id = 'test run id' fake_db_test_run = mock.Mock(name='db test run') fake_db_test_run.id = fake_db_test_run_id self.db_api_mock.create_test_run.return_value = fake_db_test_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Run process_results shell.process_results(fake_results_all) # Check test metadata is not added self.db_api_mock.add_test_metadata.assert_not_called() # Check all metadata is added to test_run_metadata self.db_api_mock.add_test_run_metadata.assert_has_calls([ mock.call(fake_results['test1']['metadata'], fake_db_test_run_id, self.fake_session)]) self.fake_session.close.assert_called_once()
def test_process_result_test_run_attachments(self): fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results fake_attachment = 'some text' fake_results = dict( test1={ 'status': 'fail', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': fake_attachment }) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Mock create test run fake_db_test_run_id = 'test run id' fake_db_test_run = mock.Mock(name='db test run') fake_db_test_run.id = fake_db_test_run_id self.db_api_mock.create_test_run.return_value = fake_db_test_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Run process_results shell.process_results(fake_results_all) # Check attachments are added to the test run self.db_api_mock.add_test_metadata.assert_not_called() # Check all metadata is added to test_run_metadata self.db_api_mock.add_test_run_attachments.assert_has_calls([ mock.call(fake_attachment, fake_db_test_run_id, self.fake_session) ]) self.fake_session.close.assert_called_once()
def test_process_results_existing_tests_invalid_status(self): fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.run_count = 3 # Setup results fake_results = dict( test1={ 'status': 'invalid', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': None }) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test with testtools.ExpectedException(exceptions.UnknownStatus, '^.*\n.*%s$' % 'invalid'): # Run process_results shell.process_results(fake_results_all) # Check that we lookup all tests in the DB expected_test_by_id_calls = [ mock.call(x, self.fake_session) for x in fake_results ] self.db_api_mock.get_test_by_test_id.assert_has_calls( expected_test_by_id_calls, any_order=True) # Check that a run is created in the DB self.db_api_mock.create_run.assert_called_once_with( self.fake_totals['skips'], self.fake_totals['fails'], self.fake_totals['success'], fake_run_time, None, id=None, run_at=None, session=self.fake_session) # Check no test run was added to the DB self.db_api_mock.create_test_run.assert_not_called()
def test_process_result_test_run_attachments(self): fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results fake_attachment = 'some text' fake_results = dict( test1={'status': 'fail', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': fake_attachment}) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Mock create test run fake_db_test_run_id = 'test run id' fake_db_test_run = mock.Mock(name='db test run') fake_db_test_run.id = fake_db_test_run_id self.db_api_mock.create_test_run.return_value = fake_db_test_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Run process_results shell.process_results(fake_results_all) # Check attachments are added to the test run self.db_api_mock.add_test_metadata.assert_not_called() # Check all metadata is added to test_run_metadata self.db_api_mock.add_test_run_attachments.assert_has_calls([ mock.call(fake_attachment, fake_db_test_run_id, self.fake_session)]) self.fake_session.close.assert_called_once()
def test_process_results_existing_tests(self, running_avg_mock): fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results fake_results = dict(test1={ 'status': 'success', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': None }, test2={ 'status': 'fail', 'start_time': 0, 'end_time': 2, 'metadata': None, 'attachments': None }, test3={ 'status': 'skip', 'start_time': 0, 'end_time': 3, 'metadata': None, 'attachments': None }) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Mock running avg fake_running_avg = 'running average' def fake_running_avg_method(test, values, result): values['run_time'] = fake_running_avg return values running_avg_mock.side_effect = fake_running_avg_method # Run process_results shell.process_results(fake_results_all) # Check that we lookup all tests in the DB expected_test_by_id_calls = [ mock.call(x, self.fake_session) for x in fake_results ] self.db_api_mock.get_test_by_test_id.assert_has_calls( expected_test_by_id_calls, any_order=True) # Check that counters for tests are updated (if not skip) expected_update_test_calls = [ mock.call( { 'run_count': 4, 'success': 3, 'run_time': fake_running_avg }, fake_db_test.id, self.fake_session), mock.call({ 'run_count': 4, 'failure': 2 }, fake_db_test.id, self.fake_session) ] self.db_api_mock.update_test.assert_has_calls( expected_update_test_calls, any_order=True) # Check that a test_run for each test is created in the DB expected_create_test_run_calls = [ mock.call(fake_db_test.id, fake_db_run_id, fake_results[x]['status'], fake_results[x]['start_time'], fake_results[x]['end_time'], self.fake_session) for x in fake_results ] self.db_api_mock.create_test_run.assert_has_calls( expected_create_test_run_calls, any_order=True) self.fake_session.close.assert_called_once()
def test_process_results_new_tests(self, get_duration_mock): fake_run_time = 'run time' fake_results = dict(test1={ 'status': 'success', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': None }, test2={ 'status': 'fail', 'start_time': 0, 'end_time': 2, 'metadata': None, 'attachments': None }, test3={ 'status': 'skip', 'start_time': 0, 'end_time': 3, 'metadata': None, 'attachments': None }) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create_run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Tests are not found in the DB self.db_api_mock.get_test_by_test_id.return_value = None # Mock create test get_duration_mock.return_value = 'fake_duration' fake_db_test_id = 'test_db_id' fake_test_create = mock.Mock(name='db test') fake_test_create.id = fake_db_test_id self.db_api_mock.create_test.return_value = fake_test_create # Run process_results shell.process_results(fake_results_all) # Check that we lookup all tests in the DB expected_test_by_id_calls = [ mock.call(x, self.fake_session) for x in fake_results ] self.db_api_mock.get_test_by_test_id.assert_has_calls( expected_test_by_id_calls, any_order=True) # Check that all tests are created in the DB expected_create_test_calls = [ mock.call('test1', 1, 1, 0, 'fake_duration', self.fake_session), mock.call('test2', 1, 0, 1, 'fake_duration', self.fake_session), mock.call('test3', 0, 0, 0, 'fake_duration', self.fake_session) ] self.db_api_mock.create_test.assert_has_calls( expected_create_test_calls, any_order=True) # Check that a test_run for each test is created in the DB expected_create_test_run_calls = [ mock.call(fake_db_test_id, fake_db_run_id, fake_results[x]['status'], fake_results[x]['start_time'], fake_results[x]['end_time'], self.fake_session) for x in fake_results ] self.db_api_mock.create_test_run.assert_has_calls( expected_create_test_run_calls, any_order=True) self.fake_session.close.assert_called_once()
def test_process_results_existing_tests(self, running_avg_mock): fake_run_time = 'run time' # Setup a common fake DB test fake_db_test = mock.Mock(name='db test') fake_db_test.id = 'test id' fake_db_test.run_count = 3 fake_db_test.success = 2 fake_db_test.failure = 1 # Setup results fake_results = dict(test1={'status': 'success', 'start_time': 0, 'end_time': 1, 'metadata': None, 'attachments': None}, test2={'status': 'fail', 'start_time': 0, 'end_time': 2, 'metadata': None, 'attachments': None}, test3={'status': 'skip', 'start_time': 0, 'end_time': 3, 'metadata': None, 'attachments': None}) fake_results_all = copy.deepcopy(fake_results) fake_results_all['run_time'] = fake_run_time # Mock create run fake_db_run_id = 'run id' fake_db_run = mock.Mock(name='db run') fake_db_run.id = fake_db_run_id self.db_api_mock.create_run.return_value = fake_db_run # Tests are found in the DB self.db_api_mock.get_test_by_test_id.return_value = fake_db_test # Mock running avg fake_running_avg = 'running average' def fake_running_avg_method(test, values, result): values['run_time'] = fake_running_avg return values running_avg_mock.side_effect = fake_running_avg_method # Run process_results shell.process_results(fake_results_all) # Check that we lookup all tests in the DB expected_test_by_id_calls = [mock.call(x, self.fake_session) for x in fake_results] self.db_api_mock.get_test_by_test_id.assert_has_calls( expected_test_by_id_calls, any_order=True) # Check that counters for tests are updated (if not skip) expected_update_test_calls = [ mock.call({'run_count': 4, 'success': 3, 'run_time': fake_running_avg}, fake_db_test.id, self.fake_session), mock.call({'run_count': 4, 'failure': 2}, fake_db_test.id, self.fake_session)] self.db_api_mock.update_test.assert_has_calls( expected_update_test_calls, any_order=True) # Check that a test_run for each test is created in the DB expected_create_test_run_calls = [ mock.call(fake_db_test.id, fake_db_run_id, fake_results[x]['status'], fake_results[x]['start_time'], fake_results[x]['end_time'], self.fake_session) for x in fake_results] self.db_api_mock.create_test_run.assert_has_calls( expected_create_test_run_calls, any_order=True) self.fake_session.close.assert_called_once()