diff --git a/src/sonic_ax_impl/utils/arg_parser.py b/src/sonic_ax_impl/utils/arg_parser.py index 60f856a36..788c4b2f9 100644 --- a/src/sonic_ax_impl/utils/arg_parser.py +++ b/src/sonic_ax_impl/utils/arg_parser.py @@ -1,22 +1,13 @@ -""" -Syslog and daemon script utility library. -""" - from __future__ import print_function -import json -import logging -import logging.config import sys from getopt import getopt -# TODO: move to dbsync project. def usage(script_name): print('Usage: python ', script_name, - '-t [host] -p [port] -s [unix_socket_path] -d [logging_level] -f [update_frequency] -h [help]') + '-t [host] -p [port] -s [unix_socket_path] -d [logging_level] -f [update_frequency] -r [enable_dynamic_frequency] -h [help]') -# TODO: move to dbsync project. def process_options(script_name): """ Process command line options @@ -32,7 +23,7 @@ def process_options(script_name): args['host'] = arg elif opt in ('-p', '--port'): args['port'] = int(arg) - elif opt in ('-s', 'unix_socket_path'): + elif opt in ('-s', '--unix_socket_path'): args['unix_socket_path'] = arg elif opt in ('-f', '--frequency'): args['update_frequency'] = int(arg) @@ -46,25 +37,3 @@ def process_options(script_name): sys.exit(1) return args - - -# TODO: move -def setup_logging(config_file_path, log_level=logging.INFO): - """ - Logging configuration helper. - - :param config_file_path: file path to logging configuration file. - https://docs.python.org/3/library/logging.config.html#object-connections - :param log_level: defaults to logging.INFO - :return: None - access the logger by name as described in the config--or the "root" logger as a backup. - """ - try: - with open(config_file_path, 'rt') as f: - config = json.load(f) - logging.config.dictConfig(config) - except (ValueError, IOError, OSError): - # json.JSONDecodeError is throwable in Python3.5+ -- subclass of ValueError - logging.basicConfig(log_level=log_level) - logging.root.exception( - "Could not load specified logging configuration '{}'. Verify the filepath exists and is compliant with: " - "[https://docs.python.org/3/library/logging.config.html#object-connections]".format(config_file_path)) diff --git a/tests/test_arg_parser.py b/tests/test_arg_parser.py new file mode 100644 index 000000000..d8630028e --- /dev/null +++ b/tests/test_arg_parser.py @@ -0,0 +1,141 @@ +from unittest import TestCase +from unittest.mock import patch + +import pytest +from sonic_ax_impl.utils.arg_parser import process_options + + +class TestUtil(TestCase): + + # Given: Don't pass any parameter + # When: Parse args + # Then: Return empty dict + @patch('sys.argv', ['sonic_ax_impl']) + def test_valid_options_default_value_none(self): + args = process_options("sonic_ax_impl") + + self.assertNotIn("log_level", args) + self.assertNotIn("host", args) + self.assertNotIn("port", args) + self.assertNotIn("unix_socket_path", args) + self.assertNotIn("update_frequency", args) + self.assertNotIn("enable_dynamic_frequency", args) + + # Given: Pass -h + # When: Parse args + # Then: Print help logs + @patch('builtins.print') + @patch('sys.argv', ['sonic_ax_impl', '-h']) + def test_valid_options_help(self, mock_print): + with pytest.raises(SystemExit) as excinfo: + process_options("sonic_ax_impl") + assert excinfo.value.code == 0 + mock_print.assert_called_with('Usage: python ', 'sonic_ax_impl', '-t [host] -p [port] -s [unix_socket_path] -d [logging_level] -f [update_frequency] -r [enable_dynamic_frequency] -h [help]') + + # Given: Pass help + # When: Parse args + # Then: Print help logs + @patch('builtins.print') + @patch('sys.argv', ['sonic_ax_impl', '--help']) + def test_valid_options_help_long(self, mock_print): + with pytest.raises(SystemExit) as excinfo: + process_options("sonic_ax_impl") + assert excinfo.value.code == 0 + mock_print.assert_called_with('Usage: python ', 'sonic_ax_impl', '-t [host] -p [port] -s [unix_socket_path] -d [logging_level] -f [update_frequency] -r [enable_dynamic_frequency] -h [help]') + + # Given: Pass -r + # When: Parse args + # Then: Enable enable_dynamic_frequency + @patch('sys.argv', ['sonic_ax_impl', '-r']) + def test_valid_options_enable_dynamic_frequency(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["enable_dynamic_frequency"], True) + + # Given: Pass --enable_dynamic_frequency + # When: Parse args + # Then: Enable enable_dynamic_frequency + @patch('sys.argv', ['sonic_ax_impl', '--enable_dynamic_frequency']) + def test_valid_options_enable_dynamic_frequency_long(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["enable_dynamic_frequency"], True) + + # Given: Pass -f + # When: Parse args + # Then: Enable enable_dynamic_frequency + @patch('sys.argv', ['sonic_ax_impl', '-f9']) + def test_valid_options_update_frequency(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["update_frequency"], 9) + + # Given: Pass --frequency + # When: Parse args + # Then: Enable enable_dynamic_frequency + @patch('sys.argv', ['sonic_ax_impl', '--frequency=9']) + def test_valid_options_update_frequency_long(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["update_frequency"], 9) + + # Given: Pass -s + # When: Parse args + # Then: Parse socket + @patch('sys.argv', ['sonic_ax_impl', '-s/unix/socket']) + def test_valid_options_socket(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["unix_socket_path"], "/unix/socket") + + # Given: Pass --unix_socket_path + # When: Parse args + # Then: Parse socket + @patch('sys.argv', ['sonic_ax_impl', '--unix_socket_path=/unix/socket']) + def test_valid_options_socket_long(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["unix_socket_path"], "/unix/socket") + + # Given: Pass -p + # When: Parse args + # Then: Parse port + @patch('sys.argv', ['sonic_ax_impl', '-p6666']) + def test_valid_options_port(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["port"], 6666) + + # Given: Pass --port + # When: Parse args + # Then: Parse port + @patch('sys.argv', ['sonic_ax_impl', '--port=6666']) + def test_valid_options_port_long(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["port"], 6666) + + # Given: Pass -t + # When: Parse args + # Then: Parse host + @patch('sys.argv', ['sonic_ax_impl', '-ttest.snmp.com']) + def test_valid_options_host(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["host"], 'test.snmp.com') + + # Given: Pass --host + # When: Parse args + # Then: Parse host + @patch('sys.argv', ['sonic_ax_impl', '--host=test.snmp.com']) + def test_valid_options_host_long(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["host"], 'test.snmp.com') + + # Given: Pass -d + # When: Parse args + # Then: Parse log_level + @patch('sys.argv', ['sonic_ax_impl', '-d9']) + def test_valid_options_host(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["log_level"], 9) + + # Given: Pass --debug + # When: Parse args + # Then: Parse log_level + @patch('sys.argv', ['sonic_ax_impl', '--debug=9']) + def test_valid_options_host_long(self): + args = process_options("sonic_ax_impl") + self.assertEqual(args["log_level"], 9) +