Skip to content

Commit

Permalink
refactor: deduplicate code in templates and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Izquierdo committed Oct 27, 2023
1 parent 9b9795a commit 63b1497
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 108 deletions.
10 changes: 2 additions & 8 deletions ansible_rulebook/action/run_job_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
JobTemplateNotFoundException,
)
from ansible_rulebook.job_template_runner import job_template_runner
from ansible_rulebook.util import run_at
from ansible_rulebook.util import process_controller_host_limit, run_at

from .control import Control
from .helper import Helper
Expand All @@ -46,13 +46,7 @@ def __init__(self, metadata: Metadata, control: Control, **action_args):
self.organization = self.action_args["organization"]
self.job_id = str(uuid.uuid4())
self.job_args = self.action_args.get("job_args", {})
if "limit" in self.job_args:
if isinstance(self.job_args["limit"], list):
self.job_args["limit"] = ",".join(self.job_args["limit"])
else:
self.job_args["limit"] = str(self.job_args["limit"])
else:
self.job_args["limit"] = ",".join(self.helper.control.hosts)
process_controller_host_limit(self)
self.controller_job = {}

async def __call__(self):
Expand Down
10 changes: 2 additions & 8 deletions ansible_rulebook/action/run_workflow_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
WorkflowJobTemplateNotFoundException,
)
from ansible_rulebook.job_template_runner import job_template_runner
from ansible_rulebook.util import run_at
from ansible_rulebook.util import process_controller_host_limit, run_at

from .control import Control
from .helper import Helper
Expand All @@ -46,13 +46,7 @@ def __init__(self, metadata: Metadata, control: Control, **action_args):
self.organization = self.action_args["organization"]
self.job_id = str(uuid.uuid4())
self.job_args = self.action_args.get("job_args", {})
if "limit" in self.job_args:
if isinstance(self.job_args["limit"], list):
self.job_args["limit"] = ",".join(self.job_args["limit"])
else:
self.job_args["limit"] = str(self.job_args["limit"])
else:
self.job_args["limit"] = ",".join(self.helper.control.hosts)
process_controller_host_limit(self)
self.controller_job = {}

async def __call__(self):
Expand Down
18 changes: 18 additions & 0 deletions ansible_rulebook/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,21 @@ def _builtin_filter_path(name: str) -> Tuple[bool, str]:
dirname = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(dirname, "event_filter", filter_name + ".py")
return os.path.exists(path), path


# TODO(alex): This function should be removed after the
# controller templates are refactored to deduplicate code
def process_controller_host_limit(template_obj):
if "limit" in template_obj.job_args:
if isinstance(template_obj.job_args["limit"], list):
template_obj.job_args["limit"] = ",".join(
template_obj.job_args["limit"],
)
else:
template_obj.job_args["limit"] = str(
template_obj.job_args["limit"],
)
else:
template_obj.job_args["limit"] = ",".join(
template_obj.helper.control.hosts,
)
28 changes: 28 additions & 0 deletions tests/unit/action/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import asyncio

import pytest

from ansible_rulebook.action.control import Control
from ansible_rulebook.action.metadata import Metadata


@pytest.fixture
def base_metadata():
return Metadata(
rule="r1",
rule_set="rs1",
rule_uuid="u1",
rule_set_uuid="u2",
rule_run_at="abc",
)


@pytest.fixture
def base_control():
return Control(
queue=asyncio.Queue(),
inventory="abc",
hosts=["all"],
variables={"a": 1},
project_data_file="",
)
46 changes: 46 additions & 0 deletions tests/unit/action/test_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from ansible_rulebook.action.run_job_template import RunJobTemplate
from ansible_rulebook.action.run_workflow_template import RunWorkflowTemplate


@pytest.mark.parametrize(
"template_class",
[
pytest.param(RunJobTemplate, id="job_template"),
pytest.param(RunWorkflowTemplate, id="workflow_template"),
],
)
@pytest.mark.parametrize(
"input,expected",
[
pytest.param({"limit": "localhost"}, "localhost", id="single_host"),
pytest.param(
{"limit": "localhost,localhost2"},
"localhost,localhost2",
id="multiple_hosts_str",
),
pytest.param(
{"limit": ["localhost", "localhost2"]},
"localhost,localhost2",
id="multiple_hosts",
),
pytest.param({}, "all", id="default"),
],
)
@pytest.mark.asyncio
async def test_controller_custom_host_limit(
input, expected, template_class, base_metadata, base_control
):
"""Test controller templates process the host limit in job_args."""
action_args = {
"name": "fred",
"organization": "Default",
"retries": 1,
"retry": True,
"delay": 1,
"set_facts": True,
"job_args": input,
}
template = template_class(base_metadata, base_control, **action_args)
assert template.job_args["limit"] == expected
46 changes: 0 additions & 46 deletions tests/unit/action/test_run_job_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,49 +215,3 @@ async def test_run_job_template_retries():
drools_mock.assert_called_once()

_validate(queue, True)


@pytest.mark.parametrize(
"input,output",
[
pytest.param({"limit": "localhost"}, "localhost", id="single_host"),
pytest.param(
{"limit": "localhost,localhost2"},
"localhost,localhost2",
id="multiple_hosts_str",
),
pytest.param(
{"limit": ["localhost", "localhost2"]},
"localhost,localhost2",
id="multiple_hosts",
),
pytest.param({}, "all", id="default"),
],
)
def test_custom_host_limit(input, output):
queue = asyncio.Queue()
metadata = Metadata(
rule="r1",
rule_set="rs1",
rule_uuid="u1",
rule_set_uuid="u2",
rule_run_at="abc",
)
control = Control(
queue=queue,
inventory="abc",
hosts=["all"],
variables={"a": 1},
project_data_file="",
)
action_args = {
"name": "fred",
"organization": "Default",
"retries": 1,
"retry": True,
"delay": 1,
"set_facts": True,
"job_args": input,
}
template = RunJobTemplate(metadata, control, **action_args)
assert template.job_args["limit"] == output
46 changes: 0 additions & 46 deletions tests/unit/action/test_run_workflow_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,49 +218,3 @@ async def test_run_workflow_template_retries():
drools_mock.assert_called_once()

_validate(queue, True)


@pytest.mark.parametrize(
"input,output",
[
pytest.param({"limit": "localhost"}, "localhost", id="single_host"),
pytest.param(
{"limit": "localhost,localhost2"},
"localhost,localhost2",
id="multiple_hosts_str",
),
pytest.param(
{"limit": ["localhost", "localhost2"]},
"localhost,localhost2",
id="multiple_hosts",
),
pytest.param({}, "all", id="default"),
],
)
def test_custom_host_limit(input, output):
queue = asyncio.Queue()
metadata = Metadata(
rule="r1",
rule_set="rs1",
rule_uuid="u1",
rule_set_uuid="u2",
rule_run_at="abc",
)
control = Control(
queue=queue,
inventory="abc",
hosts=["all"],
variables={"a": 1},
project_data_file="",
)
action_args = {
"name": "fred",
"organization": "Default",
"retries": 1,
"retry": True,
"delay": 1,
"set_facts": True,
"job_args": input,
}
template = RunWorkflowTemplate(metadata, control, **action_args)
assert template.job_args["limit"] == output

0 comments on commit 63b1497

Please sign in to comment.