Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "list-testplan" subcommand (New) #1047

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions checkbox-ng/checkbox_ng/launcher/checkbox_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
StartProvider,
Submit,
ListBootstrapped,
ListTestplan,
TestPlanExport,
Show,
)
Expand Down Expand Up @@ -72,6 +73,7 @@ def main():
"submit": Submit,
"show": Show,
"list-bootstrapped": ListBootstrapped,
"list-testplan": ListTestplan,
"merge-reports": MergeReports,
"merge-submissions": MergeSubmissions,
"tp-export": TestPlanExport,
Expand Down
42 changes: 42 additions & 0 deletions checkbox-ng/checkbox_ng/launcher/subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,48 @@ def __missing__(self, key):
print(job_id)


class ListTestplan:
@property
def sa(self):
return self.ctx.sa

def register_arguments(self, parser):
parser.description = (
"Outputs the list of jobs and templates that will be run by a "
"given test plan as JSON. Instantiated jobs are ignored."
)
parser.add_argument("TEST_PLAN", help=_("test plan id to use"))

def invoked(self, ctx):
self.ctx = ctx
self.sa.start_new_session("checkbox-listing-ephemeral")
tps = self.sa.get_test_plans()
if ctx.args.TEST_PLAN not in tps:
raise SystemExit("Test plan not found")
self.sa.select_test_plan(ctx.args.TEST_PLAN)
self.sa.bootstrap()
job_and_template_list = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor small fix, use a set here, way faster to check if something is in there or not

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I use a set() instead of a list [], I get the following error:

Traceback (most recent call last):
  File "/home/pieq/.venvs/checkbox/bin/checkbox-cli", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/pieq/dev/work/checkbox/checkbox-ng/checkbox_ng/launcher/checkbox_cli.py", line 165, in main
    return subcmd.invoked(ctx)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/pieq/dev/work/checkbox/checkbox-ng/checkbox_ng/launcher/subcommands.py", line 1375, in invoked
    job_and_template_set.add(attrs)
TypeError: unhashable type: 'dict'

all_template_map = {
unit.template_id: unit
for unit in self.sa._context.state.unit_list
if unit.unit == "template"
}
for job in self.sa.get_static_todo_list():
job_unit = self.sa.get_job(job)
if job_unit.template_id:
template_unit = all_template_map[job_unit.template_id]
attrs = template_unit._raw_data.copy()
else:
attrs = job_unit._raw_data.copy()
attrs["unit"] = "job"
attrs["certification_status"] = self.ctx.sa.get_job_state(
job
).effective_certification_status
if attrs not in job_and_template_list:
job_and_template_list.append(attrs)
print(json.dumps(job_and_template_list))


class TestPlanExport:
@property
def sa(self):
Expand Down
77 changes: 77 additions & 0 deletions checkbox-ng/checkbox_ng/launcher/test_subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
from checkbox_ng.launcher.subcommands import (
Launcher,
ListBootstrapped,
ListTestplan,
IncompatibleJobError,
ResumeInstead,
IJobResult,
request_comment,
generate_resume_candidate_description,
)
from plainbox.impl.unit.template import TemplateUnit


class TestLauncher(TestCase):
Expand Down Expand Up @@ -682,6 +684,81 @@ def test_invoke_print_output_customized_format(self, stdout):
self.assertEqual(stdout.getvalue(), expected_out)


class TestListTestplan(TestCase):
def setUp(self):
self.launcher = ListTestplan()
self.ctx = Mock()
self.ctx.args = Mock(TEST_PLAN="", format="")
test_template = TemplateUnit({
"template-id": "test-template",
"id": "test-{res}",
"template-summary": "Test Template Summary",
})
self.ctx.sa = Mock(
start_new_session=Mock(),
get_test_plans=Mock(return_value=["test-plan1", "test-plan2"]),
select_test_plan=Mock(),
bootstrap=Mock(),
get_static_todo_list=Mock(return_value=["test-job1", "test-job2"]),
get_job=Mock(
side_effect=[
Mock(
_raw_data={
"id": "namespace1::test-job1",
"summary": "fake-job1",
"plugin": "manual",
"description": "fake-description1",
"certification_status": "unspecified",
},
id="namespace1::test-job1",
partial_id="test-job1",
template_id="test-template",
),
Mock(
_raw_data={
"id": "namespace2::test-job2",
"summary": "fake-job2",
"plugin": "shell",
"command": "ls",
"certification_status": "unspecified",
},
id="namespace2::test-job2",
partial_id="test-job2",
template_id=None,
),
]
),
get_job_state=Mock(
return_value=Mock(effective_certification_status="blocker")
),
get_resumable_sessions=Mock(return_value=[]),
get_dynamic_todo_list=Mock(return_value=[]),
_context=Mock(
state=Mock(
unit_list=[test_template,]
),
),
)

def test_register_arguments(self):
parser_mock = Mock()
self.launcher.register_arguments(parser_mock)
self.assertTrue(parser_mock.add_argument.called)

def test_invoke_test_plan_not_found(self):
self.ctx.args.TEST_PLAN = "test-plan3"

with self.assertRaisesRegex(SystemExit, "Test plan not found"):
self.launcher.invoked(self.ctx)

@patch("sys.stdout", new_callable=StringIO)
def test_invoke_print_output(self, stdout):
self.ctx.args.TEST_PLAN = "test-plan1"

self.launcher.invoked(self.ctx)
self.assertIn("Test Template Summary", stdout.getvalue())


class TestUtilsFunctions(TestCase):
@patch("checkbox_ng.launcher.subcommands.Colorizer", new=MagicMock())
@patch("builtins.print")
Expand Down
5 changes: 2 additions & 3 deletions checkbox-ng/plainbox/impl/secure/qualifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,12 @@ def _handle_vote(qualifier, job):
excluded_set.add(job)

for qualifier in flat_qualifier_list:
# optimize the super-common case where a qualifier refers to
# a specific job
if (isinstance(qualifier, FieldQualifier) and
qualifier.field == 'id' and
isinstance(qualifier.matcher, OperatorMatcher) and
qualifier.matcher.op == operator.eq):
# optimize the super-common case where a qualifier refers to
# a specific job by using the id_to_index_map to instantly
# perform the requested operation on a single job
for job in job_list:
if job.id == qualifier.matcher.value:
_handle_vote(qualifier, job)
Expand Down
Loading