nagios-http-json/test/test_main.py

63 lines
1.4 KiB
Python
Raw Normal View History

2020-03-15 10:53:00 +01:00
#!/usr/bin/env python3
import unittest
import unittest.mock as mock
import sys
2020-03-15 11:04:50 +01:00
import os
2020-03-15 10:53:00 +01:00
sys.path.append('..')
from check_http_json import main
class MockResponse():
def __init__(self, status_code=200, content='{}'):
self.status_code = status_code
self.content = content
def read(self):
return self.content
2020-03-15 10:53:00 +01:00
class MainTest(unittest.TestCase):
"""
Tests for Main
2020-03-15 10:53:00 +01:00
"""
@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)
2020-03-15 11:04:50 +01:00
self.assertEqual(test.exception.code, 0)
2020-03-15 11:04:50 +01:00
2020-03-15 10:53:00 +01:00
@mock.patch('builtins.print')
@mock.patch('urllib.request.urlopen')
def test_main_with_error(self, mock_request, mock_print):
args = '-H localhost'.split(' ')
2020-03-15 11:04:50 +01:00
mock_request.return_value = MockResponse(content='not JSON')
2020-03-15 11:04:50 +01:00
with self.assertRaises(SystemExit) as test:
main(args)
2020-03-15 11:04:50 +01:00
# Returns Parser Error
self.assertEqual(test.exception.code, 3)