Skip to content

Commit

Permalink
Refactor tests to avoid code repetition.
Browse files Browse the repository at this point in the history
  • Loading branch information
johanthoren committed Nov 29, 2024
1 parent 37828c3 commit 7f29a8e
Showing 1 changed file with 48 additions and 97 deletions.
145 changes: 48 additions & 97 deletions check_sentinel/tests/test_check_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,120 +6,71 @@
from collections import namedtuple


def test_sentinel_check_init():
"""Test initialization of SentinelCheck."""
args = argparse.Namespace(
subscription_id="test_subscription_id",
resource_group="test_resource_group",
workspace_name="test_workspace_name",
warning=None,
critical=None,
)
check = SentinelCheck({}, {}, "Custom", "check_open_incidents; 1", "", 300, args)
assert check._args.subscription_id == "test_subscription_id"
assert check._num_thresholds == 1


def test_invalid_metric_type():
"""Test handling of invalid metric type."""
args = argparse.Namespace(
subscription_id="test_subscription_id",
resource_group="test_resource_group",
workspace_name="test_workspace_name",
warning=None,
critical=None,
)
with pytest.raises(ParamError):
SentinelCheck({}, {}, "InvalidType", "", "", 300, args)


@patch("check_sentinel.SentinelAPI.get_incidents")
def test_check_open_incidents_with_no_incidents(mock_get_incidents):
"""Test the check_incidents method with no incidents."""
mock_get_incidents.return_value = [] # Return an empty list for incidents
args = argparse.Namespace(
# Define reusable fixtures and helper functions
@pytest.fixture
def sentinel_args():
"""Fixture for common SentinelCheck arguments."""
return argparse.Namespace(
subscription_id="test_subscription_id",
resource_group="test_resource_group",
workspace_name="test_workspace_name",
warning=None,
critical=None,
)
check = SentinelCheck({}, {}, "Custom", "check_open_incidents; 1", "", 300, args)
metrics = check.check_open_incidents()
assert len(metrics) == 1
assert metrics[0].value == 0 # Since we mocked no incidents


@patch("check_sentinel.SentinelAPI.get_incidents")
def test_check_open_incidents_with_some_incidents(mock_get_incidents):
"""Test the check_open_incidents method with some incidents."""
# Mock incidents with different statuses
def create_mock_incidents(status_list):
"""Helper to create mock incidents from a list of statuses."""
MockIncident = namedtuple("MockIncident", ["properties"])
MockProperties = namedtuple("Properties", ["status"])
return [MockIncident(MockProperties(status=status)) for status in status_list]

incidents = [
MockIncident(MockProperties(status="New")),
MockIncident(MockProperties(status="new")),
MockIncident(MockProperties(status="Active")),
MockIncident(MockProperties(status="Closed")),
]
mock_get_incidents.return_value = incidents

args = argparse.Namespace(
subscription_id="test_subscription_id",
resource_group="test_resource_group",
workspace_name="test_workspace_name",
warning=None,
critical=None,
)
check = SentinelCheck({}, {}, "Custom", "check_open_incidents; 1", "", 300, args)
metrics = check.check_open_incidents()
def assert_metrics(metrics, expected_value, expected_display_name=None):
"""Helper to assert metrics' value and optionally display_name."""
assert len(metrics) == 1
assert metrics[0].value == 3 # There are 3 open incidents
assert metrics[0].display_name == "Open Incidents"
assert metrics[0].value == expected_value
if expected_display_name:
assert metrics[0].display_name == expected_display_name


@patch("check_sentinel.SentinelAPI.get_incidents")
def test_check_new_incidents_with_no_incidents(mock_get_incidents):
"""Test the check_new_incidents method with no incidents."""
mock_get_incidents.return_value = []
args = argparse.Namespace(
subscription_id="test_subscription_id",
resource_group="test_resource_group",
workspace_name="test_workspace_name",
warning=None,
critical=None,
)
check = SentinelCheck({}, {}, "Custom", "check_new_incidents; 1", "", 300, args)
metrics = check.check_new_incidents()
assert len(metrics) == 1
assert metrics[0].value == 0
# Tests start here
def test_sentinel_check_init(sentinel_args):
"""Test initialization of SentinelCheck."""
check = SentinelCheck({}, {}, "Custom", "check_open_incidents; 1", "", 300, sentinel_args)
assert check._args.subscription_id == sentinel_args.subscription_id
assert check._num_thresholds == 1


@patch("check_sentinel.SentinelAPI.get_incidents")
def test_check_new_incidents_with_some_incidents(mock_get_incidents):
"""Test the check_new_incidents method with some incidents."""
# Mock incidents with different statuses
MockIncident = namedtuple("MockIncident", ["properties"])
MockProperties = namedtuple("Properties", ["status"])
def test_invalid_metric_type(sentinel_args):
"""Test handling of invalid metric type."""
with pytest.raises(ParamError):
SentinelCheck({}, {}, "InvalidType", "", "", 300, sentinel_args)


incidents = [
MockIncident(MockProperties(status="New")),
MockIncident(MockProperties(status="new")),
MockIncident(MockProperties(status="Active")),
MockIncident(MockProperties(status="Closed")),
]
@patch("check_sentinel.SentinelAPI.get_incidents")
@pytest.mark.parametrize(
"mock_data, method, expected_value, display_name",
[
([], "check_open_incidents", 0, "Open Incidents"), # No incidents
(["New", "new", "Active", "Closed"], "check_open_incidents", 3, "Open Incidents"), # 3 open
(["Closed", "Closed", "Closed"], "check_open_incidents", 0, "Open Incidents"), # All closed
([], "check_new_incidents", 0, "New Incidents"), # No new incidents
(["New", "new", "Active", "Closed"], "check_new_incidents", 2, "New Incidents"), # 2 new
(["Closed", "Closed", "Closed"], "check_new_incidents", 0, "New Incidents"), # All closed
],
)
def test_check_incidents(
mock_get_incidents, sentinel_args, mock_data, method, expected_value, display_name
):
"""Test check_open_incidents and check_new_incidents using parameterized tests."""
# Mock data
incidents = create_mock_incidents(mock_data)
mock_get_incidents.return_value = incidents

args = argparse.Namespace(
subscription_id="test_subscription_id",
resource_group="test_resource_group",
workspace_name="test_workspace_name",
warning=None,
critical=None,
)
check = SentinelCheck({}, {}, "Custom", "check_new_incidents; 1", "", 300, args)
metrics = check.check_new_incidents()
assert len(metrics) == 1
assert metrics[0].value == 2 # There are 2 new incidents
assert metrics[0].display_name == "New Incidents"
# Initialize SentinelCheck
check = SentinelCheck({}, {}, "Custom", f"{method}; 1", "", 300, sentinel_args)

# Call the appropriate method dynamically
metrics = getattr(check, method)()
assert_metrics(metrics, expected_value, display_name)

0 comments on commit 7f29a8e

Please sign in to comment.