91acb2
From 3cf7fb1014ae40fd5a5278f27577a8196a4af051 Mon Sep 17 00:00:00 2001
91acb2
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
91acb2
Date: Fri, 7 Aug 2020 07:51:53 +0200
91acb2
Subject: [PATCH] ipatests: test_epn: add test_EPN_connection_refused
91acb2
MIME-Version: 1.0
91acb2
Content-Type: text/plain; charset=UTF-8
91acb2
Content-Transfer-Encoding: 8bit
91acb2
91acb2
Add a test for EPN behavior when the configured SMTP does not
91acb2
accept connections.
91acb2
91acb2
Fixes: https://pagure.io/freeipa/issue/8445
91acb2
Signed-off-by: François Cami <fcami@redhat.com>
91acb2
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
91acb2
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
91acb2
---
91acb2
 ipatests/test_integration/test_epn.py | 27 ++++++++++++++++++++++++---
91acb2
 1 file changed, 24 insertions(+), 3 deletions(-)
91acb2
91acb2
diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py
91acb2
index c5c73835a..1a25d3710 100644
91acb2
--- a/ipatests/test_integration/test_epn.py
91acb2
+++ b/ipatests/test_integration/test_epn.py
91acb2
@@ -182,14 +182,20 @@ class TestEPN(IntegrationTest):
91acb2
         self,
91acb2
         host,
91acb2
         dry_run=False,
91acb2
+        mailtest=False,
91acb2
         from_nbdays=None,
91acb2
         to_nbdays=None,
91acb2
         raiseonerr=True,
91acb2
         validatejson=True
91acb2
     ):
91acb2
-        result = tasks.ipa_epn(host, raiseonerr=raiseonerr, dry_run=dry_run,
91acb2
-                               from_nbdays=from_nbdays,
91acb2
-                               to_nbdays=to_nbdays)
91acb2
+        result = tasks.ipa_epn(
91acb2
+            host,
91acb2
+            from_nbdays=from_nbdays,
91acb2
+            to_nbdays=to_nbdays,
91acb2
+            mailtest=mailtest,
91acb2
+            dry_run=dry_run,
91acb2
+            raiseonerr=raiseonerr
91acb2
+        )
91acb2
         if validatejson:
91acb2
             json.dumps(json.loads(result.stdout_text), ensure_ascii=False)
91acb2
         return (result.stdout_text, result.stderr_text, result.returncode)
91acb2
@@ -243,6 +249,21 @@ class TestEPN(IntegrationTest):
91acb2
         ck = "192481b52fb591112afd7b55b12a44c6618fdbc7e05a3b1866fd67ec579c51df"
91acb2
         assert cmd2.stdout_text.find(ck) == 0
91acb2
 
91acb2
+    @pytest.mark.xfail(reason='freeipa ticket 8445', strict=True)
91acb2
+    def test_EPN_connection_refused(self):
91acb2
+        """Test EPN behavior when the configured SMTP is down
91acb2
+        """
91acb2
+
91acb2
+        self.master.run_command(["systemctl", "stop", "postfix"])
91acb2
+        (unused, stderr_text, rc) = self._check_epn_output(
91acb2
+            self.master, mailtest=True,
91acb2
+            raiseonerr=False, validatejson=False
91acb2
+        )
91acb2
+        self.master.run_command(["systemctl", "start", "postfix"])
91acb2
+        assert "IPA-EPN: Could not connect to the configured SMTP server" in \
91acb2
+            stderr_text
91acb2
+        assert rc > 0
91acb2
+
91acb2
     def test_EPN_smoketest_1(self):
91acb2
         """No users except admin. Check --dry-run output.
91acb2
            With the default configuration, the result should be an empty list.
91acb2
-- 
91acb2
2.26.2
91acb2
91acb2
From 53f330b053740b169d211aa16b3b36fb61157bbd Mon Sep 17 00:00:00 2001
91acb2
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
91acb2
Date: Fri, 7 Aug 2020 06:19:31 +0200
91acb2
Subject: [PATCH] IPA-EPN: Fix SMTP connection error handling
91acb2
MIME-Version: 1.0
91acb2
Content-Type: text/plain; charset=UTF-8
91acb2
Content-Transfer-Encoding: 8bit
91acb2
91acb2
Enhance error message when SMTP is down.
91acb2
91acb2
Fixes: https://pagure.io/freeipa/issue/8445
91acb2
Signed-off-by: François Cami <fcami@redhat.com>
91acb2
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
91acb2
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
91acb2
---
91acb2
 ipaclient/install/ipa_epn.py          | 17 ++++++++++-------
91acb2
 ipatests/test_integration/test_epn.py |  1 -
91acb2
 2 files changed, 10 insertions(+), 8 deletions(-)
91acb2
91acb2
diff --git a/ipaclient/install/ipa_epn.py b/ipaclient/install/ipa_epn.py
91acb2
index 0d1ae2add..82d7b3f57 100644
91acb2
--- a/ipaclient/install/ipa_epn.py
91acb2
+++ b/ipaclient/install/ipa_epn.py
91acb2
@@ -38,6 +38,7 @@ from email.mime.multipart import MIMEMultipart
91acb2
 from email.mime.text import MIMEText
91acb2
 from email.header import Header
91acb2
 from email.utils import make_msgid
91acb2
+from socket import error as socketerror
91acb2
 
91acb2
 from ipaplatform.paths import paths
91acb2
 from ipalib import api, errors
91acb2
@@ -640,13 +641,15 @@ class MTAClient:
91acb2
                     port=self._smtp_port,
91acb2
                     timeout=self._smtp_timeout,
91acb2
                 )
91acb2
-        except smtplib.SMTPException as e:
91acb2
-            logger.error(
91acb2
-                "IPA-EPN: Unable to connect to %s:%s: %s",
91acb2
-                self._smtp_hostname,
91acb2
-                self._smtp_port,
91acb2
-                e,
91acb2
-            )
91acb2
+        except (socketerror, smtplib.SMTPException) as e:
91acb2
+            msg = \
91acb2
+                "IPA-EPN: Could not connect to the configured SMTP server: " \
91acb2
+                "{host}:{port}: {error}".format(
91acb2
+                    host=self._smtp_hostname,
91acb2
+                    port=self._smtp_port,
91acb2
+                    error=e
91acb2
+                )
91acb2
+            raise admintool.ScriptError(msg)
91acb2
 
91acb2
         try:
91acb2
             self._conn.ehlo()
91acb2
diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py
91acb2
index 1a25d3710..e03521193 100644
91acb2
--- a/ipatests/test_integration/test_epn.py
91acb2
+++ b/ipatests/test_integration/test_epn.py
91acb2
@@ -249,7 +249,6 @@ class TestEPN(IntegrationTest):
91acb2
         ck = "192481b52fb591112afd7b55b12a44c6618fdbc7e05a3b1866fd67ec579c51df"
91acb2
         assert cmd2.stdout_text.find(ck) == 0
91acb2
 
91acb2
-    @pytest.mark.xfail(reason='freeipa ticket 8445', strict=True)
91acb2
     def test_EPN_connection_refused(self):
91acb2
         """Test EPN behavior when the configured SMTP is down
91acb2
         """
91acb2
-- 
91acb2
2.26.2
91acb2