From b7c0b0595e233da7b41c233c21d2bd96912e4279 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Tue, 3 Mar 2020 12:12:52 +0100 Subject: [PATCH] Add unittest for argsparse --- check_http_json.py | 7 ++++--- test/test_args.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 test/test_args.py diff --git a/check_http_json.py b/check_http_json.py index 0b5e908..e4d70c3 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -398,7 +398,7 @@ class JsonRuleProcessor: return ("%s" % metrics, warning, critical) -def parseArgs(): +def parseArgs(args): """ CLI argument definitions and parsing """ @@ -497,7 +497,7 @@ def parseArgs(): (key[>alias],UnitOfMeasure,WarnRange, CriticalRange).''') - return parser.parse_args() + return parser.parse_args(args) def debugPrint(debug_flag, message, pretty_flag=False): @@ -514,7 +514,8 @@ def debugPrint(debug_flag, message, pretty_flag=False): """Program entry point""" if __name__ == "__main__": - args = parseArgs() + + args = parseArgs(sys.argv[1:]) nagios = NagiosHelper() context = None diff --git a/test/test_args.py b/test/test_args.py new file mode 100644 index 0000000..b2d90e4 --- /dev/null +++ b/test/test_args.py @@ -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')