You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ad...@apache.org on 2017/12/05 07:30:47 UTC

[07/19] james-project git commit: JAMES-2242 Implement test demonstrating RemoteDelivery gateway feature works

JAMES-2242 Implement test demonstrating RemoteDelivery gateway feature works


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0b4923b2
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0b4923b2
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0b4923b2

Branch: refs/heads/master
Commit: 0b4923b2e54f8e2a0bf038bce2f2b53969f5b54b
Parents: 2949c91
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 17:29:58 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Mon Dec 4 14:42:16 2017 +0100

----------------------------------------------------------------------
 .../GatewayRemoteDeliveryIntegrationTest.java   | 482 +++++++++++++++++++
 1 file changed, 482 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0b4923b2/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
new file mode 100644
index 0000000..2e01488
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
@@ -0,0 +1,482 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailets;
+
+import static com.jayway.restassured.RestAssured.when;
+import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
+import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+
+import java.net.InetAddress;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.dnsservice.api.InMemoryDNSService;
+import org.apache.james.jmap.mailet.VacationMailet;
+import org.apache.james.mailbox.model.MailboxConstants;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.Null;
+import org.apache.james.transport.mailets.PostmasterAlias;
+import org.apache.james.transport.mailets.RemoteDelivery;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.RecipientIsLocal;
+import org.apache.james.transport.matchers.RelayLimit;
+import org.apache.james.util.streams.SwarmGenericContainer;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TemporaryFolder;
+import org.testcontainers.containers.wait.HostPortWaitStrategy;
+
+import com.google.common.base.Charsets;
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.builder.RequestSpecBuilder;
+import com.jayway.restassured.http.ContentType;
+
+public class GatewayRemoteDeliveryIntegrationTest {
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    private static final int IMAP_PORT = 1143;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String JAMES_ANOTHER_DOMAIN = "james.com";
+
+    private static final String FROM = "from@" + JAMES_APACHE_ORG;
+    private static final String RECIPIENT = "touser@" + JAMES_ANOTHER_DOMAIN;
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private final TemporaryFolder smtpFolder = new TemporaryFolder();
+    private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer("weave/rest-smtp-sink:latest")
+        .withExposedPorts(25)
+        .withAffinityToContainer()
+        .waitingFor(new HostPortWaitStrategy());
+
+    @Rule
+    public final RuleChain chain = RuleChain.outerRule(smtpFolder).around(fakeSmtp);
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+    private DataProbe dataProbe;
+
+
+    @Before
+    public void setup() throws Exception {
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+
+        RestAssured.requestSpecification = new RequestSpecBuilder()
+            .setContentType(ContentType.JSON)
+            .setAccept(ContentType.JSON)
+            .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
+            .setPort(80)
+            .setBaseUri("http://" + fakeSmtp.getContainerIp())
+            .build();
+    }
+
+    @After
+    public void tearDown() {
+        if (jamesServer != null) {
+            jamesServer.shutdown();
+        }
+    }
+
+    @Test
+    public void outgoingMailShouldTransitThroughGatewayWhenNoPort() throws Exception {
+        String gatewayProperty = fakeSmtp.getContainerIp();
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            generateMailetContainerConfiguration(gatewayProperty));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(this::messageIsReceivedByTheSmtpServer);
+        }
+    }
+
+    @Test
+    public void outgoingMailShouldTransitThroughGatewayWhenPort() throws Exception {
+        String gatewayProperty = fakeSmtp.getContainerIp() + ":25";
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            generateMailetContainerConfiguration(gatewayProperty));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(this::messageIsReceivedByTheSmtpServer);
+        }
+    }
+
+    @Test
+    public void outgoingMailShouldTransitThroughGatewayWhenSeveralIps() throws Exception {
+        String gatewayProperty = fakeSmtp.getContainerIp() + ",invalid.domain";
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            generateMailetContainerConfiguration(gatewayProperty));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(this::messageIsReceivedByTheSmtpServer);
+        }
+    }
+
+    @Test
+    public void outgoingMailShouldFallbackToSecondGatewayWhenFirstInvalid() throws Exception {
+        String gatewayProperty = "invalid.domain," + fakeSmtp.getContainerIp();
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            generateMailetContainerConfiguration(gatewayProperty));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(this::messageIsReceivedByTheSmtpServer);
+        }
+    }
+
+    @Test
+    public void outgoingMailShouldNotBeSentDirectlyToTheHostWhenGatewayFails() throws Exception {
+        String gatewayProperty = "invalid.domain";
+        InMemoryDNSService inMemoryDNSService = new InMemoryDNSService();
+        InetAddress containerIp = InetAddress.getByName(fakeSmtp.getContainerIp());
+        inMemoryDNSService.registerRecord(JAMES_ANOTHER_DOMAIN, containerIp, JAMES_ANOTHER_DOMAIN);
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            generateMailetContainerConfiguration(gatewayProperty),
+            binder -> binder.bind(DNSService.class).toInstance(inMemoryDNSService));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            Thread.sleep(TimeUnit.SECONDS.toMillis(5));
+            when()
+                .get("/api/email")
+            .then()
+                .statusCode(200)
+                .body("", hasSize(0));
+        }
+    }
+
+    @Ignore("JAMES-2242" +
+        "https://issues.apache.org/jira/browse/JAMES-2242?focusedCommentId=16270289&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-16270289" +
+        "Bouncing is failing due to several issues:" +
+        " - A loop in Bounce processor, keeps posting in bounce processor (DSNBounce processor don't rest mail state)" +
+        " - Memory mail queue don't preserve state field like other Mail Queue implementations" +
+        " - Memory DNS returns RuntimeExceptions upon absent address, not DNS failures")
+    @Test
+    public void remoteDeliveryShouldBounceUponFailure() throws Exception {
+        String gatewayProperty = "invalid.domain";
+        InMemoryDNSService inMemoryDNSService = new InMemoryDNSService();
+        InetAddress containerIp = InetAddress.getByName(fakeSmtp.getContainerIp());
+        inMemoryDNSService.registerRecord(JAMES_ANOTHER_DOMAIN, containerIp, JAMES_ANOTHER_DOMAIN);
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            MailetContainer.builder()
+                .postmaster("postmaster@" + JAMES_APACHE_ORG)
+                .threads(5)
+                .addProcessor(root())
+                .addProcessor(CommonProcessors.error())
+                .addProcessor(relayAndLocalDeliveryTransport(gatewayProperty))
+                .addProcessor(CommonProcessors.bounces())
+                .build(),
+            binder -> binder.bind(DNSService.class).toInstance(inMemoryDNSService));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() ->
+                imapMessageReader.userReceivedMessageInMailbox(FROM, PASSWORD, MailboxConstants.INBOX));
+        }
+    }
+
+    @Test
+    public void remoteDeliveryShouldBounceUponFailureWhenNoBounceProcessor() throws Exception {
+        String gatewayProperty = "invalid.domain";
+        InMemoryDNSService inMemoryDNSService = new InMemoryDNSService();
+        InetAddress containerIp = InetAddress.getByName(fakeSmtp.getContainerIp());
+        inMemoryDNSService.registerRecord(JAMES_ANOTHER_DOMAIN, containerIp, JAMES_ANOTHER_DOMAIN);
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            MailetContainer.builder()
+                .postmaster("postmaster@" + JAMES_APACHE_ORG)
+                .threads(5)
+                .addProcessor(root())
+                .addProcessor(CommonProcessors.error())
+                .addProcessor(ProcessorConfiguration.builder()
+                    .state("transport")
+                    .enableJmx(true)
+                    .addMailet(MailetConfiguration.builder()
+                        .matcher(All.class)
+                        .mailet(RemoveMimeHeader.class)
+                        .addProperty("name", "bcc")
+                        .build())
+                    .addMailet(MailetConfiguration.builder()
+                        .matcher(RecipientIsLocal.class)
+                        .mailet(VacationMailet.class)
+                        .build())
+                    .addMailet(MailetConfiguration.builder()
+                        .matcher(RecipientIsLocal.class)
+                        .mailet(LocalDelivery.class)
+                        .build())
+                    .addMailet(MailetConfiguration.builder()
+                        .matcher(All.class)
+                        .mailet(RemoteDelivery.class)
+                        .addProperty("outgoingQueue", "outgoing")
+                        .addProperty("delayTime", "5000, 100000, 500000")
+                        .addProperty("maxRetries", "2")
+                        .addProperty("maxDnsProblemRetries", "0")
+                        .addProperty("deliveryThreads", "2")
+                        .addProperty("sendpartial", "true")
+                        .addProperty("gateway", gatewayProperty)
+                        .build())
+                    .build())
+                .addProcessor(CommonProcessors.bounces())
+                .build(),
+            binder -> binder.bind(DNSService.class).toInstance(inMemoryDNSService));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() ->
+                imapMessageReader.userReceivedMessageInMailbox(FROM, PASSWORD, MailboxConstants.INBOX));
+        }
+    }
+
+    @Test
+    public void directResolutionShouldBeWellPerformed() throws Exception {
+        InMemoryDNSService inMemoryDNSService = new InMemoryDNSService();
+        InetAddress containerIp = InetAddress.getByName(fakeSmtp.getContainerIp());
+        inMemoryDNSService.registerRecord(JAMES_ANOTHER_DOMAIN, containerIp, JAMES_ANOTHER_DOMAIN);
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder,
+            MailetContainer.builder()
+                .postmaster("postmaster@" + JAMES_APACHE_ORG)
+                .threads(5)
+                .addProcessor(root())
+                .addProcessor(CommonProcessors.error())
+                .addProcessor(directResolutionTransport())
+                .addProcessor(CommonProcessors.bounces())
+                .build(),
+            binder -> binder.bind(DNSService.class).toInstance(inMemoryDNSService));
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(this::messageIsReceivedByTheSmtpServer);
+        }
+    }
+
+    private boolean messageIsReceivedByTheSmtpServer() {
+        try {
+            when()
+                .get("/api/email")
+            .then()
+                .statusCode(200)
+                .body("", hasSize(1))
+                .body("[0].from", equalTo(FROM))
+                .body("[0].subject", equalTo("test"));
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    private MailetContainer generateMailetContainerConfiguration(String gatewayProperty) {
+        return MailetContainer.builder()
+            .postmaster("postmaster@" + JAMES_APACHE_ORG)
+            .threads(5)
+            .addProcessor(root())
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(relayOnlyTransport(gatewayProperty))
+            .addProcessor(CommonProcessors.bounces())
+            .build();
+    }
+
+    public ProcessorConfiguration root() {
+        // Custom in memory DNS resolution is not possible combined with InSpamerBackList
+        return ProcessorConfiguration.builder()
+            .state("root")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(PostmasterAlias.class)
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RelayLimit.class)
+                .matcherCondition("30")
+                .mailet(Null.class)
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .build();
+    }
+
+    private ProcessorConfiguration relayOnlyTransport(String gatewayProperty) {
+        return ProcessorConfiguration.builder()
+            .state("transport")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoveMimeHeader.class)
+                .addProperty("name", "bcc")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RecipientIsLocal.class)
+                .mailet(VacationMailet.class)
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoteDelivery.class)
+                .addProperty("outgoingQueue", "outgoing")
+                .addProperty("delayTime", "5000, 100000, 500000")
+                .addProperty("maxRetries", "2")
+                .addProperty("maxDnsProblemRetries", "0")
+                .addProperty("deliveryThreads", "2")
+                .addProperty("sendpartial", "true")
+                .addProperty("bounceProcessor", "bounces")
+                .addProperty("gateway", gatewayProperty)
+                .build())
+            .build();
+    }
+
+    private ProcessorConfiguration relayAndLocalDeliveryTransport(String gatewayProperty) {
+        return ProcessorConfiguration.builder()
+            .state("transport")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoveMimeHeader.class)
+                .addProperty("name", "bcc")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RecipientIsLocal.class)
+                .mailet(VacationMailet.class)
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RecipientIsLocal.class)
+                .mailet(LocalDelivery.class)
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoteDelivery.class)
+                .addProperty("outgoingQueue", "outgoing")
+                .addProperty("delayTime", "5000, 100000, 500000")
+                .addProperty("maxRetries", "2")
+                .addProperty("maxDnsProblemRetries", "0")
+                .addProperty("deliveryThreads", "2")
+                .addProperty("sendpartial", "true")
+                .addProperty("bounceProcessor", "bounces")
+                .addProperty("gateway", gatewayProperty)
+                .build())
+            .build();
+    }
+
+    private ProcessorConfiguration directResolutionTransport() {
+        return ProcessorConfiguration.builder()
+            .state("transport")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoveMimeHeader.class)
+                .addProperty("name", "bcc")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RecipientIsLocal.class)
+                .mailet(VacationMailet.class)
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoteDelivery.class)
+                .addProperty("outgoingQueue", "outgoing")
+                .addProperty("delayTime", "5000, 100000, 500000")
+                .addProperty("maxRetries", "2")
+                .addProperty("maxDnsProblemRetries", "0")
+                .addProperty("deliveryThreads", "2")
+                .addProperty("sendpartial", "true")
+                .addProperty("bounceProcessor", "bounces")
+                .build())
+            .build();
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org