| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import json |
| 8 |
import unittest |
| 9 |
from unittest.mock import patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 13 |
AnsibleExitJson, |
| 14 |
AnsibleFailJson, |
| 15 |
ModuleTestCase, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
from ansible_collections.community.general.plugins.modules import pagerduty_alert |
| 20 |
|
| 21 |
|
| 22 |
class PagerDutyAlertsTest(unittest.TestCase): |
| 23 |
def _assert_incident_api(self, module, url, method, headers): |
| 24 |
self.assertTrue("https://api.pagerduty.com/incidents" in url, "url must contain REST API v2 network path") |
| 25 |
self.assertTrue("service_ids%5B%5D=service_id" in url, "url must contain service id to filter incidents") |
| 26 |
self.assertTrue("sort_by=incident_number%3Adesc" in url, "url should contain sorting parameter") |
| 27 |
self.assertTrue("time_zone=UTC" in url, "url should contain time zone parameter") |
| 28 |
return Response(), {"status": 200} |
| 29 |
|
| 30 |
def _assert_compatibility_header(self, module, url, method, headers): |
| 31 |
self.assertEqual( |
| 32 |
"application/vnd.pagerduty+json;version=2", |
| 33 |
headers.get("Accept"), |
| 34 |
"Accept:application/vnd.pagerduty+json;version=2 HTTP header not found", |
| 35 |
) |
| 36 |
return Response(), {"status": 200} |
| 37 |
|
| 38 |
def _assert_incident_key(self, module, url, method, headers): |
| 39 |
self.assertTrue("incident_key=incident_key_value" in url, "url must contain incident key") |
| 40 |
return Response(), {"status": 200} |
| 41 |
|
| 42 |
def test_incident_url(self): |
| 43 |
pagerduty_alert.check( |
| 44 |
None, "name", "state", "service_id", "integration_key", "api_key", http_call=self._assert_incident_api |
| 45 |
) |
| 46 |
|
| 47 |
def test_compatibility_header(self): |
| 48 |
pagerduty_alert.check( |
| 49 |
None, |
| 50 |
"name", |
| 51 |
"state", |
| 52 |
"service_id", |
| 53 |
"integration_key", |
| 54 |
"api_key", |
| 55 |
http_call=self._assert_compatibility_header, |
| 56 |
) |
| 57 |
|
| 58 |
def test_incident_key_in_url_when_it_is_given(self): |
| 59 |
pagerduty_alert.check( |
| 60 |
None, |
| 61 |
"name", |
| 62 |
"state", |
| 63 |
"service_id", |
| 64 |
"integration_key", |
| 65 |
"api_key", |
| 66 |
incident_key="incident_key_value", |
| 67 |
http_call=self._assert_incident_key, |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
class Response: |
| 72 |
def read(self): |
| 73 |
return '{"incidents":[{"id": "incident_id", "status": "triggered"}]}' |
| 74 |
|
| 75 |
|
| 76 |
class TestPagerDutyAlertModule(ModuleTestCase): |
| 77 |
def setUp(self): |
| 78 |
super().setUp() |
| 79 |
self.module = pagerduty_alert |
| 80 |
|
| 81 |
def tearDown(self): |
| 82 |
super().tearDown() |
| 83 |
|
| 84 |
@pytest.fixture |
| 85 |
def fetch_url_mock(self, mocker): |
| 86 |
return mocker.patch("ansible.module_utils.monitoring.pagerduty_change.fetch_url") |
| 87 |
|
| 88 |
def test_module_fail_when_required_args_missing(self): |
| 89 |
with self.assertRaises(AnsibleFailJson): |
| 90 |
with set_module_args({}): |
| 91 |
self.module.main() |
| 92 |
|
| 93 |
def test_ensure_alert_created_with_minimal_data(self): |
| 94 |
with set_module_args( |
| 95 |
{ |
| 96 |
"state": "triggered", |
| 97 |
"api_version": "v2", |
| 98 |
"integration_key": "test", |
| 99 |
"source": "My Ansible Script", |
| 100 |
"desc": "Description for alert", |
| 101 |
} |
| 102 |
): |
| 103 |
with patch.object(pagerduty_alert, "fetch_url") as fetch_url_mock: |
| 104 |
fetch_url_mock.return_value = (Response(), {"status": 202}) |
| 105 |
with self.assertRaises(AnsibleExitJson): |
| 106 |
self.module.main() |
| 107 |
|
| 108 |
assert fetch_url_mock.call_count == 1 |
| 109 |
url = fetch_url_mock.call_args[0][1] |
| 110 |
json_data = fetch_url_mock.call_args[1]["data"] |
| 111 |
data = json.loads(json_data) |
| 112 |
|
| 113 |
assert url == "https://events.pagerduty.com/v2/enqueue" |
| 114 |
assert data["routing_key"] == "test" |
| 115 |
assert data["event_action"] == "trigger" |
| 116 |
assert data["payload"]["summary"] == "Description for alert" |
| 117 |
assert data["payload"]["source"] == "My Ansible Script" |
| 118 |
assert data["payload"]["severity"] == "critical" |
| 119 |
assert data["payload"]["timestamp"] is not None |
| 120 |
|
| 121 |
def test_ensure_alert_created_with_full_data(self): |
| 122 |
with set_module_args( |
| 123 |
{ |
| 124 |
"api_version": "v2", |
| 125 |
"component": "mysql", |
| 126 |
"custom_details": {"environment": "production", "notes": "this is a test note"}, |
| 127 |
"desc": "Description for alert", |
| 128 |
"incident_class": "ping failure", |
| 129 |
"integration_key": "test", |
| 130 |
"link_url": "https://pagerduty.com", |
| 131 |
"link_text": "PagerDuty", |
| 132 |
"state": "triggered", |
| 133 |
"source": "My Ansible Script", |
| 134 |
} |
| 135 |
): |
| 136 |
with patch.object(pagerduty_alert, "fetch_url") as fetch_url_mock: |
| 137 |
fetch_url_mock.return_value = (Response(), {"status": 202}) |
| 138 |
with self.assertRaises(AnsibleExitJson): |
| 139 |
self.module.main() |
| 140 |
|
| 141 |
assert fetch_url_mock.call_count == 1 |
| 142 |
url = fetch_url_mock.call_args[0][1] |
| 143 |
json_data = fetch_url_mock.call_args[1]["data"] |
| 144 |
data = json.loads(json_data) |
| 145 |
|
| 146 |
assert url == "https://events.pagerduty.com/v2/enqueue" |
| 147 |
assert data["routing_key"] == "test" |
| 148 |
assert data["payload"]["summary"] == "Description for alert" |
| 149 |
assert data["payload"]["source"] == "My Ansible Script" |
| 150 |
assert data["payload"]["class"] == "ping failure" |
| 151 |
assert data["payload"]["component"] == "mysql" |
| 152 |
assert data["payload"]["custom_details"]["environment"] == "production" |
| 153 |
assert data["payload"]["custom_details"]["notes"] == "this is a test note" |
| 154 |
assert data["links"][0]["href"] == "https://pagerduty.com" |
| 155 |
assert data["links"][0]["text"] == "PagerDuty" |
| 156 |
|
| 157 |
def test_ensure_alert_acknowledged(self): |
| 158 |
with set_module_args( |
| 159 |
{ |
| 160 |
"state": "acknowledged", |
| 161 |
"api_version": "v2", |
| 162 |
"integration_key": "test", |
| 163 |
"incident_key": "incident_test_id", |
| 164 |
} |
| 165 |
): |
| 166 |
with patch.object(pagerduty_alert, "fetch_url") as fetch_url_mock: |
| 167 |
fetch_url_mock.return_value = (Response(), {"status": 202}) |
| 168 |
with self.assertRaises(AnsibleExitJson): |
| 169 |
self.module.main() |
| 170 |
|
| 171 |
assert fetch_url_mock.call_count == 1 |
| 172 |
url = fetch_url_mock.call_args[0][1] |
| 173 |
json_data = fetch_url_mock.call_args[1]["data"] |
| 174 |
data = json.loads(json_data) |
| 175 |
|
| 176 |
assert url == "https://events.pagerduty.com/v2/enqueue" |
| 177 |
assert data["routing_key"] == "test" |
| 178 |
assert data["event_action"] == "acknowledge" |
| 179 |
assert data["dedup_key"] == "incident_test_id" |