mirror of
https://github.com/drewkerrigan/nagios-http-json.git
synced 2024-11-24 03:13:51 +01:00
e96bba0eb8
- Also added tests for TLS options
132 lines
3.7 KiB
Python
132 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
|
|
import unittest
|
|
import unittest.mock as mock
|
|
import sys
|
|
import os
|
|
|
|
sys.path.append('..')
|
|
|
|
from check_http_json import main
|
|
|
|
|
|
class MockResponse():
|
|
def __init__(self, status_code=200, content='{"foo": "bar"}'):
|
|
self.status_code = status_code
|
|
self.content = content
|
|
|
|
def read(self):
|
|
return self.content
|
|
|
|
|
|
class MainTest(unittest.TestCase):
|
|
"""
|
|
Tests for Main
|
|
"""
|
|
|
|
@mock.patch('builtins.print')
|
|
def test_main_version(self, mock_print):
|
|
args = ['--version']
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
mock_print.assert_called_once()
|
|
self.assertEqual(test.exception.code, 0)
|
|
|
|
@mock.patch('builtins.print')
|
|
@mock.patch('urllib.request.urlopen')
|
|
def test_main_with_ssl(self, mock_request, mock_print):
|
|
args = '-H localhost --ssl'.split(' ')
|
|
|
|
mock_request.return_value = MockResponse()
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
self.assertEqual(test.exception.code, 0)
|
|
|
|
|
|
@mock.patch('builtins.print')
|
|
@mock.patch('urllib.request.urlopen')
|
|
def test_main_with_parse_error(self, mock_request, mock_print):
|
|
args = '-H localhost'.split(' ')
|
|
|
|
mock_request.return_value = MockResponse(content='not JSON')
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
self.assertTrue('Parser error' in str(mock_print.call_args))
|
|
self.assertEqual(test.exception.code, 3)
|
|
|
|
@mock.patch('builtins.print')
|
|
def test_main_with_url_error(self, mock_print):
|
|
args = '-H localhost'.split(' ')
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
self.assertTrue('URLError' in str(mock_print.call_args))
|
|
self.assertEqual(test.exception.code, 3)
|
|
|
|
@mock.patch('builtins.print')
|
|
@mock.patch('urllib.request.urlopen')
|
|
def test_main_with_http_error_no_json(self, mock_request, mock_print):
|
|
args = '-H localhost'.split(' ')
|
|
|
|
mock_request.return_value = MockResponse(content='not JSON', status_code=503)
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
self.assertTrue('Parser error' in str(mock_print.call_args))
|
|
self.assertEqual(test.exception.code, 3)
|
|
|
|
@mock.patch('builtins.print')
|
|
@mock.patch('urllib.request.urlopen')
|
|
def test_main_with_http_error_valid_json(self, mock_request, mock_print):
|
|
args = '-H localhost'.split(' ')
|
|
|
|
mock_request.return_value = MockResponse(status_code=503)
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
self.assertEqual(test.exception.code, 0)
|
|
|
|
@mock.patch('builtins.print')
|
|
def test_main_with_tls(self, mock_print):
|
|
args = ['-H', 'localhost',
|
|
'--ssl',
|
|
'--cacert',
|
|
'test/tls/ca-root.pem',
|
|
'--cert',
|
|
'test/tls/cert.pem',
|
|
'--key',
|
|
'test/tls/key.pem']
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
self.assertTrue('https://localhost' in str(mock_print.call_args))
|
|
self.assertEqual(test.exception.code, 3)
|
|
|
|
@mock.patch('builtins.print')
|
|
def test_main_with_tls_wrong_ca(self, mock_print):
|
|
args = ['-H', 'localhost',
|
|
'--ssl',
|
|
'--cacert',
|
|
'test/tls/key.pem',
|
|
'--cert',
|
|
'test/tls/cert.pem',
|
|
'--key',
|
|
'test/tls/key.pem']
|
|
|
|
with self.assertRaises(SystemExit) as test:
|
|
main(args)
|
|
|
|
self.assertTrue('Error loading SSL CA' in str(mock_print.call_args))
|
|
self.assertEqual(test.exception.code, 3)
|