mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): keep MicroVM version update timestamps independent (#9021)
Each MicroVM image version now keeps its own updatedAt instead of reporting the parent image's. Building another version leaves older versions' timestamps alone, and a status update advances only the version it changes. A stored version without the field reports its own createdAt, including after a restart, and reading it does not rewrite storage. Closes #8616
This commit is contained in:
1 parent
a283c7a49b
commit
c1e7b240ea
5 files changed
+147
-1
No files matched your search
+90
@@ -0,0 +1,90 @@
|
||||
package io.floci.aws.services.lambdamicrovms;
|
||||
|
||||
import io.floci.aws.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.config.JsonPathConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class LambdaMicrovmImageVersionTimestampIntegrationTest {
|
||||
|
||||
private static final String IMAGES = "/2025-09-09/microvm-images";
|
||||
|
||||
private static final JsonPathConfig EXACT_NUMBERS =
|
||||
new JsonPathConfig(JsonPathConfig.NumberReturnType.BIG_DECIMAL);
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void getPreservesVersionTimestampWhenParentBuildsAnotherVersion() {
|
||||
String image = createImage();
|
||||
BigDecimal original = versionTimestamp(image, "1.0");
|
||||
updateImage(image);
|
||||
|
||||
assertEquals(original, versionTimestamp(image, "1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPreservesVersionTimestampWhenParentBuildsAnotherVersion() {
|
||||
String image = createImage();
|
||||
BigDecimal original = versionTimestamp(image, "1.0");
|
||||
updateImage(image);
|
||||
|
||||
BigDecimal listed = given().when().get(IMAGES + "/" + image + "/versions")
|
||||
.then().statusCode(200).extract().jsonPath(EXACT_NUMBERS).getObject("items[0].updatedAt", BigDecimal.class);
|
||||
assertEquals(original, listed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void statusChangeUpdatesOnlyTheTargetVersionTimestamp() {
|
||||
String image = createImage();
|
||||
updateImage(image);
|
||||
BigDecimal original = versionTimestamp(image, "1.0");
|
||||
BigDecimal other = versionTimestamp(image, "2.0");
|
||||
BigDecimal parent = given().when().get(IMAGES + "/" + image).then().statusCode(200)
|
||||
.extract().jsonPath(EXACT_NUMBERS).getObject("updatedAt", BigDecimal.class);
|
||||
|
||||
given().contentType("application/json").body("{\"status\":\"INACTIVE\"}")
|
||||
.when().patch(IMAGES + "/" + image + "/versions/1.0").then().statusCode(200);
|
||||
|
||||
assertTrue(versionTimestamp(image, "1.0").compareTo(original) > 0,
|
||||
"Changing a version's status must advance its own updatedAt");
|
||||
assertEquals(other, versionTimestamp(image, "2.0"));
|
||||
assertEquals(parent, given().when().get(IMAGES + "/" + image).then().statusCode(200)
|
||||
.extract().jsonPath(EXACT_NUMBERS).getObject("updatedAt", BigDecimal.class));
|
||||
}
|
||||
|
||||
private String createImage() {
|
||||
String image = "version-time-" + UUID.randomUUID();
|
||||
given().contentType("application/json")
|
||||
.body("{\"name\":\"" + image + "\"," + fields("original") + "}")
|
||||
.when().post(IMAGES).then().statusCode(201);
|
||||
cleanup.register(() -> given().when().delete(IMAGES + "/" + image).then().statusCode(200));
|
||||
return image;
|
||||
}
|
||||
|
||||
private void updateImage(String image) {
|
||||
given().contentType("application/json").body("{" + fields("replacement") + "}")
|
||||
.when().put(IMAGES + "/" + image).then().statusCode(200);
|
||||
}
|
||||
|
||||
private BigDecimal versionTimestamp(String image, String version) {
|
||||
return given().when().get(IMAGES + "/" + image + "/versions/" + version)
|
||||
.then().statusCode(200).extract().jsonPath(EXACT_NUMBERS).getObject("updatedAt", BigDecimal.class);
|
||||
}
|
||||
|
||||
private static String fields(String label) {
|
||||
return "\"baseImageArn\":\"arn:aws:lambda:us-east-1:aws:microvm-image:" + label + "\","
|
||||
+ "\"buildRoleArn\":\"arn:aws:iam::000000000000:role/build\","
|
||||
+ "\"codeArtifact\":{\"uri\":\"s3://artifacts/" + label + ".zip\"}";
|
||||
}
|
||||
}
|
||||
+51
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.PersistentStorage;
|
||||
@@ -15,6 +16,7 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -22,6 +24,7 @@ import java.util.Map;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -127,6 +130,54 @@ class LambdaMicrovmsServicePersistenceTest {
|
||||
assertEquals(beforeRead, Files.readString(file));
|
||||
}
|
||||
|
||||
@Test
|
||||
void versionUpdateTimeSurvivesReloadWithoutChangingOtherVersionsOrTheParent() {
|
||||
createAndUpdateVersionedImage();
|
||||
LambdaMicrovmsService first = process();
|
||||
Instant otherVersionTime = first.getVersion(REGION, "versioned-image", "2.0").updatedAt;
|
||||
Instant parentTime = first.getImage(REGION, "versioned-image").updatedAt;
|
||||
Instant changed = first.updateVersionStatus(REGION, "versioned-image", "1.0", "INACTIVE").updatedAt;
|
||||
|
||||
LambdaMicrovmsService reloaded = process();
|
||||
assertNotNull(changed);
|
||||
assertEquals(changed, reloaded.getVersion(REGION, "versioned-image", "1.0").updatedAt);
|
||||
assertEquals(otherVersionTime, reloaded.getVersion(REGION, "versioned-image", "2.0").updatedAt);
|
||||
assertEquals(parentTime, reloaded.getImage(REGION, "versioned-image").updatedAt);
|
||||
assertThrows(AwsException.class,
|
||||
() -> reloaded.updateVersionStatus(REGION, "versioned-image", "1.0", "INVALID"));
|
||||
assertEquals(changed, process().getVersion(REGION, "versioned-image", "1.0").updatedAt);
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyVersionTimeStaysAtCreationAfterParentUpdateAndReload() throws IOException {
|
||||
createAndUpdateVersionedImage();
|
||||
Path file = storageDir.resolve("lambda-microvm-images.json");
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode stored = mapper.readTree(file.toFile());
|
||||
for (JsonNode versions : stored.findValues("versions")) {
|
||||
for (JsonNode version : versions) {
|
||||
((ObjectNode) version).remove("updatedAt");
|
||||
}
|
||||
}
|
||||
mapper.writeValue(file.toFile(), stored);
|
||||
String beforeRead = Files.readString(file);
|
||||
JsonNode initial = versionResponse(process(), "1.0");
|
||||
assertEquals(initial.path("createdAt"), initial.path("updatedAt"));
|
||||
assertEquals(beforeRead, Files.readString(file));
|
||||
|
||||
LambdaMicrovmsService reloaded = process();
|
||||
reloaded.updateImage(REGION, "versioned-image",
|
||||
"arn:aws:lambda:us-east-1:aws:microvm-image:replacement",
|
||||
"arn:aws:iam::000000000000:role/replacement", "s3://artifacts/newest.zip", null,
|
||||
"3.0", LambdaMicrovmsService.RuntimeSettings.defaults());
|
||||
String afterUpdate = Files.readString(file);
|
||||
LambdaMicrovmsService restarted = process();
|
||||
assertNull(restarted.getVersion(REGION, "versioned-image", "1.0").updatedAt);
|
||||
assertEquals(initial.path("createdAt"), versionResponse(restarted, "1.0").path("updatedAt"));
|
||||
assertEquals(initial.path("createdAt"), versionResponse(process(), "1.0").path("updatedAt"));
|
||||
assertEquals(afterUpdate, Files.readString(file));
|
||||
}
|
||||
|
||||
private void createAndUpdateVersionedImage() {
|
||||
LambdaMicrovmsService service = process();
|
||||
service.createImage(REGION, ACCOUNT_ID, "versioned-image",
|
||||
|
||||
Reference in new issue
Block a user