From acf52be716d2791220c171174c9c979ab3bcaf47 Mon Sep 17 00:00:00 2001 From: serosset Date: Tue, 1 Feb 2022 14:20:01 +0000 Subject: [PATCH] fix linter issues, add changelog --- changelogs/fragments/add_ipfilter_doc_ut.yaml | 5 + plugins/plugin_utils/base/ipaddr_utils.py | 2 +- tests/unit/plugins/action/test_cli_parse.py | 100 +++++++----------- 3 files changed, 42 insertions(+), 65 deletions(-) create mode 100644 changelogs/fragments/add_ipfilter_doc_ut.yaml diff --git a/changelogs/fragments/add_ipfilter_doc_ut.yaml b/changelogs/fragments/add_ipfilter_doc_ut.yaml new file mode 100644 index 00000000..9183293d --- /dev/null +++ b/changelogs/fragments/add_ipfilter_doc_ut.yaml @@ -0,0 +1,5 @@ +--- +minor_changes: + - Add example for IPv6 in ipsubnet filter documentation. + - Fix typo in ipsubnet documentation. + - Add unit tests for ipsubnet filter. diff --git a/plugins/plugin_utils/base/ipaddr_utils.py b/plugins/plugin_utils/base/ipaddr_utils.py index 59b8e063..fe020ce0 100644 --- a/plugins/plugin_utils/base/ipaddr_utils.py +++ b/plugins/plugin_utils/base/ipaddr_utils.py @@ -368,7 +368,7 @@ def _wrap_query(v, vtype, value): def ipaddr(value, query="", version=False, alias="ipaddr"): - """ Check if string is an IP address or network and filter it """ + """Check if string is an IP address or network and filter it""" query_func_extra_args = { "": ("vtype",), diff --git a/tests/unit/plugins/action/test_cli_parse.py b/tests/unit/plugins/action/test_cli_parse.py index 901033e3..7573bb7e 100644 --- a/tests/unit/plugins/action/test_cli_parse.py +++ b/tests/unit/plugins/action/test_cli_parse.py @@ -58,7 +58,7 @@ def setUp(self): @staticmethod def _load_fixture(filename): - """ Load a fixture from the filesystem + """Load a fixture from the filesystem :param filename: The name of the file to load :type filename: str @@ -72,23 +72,20 @@ def _load_fixture(filename): return fhand.read() def test_fn_debug(self): - """ Confirm debug doesn't fail and return None - """ + """Confirm debug doesn't fail and return None""" msg = "some message" result = self._plugin._debug(msg) self.assertEqual(result, None) def test_fn_ail_json(self): - """ Confirm fail json replaces basic.py in msg - """ + """Confirm fail json replaces basic.py in msg""" msg = "text (basic.py)" with self.assertRaises(Exception) as error: self._plugin._fail_json(msg) self.assertEqual("text cli_parse", str(error.exception)) def test_fn_check_argspec_pass(self): - """ Confirm a valid argspec passes - """ + """Confirm a valid argspec passes""" kwargs = { "text": "text", "parser": { @@ -102,8 +99,7 @@ def test_fn_check_argspec_pass(self): self.assertEqual(valid, True) def test_fn_check_argspec_fail_no_test_or_command(self): - """ Confirm failed argpsec w/o text or command - """ + """Confirm failed argpsec w/o text or command""" kwargs = { "parser": { "name": "ansible.utils.textfsm", @@ -122,8 +118,7 @@ def test_fn_check_argspec_fail_no_test_or_command(self): ) def test_fn_check_argspec_fail_no_parser_name(self): - """ Confirm failed argspec no parser name - """ + """Confirm failed argspec no parser name""" kwargs = {"text": "anything", "parser": {"command": "show version"}} valid, result, updated_params = check_argspec( DOCUMENTATION, @@ -137,8 +132,7 @@ def test_fn_check_argspec_fail_no_parser_name(self): ) def test_fn_extended_check_argspec_parser_name_not_coll(self): - """ Confirm failed argpsec parser not collection format - """ + """Confirm failed argpsec parser not collection format""" self._plugin._task.args = { "text": "anything", "parser": { @@ -151,7 +145,7 @@ def test_fn_extended_check_argspec_parser_name_not_coll(self): self.assertIn("including collection", self._plugin._result["msg"]) def test_fn_extended_check_argspec_missing_tpath_or_command(self): - """ Confirm failed argpsec missing template_path + """Confirm failed argpsec missing template_path or command when text provided """ self._plugin._task.args = { @@ -165,8 +159,7 @@ def test_fn_extended_check_argspec_missing_tpath_or_command(self): ) def test_fn_load_parser_pass(self): - """ Confirm each each of the parsers loads from the filesystem - """ + """Confirm each each of the parsers loads from the filesystem""" parser_names = ["json", "textfsm", "ttp", "xml"] for parser_name in parser_names: self._plugin._task.args = { @@ -179,8 +172,7 @@ def test_fn_load_parser_pass(self): self.assertTrue(callable(parser.parse)) def test_fn_load_parser_fail(self): - """ Confirm missing parser fails gracefully - """ + """Confirm missing parser fails gracefully""" self._plugin._task.args = { "text": "anything", "parser": {"name": "a.b.c"}, @@ -191,7 +183,7 @@ def test_fn_load_parser_fail(self): self.assertIn("No module named", self._plugin._result["msg"]) def test_fn_set_parser_command_missing(self): - """ Confirm parser/command is set if missing + """Confirm parser/command is set if missing and command provided """ self._plugin._task.args = { @@ -204,8 +196,7 @@ def test_fn_set_parser_command_missing(self): ) def test_fn_set_parser_command_present(self): - """ Confirm parser/command is not changed if provided - """ + """Confirm parser/command is not changed if provided""" self._plugin._task.args = { "command": "anything", "parser": {"command": "something", "name": "a.b.c"}, @@ -216,15 +207,13 @@ def test_fn_set_parser_command_present(self): ) def test_fn_set_parser_command_absent(self): - """ Confirm parser/command is not added - """ + """Confirm parser/command is not added""" self._plugin._task.args = {"parser": {}} self._plugin._set_parser_command() self.assertNotIn("command", self._plugin._task.args["parser"]) def test_fn_set_text_present(self): - """ Check task args text is set to stdout - """ + """Check task args text is set to stdout""" expected = "output" self._plugin._result["stdout"] = expected self._plugin._task.args = {} @@ -232,16 +221,14 @@ def test_fn_set_text_present(self): self.assertEqual(self._plugin._task.args["text"], expected) def test_fn_set_text_absent(self): - """ Check task args text is set to stdout - """ + """Check task args text is set to stdout""" self._plugin._result["stdout"] = None self._plugin._task.args = {} self._plugin._set_text() self.assertNotIn("text", self._plugin._task.args) def test_fn_os_from_task_vars(self): - """ Confirm os is set based on task vars - """ + """Confirm os is set based on task vars""" checks = [ ("ansible_network_os", "cisco.nxos.nxos", "nxos"), ("ansible_network_os", "NXOS", "nxos"), @@ -254,7 +241,7 @@ def test_fn_os_from_task_vars(self): self.assertEqual(result, check[2]) def test_fn_update_template_path_not_exist(self): - """ Check the creation of the template_path if + """Check the creation of the template_path if it doesn't exist in the user provided data """ self._plugin._task.args = { @@ -269,7 +256,7 @@ def test_fn_update_template_path_not_exist(self): ) def test_fn_update_template_path_not_exist_os(self): - """ Check the creation of the template_path if + """Check the creation of the template_path if it doesn't exist in the user provided data name based on os provided in task """ @@ -284,7 +271,7 @@ def test_fn_update_template_path_not_exist_os(self): ) def test_fn_update_template_path_mock_find_needle(self): - """ Check the creation of the template_path + """Check the creation of the template_path mock the find needle fn so the template doesn't need to be in the default template folder """ @@ -302,8 +289,7 @@ def test_fn_update_template_path_mock_find_needle(self): ) def test_fn_get_template_contents_pass(self): - """ Check the retrieval of the template contents - """ + """Check the retrieval of the template contents""" temp = tempfile.NamedTemporaryFile() contents = "abcdef" with open(temp.name, "w") as fileh: @@ -314,8 +300,7 @@ def test_fn_get_template_contents_pass(self): self.assertEqual(result, contents) def test_fn_get_template_contents_missing(self): - """ Check the retrieval of the template contents - """ + """Check the retrieval of the template contents""" self._plugin._task.args = {"parser": {"template_path": "non-exist"}} with self.assertRaises(Exception) as error: self._plugin._get_template_contents() @@ -324,15 +309,13 @@ def test_fn_get_template_contents_missing(self): ) def test_fn_get_template_contents_not_specified(self): - """ Check the none when template_path not specified - """ + """Check the none when template_path not specified""" self._plugin._task.args = {"parser": {}} result = self._plugin._get_template_contents() self.assertIsNone(result) def test_fn_prune_result_pass(self): - """ Test the removal of stdout and stdout_lines from the _result - """ + """Test the removal of stdout and stdout_lines from the _result""" self._plugin._result["stdout"] = "abc" self._plugin._result["stdout_lines"] = "abc" self._plugin._prune_result() @@ -340,15 +323,13 @@ def test_fn_prune_result_pass(self): self.assertNotIn("stdout_lines", self._plugin._result) def test_fn_prune_result_not_exist(self): - """ Test the removal of stdout and stdout_lines from the _result - """ + """Test the removal of stdout and stdout_lines from the _result""" self._plugin._prune_result() self.assertNotIn("stdout", self._plugin._result) self.assertNotIn("stdout_lines", self._plugin._result) def test_fn_run_command_lx_rc0(self): - """ Check run command for non network - """ + """Check run command for non network""" response = "abc" self._plugin._connection.socket_path = None self._plugin._low_level_execute_command = MagicMock() @@ -363,8 +344,7 @@ def test_fn_run_command_lx_rc0(self): self.assertEqual(self._plugin._result["stdout_lines"], response) def test_fn_run_command_lx_rc1(self): - """ Check run command for non network - """ + """Check run command for non network""" response = "abc" self._plugin._connection.socket_path = None self._plugin._low_level_execute_command = MagicMock() @@ -381,8 +361,7 @@ def test_fn_run_command_lx_rc1(self): @patch("ansible.module_utils.connection.Connection.__rpc__") def test_fn_run_command_network(self, mock_rpc): - """ Check run command for network - """ + """Check run command for network""" expected = "abc" mock_rpc.return_value = expected self._plugin._connection.socket_path = ( @@ -394,16 +373,14 @@ def test_fn_run_command_network(self, mock_rpc): self.assertEqual(self._plugin._result["stdout_lines"], [expected]) def test_fn_run_command_not_specified(self): - """ Check run command for network - """ + """Check run command for network""" self._plugin._task.args = {"command": None} result = self._plugin._run_command() self.assertIsNone(result) @patch("ansible.module_utils.connection.Connection.__rpc__") def test_fn_run_pass_w_fact(self, mock_rpc): - """ Check full module run with valid params - """ + """Check full module run with valid params""" mock_out = self._load_fixture("nxos_show_version.txt") mock_rpc.return_value = mock_out self._plugin._connection.socket_path = ( @@ -431,8 +408,7 @@ def test_fn_run_pass_w_fact(self, mock_rpc): @patch("ansible.module_utils.connection.Connection.__rpc__") def test_fn_run_pass_wo_fact(self, mock_rpc): - """ Check full module run with valid params - """ + """Check full module run with valid params""" mock_out = self._load_fixture("nxos_show_version.txt") mock_rpc.return_value = mock_out self._plugin._connection.socket_path = ( @@ -456,8 +432,7 @@ def test_fn_run_pass_wo_fact(self, mock_rpc): self.assertNotIn("ansible_facts", result) def test_fn_run_fail_argspec(self): - """ Check full module run with invalid params - """ + """Check full module run with invalid params""" self._plugin._task.args = { "text": "anything", "parser": { @@ -470,8 +445,7 @@ def test_fn_run_fail_argspec(self): self.assertIn("including collection", self._plugin._result["msg"]) def test_fn_run_fail_command(self): - """ Confirm clean fail with rc 1 - """ + """Confirm clean fail with rc 1""" self._plugin._connection.socket_path = None self._plugin._low_level_execute_command = MagicMock() self._plugin._low_level_execute_command.return_value = { @@ -495,8 +469,7 @@ def test_fn_run_fail_command(self): self.assertEqual(result, expected) def test_fn_run_fail_missing_parser(self): - """Confirm clean fail with missing parser - """ + """Confirm clean fail with missing parser""" self._plugin._task.args = {"text": None, "parser": {"name": "a.b.c"}} task_vars = {"inventory_hostname": "mockdevice"} result = self._plugin.run(task_vars=task_vars) @@ -505,7 +478,7 @@ def test_fn_run_fail_missing_parser(self): @patch("ansible.module_utils.connection.Connection.__rpc__") def test_fn_run_pass_missing_parser_constants(self, mock_rpc): - """ Check full module run using parser w/o + """Check full module run using parser w/o DEFAULT_TEMPLATE_EXTENSION or PROVIDE_TEMPLATE_CONTENTS defined in the parser """ @@ -542,7 +515,7 @@ def parse(self, *_args, **kwargs): @patch("ansible.module_utils.connection.Connection.__rpc__") def test_fn_run_pass_missing_parser_in_parser(self, mock_rpc): - """ Check full module run using parser w/o + """Check full module run using parser w/o a parser function defined in the parser defined in the parser """ @@ -577,8 +550,7 @@ class CliParser(CliParserBase): @patch("ansible.module_utils.connection.Connection.__rpc__") def test_fn_run_net_device_error(self, mock_rpc): - """ Check full module run mock error from network device - """ + """Check full module run mock error from network device""" msg = "I was mocked" mock_rpc.side_effect = AnsibleConnectionError(msg) self._plugin._connection.socket_path = (