mirror of
https://github.com/drewkerrigan/nagios-http-json.git
synced 2024-11-22 10:23:50 +01:00
Move main entrypoint to own function for simpler testing
This commit is contained in:
parent
b9a583f281
commit
941afeed89
@ -424,7 +424,7 @@ def parseArgs(args):
|
|||||||
parser.add_argument('-s', '--ssl', action='store_true',
|
parser.add_argument('-s', '--ssl', action='store_true',
|
||||||
help='use TLS to connect to remote host')
|
help='use TLS to connect to remote host')
|
||||||
parser.add_argument('-H', '--host', dest='host',
|
parser.add_argument('-H', '--host', dest='host',
|
||||||
required=not ('-V' in sys.argv or '--version' in sys.argv),
|
required=not ('-V' in args or '--version' in args),
|
||||||
help='remote host to query')
|
help='remote host to query')
|
||||||
parser.add_argument('-k', '--insecure', action='store_true',
|
parser.add_argument('-k', '--insecure', action='store_true',
|
||||||
help='do not check server SSL certificate')
|
help='do not check server SSL certificate')
|
||||||
@ -524,10 +524,12 @@ def debugPrint(debug_flag, message, pretty_flag=False):
|
|||||||
print(message)
|
print(message)
|
||||||
|
|
||||||
|
|
||||||
# Program entry point
|
def main(cliargs):
|
||||||
if __name__ == "__main__":
|
"""
|
||||||
|
Main entrypoint for CLI
|
||||||
|
"""
|
||||||
|
|
||||||
args = parseArgs(sys.argv[1:])
|
args = parseArgs(cliargs)
|
||||||
nagios = NagiosHelper()
|
nagios = NagiosHelper()
|
||||||
context = None
|
context = None
|
||||||
|
|
||||||
@ -630,4 +632,9 @@ if __name__ == "__main__":
|
|||||||
print(nagios.getMessage())
|
print(nagios.getMessage())
|
||||||
sys.exit(nagios.getCode())
|
sys.exit(nagios.getCode())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Program entry point
|
||||||
|
main(sys.argv[1:])
|
||||||
|
|
||||||
#EOF
|
#EOF
|
||||||
|
44
test/test_cli.py
Normal file
44
test/test_cli.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import unittest.mock as mock
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
sys.path.append('..')
|
||||||
|
|
||||||
|
from check_http_json import debugPrint
|
||||||
|
|
||||||
|
|
||||||
|
class CLITest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Tests for CLI
|
||||||
|
"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""
|
||||||
|
Defining the exitcodes
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.exit_0 = 0 << 8
|
||||||
|
self.exit_1 = 1 << 8
|
||||||
|
self.exit_2 = 2 << 8
|
||||||
|
self.exit_3 = 3 << 8
|
||||||
|
|
||||||
|
def test_debugprint(self):
|
||||||
|
with mock.patch('builtins.print') as mock_print:
|
||||||
|
debugPrint(True, 'debug')
|
||||||
|
mock_print.assert_called_once_with('debug')
|
||||||
|
|
||||||
|
def test_debugprint_pprint(self):
|
||||||
|
with mock.patch('check_http_json.pprint') as mock_pprint:
|
||||||
|
debugPrint(True, 'debug', True)
|
||||||
|
mock_pprint.assert_called_once_with('debug')
|
||||||
|
|
||||||
|
def test_cli_without_params(self):
|
||||||
|
|
||||||
|
command = '/usr/bin/env python3 check_http_json.py > /dev/null 2>&1'
|
||||||
|
status = os.system(command)
|
||||||
|
|
||||||
|
self.assertEqual(status, self.exit_2)
|
@ -8,37 +8,55 @@ import os
|
|||||||
|
|
||||||
sys.path.append('..')
|
sys.path.append('..')
|
||||||
|
|
||||||
from check_http_json import debugPrint
|
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
|
||||||
|
|
||||||
|
|
||||||
class MainTest(unittest.TestCase):
|
class MainTest(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
Tests for main
|
Tests for Main
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
@mock.patch('builtins.print')
|
||||||
"""
|
def test_main_version(self, mock_print):
|
||||||
Defining the exitcodes
|
args = ['--version']
|
||||||
"""
|
|
||||||
|
|
||||||
self.exit_0 = 0 << 8
|
with self.assertRaises(SystemExit) as test:
|
||||||
self.exit_1 = 1 << 8
|
main(args)
|
||||||
self.exit_2 = 2 << 8
|
|
||||||
self.exit_3 = 3 << 8
|
|
||||||
|
|
||||||
def test_debugprint(self):
|
mock_print.assert_called_once()
|
||||||
with mock.patch('builtins.print') as mock_print:
|
self.assertEqual(test.exception.code, 0)
|
||||||
debugPrint(True, 'debug')
|
|
||||||
mock_print.assert_called_once_with('debug')
|
|
||||||
|
|
||||||
def test_debugprint_pprint(self):
|
@mock.patch('builtins.print')
|
||||||
with mock.patch('check_http_json.pprint') as mock_pprint:
|
@mock.patch('urllib.request.urlopen')
|
||||||
debugPrint(True, 'debug', True)
|
def test_main_with_ssl(self, mock_request, mock_print):
|
||||||
mock_pprint.assert_called_once_with('debug')
|
args = '-H localhost --ssl'.split(' ')
|
||||||
|
|
||||||
def test_cli_without_params(self):
|
mock_request.return_value = MockResponse()
|
||||||
|
|
||||||
command = '/usr/bin/env python3 check_http_json.py > /dev/null 2>&1'
|
with self.assertRaises(SystemExit) as test:
|
||||||
status = os.system(command)
|
main(args)
|
||||||
|
|
||||||
self.assertEqual(status, self.exit_2)
|
self.assertEqual(test.exception.code, 0)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('builtins.print')
|
||||||
|
@mock.patch('urllib.request.urlopen')
|
||||||
|
def test_main_with_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)
|
||||||
|
|
||||||
|
# Returns Parser Error
|
||||||
|
self.assertEqual(test.exception.code, 3)
|
||||||
|
Loading…
Reference in New Issue
Block a user