fix(cloudfront): separate DEVELOPMENT and LIVE function stages (#2622)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-08-29 14:33:00 -04:00
1 parent 67f75c7e1d
commit 0dfec3cf3c
5 files changed
+251 -19

No files matched your search

@@ -6,6 +6,8 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
@@ -105,6 +107,87 @@ class CloudFrontManagementProtocolTest {
equalTo("InvalidArgument")));
}
@Test
void describeAndPublishFunctionPreserveBothStages() {
String name = "describe-test-" + UUID.randomUUID();
String createBody = """
<CreateFunctionRequest xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
<Name>%s</Name>
<FunctionConfig>
<Comment>routing function</Comment>
<Runtime>cloudfront-js-2.0</Runtime>
</FunctionConfig>
<FunctionCode>function handler(event) { return event.request; }</FunctionCode>
</CreateFunctionRequest>
""".formatted(name);
String developmentEtag = given()
.contentType("application/xml")
.body(createBody)
.when()
.post(API + "function")
.then()
.statusCode(201)
.extract().header("ETag");
given()
.queryParam("Stage", "DEVELOPMENT")
.when()
.get(API + "function/" + name + "/describe")
.then()
.statusCode(200)
.header("ETag", equalTo(developmentEtag))
.body(hasXPath("//*[local-name()='Stage']/text()", equalTo("DEVELOPMENT")));
given()
.header("If-Match", developmentEtag)
.when()
.post(API + "function/" + name + "/publish")
.then()
.statusCode(200)
.body(hasXPath("//*[local-name()='Stage']/text()", equalTo("LIVE")));
given()
.queryParam("Stage", "DEVELOPMENT")
.when()
.get(API + "function/" + name + "/describe")
.then()
.statusCode(200)
.header("ETag", equalTo(developmentEtag));
given()
.queryParam("Stage", "LIVE")
.when()
.get(API + "function/" + name + "/describe")
.then()
.statusCode(200)
.body(hasXPath("//*[local-name()='Stage']/text()", equalTo("LIVE")));
given()
.queryParam("Stage", "TESTING")
.when()
.get(API + "function/" + name + "/describe")
.then()
.statusCode(400)
.body(hasXPath("//*[local-name()='Code']/text()", equalTo("InvalidArgument")));
given()
.queryParam("Stage", "DEVELOPMENT")
.when()
.get(API + "function/" + name)
.then()
.statusCode(200)
.contentType("application/octet-stream")
.body(equalTo("function handler(event) { return event.request; }"));
given()
.header("If-Match", developmentEtag)
.when()
.delete(API + "function/" + name)
.then()
.statusCode(204);
}
@Test
void createOriginAccessControlDoesNotDefaultRequiredConfiguration() {
String body = """
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.cloudfront.model.CacheBehavior;
import io.github.hectorvent.floci.services.cloudfront.model.CloudFrontFunction;
import io.github.hectorvent.floci.services.cloudfront.model.CloudFrontOriginAccessIdentity;
import io.github.hectorvent.floci.services.cloudfront.model.DefaultCacheBehavior;
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
@@ -32,6 +33,7 @@ import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -133,6 +135,90 @@ class CloudFrontServiceTest {
return new CloudFrontService(storageFactory, config);
}
@Test
void publishFunctionCopiesDevelopmentToLive() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
CloudFrontFunction function = new CloudFrontFunction();
function.setName("routing");
function.setFunctionCode("function handler(event) { return event.request; }");
function.setRuntime("cloudfront-js-2.0");
function.setComment("routing function");
CloudFrontFunction development = service.createFunction(function);
CloudFrontFunction live = service.publishFunction(function.getName(), development.getEtag());
CloudFrontFunction describedDevelopment = service.describeFunction(function.getName(), "DEVELOPMENT");
CloudFrontFunction describedLive = service.describeFunction(function.getName(), "LIVE");
assertEquals("DEVELOPMENT", describedDevelopment.getStage());
assertEquals("LIVE", describedLive.getStage());
assertEquals(development.getFunctionCode(), describedLive.getFunctionCode());
assertEquals(development.getEtag(), describedDevelopment.getEtag());
assertNotEquals(development.getEtag(), live.getEtag());
}
@Test
void listFunctionsWithoutStageReturnsDevelopmentAndLive() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
CloudFrontFunction function = new CloudFrontFunction();
function.setName("routing");
CloudFrontFunction development = service.createFunction(function);
service.publishFunction(function.getName(), development.getEtag());
assertEquals(List.of("DEVELOPMENT", "LIVE"), service.listFunctions(null, null, LIST_MAX_ITEMS)
.stream()
.map(CloudFrontFunction::getStage)
.toList());
}
@Test
void functionStageLookupRejectsUnsupportedValues() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
CloudFrontFunction function = new CloudFrontFunction();
function.setName("routing");
service.createFunction(function);
AwsException error = assertThrows(AwsException.class,
() -> service.describeFunction(function.getName(), "TESTING"));
assertEquals("InvalidArgument", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@Test
void legacyLiveFunctionIsNeverReturnedAsDevelopment() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
CloudFrontFunction function = new CloudFrontFunction();
function.setName("legacy-routing");
CloudFrontFunction legacyLive = service.createFunction(function);
legacyLive.setStage("LIVE");
legacyLive.setStatus("DEPLOYED");
AwsException developmentError = assertThrows(AwsException.class,
() -> service.describeFunction(function.getName(), "DEVELOPMENT"));
assertEquals("NoSuchFunctionExists", developmentError.getErrorCode());
assertEquals(legacyLive, service.describeFunction(function.getName(), "LIVE"));
AwsException publishError = assertThrows(AwsException.class,
() -> service.publishFunction(function.getName(), legacyLive.getEtag()));
assertEquals("NoSuchFunctionExists", publishError.getErrorCode());
}
@Test
void legacyLiveFunctionCanBeDeleted() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
CloudFrontFunction function = new CloudFrontFunction();
function.setName("legacy-routing");
CloudFrontFunction legacyLive = service.createFunction(function);
legacyLive.setStage("LIVE");
legacyLive.setStatus("DEPLOYED");
service.deleteFunction(function.getName(), legacyLive.getEtag());
AwsException error = assertThrows(AwsException.class,
() -> service.describeFunction(function.getName(), "LIVE"));
assertEquals("NoSuchFunctionExists", error.getErrorCode());
}
@Test
void createDistributionUsesDefaultDomainSuffix() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");