| 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 |
from unittest.mock import patch |
| 9 |
|
| 10 |
import pytest |
| 11 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 12 |
AnsibleExitJson, |
| 13 |
AnsibleFailJson, |
| 14 |
ModuleTestCase, |
| 15 |
set_module_args, |
| 16 |
) |
| 17 |
|
| 18 |
from ansible_collections.community.general.plugins.modules import pagerduty_change |
| 19 |
|
| 20 |
|
| 21 |
class TestPagerDutyChangeModule(ModuleTestCase): |
| 22 |
def setUp(self): |
| 23 |
super().setUp() |
| 24 |
self.module = pagerduty_change |
| 25 |
|
| 26 |
def tearDown(self): |
| 27 |
super().tearDown() |
| 28 |
|
| 29 |
@pytest.fixture |
| 30 |
def fetch_url_mock(self, mocker): |
| 31 |
return mocker.patch("ansible.module_utils.monitoring.pagerduty_change.fetch_url") |
| 32 |
|
| 33 |
def test_module_fail_when_required_args_missing(self): |
| 34 |
with self.assertRaises(AnsibleFailJson): |
| 35 |
with set_module_args({}): |
| 36 |
self.module.main() |
| 37 |
|
| 38 |
def test_ensure_change_event_created_with_minimal_data(self): |
| 39 |
with set_module_args({"integration_key": "test", "summary": "Testing"}): |
| 40 |
with patch.object(pagerduty_change, "fetch_url") as fetch_url_mock: |
| 41 |
fetch_url_mock.return_value = (None, {"status": 202}) |
| 42 |
with self.assertRaises(AnsibleExitJson): |
| 43 |
self.module.main() |
| 44 |
|
| 45 |
assert fetch_url_mock.call_count == 1 |
| 46 |
url = fetch_url_mock.call_args[0][1] |
| 47 |
json_data = fetch_url_mock.call_args[1]["data"] |
| 48 |
data = json.loads(json_data) |
| 49 |
|
| 50 |
assert url == "https://events.pagerduty.com/v2/change/enqueue" |
| 51 |
assert data["routing_key"] == "test" |
| 52 |
assert data["payload"]["summary"] == "Testing" |
| 53 |
assert data["payload"]["source"] == "Ansible" |
| 54 |
|
| 55 |
def test_ensure_change_event_created_with_full_data(self): |
| 56 |
with set_module_args( |
| 57 |
{ |
| 58 |
"integration_key": "test", |
| 59 |
"summary": "Testing", |
| 60 |
"source": "My Ansible Script", |
| 61 |
"user": "ansible", |
| 62 |
"repo": "github.com/ansible/ansible", |
| 63 |
"revision": "8c67432", |
| 64 |
"environment": "production", |
| 65 |
"link_url": "https://pagerduty.com", |
| 66 |
"link_text": "PagerDuty", |
| 67 |
} |
| 68 |
): |
| 69 |
with patch.object(pagerduty_change, "fetch_url") as fetch_url_mock: |
| 70 |
fetch_url_mock.return_value = (None, {"status": 202}) |
| 71 |
with self.assertRaises(AnsibleExitJson): |
| 72 |
self.module.main() |
| 73 |
|
| 74 |
assert fetch_url_mock.call_count == 1 |
| 75 |
url = fetch_url_mock.call_args[0][1] |
| 76 |
json_data = fetch_url_mock.call_args[1]["data"] |
| 77 |
data = json.loads(json_data) |
| 78 |
|
| 79 |
assert url == "https://events.pagerduty.com/v2/change/enqueue" |
| 80 |
assert data["routing_key"] == "test" |
| 81 |
assert data["payload"]["summary"] == "Testing" |
| 82 |
assert data["payload"]["source"] == "My Ansible Script" |
| 83 |
assert data["payload"]["custom_details"]["user"] == "ansible" |
| 84 |
assert data["payload"]["custom_details"]["repo"] == "github.com/ansible/ansible" |
| 85 |
assert data["payload"]["custom_details"]["revision"] == "8c67432" |
| 86 |
assert data["payload"]["custom_details"]["environment"] == "production" |
| 87 |
assert data["links"][0]["href"] == "https://pagerduty.com" |
| 88 |
assert data["links"][0]["text"] == "PagerDuty" |