-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2573dd5
commit e5f4cd0
Showing
2 changed files
with
143 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|