Skip to content
Snippets Groups Projects
Commit d4c34a28 authored by Thomas Woerner's avatar Thomas Woerner
Browse files

ansible_ipa_replica: Fix ansible-test fake execution test findings

All imports that are only available after installing IPA need to be in a
try exception clause to be able to pass the fake execution test. The old
workaround "if 'ansible.executor' in sys.modules:" is not working with
this test anymore.

If the imports can not be done, all used and needed attributes are
defines with the value None.

The new function check_imports has been added to fail with module.fail_json
if an import exception occured and ANSIBLE_IPA_REPLICA_MODULE_IMPORT_ERROR is
not None. This function needs to be called in all modules.

The `copyright` date is extended with `-2022`.
parent 0f0c098f
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# #
# Based on ipa-replica-install code # Based on ipa-replica-install code
# #
# Copyright (C) 2018 Red Hat # Copyright (C) 2018-2022 Red Hat
# see file 'COPYING' for use and warranty information # see file 'COPYING' for use and warranty information
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
...@@ -47,18 +47,12 @@ __all__ = ["contextlib", "dnsexception", "dnsresolver", "dnsreversename", ...@@ -47,18 +47,12 @@ __all__ = ["contextlib", "dnsexception", "dnsresolver", "dnsreversename",
"check_domain_level_is_supported", "promotion_check_ipa_domain", "check_domain_level_is_supported", "promotion_check_ipa_domain",
"SSSDConfig", "CalledProcessError", "timeconf", "ntpinstance", "SSSDConfig", "CalledProcessError", "timeconf", "ntpinstance",
"dnsname", "kernel_keyring", "krbinstance", "getargspec", "dnsname", "kernel_keyring", "krbinstance", "getargspec",
"adtrustinstance"] "adtrustinstance", "paths", "api", "dsinstance", "ipaldap", "Env",
"ipautil", "installutils", "IPA_PYTHON_VERSION", "NUM_VERSION",
"ReplicaConfig", "create_api"]
import sys import sys
# HACK: workaround for Ansible 2.9
# https://github.com/ansible/ansible/issues/68361
if 'ansible.executor' in sys.modules:
for attr in __all__:
setattr(sys.modules[__name__], attr, None)
else:
import logging import logging
from contextlib import contextmanager as contextlib_contextmanager
# Import getargspec from inspect or provide own getargspec for # Import getargspec from inspect or provide own getargspec for
# Python 2 compatibility with Python 3.11+. # Python 2 compatibility with Python 3.11+.
...@@ -82,6 +76,9 @@ else: ...@@ -82,6 +76,9 @@ else:
", use inspect.signature() API which can support them") ", use inspect.signature() API which can support them")
return ArgSpec(args, varargs, varkw, defaults) return ArgSpec(args, varargs, varkw, defaults)
try:
from contextlib import contextmanager as contextlib_contextmanager
from ipapython.version import NUM_VERSION, VERSION from ipapython.version import NUM_VERSION, VERSION
if NUM_VERSION < 30201: if NUM_VERSION < 30201:
...@@ -177,14 +174,26 @@ else: ...@@ -177,14 +174,26 @@ else:
raise Exception("freeipa version '%s' is too old" % VERSION) raise Exception("freeipa version '%s' is too old" % VERSION)
except ImportError as _err:
ANSIBLE_IPA_REPLICA_MODULE_IMPORT_ERROR = str(_err)
for attr in __all__:
setattr(sys.modules[__name__], attr, None)
else:
ANSIBLE_IPA_REPLICA_MODULE_IMPORT_ERROR = None
logger = logging.getLogger("ipa-server-install") logger = logging.getLogger("ipa-server-install")
def setup_logging(): def setup_logging():
# logger.setLevel(logging.DEBUG) # logger.setLevel(logging.DEBUG)
standard_logging_setup( standard_logging_setup(
paths.IPAREPLICA_INSTALL_LOG, verbose=False, debug=False, paths.IPAREPLICA_INSTALL_LOG, verbose=False, debug=False,
filemode='a', console_format='%(message)s') filemode='a', console_format='%(message)s')
@contextlib_contextmanager @contextlib_contextmanager
def redirect_stdout(stream): def redirect_stdout(stream):
sys.stdout = stream sys.stdout = stream
...@@ -193,6 +202,7 @@ else: ...@@ -193,6 +202,7 @@ else:
finally: finally:
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
class AnsibleModuleLog(): class AnsibleModuleLog():
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
...@@ -230,6 +240,7 @@ else: ...@@ -230,6 +240,7 @@ else:
self.module.debug(msg) self.module.debug(msg)
# self.module.warn(msg) # self.module.warn(msg)
# pylint: disable=too-many-instance-attributes, useless-object-inheritance # pylint: disable=too-many-instance-attributes, useless-object-inheritance
class installer_obj(object): # pylint: disable=invalid-name class installer_obj(object): # pylint: disable=invalid-name
def __init__(self): def __init__(self):
...@@ -287,8 +298,10 @@ else: ...@@ -287,8 +298,10 @@ else:
for name in self.__dict__: for name in self.__dict__:
yield self, name yield self, name
# pylint: enable=too-many-instance-attributes, useless-object-inheritance # pylint: enable=too-many-instance-attributes, useless-object-inheritance
# pylint: disable=attribute-defined-outside-init # pylint: disable=attribute-defined-outside-init
installer = installer_obj() installer = installer_obj()
options = installer options = installer
...@@ -308,6 +321,7 @@ else: ...@@ -308,6 +321,7 @@ else:
options.ca_subject = None options.ca_subject = None
# pylint: enable=attribute-defined-outside-init # pylint: enable=attribute-defined-outside-init
def gen_env_boostrap_finalize_core(etc_ipa, default_config): def gen_env_boostrap_finalize_core(etc_ipa, default_config):
env = Env() env = Env()
# env._bootstrap(context='installer', confdir=paths.ETC_IPA, log=None) # env._bootstrap(context='installer', confdir=paths.ETC_IPA, log=None)
...@@ -316,6 +330,7 @@ else: ...@@ -316,6 +330,7 @@ else:
env._finalize_core(**dict(default_config)) env._finalize_core(**dict(default_config))
return env return env
def api_bootstrap_finalize(env): def api_bootstrap_finalize(env):
# pylint: disable=no-member # pylint: disable=no-member
xmlrpc_uri = \ xmlrpc_uri = \
...@@ -328,6 +343,7 @@ else: ...@@ -328,6 +343,7 @@ else:
# pylint: enable=no-member # pylint: enable=no-member
api.finalize() api.finalize()
def gen_ReplicaConfig(): # pylint: disable=invalid-name def gen_ReplicaConfig(): # pylint: disable=invalid-name
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
class ExtendedReplicaConfig(ReplicaConfig): class ExtendedReplicaConfig(ReplicaConfig):
...@@ -381,6 +397,7 @@ else: ...@@ -381,6 +397,7 @@ else:
return config return config
def replica_ds_init_info(ansible_log, def replica_ds_init_info(ansible_log,
config, options_, ca_is_configured, remote_api, config, options_, ca_is_configured, remote_api,
ds_ca_subject, ca_file, ds_ca_subject, ca_file,
...@@ -446,6 +463,7 @@ else: ...@@ -446,6 +463,7 @@ else:
return ds return ds
def ansible_module_get_parsed_ip_addresses(ansible_module, def ansible_module_get_parsed_ip_addresses(ansible_module,
param='ip_addresses'): param='ip_addresses'):
ip_addrs = [] ip_addrs = []
...@@ -458,6 +476,7 @@ else: ...@@ -458,6 +476,7 @@ else:
ip_addrs.append(ip_parsed) ip_addrs.append(ip_parsed)
return ip_addrs return ip_addrs
def gen_remote_api(master_host_name, etc_ipa): def gen_remote_api(master_host_name, etc_ipa):
ldapuri = 'ldaps://%s' % ipautil.format_netloc(master_host_name) ldapuri = 'ldaps://%s' % ipautil.format_netloc(master_host_name)
xmlrpc_uri = 'https://{}/ipa/xml'.format( xmlrpc_uri = 'https://{}/ipa/xml'.format(
...@@ -470,3 +489,8 @@ else: ...@@ -470,3 +489,8 @@ else:
xmlrpc_uri=xmlrpc_uri) xmlrpc_uri=xmlrpc_uri)
remote_api.finalize() remote_api.finalize()
return remote_api return remote_api
def check_imports(module):
if ANSIBLE_IPA_REPLICA_MODULE_IMPORT_ERROR is not None:
module.fail_json(msg=ANSIBLE_IPA_REPLICA_MODULE_IMPORT_ERROR)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment