Skip to content

Commit

Permalink
Update the list of rejected jobs if match function is used
Browse files Browse the repository at this point in the history
The jobs that are being pruned by the `match` function should be added
to the list of rejected jobs, otherwise the metadata generated at the
end of a test run is incorrect.

Because the list of rejected jobs is initially empty but can be altered
in several places (`run_alternate_selection()` and
`finish_bootstrap()`), it is updated in these functions instead of being
replaced by another list.

The metadata that states if a test plan has been modified
(`custom_joblist`) should also be set to True in these cases.

Fix #1568
  • Loading branch information
pieqq committed Nov 2, 2024
1 parent 086d1dc commit 6d9fdc4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
16 changes: 12 additions & 4 deletions checkbox-ng/plainbox/impl/session/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ def finish_bootstrap(self):
if self._match_qualifiers:
# when `match` is provided, use the test plan but prune it to
# only pull the jobs asked in the launcher or their dependencies
desired_job_list = select_units(
desired_matching_job_list = select_units(
desired_job_list,
self._match_qualifiers
+ self._exclude_qualifiers
Expand All @@ -956,6 +956,16 @@ def finish_bootstrap(self):
)
],
)
rejected_job_list = [
job.id
for job in desired_job_list
if job not in desired_matching_job_list
]
if rejected_job_list:
for rejected_job in rejected_job_list:
self._metadata.rejected_jobs.append(rejected_job)
self._metadata.custom_joblist = True
desired_job_list = desired_matching_job_list

self._context.state.update_desired_job_list(desired_job_list)
# Set subsequent usage expectations i.e. all of the runtime parts are
Expand Down Expand Up @@ -997,13 +1007,11 @@ def use_alternate_selection(self, selection: "Iterable[str]"):
UsageExpectation.of(self).enforce()
self._metadata.custom_joblist = True
desired_job_list = []
rejected_job_list = []
for job_id in self.get_static_todo_list():
if job_id in selection:
desired_job_list.append(self._context.get_unit(job_id, "job"))
else:
rejected_job_list.append(job_id)
self._metadata.rejected_jobs = rejected_job_list
self._metadata.rejected_jobs.append(job_id)
self._context.state.update_desired_job_list(desired_job_list)

@raises(UnexpectedMethodCall)
Expand Down
30 changes: 29 additions & 1 deletion checkbox-ng/plainbox/impl/session/test_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,33 @@ def test_finish_bootstrap_match_no_match(
# and another time to prune it for match
self.assertEqual(select_units_mock.call_count, 1)

@mock.patch("plainbox.impl.session.assistant.UsageExpectation")
@mock.patch("plainbox.impl.session.assistant.select_units")
def test_finish_bootstrap_match_rejected_jobs(
self, select_units_mock, ue_mock, get_providers_mock
):
self_mock = mock.MagicMock()
self_mock._metadata.rejected_jobs = []
# this is just to test that the subfunction is called if this arr is
# defined, assumes the select_units function is mocked
self_mock._match_qualifiers = [1, 2, 3]

job1_id = "com.canonical.certification::job_1"
job2_id = "com.canonical.certification::job_2"
job1 = JobDefinition({"id": job1_id})
job2 = JobDefinition({"id": job2_id})
select_units_mock.side_effect = [[job1, job2], [job2]]

SessionAssistant.finish_bootstrap(self_mock)

# called once to get all the jobs for the selected testplan
# and another time to prune it for match`
self.assertEqual(select_units_mock.call_count, 2)

# job1 is rejected, so the metadata is updated accordingly
self.assertEqual(self_mock._metadata.rejected_jobs, [job1_id])
self.assertTrue(self_mock._metadata.custom_joblist)

@mock.patch(
"plainbox.impl.session.assistant.UsageExpectation",
new=mock.MagicMock(),
Expand All @@ -290,12 +317,13 @@ def test_use_alternate_selection(self, mock_get_providers):
job1 = JobDefinition({"id": job1_id})
job2 = JobDefinition({"id": job2_id})

self_mock._metadata.rejected_jobs = ["already-rejected-job"]
self_mock.get_static_todo_list.return_value = [job1_id, job2_id]
self_mock._context.get_unit.side_effect = [job1, job2]
selection = [job1_id]

SessionAssistant.use_alternate_selection(self_mock, selection)
self.assertEqual(len(self_mock._metadata.rejected_jobs), 1)
self.assertEqual(len(self_mock._metadata.rejected_jobs), 2)
self_mock._context.state.update_desired_job_list.assert_called_with(
[job1]
)

0 comments on commit 6d9fdc4

Please sign in to comment.