ansible_ipa_server: 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_SERVER_MODULE_IMPORT_ERROR is
not None. This function needs to be called in all modules.

The `copyright` date is extended with `-2022`.
This commit is contained in:
Thomas Woerner
2022-11-08 16:26:48 +01:00
parent 0f0c098fa2
commit 446107f1cb

View File

@@ -3,9 +3,9 @@
# Authors: # Authors:
# Thomas Woerner <twoerner@redhat.com> # Thomas Woerner <twoerner@redhat.com>
# #
# Based on ipa-client-install code # Based on ipa-server-install code
# #
# Copyright (C) 2017 Red Hat # Copyright (C) 2017-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
@@ -23,12 +23,12 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type # pylint: disable=invalid-name
__all__ = ["IPAChangeConf", "certmonger", "sysrestore", "root_logger", __all__ = ["IPAChangeConf", "certmonger", "sysrestore", "root_logger",
"ipa_generate_password", "run", "ScriptError", "services", "ipa_generate_password", "run", "ScriptError", "services",
"tasks", "errors", "x509", "DOMAIN_LEVEL_0", "MIN_DOMAIN_LEVEL", "tasks", "errors", "x509", "DOMAIN_LEVEL_0", "MIN_DOMAIN_LEVEL",
"validate_domain_name", "MAX_DOMAIN_LEVEL", "validate_domain_name",
"no_matching_interface_for_ip_address_warning", "no_matching_interface_for_ip_address_warning",
"check_zone_overlap", "timeconf", "ntpinstance", "adtrust", "check_zone_overlap", "timeconf", "ntpinstance", "adtrust",
"bindinstance", "ca", "dns", "httpinstance", "installutils", "bindinstance", "ca", "dns", "httpinstance", "installutils",
@@ -41,22 +41,13 @@ __all__ = ["IPAChangeConf", "certmonger", "sysrestore", "root_logger",
"adtrustinstance", "IPAAPI_USER", "sync_time", "PKIIniLoader", "adtrustinstance", "IPAAPI_USER", "sync_time", "PKIIniLoader",
"default_subject_base", "default_ca_subject_dn", "default_subject_base", "default_ca_subject_dn",
"check_ldap_conf", "encode_certificate", "decode_certificate", "check_ldap_conf", "encode_certificate", "decode_certificate",
"check_available_memory", "getargspec", "get_min_idstart"] "check_available_memory", "getargspec", "get_min_idstart",
"paths", "api", "ipautil", "adtrust_imported", "NUM_VERSION",
"time_service", "kra_imported", "dsinstance", "IPA_PYTHON_VERSION",
"NUM_VERSION"]
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
from ansible.module_utils import six
import base64
# 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+.
@@ -80,6 +71,12 @@ 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 ansible.module_utils import six
import base64
from ipapython.version import NUM_VERSION, VERSION from ipapython.version import NUM_VERSION, VERSION
if NUM_VERSION < 30201: if NUM_VERSION < 30201:
@@ -211,14 +208,25 @@ 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_SERVER_MODULE_IMPORT_ERROR = str(_err)
for attr in __all__:
setattr(sys.modules[__name__], attr, None)
else:
ANSIBLE_IPA_SERVER_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.IPASERVER_INSTALL_LOG, verbose=False, debug=False, paths.IPASERVER_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
@@ -227,6 +235,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
@@ -264,6 +273,7 @@ else:
self.module.debug(msg) self.module.debug(msg)
# self.module.warn(msg) # self.module.warn(msg)
# pylint: disable=too-few-public-methods, useless-object-inheritance # pylint: disable=too-few-public-methods, useless-object-inheritance
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
class options_obj(object): # pylint: disable=invalid-name class options_obj(object): # pylint: disable=invalid-name
@@ -289,8 +299,10 @@ else:
for name in self.__dict__: for name in self.__dict__:
yield self, name yield self, name
# pylint: enable=too-few-public-methods, useless-object-inheritance # pylint: enable=too-few-public-methods, useless-object-inheritance
# pylint: enable=too-many-instance-attributes # pylint: enable=too-many-instance-attributes
options = options_obj() options = options_obj()
installer = options installer = options
@@ -340,6 +352,7 @@ else:
# pylint: enable=attribute-defined-outside-init # pylint: enable=attribute-defined-outside-init
# pylint: disable=invalid-name # pylint: disable=invalid-name
def api_Backend_ldap2(host_name, setup_ca, connect=False): def api_Backend_ldap2(host_name, setup_ca, connect=False):
# we are sure we have the configuration file ready. # we are sure we have the configuration file ready.
@@ -354,8 +367,10 @@ else:
if connect: if connect:
api.Backend.ldap2.connect() api.Backend.ldap2.connect()
# pylint: enable=invalid-name # pylint: enable=invalid-name
def ds_init_info(ansible_log, fstore, domainlevel, dirsrv_config_file, def ds_init_info(ansible_log, fstore, domainlevel, dirsrv_config_file,
realm_name, host_name, domain_name, dm_password, realm_name, host_name, domain_name, dm_password,
idstart, idmax, subject_base, ca_subject, idstart, idmax, subject_base, ca_subject,
@@ -387,6 +402,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 = []
@@ -399,6 +415,7 @@ else:
ip_addrs.append(ip_parsed) ip_addrs.append(ip_parsed)
return ip_addrs return ip_addrs
def encode_certificate(cert): def encode_certificate(cert):
""" """
Encode a certificate using base64. Encode a certificate using base64.
@@ -413,6 +430,7 @@ else:
encoded = encoded.decode('ascii') encoded = encoded.decode('ascii')
return encoded return encoded
def decode_certificate(cert): def decode_certificate(cert):
""" """
Decode a certificate using base64. Decode a certificate using base64.
@@ -431,3 +449,8 @@ else:
else: else:
cert = base64.b64decode(cert) cert = base64.b64decode(cert)
return cert return cert
def check_imports(module):
if ANSIBLE_IPA_SERVER_MODULE_IMPORT_ERROR is not None:
module.fail_json(msg=ANSIBLE_IPA_SERVER_MODULE_IMPORT_ERROR)