Skip to content

Commit bd1ee0e

Browse files
AndreMarcel99André Marcel Gutiérrez Benítezfernandofloresg
authored
[Bugfix][2285]zos_mount_persistant_delete_all_content (#2347)
* First iteration * Second definition * Fix problem of testing binary * Add fragment * Modify fragment --------- Co-authored-by: André Marcel Gutiérrez Benítez <[email protected]> Co-authored-by: Fernando Flores <[email protected]>
1 parent daf41f6 commit bd1ee0e

File tree

3 files changed

+99
-41
lines changed

3 files changed

+99
-41
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bugfixes:
2+
- zos_mount - Previously, using the persistent parameter caused the module to clear the entire member or data set
3+
provided even without a pattern match, leaving it empty despite a successful mount.
4+
The fix now ensures content is only deleted when a pattern match is detected, preserving existing configuration.
5+
(https://github.com/ansible-collections/ibm_zos_core/pull/2347).

plugins/modules/zos_mount.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,30 +1048,26 @@ def run_module(module, arg_def):
10481048
stderr=str(res_args),
10491049
)
10501050

1051-
bk_ds = datasets.tmp_name(high_level_qualifier=tmphlq)
1052-
datasets.create(name=bk_ds, dataset_type="SEQ")
1053-
10541051
new_str = get_str_to_keep(dataset=name, src=src)
10551052

1056-
rc_write = 0
1053+
if new_str:
1054+
modified_str = [line for line in new_str if line.strip() or line.lstrip()]
1055+
1056+
rc_write = 0
10571057

1058-
try:
1059-
for line in new_str:
1060-
rc_write = datasets.write(dataset_name=bk_ds, content=line.rstrip(), append=True)
1058+
try:
1059+
# zoau_io.zopen on mode w allow delete all the content inside the dataset allowing to write the new one
1060+
with zoau_io.zopen(f"//'{name}'", "w", "cp1047", recfm="*") as ds:
1061+
pass
1062+
full_text = "\n".join(modified_str)
1063+
rc_write = datasets.write(dataset_name=name, content=full_text, append=True, force=True)
10611064
if rc_write != 0:
10621065
raise Exception("Non zero return code from datasets.write.")
1063-
except Exception as e:
1064-
datasets.delete(dataset=bk_ds)
1065-
module.fail_json(
1066-
msg="Unable to write on persistent data set {0}. {1}".format(name, e),
1067-
stderr=str(res_args),
1068-
)
1069-
1070-
try:
1071-
datasets.delete(dataset=name)
1072-
datasets.copy(source=bk_ds, target=name)
1073-
finally:
1074-
datasets.delete(dataset=bk_ds)
1066+
except Exception as e:
1067+
module.fail_json(
1068+
msg="Unable to write on persistent data set {0}. {1}".format(name, e),
1069+
stderr=str(res_args),
1070+
)
10751071

10761072
if will_mount:
10771073
d = datetime.today()

tests/functional/modules/test_zos_mount_func.py

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
SECURITY
3939
"""
4040

41+
TEXT_TO_KEEP = """/* Service path */
42+
MOUNT FILESYSTEM('{0}')
43+
TYPE(ZFS) MODE(RDWR) AUTOMOVE
44+
MOUNTPOINT('/Service')
45+
"""
46+
4147
SHELL_EXECUTABLE = "/bin/sh"
4248

4349

@@ -315,6 +321,75 @@ def test_basic_mount_with_bpx_no_utf_8_characters(ansible_zos_module, volumes_on
315321
stdin="",
316322
)
317323

324+
def test_basic_mount_with_persistent_keep_dataset(ansible_zos_module, volumes_on_systems):
325+
hosts = ansible_zos_module
326+
volumes = Volume_Handler(volumes_on_systems)
327+
volume_1 = volumes.get_available_vol()
328+
srcfn = create_sourcefile(hosts, volume_1)
329+
330+
tmp_file_filename = "/tmp/testfile.txt"
331+
332+
hosts.all.shell(
333+
cmd="touch {0}".format(tmp_file_filename)
334+
)
335+
336+
dest = get_tmp_ds_name()
337+
dest_path = dest + "(AUTO1)"
338+
339+
hosts.all.zos_blockinfile(path=tmp_file_filename, insertafter="EOF", block=TEXT_TO_KEEP.format(srcfn))
340+
341+
hosts.all.shell(
342+
cmd="dtouch -tpdse {0}".format(dest)
343+
)
344+
345+
hosts.all.zos_copy(
346+
src=tmp_file_filename,
347+
dest=dest_path,
348+
binary=True,
349+
remote_src=True,
350+
)
351+
352+
try:
353+
mount_result = hosts.all.zos_mount(
354+
src=srcfn,
355+
path="/pythonx",
356+
fs_type="zfs",
357+
state="mounted",
358+
persistent=dict(name=dest_path),
359+
)
360+
361+
for result in mount_result.values():
362+
assert result.get("rc") == 0
363+
assert result.get("changed") is True
364+
365+
result_cat = hosts.all.shell(
366+
cmd="dcat '{0}'".format(dest_path),
367+
)
368+
369+
for result in result_cat.contacted.values():
370+
print(result)
371+
assert srcfn in result.get("stdout")
372+
assert "Service path" in result.get("stdout")
373+
finally:
374+
hosts.all.zos_mount(
375+
src=srcfn,
376+
path="/pythonx",
377+
fs_type="zfs",
378+
state="absent",
379+
)
380+
hosts.all.shell(
381+
cmd="drm " + DataSet.escape_data_set_name(srcfn),
382+
executable=SHELL_EXECUTABLE,
383+
stdin="",
384+
)
385+
hosts.all.file(path=tmp_file_filename, state="absent")
386+
hosts.all.file(path="/pythonx/", state="absent")
387+
hosts.all.shell(
388+
cmd="drm " + dest,
389+
executable=SHELL_EXECUTABLE,
390+
stdin="",
391+
)
392+
318393
def test_basic_mount_with_bpx_marker_backup(ansible_zos_module, volumes_on_systems):
319394
hosts = ansible_zos_module
320395
volumes = Volume_Handler(volumes_on_systems)
@@ -326,13 +401,6 @@ def test_basic_mount_with_bpx_marker_backup(ansible_zos_module, volumes_on_syste
326401
hosts.all.zos_copy(
327402
content=INITIAL_PRM_MEMBER,
328403
dest=tmp_file_filename,
329-
binary=True,
330-
)
331-
# Make it readable at console
332-
hosts.all.shell(
333-
cmd="chtag -t -c ISO8859-1 " + tmp_file_filename,
334-
executable=SHELL_EXECUTABLE,
335-
stdin="",
336404
)
337405

338406
# Dump the values of the file once copied to the target(s)
@@ -356,7 +424,6 @@ def test_basic_mount_with_bpx_marker_backup(ansible_zos_module, volumes_on_syste
356424
hosts.all.zos_copy(
357425
src=tmp_file_filename,
358426
dest=dest_path,
359-
binary=True,
360427
remote_src=True,
361428
)
362429

@@ -375,25 +442,16 @@ def test_basic_mount_with_bpx_marker_backup(ansible_zos_module, volumes_on_syste
375442
marker=["bpxtablemarker - try this", "second line of marker"],
376443
),
377444
)
378-
# copying from dataset to make editable copy on target
379-
test_tmp_file_filename = tmp_file_filename + "-a"
380-
381-
hosts.all.zos_copy(
382-
src=dest_path,
383-
dest=test_tmp_file_filename,
384-
binary=True,
385-
remote_src=True,
386-
)
445+
387446
results = hosts.all.shell(
388-
cmd="cat " + test_tmp_file_filename, executable=SHELL_EXECUTABLE, stdin=""
447+
cmd="dcat '{0}'".format(dest_path),
389448
)
390-
data = ""
391-
for result in results.values():
449+
450+
for result in results.contacted.values():
392451
print("\nbcb-postmount result: {0}\n".format(result.get("stdout")))
393452
data += result.get("stdout")
394453

395454
print("\n====================================================\n")
396-
397455
for result in mount_result.values():
398456
assert result.get("rc") == 0
399457
assert result.get("changed") is True
@@ -414,7 +472,6 @@ def test_basic_mount_with_bpx_marker_backup(ansible_zos_module, volumes_on_syste
414472
)
415473

416474
hosts.all.file(path=tmp_file_filename, state="absent")
417-
hosts.all.file(path=test_tmp_file_filename, state="absent")
418475
hosts.all.file(path="/pythonx/", state="absent")
419476
hosts.all.shell(cmd=f"drm {dest}")
420477

0 commit comments

Comments
 (0)