diff --git a/plugins/module_utils/data_set.py b/plugins/module_utils/data_set.py index 8bc62b15..01beaa83 100644 --- a/plugins/module_utils/data_set.py +++ b/plugins/module_utils/data_set.py @@ -141,12 +141,16 @@ def get_target_method(self, target): # type: (str) -> [str | invalid_target_sta }.get(target, self.invalid_target_state) def get_data_set_state(self, data_set): # type: (dict) -> dict - listds_executions, ds_status = dataset_utils._run_listds(data_set["name"]) + try: + listds_executions, ds_status = dataset_utils._run_listds(data_set["name"]) - data_set["exists"] = ds_status["exists"] - data_set["vsam"] = ds_status["vsam"] + data_set["exists"] = ds_status["exists"] + data_set["vsam"] = ds_status["vsam"] - self.result["executions"] = self.result["executions"] + listds_executions + self.result["executions"] = self.result["executions"] + listds_executions + except Exception as e: + self.result["executions"] = self.result["executions"] + e.args[1] + self._fail(e.args[0]) return data_set diff --git a/plugins/module_utils/dataset_utils.py b/plugins/module_utils/dataset_utils.py index 1b563a68..e7ab9d2e 100644 --- a/plugins/module_utils/dataset_utils.py +++ b/plugins/module_utils/dataset_utils.py @@ -5,13 +5,12 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from typing import Dict, List from ansible_collections.ibm.ibm_zos_core.plugins.module_utils.mvs_cmd import idcams, ikjeft01 from ansible_collections.ibm.ibm_zos_cics.plugins.module_utils.response import _execution, _state -def _dataset_size(unit, primary, secondary): # type: (str,int,int) -> Dict +def _dataset_size(unit, primary, secondary): # type: (str,int,int) -> dict return { "unit": unit, "primary": primary, @@ -19,7 +18,7 @@ def _dataset_size(unit, primary, secondary): # type: (str,int,int) -> Dict } -def _run_idcams(cmd, name, location, delete=False): # type: (str, str, str, bool) -> List +def _run_idcams(cmd, name, location, delete=False): # type: (str, str, str, bool) -> list executions = [] for x in range(10): @@ -73,7 +72,7 @@ def _get_dataset_size_unit(unit_symbol): # type: (str) -> str }.get(unit_symbol, "MEGABYTES") -def _build_idcams_define_cmd(dataset): # type: (Dict) -> str +def _build_idcams_define_cmd(dataset): # type: (dict) -> str index_statement = (""" - INDEX({0})""".format(_build_idcams_define_index_parms(dataset)) if dataset.get("INDEX", None) @@ -88,7 +87,7 @@ def _build_idcams_define_cmd(dataset): # type: (Dict) -> str index_statement) -def _build_idcams_define_cluster_parms(dataset): # type: (Dict) -> str +def _build_idcams_define_cluster_parms(dataset): # type: (dict) -> str clusterStr = "NAME({0}) -\n {1}({2} {3})".format( dataset["name"], @@ -111,7 +110,7 @@ def _build_idcams_define_cluster_parms(dataset): # type: (Dict) -> str return clusterStr -def _build_idcams_define_data_parms(dataset): # type: (Dict) -> str +def _build_idcams_define_data_parms(dataset): # type: (dict) -> str dataStr = "NAME({0}.DATA)".format(dataset["name"]) if isinstance(dataset["DATA"], dict): dataStr += " -\n " @@ -128,7 +127,7 @@ def _build_idcams_define_data_parms(dataset): # type: (Dict) -> str return dataStr -def _build_idcams_define_index_parms(dataset): # type: (Dict) -> str +def _build_idcams_define_index_parms(dataset): # type: (dict) -> str indexStr = "NAME({0}.INDEX)".format(dataset["name"]) if isinstance(dataset["INDEX"], dict): indexStr += " -\n " @@ -145,7 +144,7 @@ def _build_idcams_define_index_parms(dataset): # type: (Dict) -> str return indexStr -def _run_listds(location): # type: (str) -> [List, _state] +def _run_listds(location): # type: (str) -> [list, _state] cmd = " LISTDS '{0}'".format(location) executions = [] @@ -162,7 +161,7 @@ def _run_listds(location): # type: (str) -> [List, _state] break if location.upper() not in stdout.upper(): - raise Exception("LISTDS Command output not recognised") + raise Exception("LISTDS Command output not recognised", executions) # DS Name in output, good output @@ -172,7 +171,7 @@ def _run_listds(location): # type: (str) -> [List, _state] # Exists if rc != 0: - raise Exception("RC {0} running LISTDS Command".format(rc)) + raise Exception("RC {0} running LISTDS Command".format(rc), executions) # Exists, RC 0 @@ -186,7 +185,7 @@ def _run_listds(location): # type: (str) -> [List, _state] return executions, _state(exists=True, vsam=True) -def _data_set(size, name, state, exists, vsam, **kwargs): # type: (_dataset_size, str, str, bool, bool, Dict) -> Dict +def _data_set(size, name, state, exists, vsam, **kwargs): # type: (_dataset_size, str, str, bool, bool, dict) -> dict data_set = { "size": size, "name": name, diff --git a/plugins/modules/global_catalog.py b/plugins/modules/global_catalog.py index 69c49bb5..19c8d974 100644 --- a/plugins/modules/global_catalog.py +++ b/plugins/modules/global_catalog.py @@ -241,7 +241,7 @@ _run_dfhrmutl, _get_idcams_cmd_gcd) from ansible_collections.ibm.ibm_zos_cics.plugins.module_utils.data_set import DataSet from ansible_collections.ibm.ibm_zos_cics.plugins.module_utils.dataset_utils import ( - _dataset_size, _run_listds, _data_set, _build_idcams_define_cmd) + _dataset_size, _data_set, _build_idcams_define_cmd) from ansible_collections.ibm.ibm_zos_core.plugins.module_utils.better_arg_parser import BetterArgParser from typing import Dict @@ -484,24 +484,24 @@ def get_target_method(self, target): }.get(target, self.invalid_target_state) def get_data_set_state(self, data_set): - listds_executions, ds_status = _run_listds(data_set["name"]) - - data_set["exists"] = ds_status['exists'] - data_set["vsam"] = ds_status['vsam'] - - self.result["executions"] = self.result["executions"] + listds_executions + super().get_data_set_state(data_set) if data_set["exists"] and data_set["vsam"]: - dfhrmutl_executions, catalog_status = _run_dfhrmutl( - data_set["name"], data_set[ds_constants["SDFHLOAD_ALIAS"]]) + try: + dfhrmutl_executions, catalog_status = _run_dfhrmutl( + data_set["name"], data_set[ds_constants["SDFHLOAD_ALIAS"]]) - data_set["autostart_override"] = catalog_status['autostart_override'] - data_set["nextstart"] = catalog_status['next_start'] + data_set["autostart_override"] = catalog_status['autostart_override'] + data_set["nextstart"] = catalog_status['next_start'] - self.result["executions"] = self.result["executions"] + dfhrmutl_executions + self.result["executions"] = self.result["executions"] + dfhrmutl_executions + except Exception as e: + self.result["executions"] = self.result["executions"] + e.args[1] + self._fail(e.args[0]) else: data_set["autostart_override"] = "" data_set["nextstart"] = "" + return data_set def main(self): diff --git a/tests/unit/module_utils/test_dataset_utils.py b/tests/unit/module_utils/test_dataset_utils.py index 008ca1be..fc59504f 100644 --- a/tests/unit/module_utils/test_dataset_utils.py +++ b/tests/unit/module_utils/test_dataset_utils.py @@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type from ansible_collections.ibm.ibm_zos_cics.plugins.module_utils import dataset_utils -import pytest +from ansible_collections.ibm.ibm_zos_cics.plugins.module_utils.response import _execution try: from unittest.mock import MagicMock except ImportError: @@ -347,16 +347,19 @@ def test__run_listds_exists_not_vsam(): def test__run_listds_bad_rc(): + name = "IKJEFT01 - Get Data Set Status - Run 1" rc = 16 - stdout = "" + stdout = "ANSIBIT.CICS.TESTS.A365D7A.DFHGCD" stderr = "" dataset_utils.ikjeft01 = MagicMock(return_value=[rc, stdout, stderr]) - with pytest.raises(Exception) as e_info: - result_exececutions, result_status = dataset_utils._run_listds( - "ANSIBIT.CICS.TESTS.A365D7A.DFHGCD") + expected_executions = [_execution(name=name, rc=rc, stdout=stdout, stderr=stderr)] - assert e_info == "LISTDS Command output not recognised" + try: + dataset_utils._run_listds("ANSIBIT.CICS.TESTS.A365D7A.DFHGCD") + except Exception as e: + assert e.args[0] == "RC 16 running LISTDS Command" + assert e.args[1] == expected_executions def test__run_listds_not_exists(): @@ -391,3 +394,29 @@ def test__run_listds_not_exists(): "exists": False, "vsam": False, } + + +def test__run_listds_with_no_zoau_response(): + rc = 0 + stdout = "" + stderr = "" + dataset_utils.ikjeft01 = MagicMock(return_value=[rc, stdout, stderr]) + + expected_executions = [ + _execution(name="IKJEFT01 - Get Data Set Status - Run 1", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 2", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 3", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 4", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 5", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 6", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 7", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 8", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 9", rc=rc, stdout=stdout, stderr=stderr), + _execution(name="IKJEFT01 - Get Data Set Status - Run 10", rc=rc, stdout=stdout, stderr=stderr) + ] + + try: + dataset_utils._run_listds("LOCATION THATS NOT IN STDOUT") + except Exception as e: + assert e.args[0] == "LISTDS Command output not recognised" + assert e.args[1] == expected_executions diff --git a/tests/unit/modules/test_global_catalog.py b/tests/unit/modules/test_global_catalog.py index fa333f5c..05222afc 100644 --- a/tests/unit/modules/test_global_catalog.py +++ b/tests/unit/modules/test_global_catalog.py @@ -214,6 +214,7 @@ def test_error_warm_start_a_unused_global_catalog(): assert gcd_module.result == expected_result +@pytest.mark.skipif(sys.version_info.major < 3, reason="Requires python 3 language features") def test_error_warm_start_a_non_existent_global_catalog(): gcd_module = initialise_module(state="warm") @@ -251,6 +252,7 @@ def test_error_warm_start_a_non_existent_global_catalog(): assert gcd_module.result == expected_result +@pytest.mark.skipif(sys.version_info.major < 3, reason="Requires python 3 language features") def tests_cold_start_non_existent_catalog(): gcd_module = initialise_module(state="cold") @@ -288,6 +290,7 @@ def tests_cold_start_non_existent_catalog(): assert gcd_module.result == expected_result +@pytest.mark.skipif(sys.version_info.major < 3, reason="Requires python 3 language features") def test_cold_start_unused_catalog(): gcd_module = initialise_module(state="cold") @@ -337,6 +340,7 @@ def test_cold_start_unused_catalog(): assert gcd_module.result == expected_result +@pytest.mark.skipif(sys.version_info.major < 3, reason="Requires python 3 language features") def test_cold_start_global_catalog(): gcd_module = initialise_module(state="cold")