-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
61 lines (52 loc) · 2.4 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import unittest
from unittest.mock import patch
from io import BytesIO
from http.client import HTTPConnection
from sng_exporter import PrometheusRequestHandler, SngExporterServer, main
import multiprocessing
import socket
from time import sleep
import requests
from http.server import BaseHTTPRequestHandler, HTTPServer
from io import BytesIO as IO
class TestPrometheusServer(unittest.TestCase):
def test_request(self):
args = ['--listen-address', 'localhost:9577', '--socket-path', '/var/lib/syslog-ng/syslog-ng.ctl', '--stats-with-legacy']
with patch('sys.argv', ['sng_exporter.py'] + args):
server_proc = multiprocessing.Process(target=main, args=())
server_proc.start()
# Terminate the process
sleep(1)
r = requests.get('http://localhost:9577/metrics')
self.assertEqual(r.status_code, 200)
self.assertIsNotNone(r.content)
self.assertNotEqual(r.content[-1], '.')
server_proc.terminate() # sends a SIGTERMimport socket
def test_client(self):
args = ['--listen-address', 'localhost:9577', '--socket-path', '/var/lib/syslog-ng/syslog-ng.ctl', '--stats-with-legacy']
with patch('sys.argv', ['sng_exporter.py'] + args):
server_proc = multiprocessing.Process(target=main, args=())
server_proc.start()
# Terminate the process
sleep(1)
fake_client = socket.socket()
fake_client.settimeout(1)
fake_client.connect(('localhost', 9577))
fake_client.close()
print(fake_client)
server_proc.terminate() # sends a SIGTERMimport socket
def test_mock_server(self):
"""Test the custom HTTP request handler by mocking a server"""
class MockRequest(object):
def makefile(self, *args, **kwargs):
return IO(b"GET /")
class MockServer(object):
def __init__(self, ip_port, Handler):
handler = Handler(MockRequest(), ip_port, self)
# The GET request will be sent here
# and any exceptions will be propagated through.
PrometheusRequestHandler.socket_path = '/var/lib/syslog-ng/syslog-ng.ctl'
PrometheusRequestHandler.stats_with_legacy = 'STATS PROMETHEUS WITH_LEGACY\n'
server = MockServer(('0.0.0.0', 8888), PrometheusRequestHandler)
if __name__ == '__main__':
unittest.main()