Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(lambda): restore create-copy-start ordering for provided runtimes (#524)

Kenshin Okinakabc951bf4bfac
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java
index a083ac2ca8..4e32bc5a94 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java
@@ -33,6 +33,10 @@ class LambdaTest {
lambda.deleteFunction(DeleteFunctionRequest.builder()
.functionName("sdk-test-ruby-fn").build());
} catch (Exception ignored) {}
+ try {
+ lambda.deleteFunction(DeleteFunctionRequest.builder()
+ .functionName("sdk-test-provided-fn").build());
+ } catch (Exception ignored) {}
lambda.close();
}
}
@@ -306,6 +310,40 @@ class LambdaTest {
@Test
@Order(17)
+ void providedRuntimeInvoke() {
+ String providedFn = "sdk-test-provided-fn";
+
+ lambda.createFunction(CreateFunctionRequest.builder()
+ .functionName(providedFn)
+ .runtime(Runtime.PROVIDED_AL2023)
+ .role(ROLE)
+ .handler("bootstrap")
+ .timeout(30)
+ .code(FunctionCode.builder()
+ .zipFile(SdkBytes.fromByteArray(LambdaUtils.providedRuntimeZip()))
+ .build())
+ .build());
+
+ // RequestResponse invoke — the bootstrap shell script POSTs a
+ // response via the Runtime API, so we should get a real payload
+ // back rather than a timeout.
+ InvokeResponse response = lambda.invoke(InvokeRequest.builder()
+ .functionName(providedFn)
+ .invocationType(InvocationType.REQUEST_RESPONSE)
+ .payload(SdkBytes.fromUtf8String("{\"test\":true}"))
+ .build());
+
+ assertThat(response.statusCode()).isEqualTo(200);
+ assertThat(response.functionError()).isNull();
+ String payload = response.payload().asUtf8String();
+ assertThat(payload).contains("hello from provided runtime");
+
+ lambda.deleteFunction(DeleteFunctionRequest.builder()
+ .functionName(providedFn).build());
+ }
+
+ @Test
+ @Order(18)
void rubyRuntimeSupport() {
String rubyFn = "sdk-test-ruby-fn";
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
index ebeb1eb1ef..1bc852827f 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
@@ -48,22 +48,33 @@ public class ContainerLifecycleManager {
}
/**
- * Creates and starts a container from the given specification.
- * Automatically pulls the image if not present locally.
+ * Creates and immediately starts a container. Delegates to
+ * {@link #create} and {@link #startCreated}. Suitable when no
+ * filesystem modifications are needed between creation and start.
*
* @param spec the container specification
* @return information about the created container including resolved endpoints
*/
public ContainerInfo createAndStart(ContainerSpec spec) {
+ String containerId = create(spec);
+ return startCreated(containerId, spec);
+ }
+
+ /**
+ * Creates a container without starting it. Use {@link #startCreated} to
+ * start it after any pre-start setup (e.g. copying files into the
+ * container filesystem).
+ *
+ * @param spec the container specification
+ * @return the container ID
+ */
+ public String create(ContainerSpec spec) {
LOG.debugv("Creating container from spec: image={0}, name={1}", spec.image(), spec.name());
- // Ensure image is available
imageCacheService.ensureImageExists(spec.image());
- // Build HostConfig
HostConfig hostConfig = buildHostConfig(spec);
- // Create container command
CreateContainerCmd createCmd = dockerClient.createContainerCmd(spec.image())
.withHostConfig(hostConfig);
@@ -86,18 +97,23 @@ public class ContainerLifecycleManager {
createCmd.withExposedPorts(exposed);
}
- // Create the container
CreateContainerResponse response = createCmd.exec();
String containerId = response.getId();
- LOG.infov("Created container {0} (name={1})", containerId, spec.name());
+ LOG.infov("Created container {0} (name={1}, not yet started)", containerId, spec.name());
+ return containerId;
+ }
- // Start the container
+ /**
+ * Starts a previously created container and resolves its endpoints.
+ *
+ * @param containerId the container ID returned by {@link #create}
+ * @param spec the original spec (needed for network and endpoint resolution)
+ * @return information about the running container including resolved endpoints
+ */
+ public ContainerInfo startCreated(String containerId, ContainerSpec spec) {
dockerClient.startContainerCmd(containerId).exec();
LOG.infov("Started container {0}", containerId);
- // For containers with port bindings, withNetworkMode was skipped during creation
- // (it suppresses port publishing on macOS Docker Desktop). Connect to the
- // configured network now, after the host port bindings are established.
if (spec.networkMode() != null && !spec.networkMode().isBlank() && spec.hasPortBindings()) {
try {
dockerClient.connectToNetworkCmd()
@@ -111,9 +127,7 @@ public class ContainerLifecycleManager {
}
}
- // Resolve endpoints
Map<Integer, EndpointInfo> endpoints = resolveEndpoints(containerId, spec);
-
return new ContainerInfo(containerId, endpoints);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
index f59f23fe38..e0597486cf 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.lambda.launcher;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
-import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
@@ -165,9 +164,9 @@ public class ContainerLauncher {
ContainerSpec spec = specBuilder.build();
- // Create container (but don't start yet - need to copy code first for Zip packages)
- ContainerInfo info = lifecycleManager.createAndStart(spec);
- String containerId = info.containerId();
+ // Create container without starting — provided.* runtimes exec
+ // /var/runtime/bootstrap on start, so code must be copied first.
+ String containerId = lifecycleManager.create(spec);
LOG.infov("Created container {0} for function {1}", containerId, fn.getFunctionName());
// Copy code into container via Docker API tar stream (works inside Docker too)
@@ -190,6 +189,9 @@ public class ContainerLauncher {
}
}
+ // Now start the container with code in place
+ lifecycleManager.startCreated(containerId, spec);
+
ContainerHandle handle = new ContainerHandle(containerId, fn.getFunctionName(), runtimeApiServer, ContainerState.WARM);
// Determine CloudWatch Logs destination for this container instance
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java
index cbb25289e3..90dafb9888 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java
@@ -11,23 +11,24 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory;
import com.github.dockerjava.api.DockerClient;
-import com.github.dockerjava.api.model.Bind;
+import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
-import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -47,15 +48,15 @@ class ContainerLauncherTest {
Path tempDir;
ContainerLauncher launcher;
- ContainerBuilder containerBuilder;
+ /** Collects remote paths passed to withRemotePath across all copy mocks. */
+ final java.util.List<String> capturedRemotePaths = new java.util.ArrayList<>();
@BeforeEach
void setUp() {
- // Setup config mocks
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);
EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
-
+
when(config.services()).thenReturn(services);
when(services.lambda()).thenReturn(lambda);
when(lambda.dockerNetwork()).thenReturn(Optional.empty());
@@ -63,23 +64,52 @@ class ContainerLauncherTest {
when(docker.logMaxSize()).thenReturn("10m");
when(docker.logMaxFile()).thenReturn("3");
- containerBuilder = new ContainerBuilder(config, dockerHostResolver);
+ ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver);
launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver,
runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager);
when(runtimeApiServerFactory.create()).thenReturn(runtimeApiServer);
when(runtimeApiServer.getPort()).thenReturn(9000);
when(dockerHostResolver.resolve()).thenReturn("127.0.0.1");
-
- ContainerLifecycleManager.ContainerInfo info = new ContainerLifecycleManager.ContainerInfo("c1", Map.of());
- when(lifecycleManager.createAndStart(any())).thenReturn(info);
+
+ when(lifecycleManager.create(any())).thenReturn("container-123");
+ ContainerLifecycleManager.ContainerInfo info =
+ new ContainerLifecycleManager.ContainerInfo("container-123", Map.of());
+ when(lifecycleManager.startCreated(eq("container-123"), any())).thenReturn(info);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
+
+ // Stub the Docker copy chain so copyDirToContainer / copyFileToContainer
+ // don't throw when the mock DockerClient is used. Each invocation
+ // returns a fresh mock that drains the tar InputStream on exec() to
+ // prevent the background PipedOutputStream writer thread from blocking
+ // when the pipe buffer fills.
+ capturedRemotePaths.clear();
+ when(dockerClient.copyArchiveToContainerCmd(any())).thenAnswer(inv -> {
+ CopyArchiveToContainerCmd cmd = mock(CopyArchiveToContainerCmd.class);
+ final java.io.InputStream[] captured = {null};
+ when(cmd.withRemotePath(any())).thenAnswer(pathInv -> {
+ capturedRemotePaths.add(pathInv.getArgument(0));
+ return cmd;
+ });
+ when(cmd.withTarInputStream(any())).thenAnswer(streamInv -> {
+ captured[0] = streamInv.getArgument(0);
+ return cmd;
+ });
+ doAnswer(execInv -> {
+ if (captured[0] != null) {
+ try { captured[0].transferTo(java.io.OutputStream.nullOutputStream()); }
+ catch (Exception ignored) {}
+ }
+ return null;
+ }).when(cmd).exec();
+ return cmd;
+ });
}
@Test
- void launchFunctionCopiesCode() throws Exception {
+ void launchFunction_createsWithoutBindMounts() throws Exception {
Path codePath = Files.createDirectory(tempDir.resolve("code"));
-
+
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("standard-fn");
fn.setRuntime("nodejs20.x");
@@ -89,12 +119,63 @@ class ContainerLauncherTest {
launcher.launch(fn);
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
- verify(lifecycleManager).createAndStart(specCaptor.capture());
+ verify(lifecycleManager).create(specCaptor.capture());
ContainerSpec spec = specCaptor.getValue();
assertTrue(spec.binds().isEmpty(), "Function should NOT have bind mounts");
-
- // Function should attempt to copy code
- verify(lifecycleManager, atLeastOnce()).getDockerClient();
+ }
+
+ @Test
+ void launchFunction_createsBeforeCopyAndStartsAfter() throws Exception {
+ Path codePath = Files.createDirectory(tempDir.resolve("code"));
+
+ LambdaFunction fn = new LambdaFunction();
+ fn.setFunctionName("order-fn");
+ fn.setRuntime("nodejs20.x");
+ fn.setHandler("index.handler");
+ fn.setCodeLocalPath(codePath.toString());
+
+ launcher.launch(fn);
+
+ // Verify ordering: create → getDockerClient → Docker copy (to /var/task) → startCreated
+ InOrder inOrder = inOrder(lifecycleManager, dockerClient);
+ inOrder.verify(lifecycleManager).create(any());
+ inOrder.verify(lifecycleManager).getDockerClient();
+ inOrder.verify(dockerClient).copyArchiveToContainerCmd("container-123");
+ inOrder.verify(lifecycleManager).startCreated(eq("container-123"), any());
+
+ // createAndStart must NOT be called — Lambda uses the split path
+ verify(lifecycleManager, never()).createAndStart(any());
+ }
+
+ @Test
+ void launchProvidedRuntime_copiesBootstrapBeforeStart() throws Exception {
+ Path codePath = Files.createDirectory(tempDir.resolve("provided-code"));
+ Files.writeString(codePath.resolve("bootstrap"), "#!/bin/sh\necho hello");
+
+ LambdaFunction fn = new LambdaFunction();
+ fn.setFunctionName("provided-fn");
+ fn.setRuntime("provided.al2023");
+ fn.setHandler("bootstrap");
+ fn.setCodeLocalPath(codePath.toString());
+
+ launcher.launch(fn);
+
+ // The critical invariant: create must happen before any Docker copy,
+ // and start must happen after. This is the exact regression from #466.
+ InOrder inOrder = inOrder(lifecycleManager, dockerClient);
+ inOrder.verify(lifecycleManager).create(any());
+ inOrder.verify(lifecycleManager).getDockerClient();
+ // Two copies: code to /var/task + bootstrap to /var/runtime
+ inOrder.verify(dockerClient, times(2)).copyArchiveToContainerCmd("container-123");
+ inOrder.verify(lifecycleManager).startCreated(eq("container-123"), any());
+
+ // Verify both /var/task and /var/runtime were targeted
+ assertTrue(capturedRemotePaths.contains("/var/task"),
+ "code should be copied to /var/task");
+ assertTrue(capturedRemotePaths.contains("/var/runtime"),
+ "bootstrap should be copied to /var/runtime");
+
+ verify(lifecycleManager, never()).createAndStart(any());
}
}