feat(lambda): add the ability to name and label lambda containers, code volumes (#2409)

This commit is contained in:
floverfelt authored and GitHub committed 2026-08-19 14:21:47 -04:00
1 parent 3f12428d97
commit f8b07a78ff
10 files changed
+321 -6

No files matched your search

@@ -62,13 +62,86 @@ class ContainerStorageHelperTest {
ContainerStorageHelper.defaultLabels(null));
}
@Test
void prefixedDockerNameSwapsTheBasePrefix() {
assertEquals("acme-my-fn-abc123",
ContainerStorageHelper.prefixedDockerName(config(""), "acme", "my-fn-abc123"));
assertEquals("acme-run-one-my-fn-abc123",
ContainerStorageHelper.prefixedDockerName(config(" run/one "), "acme", "my-fn-abc123"));
// The default prefix through this path matches what dockerName produces.
assertEquals(ContainerStorageHelper.dockerName(config("run-one"), "floci-my-fn-abc123"),
ContainerStorageHelper.prefixedDockerName(config("run-one"), "floci", "my-fn-abc123"));
}
@Test
void extraLabelsAreMergedIntoDefaultLabels() {
// A dotted key — exactly the shape that motivates list-of-entries config over a Map,
// whose env-var naming convention cannot express such keys.
EmulatorConfig config = config("", java.util.List.of(
label("com.example.project", "my-project"),
label("environment", "dev")));
assertEquals(
Map.of("floci", "true", "floci_emulator", "floci-aws",
"com.example.project", "my-project", "environment", "dev"),
ContainerStorageHelper.defaultLabels(config));
}
@Test
void extraLabelsCannotOverrideReservedKeys() {
// The reserved labels drive container/volume discovery and pruning; a user label must
// never be able to break `docker volume prune --filter label=floci=true` cleanup.
EmulatorConfig config = config(" run/one ", java.util.List.of(
label("floci", "false"),
label("floci_emulator", "spoofed"),
label("floci_namespace", "spoofed"),
label("kept", "yes")));
assertEquals(
Map.of("floci", "true", "floci_emulator", "floci-aws",
"floci_namespace", "run-one", "kept", "yes"),
ContainerStorageHelper.defaultLabels(config));
}
@Test
void blankExtraLabelKeysAreIgnored() {
EmulatorConfig config = config("", java.util.List.of(
label(" ", "dropped"),
label(null, "dropped"),
label("kept", null)));
assertEquals(
Map.of("floci", "true", "floci_emulator", "floci-aws", "kept", ""),
ContainerStorageHelper.defaultLabels(config));
}
private static EmulatorConfig.DockerConfig.LabelEntry label(String key, String value) {
return new EmulatorConfig.DockerConfig.LabelEntry() {
@Override
public String key() {
return key;
}
@Override
public String value() {
return value;
}
};
}
private static EmulatorConfig config(String namespace) {
return config(namespace, java.util.List.of());
}
private static EmulatorConfig config(
String namespace, java.util.List<EmulatorConfig.DockerConfig.LabelEntry> extraLabels) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
when(config.docker()).thenReturn(docker);
when(config.storage()).thenReturn(storage);
when(docker.resourceNamespace()).thenReturn(namespace.isBlank() ? Optional.empty() : Optional.of(namespace));
when(docker.extraLabels()).thenReturn(extraLabels);
when(storage.hostPersistentPath()).thenReturn("/tmp/floci");
return config;
}
@@ -77,8 +77,14 @@ class ContainerLauncherCodeVolumeReuseTest {
lifecycleManager = mock(ContainerLifecycleManager.class);
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);
when(config.storage()).thenReturn(storage);
when(storage.persistentPath()).thenReturn(tempDir.toString());
// ensureCodeVolume resolves the configured container-name prefix (unset = "floci").
when(config.services()).thenReturn(services);
when(services.lambda()).thenReturn(lambda);
when(lambda.containerNamePrefix()).thenReturn(Optional.empty());
launcher = new RecordingLauncher(lifecycleManager, config);
}
@@ -0,0 +1,71 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for the configurable Lambda container/code-volume name prefix
* ({@code floci.services.lambda.container-name-prefix}). Plain {@code mock()} rather than the
* Mockito extension, for the same strict-stubbing reason as
* {@link ContainerLauncherVolumeNamingTest}.
*/
class ContainerLauncherNamePrefixTest {
private static final Pattern DOCKER_NAME = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$");
@Test
void fallsBackToFlociWhenPrefixUnsetOrBlank() {
assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config(null)));
assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config(" ")));
}
@Test
void usesConfiguredPrefixWhenDockerSafe() {
assertEquals("acme", ContainerLauncher.resolveContainerNamePrefix(config("acme")));
assertEquals("acme_v1.0", ContainerLauncher.resolveContainerNamePrefix(config(" acme_v1.0 ")));
}
@Test
void fallsBackToFlociWhenPrefixIsNotDockerSafe() {
// Docker names must start alphanumeric and allow only [A-Za-z0-9_.-] after that.
assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config("-leading-dash")));
assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config("has space")));
assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config("has:colon")));
}
@Test
void codeVolumeNameHonorsConfiguredPrefix() {
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("my-fn");
fn.setCodeSha256("abc123def456");
String name = ContainerLauncher.codeVolumeName("acme", fn);
assertTrue(name.startsWith("acme-code-my-fn-"),
"should be <prefix>-code-<functionName>-<hash> shaped, was: " + name);
assertTrue(DOCKER_NAME.matcher(name).matches(),
"must be a docker-volume-safe name, was: " + name);
// The one-arg overload stays on the default prefix (used by pre-existing callers/tests).
assertEquals(ContainerLauncher.codeVolumeName(fn),
ContainerLauncher.codeVolumeName("floci", fn));
}
private static EmulatorConfig config(String prefix) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.lambda()).thenReturn(lambda);
when(lambda.containerNamePrefix()).thenReturn(Optional.ofNullable(prefix));
return config;
}
}