mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 17:58:05 -05:00
feat(cloudformation): report a stubbed unsupported resource type instead of hiding it (#2971)
An unsupported resource type was stubbed at debug, so the stack reached CREATE_COMPLETE and a green stack did not prove its resources existed. The stub is now reported at warn, naming the type, and the resource carries a status reason saying nothing was created, which DescribeStackEvents returns. A new floci.services.cloudformation.allow-stub-unsupported-resource-types setting (default true) turns the stub into a stack failure for suites that need a green stack to mean every resource exists. An unexpanded SAM resource type warns on its own, and the delete path names the physical id it does not remove. Closes #2967
This commit is contained in:
1 parent
9e2c03158d
commit
5dc0808bda
10 files changed
+419
-5
No files matched your search
+63
@@ -0,0 +1,63 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.cloudformation.CloudFormationLambdaMissingS3CodeIntegrationTest.CFN_AUTH;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.CloudFormationLambdaMissingS3CodeIntegrationTest.createStack;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* The hard gate a pipeline can set: with
|
||||
* {@code floci.services.cloudformation.allow-stub-unsupported-resource-types} off, a resource type
|
||||
* Floci has no provisioner for fails instead of being stubbed, and the stack rolls back rather than
|
||||
* reporting a green deployment of something that was never created.
|
||||
*
|
||||
* <p>A separate top-level class rather than a nested one, because {@code @TestProfile} applies per
|
||||
* test class and Surefire discovers test classes by file name: a nested class carrying the profile
|
||||
* compiles and passes when targeted directly, but is silently skipped in a full {@code mvn test}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(CloudFormationUnsupportedResourceTypeStrictIntegrationTest.StrictProfile.class)
|
||||
class CloudFormationUnsupportedResourceTypeStrictIntegrationTest {
|
||||
|
||||
public static final class StrictProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.cloudformation.allow-stub-unsupported-resource-types", "false");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void strictProfileRollsTheStackBack() {
|
||||
String stackName = "cfn-unsupported-strict-" + Long.toString(System.nanoTime(), 36);
|
||||
|
||||
createStack(stackName, """
|
||||
{
|
||||
"Resources": {
|
||||
"MyThing": {
|
||||
"Type": "AWS::Fake::Thing",
|
||||
"Properties": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"))
|
||||
.body(containsString("Resource type AWS::Fake::Thing is not supported by Floci."));
|
||||
}
|
||||
}
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
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.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A resource type Floci has no provisioner for is stubbed: a synthetic physical id, an
|
||||
* {@code arn:aws:stub:::} ARN attribute and {@code CREATE_COMPLETE}. Stubbing it is the
|
||||
* deliberate behaviour, but doing it silently is not: a green stack that created nothing reads
|
||||
* exactly like a green stack that created everything.
|
||||
*
|
||||
* <p>These tests pin the two things that make the stub observable without changing what it
|
||||
* builds: the resource status reason, which reaches DescribeStackEvents through the reason
|
||||
* CloudFormationService already copies onto the event, and the warn-level log line.
|
||||
*/
|
||||
class CloudFormationUnsupportedResourceTypeTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String STACK_NAME = "TestStack";
|
||||
private static final String LOGICAL_ID = "MyThing";
|
||||
private static final String UNSUPPORTED_TYPE = "AWS::Fake::Thing";
|
||||
|
||||
private static final String STUB_REASON =
|
||||
"Resource type AWS::Fake::Thing is not supported by Floci. "
|
||||
+ "It was stubbed and nothing was created for it.";
|
||||
private static final String STRICT_REASON =
|
||||
"Resource type AWS::Fake::Thing is not supported by Floci.";
|
||||
private static final String STUB_WARNING =
|
||||
"Stubbing unsupported resource type AWS::Fake::Thing (MyThing): nothing is created for it. "
|
||||
+ "Set floci.services.cloudformation.allow-stub-unsupported-resource-types=false "
|
||||
+ "to fail the stack instead.";
|
||||
private static final String DELETE_WARNING =
|
||||
"No delete implemented for resource type AWS::Fake::Thing: "
|
||||
+ "MyThing-11a79dff is not removed here.";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private CloudFormationResourceProvisioner provisioner;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
provisioner = CfnProvisionerFixture.builder().objectMapper(mapper).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void stubbedResourceCarriesTheUnsupportedTypeReason() {
|
||||
List<LogRecord> logged = new CopyOnWriteArrayList<>();
|
||||
StackResource resource = whileCapturingProvisionerLogs(logged, this::provisionUnsupported);
|
||||
|
||||
assertEquals("CREATE_COMPLETE", resource.getStatus());
|
||||
assertEquals(STUB_REASON, resource.getStatusReason());
|
||||
assertTrue(logged.stream().anyMatch(r ->
|
||||
r.getLevel().intValue() >= Level.WARNING.intValue()
|
||||
&& STUB_WARNING.equals(formatted(r))),
|
||||
"expected the stub to be reported at warn, got: " + rendered(logged));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stubbedResourceStillCarriesTheStubArn() {
|
||||
// The stub's shape is a persisted contract: a stack created by an older build carries the
|
||||
// synthetic physical id, and the next update has to recognise it.
|
||||
StackResource resource = provisionUnsupported();
|
||||
|
||||
assertEquals("arn:aws:stub:::" + LOGICAL_ID, resource.getAttributes().get("Arn"));
|
||||
assertTrue(resource.getPhysicalId().startsWith(LOGICAL_ID + "-"),
|
||||
"expected the synthetic physical id shape, got: " + resource.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedTypeFailsTheResourceWhenTheStubIsDisallowed() {
|
||||
StackResource resource = provisionerWithStubAllowed(false)
|
||||
.provision(LOGICAL_ID, UNSUPPORTED_TYPE, props(), engine(),
|
||||
REGION, ACCOUNT_ID, STACK_NAME);
|
||||
|
||||
assertEquals("CREATE_FAILED", resource.getStatus());
|
||||
assertEquals(STRICT_REASON, resource.getStatusReason());
|
||||
// The failure precedes the synthetic physical id, so Cloud Control's own "no physical id"
|
||||
// branch reports the same message instead of a success over a resource that is not there.
|
||||
assertNull(resource.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void absentConfigMeansTheDocumentedDefault() {
|
||||
// A provisioner built in a unit test carries no config, and the knob defaults to true, so
|
||||
// absent config has to mean the lenient behaviour. Reading it the other way round makes
|
||||
// every config-less provisioner strict.
|
||||
StackResource resource = provisionUnsupported();
|
||||
|
||||
assertEquals("CREATE_COMPLETE", resource.getStatus());
|
||||
assertEquals(STUB_REASON, resource.getStatusReason());
|
||||
assertNotNull(resource.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOfUnsupportedTypeDoesNotThrow() {
|
||||
// The same arm catches a type no provisioner ever created and a type the create switch does
|
||||
// provision and this switch does not remove, so the line says what it leaves behind rather
|
||||
// than claiming nothing was created.
|
||||
List<LogRecord> logged = new CopyOnWriteArrayList<>();
|
||||
|
||||
assertDoesNotThrow(() -> whileCapturingProvisionerLogs(logged, () -> {
|
||||
provisioner.delete(UNSUPPORTED_TYPE, LOGICAL_ID + "-11a79dff", REGION);
|
||||
return null;
|
||||
}));
|
||||
|
||||
assertTrue(logged.stream().anyMatch(r ->
|
||||
r.getLevel().intValue() >= Level.WARNING.intValue()
|
||||
&& DELETE_WARNING.equals(formatted(r))),
|
||||
"expected the skipped delete to be reported at warn, got: " + rendered(logged));
|
||||
}
|
||||
|
||||
private CloudFormationResourceProvisioner provisionerWithStubAllowed(boolean allowed) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.CloudFormationServiceConfig cloudformation =
|
||||
mock(EmulatorConfig.CloudFormationServiceConfig.class);
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.cloudformation()).thenReturn(cloudformation);
|
||||
when(cloudformation.allowStubUnsupportedResourceTypes()).thenReturn(allowed);
|
||||
return CfnProvisionerFixture.builder().objectMapper(mapper).config(config).build();
|
||||
}
|
||||
|
||||
private StackResource provisionUnsupported() {
|
||||
return provisioner.provision(LOGICAL_ID, UNSUPPORTED_TYPE, props(), engine(),
|
||||
REGION, ACCOUNT_ID, STACK_NAME);
|
||||
}
|
||||
|
||||
/** Runs {@code action} with a handler on the provisioner's logger, collecting every record. */
|
||||
private <T> T whileCapturingProvisionerLogs(List<LogRecord> collected,
|
||||
java.util.function.Supplier<T> action) {
|
||||
java.util.logging.Logger logger =
|
||||
java.util.logging.Logger.getLogger(CloudFormationResourceProvisioner.class.getName());
|
||||
Level original = logger.getLevel();
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
collected.add(logRecord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
logger.addHandler(handler);
|
||||
logger.setLevel(Level.ALL);
|
||||
try {
|
||||
return action.get();
|
||||
} finally {
|
||||
logger.setLevel(original);
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The record's text. Which logging backend is in play decides whether the parameters are
|
||||
* already substituted or still carried alongside the pattern, so substitute them here when
|
||||
* they are still there.
|
||||
*/
|
||||
private static String formatted(LogRecord record) {
|
||||
Object[] parameters = record.getParameters();
|
||||
return parameters == null || parameters.length == 0
|
||||
? record.getMessage()
|
||||
: MessageFormat.format(record.getMessage(), parameters);
|
||||
}
|
||||
|
||||
private static List<String> rendered(List<LogRecord> records) {
|
||||
return records.stream().map(r -> r.getLevel() + " " + formatted(r)).toList();
|
||||
}
|
||||
|
||||
private JsonNode props() {
|
||||
return mapper.createObjectNode();
|
||||
}
|
||||
|
||||
private CloudFormationTemplateEngine engine() {
|
||||
return new CloudFormationTemplateEngine(
|
||||
ACCOUNT_ID, REGION, STACK_NAME, "stack/id",
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), mapper,
|
||||
(Function<String, String>) name -> null);
|
||||
}
|
||||
}
|
||||
+66
@@ -1944,4 +1944,70 @@ class SamTransformProcessorTest {
|
||||
+ "'Definition' or 'DefinitionUri' property and not both.",
|
||||
exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unexpandedSamResourceTypeWarns() throws Exception {
|
||||
// A SAM type this processor cannot expand is left in the template, where the provisioner
|
||||
// stubs it. Reported at debug that is the first of two stacked silences on one path, so
|
||||
// the type reaches the stub arm with nothing said about the expansion that never happened.
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Resources": {
|
||||
"MyApp": {
|
||||
"Type": "AWS::Serverless::Application",
|
||||
"Properties": {
|
||||
"Location": "s3://example-templates/app.yaml"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
java.util.List<java.util.logging.LogRecord> logged = new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
java.util.logging.Logger logger =
|
||||
java.util.logging.Logger.getLogger(SamTransformProcessor.class.getName());
|
||||
java.util.logging.Level original = logger.getLevel();
|
||||
java.util.logging.Handler handler = new java.util.logging.Handler() {
|
||||
@Override
|
||||
public void publish(java.util.logging.LogRecord logRecord) {
|
||||
logged.add(logRecord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
logger.addHandler(handler);
|
||||
logger.setLevel(java.util.logging.Level.ALL);
|
||||
try {
|
||||
processor.expandSamTemplate(template);
|
||||
} finally {
|
||||
logger.setLevel(original);
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
|
||||
String expected = "Unsupported SAM resource type AWS::Serverless::Application (MyApp): "
|
||||
+ "left in the template for the CloudFormation provisioner.";
|
||||
assertTrue(logged.stream().anyMatch(r ->
|
||||
r.getLevel().intValue() >= java.util.logging.Level.WARNING.intValue()
|
||||
&& expected.equals(text(r))),
|
||||
"expected the unexpanded SAM type to be reported at warn, got: "
|
||||
+ logged.stream().map(r -> r.getLevel() + " " + text(r)).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* The record's text. Which logging backend is in play decides whether the parameters are
|
||||
* already substituted or still carried alongside the pattern.
|
||||
*/
|
||||
private static String text(java.util.logging.LogRecord record) {
|
||||
Object[] parameters = record.getParameters();
|
||||
return parameters == null || parameters.length == 0
|
||||
? record.getMessage()
|
||||
: java.text.MessageFormat.format(record.getMessage(), parameters);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user