def test_build_history(self): """Testing build history.""" with ndb.Client().context(): BuildsHistory(id='test-project-fuzzing', build_tag='fuzzing', project='test-project', build_ids=[str(i) for i in range(1, 65)]).put() update_build_history('test-project', '65', 'fuzzing') expected_build_ids = [str(i) for i in range(2, 66)] self.assertEqual(BuildsHistory.query().get().build_ids, expected_build_ids)
def update_build_history(project_name, build_id, build_tag): """Update build history of project.""" project_key = ndb.Key(BuildsHistory, project_name + '-' + build_tag) project = project_key.get() if not project: project = BuildsHistory(id=project_name + '-' + build_tag, build_tag=build_tag, project=project_name, build_ids=[]) if len(project.build_ids) >= MAX_BUILD_HISTORY_LENGTH: project.build_ids.pop(0) project.build_ids.append(build_id) project.put()
def update_build_status(build_tag, status_filename): """Update build statuses.""" projects = [] def process_project(project_build): """Process a project.""" project = get_build_history(project_build.build_ids) project['name'] = project_build.project print('Processing project', project['name']) return project with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor: futures = [] for project_build in BuildsHistory.query( BuildsHistory.build_tag == build_tag).order('project'): futures.append(executor.submit(process_project, project_build)) for future in concurrent.futures.as_completed(futures): project = future.result() update_last_successful_build(project, build_tag) projects.append(project) sort_projects(projects) data = {'projects': projects} upload_status(data, status_filename)
def update_build_status(build_tag_suffix, status_filename): """Update build statuses.""" statuses = {} successes = [] failures = [] for project_build in BuildsHistory.query( BuildsHistory.build_tag_suffix == build_tag_suffix): last_build = get_last_build(project_build.build_ids) if not last_build: logging.error('Failed to get last build for project %s', project_build.project) continue if last_build['status'] == 'SUCCESS': statuses[project_build.project] = True successes.append({ 'name': project_build.project, 'build_id': last_build['id'], 'finish_time': last_build['finishTime'], 'success': True, }) else: statuses[project_build.project] = False failures.append({ 'name': project_build.project, 'build_id': last_build['id'], 'finish_time': last_build['finishTime'], 'success': False, }) builds_status.upload_status(successes, failures, status_filename) return statuses
def test_build_history_no_existing_project(self): """Testing build history when build history object is missing.""" with ndb.Client().context(): update_build_history('test-project', '1', 'fuzzing') expected_build_ids = ['1'] self.assertEqual(BuildsHistory.query().get().build_ids, expected_build_ids)
def update_build_status(build_tag, status_filename): """Update build statuses.""" projects = [] statuses = {} for project_build in BuildsHistory.query( BuildsHistory.build_tag == build_tag).order('project'): project = get_build_history(project_build.build_ids) project['name'] = project_build.project projects.append(project) if project['history']: statuses[project_build.project] = project['history'][0]['success'] update_last_successful_build(project, build_tag) sort_projects(projects) data = {'projects': projects} upload_status(data, status_filename) return statuses
def test_update_build_status(self, mocked_upload_log, mocked_cloud_build, mocked_google_auth): """Testing update build status as a whole.""" del self, mocked_cloud_build, mocked_google_auth update_build_status.upload_status = MagicMock() mocked_upload_log.return_value = True status_filename = 'status.json' with ndb.Client().context(): BuildsHistory(id='test-project-1-fuzzing', build_tag='fuzzing', project='test-project-1', build_ids=['1']).put() BuildsHistory(id='test-project-2-fuzzing', build_tag='fuzzing', project='test-project-2', build_ids=['2']).put() BuildsHistory(id='test-project-3-fuzzing', build_tag='fuzzing', project='test-project-3', build_ids=['3']).put() builds = [{ 'build_id': '1', 'finishTime': 'test_time', 'status': 'SUCCESS' }, { 'build_id': '2', 'finishTime': 'test_time', 'status': 'FAILURE' }, { 'build_id': '3', 'status': 'WORKING' }] mocked_get_build = MockGetBuild(builds) update_build_status.get_build = mocked_get_build.get_build expected_data = { 'projects': [{ 'history': [{ 'build_id': '2', 'finish_time': 'test_time', 'success': False }], 'name': 'test-project-2' }, { 'history': [{ 'build_id': '1', 'finish_time': 'test_time', 'success': True }], 'last_successful_build': { 'build_id': '1', 'finish_time': 'test_time' }, 'name': 'test-project-1' }, { 'history': [], 'name': 'test-project-3' }] } update_build_status.update_build_status('fuzzing', 'status.json') update_build_status.upload_status.assert_called_with( expected_data, status_filename)