Add unittest for argsparse

This commit is contained in:
Markus Opolka 2020-03-03 12:12:52 +01:00
parent 375da5d605
commit b7c0b0595e
2 changed files with 33 additions and 3 deletions

View File

@ -398,7 +398,7 @@ class JsonRuleProcessor:
return ("%s" % metrics, warning, critical) return ("%s" % metrics, warning, critical)
def parseArgs(): def parseArgs(args):
""" """
CLI argument definitions and parsing CLI argument definitions and parsing
""" """
@ -497,7 +497,7 @@ def parseArgs():
(key[>alias],UnitOfMeasure,WarnRange, (key[>alias],UnitOfMeasure,WarnRange,
CriticalRange).''') CriticalRange).''')
return parser.parse_args() return parser.parse_args(args)
def debugPrint(debug_flag, message, pretty_flag=False): def debugPrint(debug_flag, message, pretty_flag=False):
@ -514,7 +514,8 @@ def debugPrint(debug_flag, message, pretty_flag=False):
"""Program entry point""" """Program entry point"""
if __name__ == "__main__": if __name__ == "__main__":
args = parseArgs()
args = parseArgs(sys.argv[1:])
nagios = NagiosHelper() nagios = NagiosHelper()
context = None context = None

29
test/test_args.py Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
import unittest
import sys
sys.path.append('..')
from check_http_json import *
class ArgsTest(unittest.TestCase):
"""
Tests for argsparse
"""
def test_parser_defaults(self):
parser = parseArgs(['-H', 'foobar'])
self.assertFalse(parser.debug)
self.assertFalse(parser.ssl)
self.assertFalse(parser.insecure)
def test_parser_with_debug(self):
parser = parseArgs(['-H', 'foobar', '-d'])
self.assertTrue(parser.debug)
def test_parser_with_port(self):
parser = parseArgs(['-H', 'foobar', '-P', '8888'])
self.assertEqual(parser.port, '8888')