From b5912a8b6c16c8545df189d7a4e3a64e879471bc Mon Sep 17 00:00:00 2001 From: Alex Kelly Date: Fri, 5 Nov 2021 10:00:17 -0400 Subject: [PATCH] tests: add pingdom test and supporting json examples --- tests/example_pingdom_dns.json | 33 +++++++++++++++++++++++++ tests/example_pingdom_http.json | 36 +++++++++++++++++++++++++++ tests/example_pingdom_tcp.json | 34 +++++++++++++++++++++++++ tests/test_pingdom.py | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 tests/example_pingdom_dns.json create mode 100644 tests/example_pingdom_http.json create mode 100644 tests/example_pingdom_tcp.json create mode 100644 tests/test_pingdom.py diff --git a/tests/example_pingdom_dns.json b/tests/example_pingdom_dns.json new file mode 100644 index 0000000..80e2038 --- /dev/null +++ b/tests/example_pingdom_dns.json @@ -0,0 +1,33 @@ +{ + "check_id": 12345, + "check_name": "Pingdom-dns build test", + "check_type": "DNS", + "check_params": { + "hostname": "www.example.com", + "basic_auth": false, + "expected_ip": "123.4.5.6", + "ipv6": false, + "nameserver": "example.com" + }, + "tags": [ + "example_tag" + ], + "previous_state": "UP", + "current_state": "DOWN", + "importance_level": "HIGH", + "state_changed_timestamp": 1451610061, + "state_changed_utc_time": "2016-01-01T01:01:01", + "long_description": "Long error message", + "description": "Short error message", + "first_probe": { + "ip": "123.4.5.6", + "ipv6": "2001:4800:1020:209::5", + "location": "Stockholm, Sweden" + }, + "second_probe": { + "ip": "123.4.5.6", + "ipv6": "2001:4800:1020:209::5", + "location": "Austin, US", + "version": 1 + } + } diff --git a/tests/example_pingdom_http.json b/tests/example_pingdom_http.json new file mode 100644 index 0000000..358478e --- /dev/null +++ b/tests/example_pingdom_http.json @@ -0,0 +1,36 @@ +{ + "check_id": 12345, + "check_name": "Pingdom-http build test", + "check_type": "HTTP", + "check_params": { + "basic_auth": false, + "encryption": true, + "full_url": "https://www.example.com/path", + "header": "User-Agent:Pingdom.com_bot", + "hostname": "www.example.com", + "ipv6": false, + "port": 443, + "url": "/path" + }, + "tags": [ + "example_tag" + ], + "previous_state": "UP", + "current_state": "DOWN", + "importance_level": "HIGH", + "state_changed_timestamp": 1451610061, + "state_changed_utc_time": "2016-01-01T01:01:01", + "long_description": "Long error message", + "description": "Short error message", + "first_probe": { + "ip": "123.4.5.6", + "ipv6": "2001:4800:1020:209::5", + "location": "Stockholm, Sweden" + }, + "second_probe": { + "ip": "123.4.5.6", + "ipv6": "2001:4800:1020:209::5", + "location": "Austin, US", + "version": 1 + } + } diff --git a/tests/example_pingdom_tcp.json b/tests/example_pingdom_tcp.json new file mode 100644 index 0000000..d382e9a --- /dev/null +++ b/tests/example_pingdom_tcp.json @@ -0,0 +1,34 @@ +{ +"body":{ + "check_id": 12345, + "check_name": "Pingdom-tcp build test", + "check_type": "PORT_TCP", + "check_params": { + "hostname": "www.example.com", + "basic_auth": false, + "ipv6": false, + "port": 80 + }, + "tags": [ + "example_tag" + ], + "previous_state": "UP", + "current_state": "DOWN", + "importance_level": "HIGH", + "state_changed_timestamp": 1451610061, + "state_changed_utc_time": "2016-01-01T01:01:01", + "long_description": "Long error message", + "description": "Short error message", + "first_probe": { + "ip": "123.4.5.6", + "ipv6": "2001:4800:1020:209::5", + "location": "Stockholm, Sweden" + }, + "second_probe": { + "ip": "123.4.5.6", + "ipv6": "2001:4800:1020:209::5", + "location": "Austin, US", + "version": 1 + } + } + } diff --git a/tests/test_pingdom.py b/tests/test_pingdom.py new file mode 100644 index 0000000..69cad46 --- /dev/null +++ b/tests/test_pingdom.py @@ -0,0 +1,44 @@ +""" +Test module for pingdom formatter. +""" + +import unittest + +import httpx +import nio + +from .start import BOT_URL, FULL_ID, KEY, MATRIX_ID, MATRIX_PW, MATRIX_URL + + +class PingdomFormatterTest(unittest.IsolatedAsyncioTestCase): + """Grafana formatter test class.""" + + async def test_pingdom_http_body(self): + """Send a markdown message, and check the result.""" + messages = [] + client = nio.AsyncClient(MATRIX_URL, MATRIX_ID) + + await client.login(MATRIX_PW) + room = await client.room_create() + + with open("tests/example_pingdom_http.json") as f: + example_pingdom_request = f.read() + self.assertEqual( + httpx.post( + f"{BOT_URL}/{room.room_id}", + params={"formatter": "pingdom", "key": KEY}, + content=example_pingdom_request, + ).json(), + {"status": 200, "ret": "OK"}, + ) + + sync = await client.sync() + messages = await client.room_messages(room.room_id, sync.next_batch) + await client.close() + + message = messages.chunk[0] + self.assertEqual(message.sender, FULL_ID) + self.assertEqual( + message.body, + "#### [Alerting] Panel Title alert\nNotification Message\n\n* Count: 1\n", + )