Blob Blame History Raw
From 3cf7fb1014ae40fd5a5278f27577a8196a4af051 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
Date: Fri, 7 Aug 2020 07:51:53 +0200
Subject: [PATCH] ipatests: test_epn: add test_EPN_connection_refused
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add a test for EPN behavior when the configured SMTP does not
accept connections.

Fixes: https://pagure.io/freeipa/issue/8445
Signed-off-by: François Cami <fcami@redhat.com>
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
---
 ipatests/test_integration/test_epn.py | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py
index c5c73835a..1a25d3710 100644
--- a/ipatests/test_integration/test_epn.py
+++ b/ipatests/test_integration/test_epn.py
@@ -182,14 +182,20 @@ class TestEPN(IntegrationTest):
         self,
         host,
         dry_run=False,
+        mailtest=False,
         from_nbdays=None,
         to_nbdays=None,
         raiseonerr=True,
         validatejson=True
     ):
-        result = tasks.ipa_epn(host, raiseonerr=raiseonerr, dry_run=dry_run,
-                               from_nbdays=from_nbdays,
-                               to_nbdays=to_nbdays)
+        result = tasks.ipa_epn(
+            host,
+            from_nbdays=from_nbdays,
+            to_nbdays=to_nbdays,
+            mailtest=mailtest,
+            dry_run=dry_run,
+            raiseonerr=raiseonerr
+        )
         if validatejson:
             json.dumps(json.loads(result.stdout_text), ensure_ascii=False)
         return (result.stdout_text, result.stderr_text, result.returncode)
@@ -243,6 +249,21 @@ class TestEPN(IntegrationTest):
         ck = "192481b52fb591112afd7b55b12a44c6618fdbc7e05a3b1866fd67ec579c51df"
         assert cmd2.stdout_text.find(ck) == 0
 
+    @pytest.mark.xfail(reason='freeipa ticket 8445', strict=True)
+    def test_EPN_connection_refused(self):
+        """Test EPN behavior when the configured SMTP is down
+        """
+
+        self.master.run_command(["systemctl", "stop", "postfix"])
+        (unused, stderr_text, rc) = self._check_epn_output(
+            self.master, mailtest=True,
+            raiseonerr=False, validatejson=False
+        )
+        self.master.run_command(["systemctl", "start", "postfix"])
+        assert "IPA-EPN: Could not connect to the configured SMTP server" in \
+            stderr_text
+        assert rc > 0
+
     def test_EPN_smoketest_1(self):
         """No users except admin. Check --dry-run output.
            With the default configuration, the result should be an empty list.
-- 
2.26.2

From 53f330b053740b169d211aa16b3b36fb61157bbd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
Date: Fri, 7 Aug 2020 06:19:31 +0200
Subject: [PATCH] IPA-EPN: Fix SMTP connection error handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Enhance error message when SMTP is down.

Fixes: https://pagure.io/freeipa/issue/8445
Signed-off-by: François Cami <fcami@redhat.com>
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
---
 ipaclient/install/ipa_epn.py          | 17 ++++++++++-------
 ipatests/test_integration/test_epn.py |  1 -
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/ipaclient/install/ipa_epn.py b/ipaclient/install/ipa_epn.py
index 0d1ae2add..82d7b3f57 100644
--- a/ipaclient/install/ipa_epn.py
+++ b/ipaclient/install/ipa_epn.py
@@ -38,6 +38,7 @@ from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 from email.header import Header
 from email.utils import make_msgid
+from socket import error as socketerror
 
 from ipaplatform.paths import paths
 from ipalib import api, errors
@@ -640,13 +641,15 @@ class MTAClient:
                     port=self._smtp_port,
                     timeout=self._smtp_timeout,
                 )
-        except smtplib.SMTPException as e:
-            logger.error(
-                "IPA-EPN: Unable to connect to %s:%s: %s",
-                self._smtp_hostname,
-                self._smtp_port,
-                e,
-            )
+        except (socketerror, smtplib.SMTPException) as e:
+            msg = \
+                "IPA-EPN: Could not connect to the configured SMTP server: " \
+                "{host}:{port}: {error}".format(
+                    host=self._smtp_hostname,
+                    port=self._smtp_port,
+                    error=e
+                )
+            raise admintool.ScriptError(msg)
 
         try:
             self._conn.ehlo()
diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py
index 1a25d3710..e03521193 100644
--- a/ipatests/test_integration/test_epn.py
+++ b/ipatests/test_integration/test_epn.py
@@ -249,7 +249,6 @@ class TestEPN(IntegrationTest):
         ck = "192481b52fb591112afd7b55b12a44c6618fdbc7e05a3b1866fd67ec579c51df"
         assert cmd2.stdout_text.find(ck) == 0
 
-    @pytest.mark.xfail(reason='freeipa ticket 8445', strict=True)
     def test_EPN_connection_refused(self):
         """Test EPN behavior when the configured SMTP is down
         """
-- 
2.26.2