Skip to content

Commit 099ca31

Browse files
jardondiegoDiego Jardon
andauthored
Update swarming task check to validate Job config (#5182)
`is_swarming_task` should check if the job configuration has `IS_SWARMING_JOB` in its definition. --------- Co-authored-by: Diego Jardon <[email protected]>
1 parent 002ea81 commit 099ca31

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/clusterfuzz/_internal/swarming/__init__.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Swarming helpers."""
1515

1616
import base64
17-
from json import dumps
17+
import json
1818
import uuid
1919

2020
from google.protobuf import json_format
@@ -27,17 +27,8 @@
2727
from clusterfuzz._internal.system import environment
2828

2929

30-
def _requires_gpu() -> bool:
31-
"""Checks whether the REQUIRES_GPU env variable is set. This means
32-
that the current job needs a gpu enabled device."""
33-
requires_gpu = environment.get_value('REQUIRES_GPU')
34-
return bool(utils.string_is_true(requires_gpu))
35-
36-
3730
def _get_instance_spec(swarming_config: local_config.SwarmingConfig,
3831
job: data_types.Job) -> dict | None:
39-
if not _requires_gpu():
40-
return None
4132
return swarming_config.get('mapping').get(job.platform, None)
4233

4334

@@ -47,6 +38,11 @@ def is_swarming_task(job_name: str):
4738
job = data_types.Job.query(data_types.Job.name == job_name).get()
4839
if job is None:
4940
return False
41+
42+
job_environment = job.get_environment()
43+
if not utils.string_is_true(job_environment.get('IS_SWARMING_JOB')):
44+
return False
45+
5046
return _get_instance_spec(_get_swarming_config(), job) is not None
5147

5248

@@ -164,7 +160,7 @@ def _env_vars_to_json(
164160
env_vars_dict = {pair.key: pair.value for pair in env_vars}
165161
return swarming_pb2.StringPair( # pylint: disable=no-member
166162
key='DOCKER_ENV_VARS',
167-
value=dumps(env_vars_dict))
163+
value=json.dumps(env_vars_dict))
168164

169165

170166
def push_swarming_task(task_request: swarming_pb2.NewTaskRequest): # pylint: disable=no-member

src/clusterfuzz/_internal/tests/core/swarming/swarming_test.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,41 @@ def setUp(self):
3333
helpers.patch(self, [
3434
'clusterfuzz._internal.base.utils.post_url',
3535
'clusterfuzz._internal.swarming._get_task_name',
36-
'clusterfuzz._internal.swarming._requires_gpu'
3736
])
3837
self.mock._get_task_name.return_value = 'task_name' # pylint: disable=protected-access
39-
self.mock._requires_gpu.return_value = True # pylint: disable=protected-access
4038
self.maxDiff = None
4139

40+
def test_is_swarming_task_true(self):
41+
"""Tests that is_swarming_task returns True for a swarming job."""
42+
job = data_types.Job(
43+
name='libfuzzer_chrome_asan',
44+
platform='LINUX',
45+
environment_string='IS_SWARMING_JOB = True')
46+
job.put()
47+
self.assertTrue(swarming.is_swarming_task('libfuzzer_chrome_asan'))
48+
49+
def test_is_swarming_task_false_not_swarming_job(self):
50+
"""Tests that is_swarming_task returns False if IS_SWARMING_JOB is not True."""
51+
job = data_types.Job(
52+
name='libfuzzer_chrome_asan',
53+
platform='LINUX',
54+
environment_string='IS_SWARMING_JOB = False')
55+
job.put()
56+
self.assertFalse(swarming.is_swarming_task('libfuzzer_chrome_asan'))
57+
58+
def test_is_swarming_task_false_no_mapping(self):
59+
"""Tests that is_swarming_task returns False if there is no mapping for the platform."""
60+
job = data_types.Job(
61+
name='libfuzzer_chrome_asan',
62+
platform='UNKNOWN',
63+
environment_string='IS_SWARMING_JOB = True')
64+
job.put()
65+
self.assertFalse(swarming.is_swarming_task('libfuzzer_chrome_asan'))
66+
67+
def test_is_swarming_task_false_no_job(self):
68+
"""Tests that is_swarming_task returns False if the job doesn't exist."""
69+
self.assertFalse(swarming.is_swarming_task('non_existent_job'))
70+
4271
def test_get_spec_from_config_with_docker_image(self):
4372
"""Tests that _get_new_task_spec works as expected."""
4473
job = data_types.Job(name='libfuzzer_chrome_asan', platform='LINUX')

0 commit comments

Comments
 (0)