mirror of
https://github.com/freeipa/ansible-freeipa.git
synced 2026-08-04 05:44:41 +00:00
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:
@@ -3,9 +3,9 @@
|
||||
# Authors:
|
||||
# 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
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
@@ -23,12 +23,12 @@
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
|
||||
__metaclass__ = type
|
||||
__metaclass__ = type # pylint: disable=invalid-name
|
||||
|
||||
__all__ = ["IPAChangeConf", "certmonger", "sysrestore", "root_logger",
|
||||
"ipa_generate_password", "run", "ScriptError", "services",
|
||||
"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",
|
||||
"check_zone_overlap", "timeconf", "ntpinstance", "adtrust",
|
||||
"bindinstance", "ca", "dns", "httpinstance", "installutils",
|
||||
@@ -41,22 +41,13 @@ __all__ = ["IPAChangeConf", "certmonger", "sysrestore", "root_logger",
|
||||
"adtrustinstance", "IPAAPI_USER", "sync_time", "PKIIniLoader",
|
||||
"default_subject_base", "default_ca_subject_dn",
|
||||
"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
|
||||
|
||||
# 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
|
||||
from contextlib import contextmanager as contextlib_contextmanager
|
||||
from ansible.module_utils import six
|
||||
import base64
|
||||
|
||||
# Import getargspec from inspect or provide own getargspec for
|
||||
# Python 2 compatibility with Python 3.11+.
|
||||
@@ -80,6 +71,12 @@ else:
|
||||
", use inspect.signature() API which can support them")
|
||||
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
|
||||
|
||||
if NUM_VERSION < 30201:
|
||||
@@ -211,14 +208,25 @@ else:
|
||||
|
||||
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")
|
||||
|
||||
|
||||
def setup_logging():
|
||||
# logger.setLevel(logging.DEBUG)
|
||||
standard_logging_setup(
|
||||
paths.IPASERVER_INSTALL_LOG, verbose=False, debug=False,
|
||||
filemode='a', console_format='%(message)s')
|
||||
|
||||
|
||||
@contextlib_contextmanager
|
||||
def redirect_stdout(stream):
|
||||
sys.stdout = stream
|
||||
@@ -227,6 +235,7 @@ else:
|
||||
finally:
|
||||
sys.stdout = sys.__stdout__
|
||||
|
||||
|
||||
class AnsibleModuleLog():
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
@@ -264,6 +273,7 @@ else:
|
||||
self.module.debug(msg)
|
||||
# self.module.warn(msg)
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods, useless-object-inheritance
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
class options_obj(object): # pylint: disable=invalid-name
|
||||
@@ -289,8 +299,10 @@ else:
|
||||
for name in self.__dict__:
|
||||
yield self, name
|
||||
|
||||
|
||||
# pylint: enable=too-few-public-methods, useless-object-inheritance
|
||||
|
||||
|
||||
# pylint: enable=too-many-instance-attributes
|
||||
options = options_obj()
|
||||
installer = options
|
||||
@@ -340,6 +352,7 @@ else:
|
||||
|
||||
# pylint: enable=attribute-defined-outside-init
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def api_Backend_ldap2(host_name, setup_ca, connect=False):
|
||||
# we are sure we have the configuration file ready.
|
||||
@@ -354,8 +367,10 @@ else:
|
||||
if connect:
|
||||
api.Backend.ldap2.connect()
|
||||
|
||||
|
||||
# pylint: enable=invalid-name
|
||||
|
||||
|
||||
def ds_init_info(ansible_log, fstore, domainlevel, dirsrv_config_file,
|
||||
realm_name, host_name, domain_name, dm_password,
|
||||
idstart, idmax, subject_base, ca_subject,
|
||||
@@ -387,6 +402,7 @@ else:
|
||||
|
||||
return _ds
|
||||
|
||||
|
||||
def ansible_module_get_parsed_ip_addresses(ansible_module,
|
||||
param='ip_addresses'):
|
||||
ip_addrs = []
|
||||
@@ -399,6 +415,7 @@ else:
|
||||
ip_addrs.append(ip_parsed)
|
||||
return ip_addrs
|
||||
|
||||
|
||||
def encode_certificate(cert):
|
||||
"""
|
||||
Encode a certificate using base64.
|
||||
@@ -413,6 +430,7 @@ else:
|
||||
encoded = encoded.decode('ascii')
|
||||
return encoded
|
||||
|
||||
|
||||
def decode_certificate(cert):
|
||||
"""
|
||||
Decode a certificate using base64.
|
||||
@@ -431,3 +449,8 @@ else:
|
||||
else:
|
||||
cert = base64.b64decode(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)
|
||||
|
||||
Reference in New Issue
Block a user