Skip to content

Commit 8cf249e

Browse files
Tarun-Aroracanihavesomecoffeethealphadollar
authored
[FIX] Fix bot not commenting when test is completed, add logging (#809)
* Fix bot not commenting when test is completed, add logging * Add unittest for progress_type_request Signed-off-by: Tarun Arora <tarun.arora.030402@gmail.com> * Add gcp_instance checking in test_progress_type_request Signed-off-by: Tarun Arora <tarun.arora.030402@gmail.com> --------- Signed-off-by: Tarun Arora <tarun.arora.030402@gmail.com> Co-authored-by: Willem <github@canihavesome.coffee> Co-authored-by: Shivam Kumar Jha <code@thealphadollar.me>
1 parent faf1896 commit 8cf249e

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

mod_ci/controllers.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,26 +1163,26 @@ def progress_reporter(test_id, token):
11631163

11641164
if 'type' in request.form:
11651165
if request.form['type'] == 'progress':
1166-
log.info('[PROGRESS_REPORTER] Progress reported')
1166+
log.info(f'[PROGRESS_REPORTER][Test: {test_id}] Progress reported')
11671167
if not progress_type_request(log, test, test_id, request):
11681168
return "FAIL"
11691169

11701170
elif request.form['type'] == 'equality':
1171-
log.info('[PROGRESS_REPORTER] Equality reported')
1171+
log.info(f'[PROGRESS_REPORTER][Test: {test_id}] Equality reported')
11721172
equality_type_request(log, test_id, test, request)
11731173

11741174
elif request.form['type'] == 'logupload':
1175-
log.info('[PROGRESS_REPORTER] Log upload')
1175+
log.info(f'[PROGRESS_REPORTER][Test: {test_id}] Log upload')
11761176
if not upload_log_type_request(log, test_id, repo_folder, test, request):
11771177
return "EMPTY"
11781178

11791179
elif request.form['type'] == 'upload':
1180-
log.info('[PROGRESS_REPORTER] File upload')
1180+
log.info(f'[PROGRESS_REPORTER][Test: {test_id}] File upload')
11811181
if not upload_type_request(log, test_id, repo_folder, test, request):
11821182
return "EMPTY"
11831183

11841184
elif request.form['type'] == 'finish':
1185-
log.info('[PROGRESS_REPORTER] Test finished')
1185+
log.info(f'[PROGRESS_REPORTER][Test: {test_id}] Test finished')
11861186
finish_type_request(log, test_id, test, request)
11871187
else:
11881188
return "FAIL"
@@ -1224,7 +1224,7 @@ def progress_type_request(log, test, test_id, request) -> bool:
12241224
gcp_instance_entry = GcpInstance.query.filter(GcpInstance.test_id == test_id).first()
12251225

12261226
if status == TestStatus.testing:
1227-
log.info('test preparation finished')
1227+
log.info(f'[Test: {test_id}] Preparation finished')
12281228
prep_finish_time = datetime.datetime.now()
12291229
# save preparation finish time
12301230
gcp_instance_entry.timestamp_prep_finished = prep_finish_time
@@ -1253,21 +1253,12 @@ def progress_type_request(log, test, test_id, request) -> bool:
12531253

12541254
# If status is complete, remove the GCP Instance entry
12551255
if status in [TestStatus.completed, TestStatus.canceled]:
1256-
log.debug(f"Test {test_id} has been {status}")
1256+
log.debug(f"[Test: {test_id}] Test {status}")
12571257
var_average = 'average_time_' + test.platform.value
12581258
current_average = GeneralData.query.filter(GeneralData.key == var_average).first()
12591259
average_time = 0
12601260
total_time = 0
12611261

1262-
# Delete the current instance
1263-
from run import config
1264-
compute = get_compute_service_object()
1265-
zone = config.get('ZONE', '')
1266-
project = config.get('PROJECT_NAME', '')
1267-
vm_name = f"{test.platform.value}-{test.id}"
1268-
operation = delete_instance(compute, project, zone, vm_name)
1269-
wait_for_operation(compute, project, zone, operation['name'])
1270-
12711262
if current_average is None:
12721263
platform_tests = g.db.query(Test.id).filter(Test.platform == test.platform).subquery()
12731264
finished_tests = g.db.query(TestProgress.test_id).filter(
@@ -1290,8 +1281,8 @@ def progress_type_request(log, test, test_id, request) -> bool:
12901281

12911282
for p in times:
12921283
parts = p.time.split(',')
1293-
start = datetime.datetime.strptime(parts[0], '%Y-%m-%d %H:%M:%S')
1294-
end = datetime.datetime.strptime(parts[-1], '%Y-%m-%d %H:%M:%S')
1284+
start = datetime.datetime.strptime(parts[0], '%Y-%m-%d %H:%M:%S.%f')
1285+
end = datetime.datetime.strptime(parts[-1], '%Y-%m-%d %H:%M:%S.%f')
12951286
total_time += int((end - start).total_seconds())
12961287

12971288
if len(times) != 0:
@@ -1326,7 +1317,7 @@ def progress_type_request(log, test, test_id, request) -> bool:
13261317
gcp_instance = GcpInstance.query.filter(GcpInstance.test_id == test_id).first()
13271318

13281319
if gcp_instance is not None:
1329-
log.debug("Removing GCP Instance entry")
1320+
log.debug(f"Removing GCP Instance entry: {gcp_instance}")
13301321
g.db.delete(gcp_instance)
13311322
g.db.commit()
13321323

@@ -1360,7 +1351,7 @@ def progress_type_request(log, test, test_id, request) -> bool:
13601351
TestResultFile.got.isnot(None)
13611352
)
13621353
).scalar()
1363-
log.debug(f'Test {test.id} completed: {crashes} crashes, {results} results')
1354+
log.debug(f'[Test: {test.id}] Test completed: {crashes} crashes, {results} results')
13641355
if crashes > 0 or results > 0:
13651356
state = Status.FAILURE
13661357
message = 'Not all tests completed successfully, please check'
@@ -1381,6 +1372,16 @@ def progress_type_request(log, test, test_id, request) -> bool:
13811372
except GithubException as a:
13821373
log.error(f'Got an exception while posting to GitHub! Message: {a.data}')
13831374

1375+
if status in [TestStatus.completed, TestStatus.canceled]:
1376+
# Delete the current instance
1377+
from run import config
1378+
compute = get_compute_service_object()
1379+
zone = config.get('ZONE', '')
1380+
project = config.get('PROJECT_NAME', '')
1381+
vm_name = f"{test.platform.value}-{test.id}"
1382+
operation = delete_instance(compute, project, zone, vm_name)
1383+
wait_for_operation(compute, project, zone, operation['name'])
1384+
13841385
return True
13851386

13861387

tests/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def mock_decorator(f):
5353
@wraps(f)
5454
def decorated_function(*args, **kwargs):
5555
return f(*args, **kwargs)
56-
57-
return decorated_function
56+
return decorated_function
5857

5958

6059
def generate_keys():

tests/test_ci/test_controllers.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from mod_auth.models import Role
99
from mod_ci.controllers import (Workflow_builds, get_info_for_pr_comment,
10-
start_platforms)
10+
progress_type_request, start_platforms)
1111
from mod_ci.models import BlockedUsers
1212
from mod_customized.models import CustomizedTest
1313
from mod_home.models import CCExtractorVersion, GeneralData
@@ -1516,6 +1516,33 @@ def test_equality_type_request_rto_none(self, mock_rto):
15161516
mock_rto.query.filter.assert_called_once_with(mock_rto.id == 1)
15171517
mock_log.info.assert_called_once()
15181518

1519+
@mock.patch('mod_ci.controllers.wait_for_operation')
1520+
@mock.patch('mod_ci.controllers.delete_instance')
1521+
@mock.patch('mod_ci.controllers.get_compute_service_object')
1522+
@mock.patch('mod_ci.controllers.update_build_badge')
1523+
@mock.patch('github.Github.get_repo')
1524+
def test_progress_type_request(self, mock_repo, mock_update_build_badge, mock_get_compute_service_object,
1525+
mock_delete_instance, mock_wait_for_operation):
1526+
"""Test progress_type_request function."""
1527+
from mod_ci.models import GcpInstance
1528+
from run import log
1529+
1530+
self.create_user_with_role(
1531+
self.user.name, self.user.email, self.user.password, Role.tester)
1532+
self.create_forktest("own-fork-commit", TestPlatform.linux, regression_tests=[2])
1533+
request = MagicMock()
1534+
request.form = {'status': 'completed', 'message': 'Ran all tests'}
1535+
gcp_instance = GcpInstance(name='test_instance', test_id=3)
1536+
g.db.add(gcp_instance)
1537+
g.db.commit()
1538+
1539+
test = Test.query.filter(Test.id == 3).first()
1540+
1541+
response = progress_type_request(log, test, test.id, request)
1542+
mock_update_build_badge.assert_called_once()
1543+
mock_get_compute_service_object.assert_called()
1544+
self.assertTrue(response)
1545+
15191546
@mock.patch('mod_ci.controllers.g')
15201547
@mock.patch('mod_ci.controllers.TestResultFile')
15211548
@mock.patch('mod_ci.controllers.RegressionTestOutput')

0 commit comments

Comments
 (0)