Skip to content

Commit

Permalink
fix pylint, black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
richm committed Jun 3, 2024
1 parent d66678e commit 1e38f20
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 59 deletions.
5 changes: 2 additions & 3 deletions library/pcs_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
from ansible.module_utils.basic import AnsibleModule

# pylint: disable=no-name-in-module
# pylint: disable=import-error
from ansible.module_utils.ha_cluster_lsr import pcs_api_v2_utils as api_utils

# pylint: enable=no-name-in-module
Expand Down Expand Up @@ -191,9 +192,7 @@ def pcs(
pcs_result=api_utils.api_result_to_dict(api_result_dto),
)
except api_utils.TaskFailedError as exc:
return module.fail_json(
msg=exc.msg, changed=True, pcs_result=exc.api_result
)
return module.fail_json(msg=exc.msg, changed=True, pcs_result=exc.api_result)
except api_utils.ApiError as exc:
return module.fail_json(msg=exc.msg)

Expand Down
9 changes: 3 additions & 6 deletions library/pcs_qdevice_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
from ansible.module_utils.basic import AnsibleModule

# pylint: disable=no-name-in-module
# pylint: disable=import-error
from ansible.module_utils.ha_cluster_lsr import pcs_api_v2_utils as api_utils

# pylint: enable=no-name-in-module
Expand Down Expand Up @@ -185,9 +186,7 @@ def pcs(
pcs_result=api_utils.api_result_to_dict(check_response),
)
except api_utils.TaskFailedError as exc:
return module.fail_json(
msg=exc.msg, changed=False, pcs_result=exc.api_result
)
return module.fail_json(msg=exc.msg, changed=False, pcs_result=exc.api_result)
except api_utils.ApiError as exc:
return module.fail_json(msg=exc.msg)

Expand All @@ -203,9 +202,7 @@ def pcs(
pcs_result=api_utils.api_result_to_dict(setup_response),
)
except api_utils.TaskFailedError as exc:
return module.fail_json(
msg=exc.msg, changed=True, pcs_result=exc.api_result
)
return module.fail_json(msg=exc.msg, changed=True, pcs_result=exc.api_result)
except api_utils.ApiError as exc:
return module.fail_json(msg=exc.msg)

Expand Down
19 changes: 5 additions & 14 deletions module_utils/ha_cluster_lsr/pcs_api_v2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,19 @@ def api_result_to_dict(api_result: TaskResultDto) -> dict[str, Any]:
task_finish_type=str(api_result.task_finish_type),
result=api_result.result,
reports=[
report_item_to_dict(report_item)
for report_item in api_result.reports
report_item_to_dict(report_item) for report_item in api_result.reports
],
kill_reason=(
str(api_result.kill_reason) if api_result.kill_reason else None
),
kill_reason=(str(api_result.kill_reason) if api_result.kill_reason else None),
)


def parse_api_response(
module: AnsibleModule, response_data: bytes
) -> TaskResultDto:
def parse_api_response(module: AnsibleModule, response_data: bytes) -> TaskResultDto:
"""
Process API response, return parsed API call result or raise ApiError
"""
# pylint: disable=too-many-return-statements
try:
api_result_dto = from_dict(
TaskResultDto, module.from_json(response_data)
)
api_result_dto = from_dict(TaskResultDto, module.from_json(response_data))
except (JSONDecodeError, DaciteError) as exc:
raise ResponseFormatError(
f"Unable to parse API response: {exc}\n{response_data!r}"
Expand All @@ -187,9 +180,7 @@ def parse_api_response(
)
raise TaskFailedError("Task killed", api_result_to_dict(api_result_dto))
if api_result_dto.task_finish_type == TaskFinishType.UNHANDLED_EXCEPTION:
raise TaskFailedError(
"Unhandled exception", api_result_to_dict(api_result_dto)
)
raise TaskFailedError("Unhandled exception", api_result_to_dict(api_result_dto))

# search for errors in reports and fail if the command failed
error_list = [
Expand Down
8 changes: 2 additions & 6 deletions tests/unit/test_pcs_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
from importlib import import_module
from unittest import TestCase, mock

sys.modules["ansible.module_utils.ha_cluster_lsr"] = import_module(
"ha_cluster_lsr"
)
sys.modules["ansible.module_utils.ha_cluster_lsr"] = import_module("ha_cluster_lsr")

import pcs_api_v2
from pcs.common.async_tasks.dto import CommandDto, CommandOptionsDto
Expand All @@ -39,9 +37,7 @@ def setUp(self) -> None:
self.addCleanup(call_api_patcher.stop)

def assert_api_called(self) -> None:
self.call_api_mock.assert_called_once_with(
self.module_mock, self.cmd_dto
)
self.call_api_mock.assert_called_once_with(self.module_mock, self.cmd_dto)

def test_success(self) -> None:
self.call_api_mock.return_value = (
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/test_pcs_api_v2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ def test_fail(self) -> None:
pcs_api_v2_utils.parse_api_response(
self.module,
json.dumps(
to_dict(
fixture.task_result_dto(finish_type=TaskFinishType.FAIL)
)
to_dict(fixture.task_result_dto(finish_type=TaskFinishType.FAIL))
).encode(),
)
self.assertEqual(context.exception.msg, "Task failed")
Expand Down
36 changes: 9 additions & 27 deletions tests/unit/test_pcs_qdevice_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
from importlib import import_module
from unittest import TestCase, mock

sys.modules["ansible.module_utils.ha_cluster_lsr"] = import_module(
"ha_cluster_lsr"
)
sys.modules["ansible.module_utils.ha_cluster_lsr"] = import_module("ha_cluster_lsr")

import pcs_qdevice_certs
from pcs.common.async_tasks.dto import CommandDto, CommandOptionsDto
Expand Down Expand Up @@ -52,9 +50,7 @@ def test_success_setup(self) -> None:
self.call_api_mock.side_effect = [
(
fixture.response_from_dto(
fixture.task_result_dto(
result=False, command=self.cmd_check_dto
)
fixture.task_result_dto(result=False, command=self.cmd_check_dto)
),
dict(status=200, body="some body data"),
),
Expand All @@ -66,9 +62,7 @@ def test_success_setup(self) -> None:
),
]

pcs_qdevice_certs.pcs(
self.module_mock, self.cmd_params, self.cmd_options
)
pcs_qdevice_certs.pcs(self.module_mock, self.cmd_params, self.cmd_options)

self.call_api_mock.assert_has_calls(
[
Expand Down Expand Up @@ -104,13 +98,9 @@ def assert_check_call_only(self, certs_already_configured: bool) -> None:
dict(status=200, body="some body data"),
)

pcs_qdevice_certs.pcs(
self.module_mock, self.cmd_params, self.cmd_options
)
pcs_qdevice_certs.pcs(self.module_mock, self.cmd_params, self.cmd_options)

self.call_api_mock.assert_called_once_with(
self.module_mock, self.cmd_check_dto
)
self.call_api_mock.assert_called_once_with(self.module_mock, self.cmd_check_dto)
self.module_mock.exit_json.assert_called_once_with(
changed=not certs_already_configured,
pcs_result=dict(
Expand Down Expand Up @@ -151,12 +141,8 @@ def test_error_check(self) -> None:
dict(status=200, body="some body data"),
)

pcs_qdevice_certs.pcs(
self.module_mock, self.cmd_params, self.cmd_options
)
self.call_api_mock.assert_called_once_with(
self.module_mock, self.cmd_check_dto
)
pcs_qdevice_certs.pcs(self.module_mock, self.cmd_params, self.cmd_options)
self.call_api_mock.assert_called_once_with(self.module_mock, self.cmd_check_dto)
self.module_mock.exit_json.assert_not_called()
self.module_mock.fail_json.assert_called_once_with(
msg="Unhandled exception",
Expand All @@ -179,9 +165,7 @@ def test_error_setup(self) -> None:
self.call_api_mock.side_effect = [
(
fixture.response_from_dto(
fixture.task_result_dto(
result=False, command=self.cmd_check_dto
)
fixture.task_result_dto(result=False, command=self.cmd_check_dto)
),
dict(status=200, body="some body data"),
),
Expand All @@ -197,9 +181,7 @@ def test_error_setup(self) -> None:
),
]

pcs_qdevice_certs.pcs(
self.module_mock, self.cmd_params, self.cmd_options
)
pcs_qdevice_certs.pcs(self.module_mock, self.cmd_params, self.cmd_options)

self.call_api_mock.assert_has_calls(
[
Expand Down

0 comments on commit 1e38f20

Please sign in to comment.