forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalStackContainer.java
165 lines (144 loc) · 6.75 KB
/
LocalStackContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package org.testcontainers.containers.localstack;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.rnorth.ducttape.Preconditions;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.TestcontainersConfiguration;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* <p>Container for Atlassian Labs Localstack, 'A fully functional local AWS cloud stack'.</p>
* <p>{@link LocalStackContainer#withServices(Service...)} should be used to select which services
* are to be launched. See {@link Service} for available choices. It is advised that
* {@link LocalStackContainer#getEndpointConfiguration(Service)} and
* {@link LocalStackContainer#getDefaultCredentialsProvider()}
* be used to obtain compatible endpoint configuration and credentials, respectively.</p>
*/
public class LocalStackContainer extends GenericContainer<LocalStackContainer> {
public static final String VERSION = "0.9.4";
private static final String HOSTNAME_EXTERNAL_ENV_VAR = "HOSTNAME_EXTERNAL";
private final List<Service> services = new ArrayList<>();
public LocalStackContainer() {
this(VERSION);
}
public LocalStackContainer(String version) {
super(TestcontainersConfiguration.getInstance().getLocalStackImage() + ":" + version);
withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock");
waitingFor(Wait.forLogMessage(".*Ready\\.\n", 1));
}
@Override
protected void configure() {
super.configure();
Preconditions.check("services list must not be empty", !services.isEmpty());
withEnv("SERVICES", services.stream().map(Service::getLocalStackName).collect(Collectors.joining(",")));
String hostnameExternalReason;
if (getEnvMap().containsKey(HOSTNAME_EXTERNAL_ENV_VAR)) {
// do nothing
hostnameExternalReason = "explicitly as environment variable";
} else if (getNetwork() != null && getNetworkAliases() != null && getNetworkAliases().size() >= 1) {
withEnv(HOSTNAME_EXTERNAL_ENV_VAR, getNetworkAliases().get(getNetworkAliases().size() - 1)); // use the last network alias set
hostnameExternalReason = "to match last network alias on container with non-default network";
} else {
withEnv(HOSTNAME_EXTERNAL_ENV_VAR, getContainerIpAddress());
hostnameExternalReason = "to match host-routable address for container";
}
logger().info("{} environment variable set to {} ({})", HOSTNAME_EXTERNAL_ENV_VAR, getEnvMap().get(HOSTNAME_EXTERNAL_ENV_VAR), hostnameExternalReason);
for (Service service : services) {
addExposedPort(service.getPort());
}
}
/**
* Declare a set of simulated AWS services that should be launched by this container.
* @param services one or more service names
* @return this container object
*/
public LocalStackContainer withServices(Service... services) {
this.services.addAll(Arrays.asList(services));
return self();
}
/**
* Provides an endpoint configuration that is preconfigured to communicate with a given simulated service.
* The provided endpoint configuration should be set in the AWS Java SDK when building a client, e.g.:
* <pre><code>AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(S3))
.withCredentials(localstack.getDefaultCredentialsProvider())
.build();
</code></pre>
*
* <p><strong>Please note that this method is only intended to be used for configuring AWS SDK clients
* that are running on the test host. If other containers need to call this one, they should be configured
* specifically to do so using a Docker network and appropriate addressing.</strong></p>
*
* @param service the service that is to be accessed
* @return an {@link AwsClientBuilder.EndpointConfiguration}
*/
public AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(Service service) {
final String address = getContainerIpAddress();
String ipAddress = address;
try {
// resolve IP address and use that as the endpoint so that path-style access is automatically used for S3
ipAddress = InetAddress.getByName(address).getHostAddress();
} catch (UnknownHostException ignored) {
}
return new AwsClientBuilder.EndpointConfiguration(
"http://" +
ipAddress +
":" +
getMappedPort(service.getPort()), "us-east-1");
}
/**
* Provides a {@link AWSCredentialsProvider} that is preconfigured to communicate with a given simulated service.
* The credentials provider should be set in the AWS Java SDK when building a client, e.g.:
* <pre><code>AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(S3))
.withCredentials(localstack.getDefaultCredentialsProvider())
.build();
</code></pre>
* @return an {@link AWSCredentialsProvider}
*/
public AWSCredentialsProvider getDefaultCredentialsProvider() {
return new AWSStaticCredentialsProvider(new BasicAWSCredentials("accesskey", "secretkey"));
}
@RequiredArgsConstructor
@Getter
@FieldDefaults(makeFinal = true)
public enum Service {
API_GATEWAY("apigateway", 4567),
KINESIS("kinesis", 4568),
DYNAMODB("dynamodb", 4569),
DYNAMODB_STREAMS("dynamodbstreams", 4570),
// TODO: Clarify usage for ELASTICSEARCH and ELASTICSEARCH_SERVICE
// ELASTICSEARCH("es", 4571),
S3("s3", 4572),
FIREHOSE("firehose", 4573),
LAMBDA("lambda", 4574),
SNS("sns", 4575),
SQS("sqs", 4576),
REDSHIFT("redshift", 4577),
// ELASTICSEARCH_SERVICE("", 4578),
SES("ses", 4579),
ROUTE53("route53", 4580),
CLOUDFORMATION("cloudformation", 4581),
CLOUDWATCH("cloudwatch", 4582),
SSM("ssm", 4583),
SECRETSMANAGER("secretsmanager", 4584),
STEPFUNCTIONS("stepfunctions", 4585),
CLOUDWATCHLOGS("logs", 4586),
STS("sts", 4592),
IAM("iam", 4593);
String localStackName;
int port;
}
}