@@ -149,7 +149,7 @@ def managed_user_test_demo_how_to_use_managed_user(ansible_zos_module):
149
149
Who am I AFTER asking for a managed user = LJBXMONV
150
150
"""
151
151
152
- def __init__ (self , model_user : str = None , remote_host : str = None , zoau_path : str = None , pyz_path : str = None , pythonpath : str = None , volumes : str = None , hostpattern : str = None ) -> None :
152
+ def __init__ (self , model_user : str = None , remote_host : str = None , zoau_path : str = None , pyz_path : str = None , pythonpath : str = None , volumes : str = None , python_interpreter : str = None , hostpattern : str = None ) -> None :
153
153
"""
154
154
Initialize class with necessary parameters.
155
155
@@ -168,6 +168,7 @@ def __init__(self, model_user: str = None, remote_host: str = None, zoau_path: s
168
168
self ._pyz_path = pyz_path
169
169
self ._pythonpath = pythonpath
170
170
self ._volumes = volumes
171
+ self ._python_interpreter = python_interpreter
171
172
self ._hostpattern = "all" # can also get it from options host_pattern
172
173
self ._managed_racf_user = None
173
174
self ._managed_user_group = None
@@ -183,6 +184,7 @@ def from_fixture(cls, pytest_module_fixture, pytest_interpreter_fixture):
183
184
inventory_hosts = pytest_module_fixture ["options" ]["inventory_manager" ]._inventory .hosts
184
185
inventory_list = list (inventory_hosts .values ())[0 ].vars .get ('ansible_python_interpreter' ).split (";" )
185
186
environment_vars = pytest_interpreter_fixture [0 ]
187
+ python_interpreter = pytest_interpreter_fixture [1 ]
186
188
187
189
zoau_path = environment_vars .get ("ZOAU_HOME" )
188
190
pythonpath = environment_vars .get ("PYTHONPATH" )
@@ -193,7 +195,7 @@ def from_fixture(cls, pytest_module_fixture, pytest_interpreter_fixture):
193
195
# volumes to extra_args.
194
196
volumes = "000000,222222"
195
197
hostpattern = pytest_module_fixture ["options" ]["host_pattern" ]
196
- return cls (model_user , remote_host , zoau_path , pyz_path , pythonpath , volumes , hostpattern )
198
+ return cls (model_user , remote_host , zoau_path , pyz_path , pythonpath , volumes , python_interpreter , hostpattern )
197
199
198
200
199
201
def _connect (self , remote_host :str , model_user : str , command : str ) -> List [str ]:
@@ -423,7 +425,7 @@ def execute_managed_user_test(self, managed_user_test_case: str, debug: bool = F
423
425
inventory .update ({'host' : self ._remote_host })
424
426
inventory .update ({'user' : self ._managed_racf_user })
425
427
inventory .update ({'zoau' : self ._zoau_path }) # get this from fixture
426
- inventory .update ({'pyz' : self ._pyz_path }) # get this from fixture
428
+ inventory .update ({'pyz' : self ._python_path }) # get this from fixture
427
429
inventory .update ({'pythonpath' : self ._pythonpath }) # get this from fixture
428
430
extra_args = {}
429
431
extra_args .update ({'extra_args' :{'volumes' :self ._volumes .split ("," )}}) # get this from fixture
@@ -440,6 +442,80 @@ def execute_managed_user_test(self, managed_user_test_case: str, debug: bool = F
440
442
print (result .stdout )
441
443
442
444
445
+ def execute_managed_user_become_test (self , managed_user_test_case : str , become_method : dict [str , str ], debug : bool = False , verbose : bool = False , managed_user_type : ManagedUserType = None ) -> None :
446
+ """
447
+ Executes the test case using articulated pytest options when the test case needs a managed user. This is required
448
+ to execute any test that needs a manage user, a wrapper test case should call this method, the 'managed_user_test_case'
449
+ must begin with 'managed_user_' as opposed to 'test_', this because the pytest command built will override the ini
450
+ with this value. For running playbooks with become method.
451
+
452
+ Parameters
453
+ ----------
454
+ managed_user_test_case (str)
455
+ The managed user test case that begins with 'managed_user_'
456
+ become_method (Dict[str, str])
457
+ Key value pair of user command and options to set playbook for become method.
458
+ debug (str)
459
+ Enable verbose output for pytest, the equivalent command line option of '-s'.
460
+ verbose (str)
461
+ Enables pytest verbosity level 4 (-vvvv)
462
+
463
+ Raises
464
+ ------
465
+ Exception - if the test case fails (non-zero RC from pytest/subprocess), the stdout and stderr are returned for evaluation.
466
+ ValueError - if the managed user is not created, you must call `self._managed_racf_user()`.
467
+
468
+ See Also
469
+ --------
470
+ :py:member:`_create_managed_user` required before this function can be used as a managed user needs to exist.
471
+ """
472
+
473
+ if managed_user_test_case is None or not managed_user_test_case .startswith ("managed_user_" ):
474
+ raise ValueError ("Test cases using a managed user must begin with 'managed_user_' to be collected for execution." )
475
+
476
+ # if not self._managed_racf_user:
477
+ # raise ValueError("No managed user has been created, please ensure that the method `self._managed_racf_user()` has been called prior.")
478
+
479
+ self ._create_managed_user (managed_user_type )
480
+
481
+ # Get the file path of the caller function
482
+ calling_test_path = inspect .getfile (inspect .currentframe ().f_back )
483
+
484
+ # Get the test case name that this code is being run from, this is not an function arg.
485
+ # managed_user_test_case = inspect.stack()[1][3]
486
+
487
+ testcase = f"{ calling_test_path } ::{ managed_user_test_case } "
488
+ # hostpattern = "all" # can also get it from options host_pattern
489
+ capture = " -s"
490
+ verbosity = " -vvvv"
491
+
492
+ inventory : dict [str , str ] = {}
493
+ inventory .update ({'host' : self ._remote_host })
494
+ inventory .update ({'user' : self ._managed_racf_user })
495
+ inventory .update ({'zoau' : self ._zoau_path }) # get this from fixture
496
+ inventory .update ({'pyz' : self ._pyz_path }) # get this from fixture
497
+ inventory .update ({'python_interpreter' : self ._python_interpreter }) # get this from fixture
498
+ extra_args = {}
499
+ extra_args .update ({'extra_args' :{'volumes' :self ._volumes .split ("," )}}) # get this from fixture
500
+ inventory .update (extra_args )
501
+
502
+ node_inventory = json .dumps (inventory )
503
+
504
+ # Get options for become method
505
+ user = become_method ["user" ]
506
+ method = become_method ["method" ]
507
+ promp = become_method ["promp" ]
508
+ key = become_method ["key" ]
509
+ ssh_key = become_method ["ssh_key" ]
510
+ # Carefully crafted 'pytest' command to be allow for it to be called from anther test driven by pytest and uses the zinventory-raw fixture.
511
+ pytest_cmd = f"""pytest { testcase } --override-ini "python_functions=managed_user_" --host-pattern={ self ._hostpattern } { capture if debug else "" } { verbosity if verbose else "" } --zinventory-raw='{ node_inventory } ' --user_adm { user } --user_method { method } --ansible_promp '{ promp } ' --password { key } --ssh_key '{ ssh_key } '"""
512
+ result = subprocess .run (pytest_cmd , capture_output = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True , shell = True )
513
+ if result .returncode != 0 :
514
+ raise Exception (result .stdout + result .stderr )
515
+ else :
516
+ print (result .stdout )
517
+
518
+
443
519
def delete_managed_user (self ) -> None :
444
520
"""
445
521
Delete managed user from z/OS managed node. Performs clean up of the remote
0 commit comments