Skip to content

Commit

Permalink
Add exception handling for LISTDS
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhughes101 committed Dec 21, 2023
1 parent da7842f commit 4e210ac
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
12 changes: 8 additions & 4 deletions plugins/module_utils/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 10 additions & 11 deletions plugins/module_utils/dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@

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,
"secondary": secondary
}


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):
Expand Down Expand Up @@ -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)
Expand All @@ -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"],
Expand All @@ -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 "
Expand All @@ -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 "
Expand All @@ -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 = []

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions plugins/modules/global_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
41 changes: 35 additions & 6 deletions tests/unit/module_utils/test_dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions tests/unit/modules/test_global_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 4e210ac

Please sign in to comment.