def test_zhmc_user_facts( ansible_mod_cls, user_type, auth_type, expand, check_mode, hmc_session): # noqa: F811, E501 """ Test the zhmc_user module with state=facts. """ hd = hmc_session.hmc_definition hmc_host = hd.host hmc_auth = dict(userid=hd.userid, password=hd.password, ca_certs=hd.ca_certs, verify=hd.verify) client = zhmcclient.Client(hmc_session) console = client.consoles.console faked_session = hmc_session if hd.mock_file else None # Determine a random existing user of the desired type to test. users = console.users.list() typed_users = [u for u in users if u.get_property('authentication-type') == auth_type and u.get_property('type') == user_type] if len(typed_users) == 0: pytest.skip("HMC has no users with type '{ut}' and authentication-type " "'{at}'".format(ut=user_type, at=auth_type)) user = random.choice(typed_users) where = "user '{u}'".format(u=user.name) # Prepare module input parameters (must be all required + optional) params = { 'hmc_host': hmc_host, 'hmc_auth': hmc_auth, 'name': user.name, 'state': 'facts', 'properties': {}, 'expand': expand, 'log_file': LOG_FILE, '_faked_session': faked_session, } # Prepare mocks for AnsibleModule object mod_obj = mock_ansible_module(ansible_mod_cls, params, check_mode) # Exercise the code to be tested with pytest.raises(SystemExit) as exc_info: zhmc_user.main() exit_code = exc_info.value.args[0] # Assert module exit code assert exit_code == 0, \ "{w}: Module failed with exit code {e} and message:\n{m}". \ format(w=where, e=exit_code, m=get_failure_msg(mod_obj)) # Assert module output changed, user_props = get_module_output(mod_obj) assert changed is False, where assert_user_props(user_props, expand, where)
def test_user_facts(ansible_mod_cls, expand, check_mode, hmc_session): # noqa: F811, E501 """ Test fact gathering on all users of the HMC. """ hd = hmc_session.hmc_definition # Determine an existing user to test. client = zhmcclient.Client(hmc_session) console = client.consoles.console users = console.users.list() assert len(users) >= 1 for user in users: # Prepare module input parameters params = { 'hmc_host': hd.hmc_host, 'hmc_auth': dict(userid=hd.hmc_userid, password=hd.hmc_password), 'name': user.name, 'state': 'facts', 'expand': expand, 'log_file': LOG_FILE, 'faked_session': None, } # Prepare mocks for AnsibleModule object mod_obj = mock_ansible_module(ansible_mod_cls, params, check_mode) # Exercise the code to be tested with pytest.raises(SystemExit) as exc_info: zhmc_user.main() exit_code = exc_info.value.args[0] # Assert module exit code assert exit_code == 0, \ "Module unexpectedly failed with this message:\n{0}". \ format(get_failure_msg(mod_obj)) # Assert module output changed, user_props = get_module_output(mod_obj) assert changed is False assert_user_props(user_props, expand)
def test_user_absent_present(ansible_mod_cls, desc, initial_user_props, initial_related_names, input_state, input_props, exp_user_props, exp_changed, check_mode, hmc_session): # noqa: F811, E501 """ Test the zhmc_user module with all combinations of absent & present state. """ expand = False # Expansion is tested elsewhere hd = hmc_session.hmc_definition client = zhmcclient.Client(hmc_session) console = client.consoles.console # Create a user name that does not exist user_name = new_user_name() # Create initial user, if specified so if initial_user_props is not None: user_props = STD_USER_PROPERTIES.copy() user_props.update(initial_user_props) user_props['name'] = user_name if user_props['authentication-type'] == 'local': assert 'password' in user_props assert 'password_rule_name' in initial_related_names password_rule_name = initial_related_names['password_rule_name'] password_rule = console.password_rules.find_by_name( password_rule_name) user_props['password-rule-uri'] = password_rule.uri if user_props['authentication-type'] == 'ldap': assert 'ldap_server_definition_name' in initial_related_names ldap_srv_def_name = \ initial_related_names['ldap_server_definition_name'] ldap_srv_def = console.ldap_server_definitions.find_by_name( ldap_srv_def_name) user_props['ldap-server-definition-uri'] = ldap_srv_def.uri console.users.create(user_props) else: user_props = None try: params = { 'hmc_host': hd.hmc_host, 'hmc_auth': dict(userid=hd.hmc_userid, password=hd.hmc_password), 'name': user_name, 'state': input_state, 'expand': expand, 'log_file': LOG_FILE, 'faked_session': None, } if input_props is not None: params['properties'] = input_props mod_obj = mock_ansible_module(ansible_mod_cls, params, check_mode) # Exercise the code to be tested with pytest.raises(SystemExit) as exc_info: zhmc_user.main() exit_code = exc_info.value.args[0] assert exit_code == 0, \ "Module unexpectedly failed with this message:\n{0}". \ format(get_failure_msg(mod_obj)) changed, output_props = get_module_output(mod_obj) if changed != exp_changed: user_props_sorted = \ OrderedDict(sorted(user_props.items(), key=lambda x: x[0])) \ if user_props is not None else None input_props_sorted = \ OrderedDict(sorted(input_props.items(), key=lambda x: x[0])) \ if input_props is not None else None output_props_sorted = \ OrderedDict(sorted(output_props.items(), key=lambda x: x[0])) \ if output_props is not None else None raise AssertionError( "Unexpected change flag returned: actual: {0}, expected: {1}\n" "Initial user properties:\n{2}\n" "Module input properties:\n{3}\n" "Resulting user properties:\n{4}".format( changed, exp_changed, pformat(user_props_sorted.items(), indent=2), pformat(input_props_sorted.items(), indent=2), pformat(output_props_sorted.items(), indent=2))) if input_state == 'present': assert_user_props(output_props, expand) finally: # Delete user, if it exists try: # We invalidate the name cache of our client, because the user # was possibly deleted by the Ansible module and not through our # client instance. console.users.invalidate_cache() user = console.users.find_by_name(user_name) except zhmcclient.NotFound: user = None if user: user.delete()
def test_zhmc_user_absent_present( ansible_mod_cls, desc, initial_user_props, initial_related_names, input_state, input_props, exp_user_props, exp_changed, check_mode, hmc_session): # noqa: F811, E501 """ Test the zhmc_user module with state=absent/present. """ hd = hmc_session.hmc_definition hmc_host = hd.host hmc_auth = dict(userid=hd.userid, password=hd.password, ca_certs=hd.ca_certs, verify=hd.verify) faked_session = hmc_session if hd.mock_file else None expand = False # Expansion is tested elsewhere client = zhmcclient.Client(hmc_session) console = client.consoles.console # Create a user name that does not exist user_name = new_user_name() where = "user '{u}'".format(u=user_name) # Create initial user, if specified so if initial_user_props is not None: user_props = STD_USER_PROPERTIES.copy() user_props.update(initial_user_props) user_props['name'] = user_name if user_props['authentication-type'] == 'local': assert 'password' in user_props, where assert 'password_rule_name' in initial_related_names, where password_rule_name = initial_related_names['password_rule_name'] password_rule = console.password_rules.find_by_name( password_rule_name) user_props['password-rule-uri'] = password_rule.uri if user_props['authentication-type'] == 'ldap': assert 'ldap_server_definition_name' in initial_related_names, where ldap_srv_def_name = \ initial_related_names['ldap_server_definition_name'] ldap_srv_def = console.ldap_server_definitions.find_by_name( ldap_srv_def_name) user_props['ldap-server-definition-uri'] = ldap_srv_def.uri try: console.users.create(user_props) except zhmcclient.HTTPError as exc: if exc.http_status == 403 and exc.reason == 1: # User is not permitted to create users pytest.skip("HMC user '{u}' is not permitted to create " "initial test user". format(u=hd.userid)) else: user_props = None try: # Prepare module input parameters (must be all required + optional) params = { 'hmc_host': hmc_host, 'hmc_auth': hmc_auth, 'name': user_name, 'state': input_state, 'expand': expand, 'log_file': LOG_FILE, '_faked_session': faked_session, } if input_props is not None: params['properties'] = input_props else: params['properties'] = {} mod_obj = mock_ansible_module(ansible_mod_cls, params, check_mode) # Exercise the code to be tested with pytest.raises(SystemExit) as exc_info: zhmc_user.main() exit_code = exc_info.value.args[0] assert exit_code == 0, \ "{w}: Module failed with exit code {e} and message:\n{m}". \ format(w=where, e=exit_code, m=get_failure_msg(mod_obj)) changed, output_props = get_module_output(mod_obj) if changed != exp_changed: user_props_sorted = \ OrderedDict(sorted(user_props.items(), key=lambda x: x[0])) \ if user_props is not None else None input_props_sorted = \ OrderedDict(sorted(input_props.items(), key=lambda x: x[0])) \ if input_props is not None else None output_props_sorted = \ OrderedDict(sorted(output_props.items(), key=lambda x: x[0])) \ if output_props is not None else None raise AssertionError( "Unexpected change flag returned: actual: {0}, expected: {1}\n" "Initial user properties:\n{2}\n" "Module input properties:\n{3}\n" "Resulting user properties:\n{4}". format(changed, exp_changed, pformat(user_props_sorted.items(), indent=2), pformat(input_props_sorted.items(), indent=2), pformat(output_props_sorted.items(), indent=2))) if input_state == 'present': assert_user_props(output_props, expand, where) finally: # Delete user, if it exists try: # We invalidate the name cache of our client, because the user # was possibly deleted by the Ansible module and not through our # client instance. console.users.invalidate_cache() user = console.users.find_by_name(user_name) except zhmcclient.NotFound: user = None if user: user.delete()