Skip to content

Commit f41f8fd

Browse files
committed
First iteration solution
1 parent 102a25c commit f41f8fd

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

tests/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ def pytest_addoption(parser):
5454
default=None,
5555
help="Str - dictionary with values {'host': 'ibm.com', 'user': 'root', 'zoau': '/usr/lpp/zoau', 'pyz': '/usr/lpp/IBM/pyz'}",
5656
)
57+
parser.addoption(
58+
"--user_adm",
59+
action="store",
60+
default=None,
61+
help="Str "
62+
)
63+
parser.addoption(
64+
"--user_method",
65+
action="store",
66+
default=None,
67+
help="Str "
68+
)
69+
parser.addoption(
70+
"--ansible_promp",
71+
action="store",
72+
default=None,
73+
help="Str "
74+
)
75+
parser.addoption(
76+
"--ansible_user",
77+
action="store",
78+
default=None,
79+
help="Str "
80+
)
5781

5882

5983
@pytest.fixture(scope="session")
@@ -204,3 +228,11 @@ def get_config(request):
204228
""" Call the pytest-ansible plugin to check volumes on the system and work properly a list by session."""
205229
path = request.config.getoption("--zinventory")
206230
yield path
231+
232+
@pytest.fixture(scope='session')
233+
def get_config_for_become(request):
234+
user_value = request.config.option.user_adm
235+
user_method = request.config.option.user_method
236+
ansible_promp = request.config.option.ansible_promp
237+
ansible_user = request.config.option.ansible_user
238+
return user_value, user_method, ansible_promp, ansible_user

tests/functional/modules/test_zos_fetch_func.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
import pytest
2121
import string
2222
import random
23+
import yaml
24+
import subprocess
2325
import tempfile
2426

2527
from hashlib import sha256
2628
from ansible.utils.hashing import checksum
2729
from datetime import datetime
30+
from shellescape import quote
2831

2932
from shellescape import quote
3033

@@ -87,6 +90,46 @@
8790
00000003A record
8891
"""
8992

93+
INVENTORY = """all:
94+
hosts:
95+
zvm:
96+
ansible_host: {0}
97+
ansible_ssh_private_key_file: {1}
98+
ansible_user: {2}
99+
ansible_python_interpreter: {3}"""
100+
101+
BECOME_USER="""- hosts: zvm
102+
collections :
103+
- ibm.ibm_zos_core
104+
gather_facts: False
105+
ignore_errors: True
106+
vars:
107+
ZOAU: "{0}"
108+
PYZ: "{1}"
109+
ansible_become_user: {2}
110+
ansible_become_method: {3}
111+
ansible_su_prompt_l10n: {4}
112+
environment:
113+
_BPXK_AUTOCVT: "ALL"
114+
ZOAU_HOME: "{0}"
115+
PYTHONPATH: "{0}/lib/{5}"
116+
LIBPATH: "{0}/lib:{1}/lib:/lib:/usr/lib:."
117+
PATH: "{0}/bin:/bin:/usr/lpp/rsusr/ported/bin:/var/bin:/usr/lpp/rsusr/ported/bin:/usr/lpp/java/java180/J8.0_64/bin:{1}/bin:"
118+
_CEE_RUNOPTS: "FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)"
119+
_TAG_REDIR_ERR: "txt"
120+
_TAG_REDIR_IN: "txt"
121+
_TAG_REDIR_OUT: "txt"
122+
LANG: "C"
123+
PYTHONSTDINENCODING: "cp1047"
124+
tasks:
125+
- name: Fetch PDSE member while escalating privileges.
126+
ibm.ibm_zos_core.zos_fetch:
127+
src: {6}
128+
dest: /tmp/
129+
flat: true
130+
become: true
131+
"""
132+
90133

91134
def extract_member_name(data_set):
92135
start = data_set.find("(")
@@ -985,3 +1028,63 @@ def test_fetch_uss_file_relative_path_not_present_on_local_machine(ansible_zos_m
9851028
finally:
9861029
if os.path.exists(dest):
9871030
os.remove(dest)
1031+
1032+
1033+
def test_extra_parameters(get_config_for_become, get_config, capsys):
1034+
with capsys.disabled():
1035+
adm_user, method, promp, ansible_us = get_config_for_become
1036+
promp = promp + f" {adm_user}"
1037+
1038+
ds_name = get_tmp_ds_name()
1039+
ds_name = ds_name + "(MEMBER)"
1040+
1041+
path = get_config
1042+
with open(path, 'r') as file:
1043+
enviroment = yaml.safe_load(file)
1044+
1045+
ssh_key = enviroment["ssh_key"]
1046+
hosts = enviroment["host"].upper()
1047+
user = enviroment["user"].upper()
1048+
python_path = enviroment["python_path"]
1049+
cut_python_path = python_path[:python_path.find('/bin')].strip()
1050+
zoau = enviroment["environment"]["ZOAU_ROOT"]
1051+
python_version = cut_python_path.split('/')[2]
1052+
1053+
try:
1054+
playbook = "playbook.yml"
1055+
inventory = "inventory.yml"
1056+
1057+
os.system("echo {0} > {1}".format(quote(BECOME_USER.format(
1058+
zoau,
1059+
cut_python_path,
1060+
adm_user,
1061+
method,
1062+
promp,
1063+
python_version,
1064+
ds_name,
1065+
)), playbook))
1066+
1067+
os.system("echo {0} > {1}".format(quote(INVENTORY.format(
1068+
hosts,
1069+
ssh_key,
1070+
user,
1071+
python_path
1072+
)), inventory))
1073+
1074+
command = "ansible-playbook -i -vvv {0} {1}".format(
1075+
inventory,
1076+
playbook
1077+
)
1078+
1079+
result = subprocess.run(
1080+
command,
1081+
capture_output=True,
1082+
shell=True,
1083+
timeout=120,
1084+
encoding='utf-8'
1085+
)
1086+
1087+
assert result.returncode == 0
1088+
finally:
1089+
os.remove("inventory.yml")
1090+
os.remove("playbook.yml")

0 commit comments

Comments
 (0)