Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MailHog module (#1697) #1698

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ embedded:

=== link:embedded-wiremock/README.adoc[embedded-wiremock]

=== link:embedded-mailhog/README.adoc[embedded-mailhog]


== How to contribute

Expand Down
57 changes: 57 additions & 0 deletions embedded-mailhog/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
=== embedded-mailhog

==== Maven dependency

.pom.xml
[source,xml]
----
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-mailhog</artifactId>
<scope>test</scope>
</dependency>
----

==== Consumes (via `bootstrap.properties`)

* `embedded.mailhog.enabled` `(true|false, default is true)`
* `embedded.mailhog.reuseContainer` `(true|false, default is false)`
* `embedded.mailhog.dockerImage` `(default is 'mailhog/mailhog:v1.0.1')`
** Image versions on https://hub.docker.com/r/mailhog/mailhog/tags[dockerhub]
* `embedded.mailhog.waitTimeoutInSeconds` `(default is 60 seconds)`
* `embedded.mailhog.smtp-port` `(default is 1025)`
* `embedded.mailhog.http-port` `(default is 8025)`
* `embedded.toxiproxy.proxies.mailhog.enabled` Enables both creation of the container with ToxiProxy TCP proxy and a proxy to the `embedded-mailhog` container SMTP port.


==== Produces

* `embedded.mailhog.host`
* `embedded.mailhog.smtp-port`
* `embedded.mailhog.http-port`
* `embedded.mailhog.toxiproxy.host`
* `embedded.mailhog.toxiproxy.port`
* `embedded.mailhog.networkAlias`
* `embedded.mailhog.internalSmtpPort`
* `embedded.mailhog.internalHttpPort`
* Bean `ToxiproxyContainer.ContainerProxy mailhogSmtpContainerProxy`

==== Example (Spring Boot)

bootstrap-test.yml
[source,yaml]
----
embedded:
mailhog:
enabled: true
docker-image: 'mailhog/mailhog:v1.0.1'
----

application-test.yml
[source,yaml]
----
spring:
mail:
host: ${embedded.mailhog.host}
port: ${embedded.mailhog.smtp-port}
----
38 changes: 38 additions & 0 deletions embedded-mailhog/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>testcontainers-spring-boot-parent</artifactId>
<groupId>com.playtika.testcontainers</groupId>
<version>3.1.1</version>
<relativePath>../testcontainers-spring-boot-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>embedded-mailhog</artifactId>

<dependencies>
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>testcontainers-common</artifactId>
</dependency>
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-toxiproxy</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.playtika.testcontainer.mailhog;

import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration;
import com.playtika.testcontainer.common.utils.ContainerUtils;
import com.playtika.testcontainer.toxiproxy.condition.ConditionalOnToxiProxyEnabled;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.ToxiproxyContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart;
import static com.playtika.testcontainer.mailhog.MailHogProperties.BEAN_NAME_EMBEDDED_MAILHOG;

@Slf4j
@Configuration
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
@ConditionalOnProperty(name = "embedded.mailhog.enabled", matchIfMissing = true)
@EnableConfigurationProperties(MailHogProperties.class)
public class EmbeddedMailHogBootstrapConfiguration {

private static final String MAILHOG_NETWORK_ALIAS = "mailhog.testcontainer.docker";

@Bean
@ConditionalOnToxiProxyEnabled(module = "mailhog")
ToxiproxyContainer.ContainerProxy mailhogSmtpContainerProxy(ToxiproxyContainer toxiproxyContainer,
@Qualifier(BEAN_NAME_EMBEDDED_MAILHOG) GenericContainer<?> mailhogContainer,
MailHogProperties properties,
ConfigurableEnvironment environment) {
ToxiproxyContainer.ContainerProxy proxy = toxiproxyContainer.getProxy(mailhogContainer, properties.getSmtpPort());

Map<String, Object> map = new LinkedHashMap<>();
map.put("embedded.mailhog.smtp.toxiproxy.host", proxy.getContainerIpAddress());
map.put("embedded.mailhog.smtp.toxiproxy.port", proxy.getProxyPort());
map.put("embedded.mailhog.smtp.toxiproxy.proxyName", proxy.getName());

MapPropertySource propertySource = new MapPropertySource("embeddedMailhogSmtpToxiproxyInfo", map);
environment.getPropertySources().addFirst(propertySource);
log.info("Started MailHog SMTP ToxiProxy connection details {}", map);

return proxy;
}

@ConditionalOnMissingBean(name = BEAN_NAME_EMBEDDED_MAILHOG)
@Bean(name = BEAN_NAME_EMBEDDED_MAILHOG, destroyMethod = "stop")
public GenericContainer<?> mailHog(ConfigurableEnvironment environment,
MailHogProperties properties,
Optional<Network> network) {
GenericContainer<?> mailHog = new GenericContainer<>(ContainerUtils.getDockerImageName(properties))
.withExposedPorts(properties.getSmtpPort(), properties.getHttpPort())
.withNetworkAliases(MAILHOG_NETWORK_ALIAS)
.waitingFor(Wait.forListeningPort());

network.ifPresent(mailHog::withNetwork);

mailHog = configureCommonsAndStart(mailHog, properties, log);
registerMailHogEnvironment(mailHog, environment, properties);
return mailHog;
}

private void registerMailHogEnvironment(GenericContainer<?> mailHog, ConfigurableEnvironment environment, MailHogProperties properties) {
Integer smtpMappedPort = mailHog.getMappedPort(properties.getSmtpPort());
Integer httpMappedPort = mailHog.getMappedPort(properties.getHttpPort());

LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("embedded.mailhog.host", mailHog.getHost());
map.put("embedded.mailhog.smtp-port", smtpMappedPort);
map.put("embedded.mailhog.http-port", httpMappedPort);
map.put("embedded.mailhog.networkAlias", MAILHOG_NETWORK_ALIAS);
map.put("embedded.mailhog.internalSmtpPort", properties.getSmtpPort());
map.put("embedded.mailhog.internalHttpPort", properties.getHttpPort());

log.info("Started MailHog. Connection details: {}", map);

MapPropertySource propertySource = new MapPropertySource("embeddedMailHogInfo", map);
environment.getPropertySources().addFirst(propertySource);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.playtika.testcontainer.mailhog;

import com.playtika.testcontainer.common.properties.CommonContainerProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@EqualsAndHashCode(callSuper = true)
@ConfigurationProperties("embedded.mailhog")
public class MailHogProperties extends CommonContainerProperties {

public static final String BEAN_NAME_EMBEDDED_MAILHOG = "embeddedMailHog";

private Integer smtpPort = 1025;
private Integer httpPort = 8025;

@Override
public String getDefaultDockerImage() {
// Please don`t remove this comment.
// renovate: datasource=docker
return "mailhog/mailhog:v1.0.1";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"groups": [
{
"name": "embedded.mailhog",
"description": "Configuration properties related to embedded MailHog."
}
],
"properties": [
{
"name": "embedded.mailhog.enabled",
"type": "java.lang.Boolean",
"defaultValue": "true"
},
{
"name": "embedded.mailhog.docker-image",
"type": "java.lang.String",
"defaultValue": "mailhog/mailhog:v1.0.1"
}
],
"hints": [
{
"name": "embedded.mailhog.enabled",
"values": [
{
"value": "true",
"description": "Enables configuration of MailHog server on startup."
},
{
"value": "false",
"description": "Disables configuration of MailHog server on startup."
}
]
},
{
"name": "embedded.mailhog.docker-image",
"values": [
{
"value": "mailhog/mailhog:v1.0.1",
"description": "Default MailHog image. Ref https://hub.docker.com/r/mailhog/mailhog for further info."
}
]
}
]
}
2 changes: 2 additions & 0 deletions embedded-mailhog/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.playtika.testcontainer.mailhog.EmbeddedMailHogBootstrapConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.playtika.testcontainer.mailhog;

import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.testcontainers.containers.Container;

import static org.assertj.core.api.Assertions.assertThat;

class DisableMailHogTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(EmbeddedMailHogBootstrapConfiguration.class)
);

@Test
void contextLoads() {
contextRunner
.withPropertyValues(
"embedded.mailhog.enabled=false"
)
.run((context) -> assertThat(context)
.hasNotFailed()
.doesNotHaveBean(Container.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.playtika.testcontainer.mailhog;

import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.testcontainers.containers.ToxiproxyContainer;

import static org.assertj.core.api.Assertions.assertThat;

class DisableToxiProxyTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
EmbeddedMailHogBootstrapConfiguration.class));

@Test
void isNotEnabledByDefault() {
contextRunner
.withPropertyValues(
)
.run((context) -> assertThat(context)
.hasNotFailed()
.doesNotHaveBean(ToxiproxyContainer.ContainerProxy.class));
}

@Test
void shouldDisableToxiProxy() {
contextRunner
.withPropertyValues(
"embedded.toxiproxy.proxies.mailhog.enabled=false"
)
.run(context -> assertThat(context)
.hasNotFailed()
.doesNotHaveBean(ToxiproxyContainer.ContainerProxy.class));
}
}
Loading
Loading