feat(lambda): support custom Docker flags (#3751)

This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-17 10:57:42 -04:00
1 parent 9740b32b51
commit 75d1c2ae62
11 files changed
+307 -3

No files matched your search

@@ -400,6 +400,55 @@ class ContainerLauncherTest {
"small code should be copied directly into /var/task");
}
@Test
void launchFunction_appliesConfiguredDockerFlagsToRealContainer() throws Exception {
when(config.services().lambda().dockerFlags()).thenReturn(Optional.of(
"--env NODE_EXTRA_CA_CERTS=/opt/certs/root.pem "
+ "--volume /tmp/certs:/opt/certs:ro --add-host api.local:host-gateway "
+ "--dns 1.1.1.1 --label purpose=debug --network lambda-net "
+ "--user 1000:1000 --privileged --publish 127.0.0.1:5050:5050"));
Path codePath = Files.createDirectory(tempDir.resolve("flags-code"));
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("flags-fn");
fn.setRuntime("nodejs20.x");
fn.setHandler("index.handler");
fn.setCodeLocalPath(codePath.toString());
launcher.launch(fn);
ContainerSpec spec = captureRealContainerSpec();
assertTrue(spec.env().contains("NODE_EXTRA_CA_CERTS=/opt/certs/root.pem"));
assertEquals("lambda-net", spec.networkMode());
assertEquals("1000:1000", spec.user());
assertTrue(spec.privileged());
assertEquals(Map.of(5050, 5050), spec.portBindings());
assertEquals(List.of(5050), spec.loopbackPortBindings());
assertTrue(spec.extraHosts().contains("api.local:host-gateway"));
assertTrue(spec.dnsServers().contains("1.1.1.1"));
assertEquals("debug", spec.labels().get("purpose"));
assertEquals("/opt/certs", spec.binds().getFirst().getVolume().getPath());
assertEquals("/tmp/certs", spec.binds().getFirst().getPath());
}
@Test
void launchFunction_rejectsPublishedPortBoundToUnsupportedHostAddress() throws Exception {
when(config.services().lambda().dockerFlags()).thenReturn(Optional.of(
"--publish 192.0.2.10:5050:5050"));
Path codePath = Files.createDirectory(tempDir.resolve("unsupported-publish-address-code"));
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("unsupported-publish-address-fn");
fn.setRuntime("nodejs20.x");
fn.setHandler("index.handler");
fn.setCodeLocalPath(codePath.toString());
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> launcher.launch(fn));
assertTrue(exception.getMessage().contains("127.0.0.1"));
}
@Test
void launchFunction_mountsConfiguredFileSystemVolume() throws Exception {
Path codePath = Files.createDirectory(tempDir.resolve("efs-code"));
@@ -0,0 +1,59 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LambdaDockerFlagsTest {
@Test
void parsesSupportedFlagsAndQuotedValues() {
LambdaDockerFlags flags = LambdaDockerFlags.parse(
"--env \"NODE_EXTRA_CA_CERTS=/opt/certs/root ca.pem\" "
+ "--volume /tmp/certs:/opt/certs:ro "
+ "--publish 127.0.0.1:5050:5050 --add-host api.local:host-gateway "
+ "--dns 1.1.1.1 --label purpose=debug --network dev-net "
+ "--user 1000:1000 --privileged --platform linux/amd64");
assertEquals(List.of("NODE_EXTRA_CA_CERTS=/opt/certs/root ca.pem"), flags.environment());
assertEquals(List.of("/tmp/certs:/opt/certs:ro"), flags.volumes());
assertEquals(List.of("127.0.0.1:5050:5050"), flags.publishedPorts());
assertEquals(List.of("api.local:host-gateway"), flags.extraHosts());
assertEquals(List.of("1.1.1.1"), flags.dnsServers());
assertEquals(Map.of("purpose", "debug"), flags.labels());
assertEquals("dev-net", flags.network());
assertEquals("1000:1000", flags.user());
assertTrue(flags.privileged());
assertEquals("linux/amd64", flags.platform());
}
@Test
void supportsShortAliases() {
LambdaDockerFlags flags = LambdaDockerFlags.parse(
"-e KEY=value -v host:/container -p 8080:80 -u 1000");
assertEquals(List.of("KEY=value"), flags.environment());
assertEquals(List.of("host:/container"), flags.volumes());
assertEquals(List.of("8080:80"), flags.publishedPorts());
assertEquals("1000", flags.user());
}
@Test
void rejectsUnsupportedFlags() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> LambdaDockerFlags.parse("--cap-add NET_ADMIN"));
assertTrue(exception.getMessage().contains("--cap-add"));
}
@Test
void rejectsMissingFlagValues() {
assertThrows(IllegalArgumentException.class, () -> LambdaDockerFlags.parse("--env"));
assertThrows(IllegalArgumentException.class, () -> LambdaDockerFlags.parse("--dns --privileged"));
}
}