From 5a2675e375ba39c9f6d8d70d85c4429f3994edca Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Wed, 14 Sep 2022 19:56:28 +0200
Subject: [PATCH] ipasmartcard_server_get_vars: Fix for ansible-test fake
 execution test

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.

If the imports can not be done, all used and needed attributes are
defined with the value None, MODULE_IMPORT_ERROR is set to the import
error and fail_json is called.
---
 .../library/ipasmartcard_server_get_vars.py   | 38 ++++++++++++-------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/roles/ipasmartcard_server/library/ipasmartcard_server_get_vars.py b/roles/ipasmartcard_server/library/ipasmartcard_server_get_vars.py
index 6a7f23f2..85897914 100644
--- a/roles/ipasmartcard_server/library/ipasmartcard_server_get_vars.py
+++ b/roles/ipasmartcard_server/library/ipasmartcard_server_get_vars.py
@@ -116,22 +116,29 @@ python_interpreter:
 
 import sys
 from ansible.module_utils.basic import AnsibleModule
-from ipaplatform.paths import paths
 try:
-    from ipaserver.install.httpinstance import OCSP_ENABLED, OCSP_DIRECTIVE
-    NSS_OCSP_ENABLED = ""
-    NSS_OCSP_DIRECTIVE = ""
-    NSS_NICKNAME_DIRECTIVE = ""
-except ImportError:
-    from ipaserver.install.httpinstance import NSS_OCSP_ENABLED
-    NSS_OCSP_DIRECTIVE = "NSSOCSP"
-    NSS_NICKNAME_DIRECTIVE = "NSSNickname"
-    OCSP_ENABLED = ""
-    OCSP_DIRECTIVE = ""
-try:
-    from ipaclient.install.client import sssd_enable_ifp
-except ImportError:
+    from ipaplatform.paths import paths
+    try:
+        from ipaserver.install.httpinstance import OCSP_ENABLED, OCSP_DIRECTIVE
+        NSS_OCSP_ENABLED = ""
+        NSS_OCSP_DIRECTIVE = ""
+        NSS_NICKNAME_DIRECTIVE = ""
+    except ImportError:
+        from ipaserver.install.httpinstance import NSS_OCSP_ENABLED
+        NSS_OCSP_DIRECTIVE = "NSSOCSP"
+        NSS_NICKNAME_DIRECTIVE = "NSSNickname"
+        OCSP_ENABLED = ""
+        OCSP_DIRECTIVE = ""
+    try:
+        from ipaclient.install.client import sssd_enable_ifp
+    except ImportError:
+        sssd_enable_ifp = None
+except ImportError as _err:
+    MODULE_IMPORT_ERROR = str(_err)
+    paths = None
     sssd_enable_ifp = None
+else:
+    MODULE_IMPORT_ERROR = None
 
 
 def main():
@@ -140,6 +147,9 @@ def main():
         supports_check_mode=False,
     )
 
+    if MODULE_IMPORT_ERROR is not None:
+        ansible_module.fail_json(msg=MODULE_IMPORT_ERROR)
+
     ansible_module.exit_json(changed=False,
                              NSS_OCSP_ENABLED=NSS_OCSP_ENABLED,
                              NSS_OCSP_DIRECTIVE=NSS_OCSP_DIRECTIVE,
-- 
GitLab