1
1
package org .testcontainers .containers .localstack ;
2
2
3
+ import com .github .dockerjava .api .command .InspectContainerResponse ;
3
4
import lombok .Getter ;
4
5
import lombok .RequiredArgsConstructor ;
5
6
import lombok .experimental .FieldDefaults ;
8
9
import org .testcontainers .DockerClientFactory ;
9
10
import org .testcontainers .containers .GenericContainer ;
10
11
import org .testcontainers .containers .wait .strategy .Wait ;
12
+ import org .testcontainers .images .builder .Transferable ;
11
13
import org .testcontainers .utility .ComparableVersion ;
12
14
import org .testcontainers .utility .DockerImageName ;
13
- import org .testcontainers .utility .ResourceReaper ;
14
15
15
16
import java .net .InetAddress ;
16
17
import java .net .URI ;
20
21
import java .util .Arrays ;
21
22
import java .util .List ;
22
23
import java .util .stream .Collectors ;
23
- import java .util .stream .Stream ;
24
24
25
25
/**
26
26
* Testcontainers implementation for LocalStack.
@@ -53,6 +53,8 @@ public class LocalStackContainer extends GenericContainer<LocalStackContainer> {
53
53
54
54
private static final String DEFAULT_AWS_SECRET_ACCESS_KEY = "test" ;
55
55
56
+ private static final String STARTER_SCRIPT = "/testcontainers_start.sh" ;
57
+
56
58
@ Deprecated
57
59
public static final String VERSION = DEFAULT_TAG ;
58
60
@@ -121,6 +123,8 @@ public LocalStackContainer(final DockerImageName dockerImageName, boolean useLeg
121
123
122
124
withFileSystemBind (DockerClientFactory .instance ().getRemoteDockerUnixSocketPath (), "/var/run/docker.sock" );
123
125
waitingFor (Wait .forLogMessage (".*Ready\\ .\n " , 1 ));
126
+ withCreateContainerCmdModifier (cmd -> cmd .withEntrypoint ("sh" ));
127
+ setCommand ("-c" , "while [ ! -f " + STARTER_SCRIPT + " ]; do sleep 0.1; done; " + STARTER_SCRIPT );
124
128
}
125
129
126
130
private static boolean isVersion2 (String version ) {
@@ -167,34 +171,6 @@ static boolean shouldRunInLegacyMode(String version) {
167
171
return true ;
168
172
}
169
173
170
- /**
171
- * Provides a docker argument string including all default labels set on testcontainer containers
172
- * @return Argument string in the format `-l key1=value1 -l key2=value2`
173
- */
174
- private static String internalMarkerLabels () {
175
- return Stream
176
- .concat (
177
- DockerClientFactory .DEFAULT_LABELS .entrySet ().stream (),
178
- ResourceReaper .instance ().getLabels ().entrySet ().stream ()
179
- )
180
- .map (entry -> String .format ("-l %s=%s" , entry .getKey (), entry .getValue ()))
181
- .collect (Collectors .joining (" " ));
182
- }
183
-
184
- /**
185
- * Configure the LocalStack container to include the default testcontainer labels on all spawned lambda containers
186
- * Necessary to properly clean up lambda containers even if the LocalStack container is killed before it gets the
187
- * chance.
188
- */
189
- private void configureLambdaContainerLabels () {
190
- String lambdaDockerFlags = internalMarkerLabels ();
191
- String existingLambdaDockerFlags = getEnvMap ().get ("LAMBDA_DOCKER_FLAGS" );
192
- if (existingLambdaDockerFlags != null ) {
193
- lambdaDockerFlags = existingLambdaDockerFlags + " " + lambdaDockerFlags ;
194
- }
195
- withEnv ("LAMBDA_DOCKER_FLAGS" , lambdaDockerFlags );
196
- }
197
-
198
174
@ Override
199
175
protected void configure () {
200
176
super .configure ();
@@ -217,7 +193,50 @@ protected void configure() {
217
193
}
218
194
219
195
exposePorts ();
220
- configureLambdaContainerLabels ();
196
+ }
197
+
198
+ @ Override
199
+ protected void containerIsStarting (InspectContainerResponse containerInfo ) {
200
+ String command = "#!/bin/bash\n " ;
201
+ command += "export LAMBDA_DOCKER_FLAGS=" + configureLambdaContainerLabels () + "\n " ;
202
+ command += "/usr/local/bin/docker-entrypoint.sh\n " ;
203
+ copyFileToContainer (Transferable .of (command , 0777 ), STARTER_SCRIPT );
204
+ }
205
+
206
+ /**
207
+ * Configure the LocalStack container to include the default testcontainers labels on all spawned lambda containers
208
+ * Necessary to properly clean up lambda containers even if the LocalStack container is killed before it gets the
209
+ * chance.
210
+ * @return the lambda container labels as a string
211
+ */
212
+ private String configureLambdaContainerLabels () {
213
+ String lambdaDockerFlags = internalMarkerLabels ();
214
+ String existingLambdaDockerFlags = getEnvMap ().get ("LAMBDA_DOCKER_FLAGS" );
215
+ if (existingLambdaDockerFlags != null ) {
216
+ lambdaDockerFlags = existingLambdaDockerFlags + " " + lambdaDockerFlags ;
217
+ }
218
+ return "\" " + lambdaDockerFlags + "\" " ;
219
+ }
220
+
221
+ /**
222
+ * Provides a docker argument string including all default labels set on testcontainers containers (excluding reuse labels)
223
+ * @return Argument string in the format `-l key1=value1 -l key2=value2`
224
+ */
225
+ private String internalMarkerLabels () {
226
+ return getContainerInfo ()
227
+ .getConfig ()
228
+ .getLabels ()
229
+ .entrySet ()
230
+ .stream ()
231
+ .filter (entry -> entry .getKey ().startsWith (DockerClientFactory .TESTCONTAINERS_LABEL ))
232
+ .filter (entry -> {
233
+ return (
234
+ !entry .getKey ().equals ("org.testcontainers.hash" ) &&
235
+ !entry .getKey ().equals ("org.testcontainers.copied_files.hash" )
236
+ );
237
+ })
238
+ .map (entry -> String .format ("-l %s=%s" , entry .getKey (), entry .getValue ()))
239
+ .collect (Collectors .joining (" " ));
221
240
}
222
241
223
242
private void resolveHostname (String envVar ) {
0 commit comments