1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Copyright (c) IBM Corporation 2024
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+
15
+ from __future__ import absolute_import , division , print_function
16
+
17
+ __metaclass__ = type
18
+
19
+ import pytest
20
+
21
+ from ansible_collections .ibm .ibm_zos_core .plugins .module_utils .data_set import MVSCmdExecError
22
+
23
+ IMPORT_NAME = "ansible_collections.ibm.ibm_zos_core.plugins.module_utils.data_set"
24
+
25
+
26
+ class DummyModule (object ):
27
+ """Used in place of Ansible's module
28
+ so we can easily mock the desired behavior."""
29
+
30
+ def __init__ (self , rc = 0 , stdout = "" , stderr = "" ):
31
+ self .rc = rc
32
+ self .stdout = stdout
33
+ self .stderr = stderr
34
+
35
+ def run_command (self , * args , ** kwargs ):
36
+ return (self .rc , self .stdout , self .stderr )
37
+
38
+
39
+ # Unit tests are intended to exercise code paths (not test for functionality).
40
+
41
+ # These unit tests are NOT run on any z/OS system, so hard-coded data set names will not matter.
42
+ data_set_name = "USER.PRIVATE.TESTDS"
43
+
44
+ stdout_ds_in_catatlog = """0
45
+ LISTCAT ENTRIES('{0}')
46
+ 0NONVSAM ------- {0}
47
+ IN-CAT --- CATALOG.SVPLEX9.MASTER
48
+ 1IDCAMS SYSTEM SERVICES """ .format (data_set_name )
49
+
50
+ stdout_ds_not_in_catalog = """
51
+ 1IDCAMS SYSTEM SERVICES TIME: 13:34:18 06/06/24 PAGE 1
52
+ 0
53
+ LISTCAT ENTRIES('{0}')
54
+ 0IDC3012I ENTRY {0} NOT FOUND
55
+ IDC3009I ** VSAM CATALOG RETURN CODE IS 8 - REASON CODE IS IGG0CLEG-42
56
+ IDC1566I ** {0} NOT LISTED
57
+ 1IDCAMS SYSTEM SERVICES TIME: 13:34:18 06/06/24 PAGE 2
58
+ 0 THE NUMBER OF ENTRIES PROCESSED WAS:
59
+ 0 AIX -------------------0
60
+ ALIAS -----------------0
61
+ CLUSTER ---------------0
62
+ DATA ------------------0
63
+ GDG -------------------0
64
+ INDEX -----------------0
65
+ NONVSAM ---------------0
66
+ PAGESPACE -------------0
67
+ PATH ------------------0
68
+ SPACE -----------------0
69
+ USERCATALOG -----------0
70
+ TAPELIBRARY -----------0
71
+ TAPEVOLUME ------------0
72
+ TOTAL -----------------0
73
+ 0 THE NUMBER OF PROTECTED ENTRIES SUPPRESSED WAS 0
74
+ 0IDC0001I FUNCTION COMPLETED, HIGHEST CONDITION CODE WAS 4
75
+ 0
76
+ 0IDC0002I IDCAMS PROCESSING COMPLETE. MAXIMUM CONDITION CODE WAS 4
77
+ """ .format (data_set_name )
78
+
79
+ # passing in a lowercase data set causes idcams to fail.
80
+ # this behavior isn't possible via ansible because we upper-case the input.
81
+ stdout_mvscmd_failed = """0
82
+ LISTCAT ENTRIES('...................')
83
+ 0IDC3203I ITEM '...................' DOES NOT ADHERE TO RESTRICTIONS
84
+ 0IDC3202I ABOVE TEXT BYPASSED UNTIL NEXT COMMAND. CONDITION CODE IS 12
85
+ 0
86
+ 0IDC0002I IDCAMS PROCESSING COMPLETE. MAXIMUM CONDITION CODE WAS 12"""
87
+
88
+
89
+ @pytest .mark .parametrize (
90
+ ("rc, stdout, expected_return, expected_exception_type" ),
91
+ [
92
+ (0 , stdout_ds_in_catatlog , True , None ),
93
+ (4 , stdout_ds_not_in_catalog , False , None ),
94
+ (12 , stdout_mvscmd_failed , None , MVSCmdExecError )
95
+ ],
96
+ )
97
+ def test_dataset_cataloged_unit (zos_import_mocker , rc , stdout , expected_return , expected_exception_type ):
98
+ mocker , importer = zos_import_mocker
99
+ zos_module_util_data_set = importer (IMPORT_NAME )
100
+ mocker .patch (
101
+ "{0}.AnsibleModuleHelper" .format (IMPORT_NAME ),
102
+ create = True ,
103
+ return_value = DummyModule (rc = rc , stdout = stdout ),
104
+ )
105
+
106
+
107
+ results = None
108
+ error_raised = False
109
+ try :
110
+ results = zos_module_util_data_set .DataSet .data_set_cataloged (data_set_name )
111
+ except Exception as e :
112
+ error_raised = True
113
+ assert type (e ) == expected_exception_type
114
+ finally :
115
+ if not expected_exception_type :
116
+ assert not error_raised
117
+ assert results == expected_return
0 commit comments