Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Update unit tests for icinga to test setting all_services
  • Loading branch information
gmatthews20 committed Dec 16, 2024
1 parent 126232c commit ba073c2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lib/icinga_api/downtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def schedule_downtime(icinga_account: IcingaAccount, details: DowntimeDetails) -
raise MissingMandatoryParamError("Missing comment")

payload = {
"type": details.object_type,
"filter": f'{details.object_type.lower()}.name=="{details.object_name}"',
"type": details.object_type.name.capitalize(),
"filter": f'{details.object_type.name.lower()}.name=="{details.object_name}"',
"author": "StackStorm",
"comment": details.comment,
"start_time": details.start_time,
Expand All @@ -54,7 +54,7 @@ def schedule_downtime(icinga_account: IcingaAccount, details: DowntimeDetails) -


def remove_downtime(
icinga_account: IcingaAccount, object_type: str, object_name: str
icinga_account: IcingaAccount, object_type: IcingaObject, object_name: str
) -> None:
"""
Removes all downtimes created by stackstorm for a host or service
Expand All @@ -68,8 +68,8 @@ def remove_downtime(
raise MissingMandatoryParamError("Missing object name")

payload = {
"type": object_type,
"filter": f'{object_type.lower()}.name=="{object_name}"',
"type": object_type.name.capitalize(),
"filter": f'{object_type.name.lower()}.name=="{object_name}"',
"author": "StackStorm", # Only remove downtimes created by StackStorm
}

Expand Down
63 changes: 52 additions & 11 deletions tests/lib/icinga_api/test_downtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import pytest
from requests import HTTPError

from enums.icinga.icinga_objects import IcingaObject
from exceptions.missing_mandatory_param_error import MissingMandatoryParamError
from icinga_api.downtime import remove_downtime, schedule_downtime
from structs.icinga.downtime_details import DowntimeDetails


class TestScheduleDowntime:
@patch("icinga_api.downtime.requests.post")
def test_schedule_fixed_downtime_success(self, mock_post):
def test_schedule_host_fixed_downtime_success(self, mock_post):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type="Host",
object_type=IcingaObject.HOST,
object_name="example_host",
start_time=1725955200,
end_time=1725958800,
Expand Down Expand Up @@ -43,6 +44,45 @@ def test_schedule_fixed_downtime_success(self, mock_post):
"end_time": 1725958800,
"fixed": True,
"duration": None,
"all_services": True,
}

assert payload == expected_payload

@patch("icinga_api.downtime.requests.post")
def test_schedule_service_fixed_downtime_success(self, mock_post):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type=IcingaObject.SERVICE,
object_name="example_service",
start_time=1725955200,
end_time=1725958800,
comment="Scheduled maintenance",
is_fixed=True,
duration=3600,
)
mock_response = MagicMock()
mock_response.status_code = 201
mock_post.return_value = mock_response

schedule_downtime(icinga_account, details)

mock_post.assert_called_once()
mock_response.raise_for_status.assert_called_once()

_, kwargs = mock_post.call_args
payload = json.loads(kwargs["data"])

expected_payload = {
"type": "Service",
"filter": 'service.name=="example_service"',
"author": "StackStorm",
"comment": "Scheduled maintenance",
"start_time": 1725955200,
"end_time": 1725958800,
"fixed": True,
"duration": None,
"all_services": False,
}

assert payload == expected_payload
Expand All @@ -51,7 +91,7 @@ def test_schedule_fixed_downtime_success(self, mock_post):
def test_schedule_downtime_request_fail(self, mock_post):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type="Host",
object_type=IcingaObject.HOST,
object_name="missing_host",
start_time=1725955200,
end_time=1725958800,
Expand All @@ -74,7 +114,7 @@ def test_schedule_downtime_request_fail(self, mock_post):
def test_schedule_flexible_downtime_success(self, mock_post):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type="Host",
object_type=IcingaObject.HOST,
object_name="example_host",
start_time=1725955200,
end_time=1725958800,
Expand Down Expand Up @@ -103,14 +143,15 @@ def test_schedule_flexible_downtime_success(self, mock_post):
"end_time": 1725958800,
"fixed": False,
"duration": 3600,
"all_services": True,
}

assert payload == expected_payload

def test_missing_schedule_downtime_object_name(self):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type="Host",
object_type=IcingaObject.HOST,
object_name="",
start_time=1725955200,
end_time=1725958800,
Expand All @@ -125,7 +166,7 @@ def test_missing_schedule_downtime_object_name(self):
def test_missing_schedule_downtime_start_time(self):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type="Host",
object_type=IcingaObject.HOST,
object_name="example_host",
start_time=None,
end_time=1725958800,
Expand All @@ -140,7 +181,7 @@ def test_missing_schedule_downtime_start_time(self):
def test_missing_schedule_downtime_end_time(self):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type="Host",
object_type=IcingaObject.HOST,
object_name="example_host",
start_time=1725958800,
end_time=None,
Expand All @@ -155,7 +196,7 @@ def test_missing_schedule_downtime_end_time(self):
def test_missing_schedule_downtime_comment(self):
icinga_account = MagicMock()
details = DowntimeDetails(
object_type="Host",
object_type=IcingaObject.HOST,
object_name="example_host",
start_time=1725955200,
end_time=1725958800,
Expand All @@ -179,7 +220,7 @@ def test_remove_downtime_success(self, mock_post):

remove_downtime(
icinga_account,
object_type="Host",
object_type=IcingaObject.HOST,
object_name="example_host",
)

Expand Down Expand Up @@ -209,7 +250,7 @@ def test_remove_downtime_request_fail(self, mock_post):
with pytest.raises(HTTPError):
remove_downtime(
icinga_account,
object_type="Host",
object_type=IcingaObject.HOST,
object_name="missing_host",
)

Expand All @@ -221,6 +262,6 @@ def test_missing_remove_downtime_object_name(self):
with pytest.raises(MissingMandatoryParamError):
remove_downtime(
icinga_account,
object_type="Host",
object_type=IcingaObject.HOST,
object_name="",
)

0 comments on commit ba073c2

Please sign in to comment.