From adc4b3627d6cc79ee52c11d983697fff201862f3 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Tue, 1 Sep 2026 17:47:23 -0500 Subject: [PATCH] test(cloudformation): wire fixture provisioners from the named services (#2760) * test(cloudformation): wire fixture provisioners from the named services CfnProvisionerFixture defaulted to an empty registry, which made every fixture-based test quietly fragile across a migration. A test that named a service and provisioned one of its types kept passing when that type moved into a provisioner: the empty registry sent it to the dispatcher's stub arm, which reports CREATE_COMPLETE with a synthetic physical id, so the assertions ran against a resource nothing had provisioned. The log group slice hit this and needed an explicit registration line to stay honest. build() now infers the provisioners CDI would discover for the services a test named, so a test follows its type into a provisioner without being edited. Twenty-eight of thirty provisioners take exactly one service, which is what makes the inference unambiguous; one service can back several, as Ec2Service does. provisioners() still takes an explicit set and now replaces the inferred one, for a provisioner the fixture cannot build itself. The log group test reverts to naming its service. Verified the inference is load-bearing rather than incidental: neutering it puts that test back on the stub arm and fails it. * test(cloudformation): resolve fixture wiring reachability instead of grepping it The six service fields added for Ec2FlowLog, LambdaMicrovms, Config, Organizations, Sqs and WafV2 had no builder setter, so they were permanently null, their construction arms were unreachable, and effective coverage stayed at 23 provisioners. The completeness gate could not catch it: it matched "new (\w+CfnProvisioner)\(" against the fixture own source text, which scores dead code as wired. It detected a deleted source line, not an unreachable one. Add the six missing setters, and replace the textual gate with a behavioural one that drives every public single-argument Builder setter, builds the registry, and reads back the provisioners that actually resolved. A field that no setter assigns can no longer pass it. --- .../cloudformation/CfnProvisionerFixture.java | 188 +++++++++++++++++- .../CfnProvisionerFixtureTest.java | 170 ++++++++++++++++ ...CloudFormationLogGroupProvisionerTest.java | 11 +- 3 files changed, 360 insertions(+), 9 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java index 40ff2b54e7..d3e397f774 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java @@ -8,6 +8,41 @@ import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service; import io.github.hectorvent.floci.services.autoscaling.AutoScalingService; import io.github.hectorvent.floci.services.batch.BatchService; import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnDynamicReferences; +import io.github.hectorvent.floci.services.cloudformation.provisioners.ConfigCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2FlowLogCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaMicrovmsCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.OrganizationsCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.SqsCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.WafV2CfnProvisioner; +import io.github.hectorvent.floci.services.configservice.AwsConfigService; +import io.github.hectorvent.floci.services.ec2.FlowLogService; +import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService; +import io.github.hectorvent.floci.services.organizations.OrganizationsService; +import io.github.hectorvent.floci.services.sqs.SqsService; +import io.github.hectorvent.floci.services.wafv2.WafV2Service; +import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayAccountCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingLifecycleHookCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.CdkMetadataCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudWatchCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2LaunchTemplateCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2NetworkAclCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2SecurityGroupRuleCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2VpcCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2VpcEndpointCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2VpcGatewayAttachmentCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.EcrCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.EcsCapacityCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.FirehoseCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.IamRoleCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.KinesisCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.KmsCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaAddressingCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaVersionAliasCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.LogsCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.PipesCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.S3CfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.SnsCfnProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.SsmCfnProvisioner; import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnResourceProvisioner; import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry; import io.github.hectorvent.floci.services.cloudfront.CloudFrontService; @@ -36,6 +71,7 @@ import io.github.hectorvent.floci.services.sns.SnsService; import io.github.hectorvent.floci.services.ssm.SsmService; import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -96,21 +132,128 @@ final class CfnProvisionerFixture { private FirehoseService firehoseService; private DocDbService docDbService; private CloudFrontService cloudFrontService; + // Services that back a provisioner without being a constructor argument of the + // dispatcher. They exist only so inferredProvisioners() can wire their provisioner. + private FlowLogService flowLogService; + private LambdaMicrovmsService lambdaMicrovmsService; + private AwsConfigService awsConfigService; + private OrganizationsService organizationsService; + private SqsService sqsService; + private WafV2Service wafV2Service; private CloudFormationResourceRegistry resourceRegistry; + private boolean registryChosenByTest; private CfnDynamicReferences dynamicReferences; private EmulatorConfig config; private Builder() { this.objectMapper = new ObjectMapper(); - this.resourceRegistry = new CloudFormationResourceRegistry(List.of()); } - /** Registers extracted provisioners, the way production CDI discovery would. */ + /** + * Registers an explicit provisioner set, replacing the ones inferred from the named + * services. Needed only for a provisioner this fixture cannot build itself, such as one + * taking collaborators beyond a single service. + */ public Builder provisioners(CfnResourceProvisioner... provisioners) { this.resourceRegistry = new CloudFormationResourceRegistry(Arrays.asList(provisioners)); + this.registryChosenByTest = true; return this; } + /** + * The provisioners inferred from the services this test named, not everything CDI would + * discover: a service the test did not name contributes nothing here. + * + *

Inferring these is what keeps a test honest across a migration. A test that names a + * service and provisions one of its types used to keep passing when that type moved to a + * provisioner: the empty registry sent it to the dispatcher's stub arm, which reports + * CREATE_COMPLETE with a synthetic id, so the assertions ran against nothing. Wiring the + * provisioner from the service means the test follows the type instead. + * + *

Covers every provisioner that takes a single service. The two that take more + * ({@code CodeBuildCfnProvisioner}, {@code CodePipelineCfnProvisioner}) must be passed to + * {@link Builder#provisioners} explicitly. {@code CfnProvisionerFixtureTest} fails if a new + * provisioner is neither wired here nor listed as an exemption there, so this cannot fall + * behind silently. + */ + private List inferredProvisioners() { + List discovered = new ArrayList<>(); + discovered.add(new CdkMetadataCfnProvisioner()); + if (s3Service != null) { + discovered.add(new S3CfnProvisioner(s3Service)); + } + if (snsService != null) { + discovered.add(new SnsCfnProvisioner(snsService)); + } + if (ssmService != null) { + discovered.add(new SsmCfnProvisioner(ssmService)); + } + if (kmsService != null) { + discovered.add(new KmsCfnProvisioner(kmsService)); + } + if (ecrService != null) { + discovered.add(new EcrCfnProvisioner(ecrService)); + } + if (pipesService != null) { + discovered.add(new PipesCfnProvisioner(pipesService)); + } + if (firehoseService != null) { + discovered.add(new FirehoseCfnProvisioner(firehoseService)); + } + if (logsService != null) { + discovered.add(new LogsCfnProvisioner(logsService)); + } + if (kinesisService != null) { + discovered.add(new KinesisCfnProvisioner(kinesisService)); + } + if (cloudWatchMetricsService != null) { + discovered.add(new CloudWatchCfnProvisioner(cloudWatchMetricsService)); + } + if (iamService != null) { + discovered.add(new IamRoleCfnProvisioner(iamService)); + } + if (ecsService != null) { + discovered.add(new EcsCapacityCfnProvisioner(ecsService)); + } + if (apiGatewayService != null) { + discovered.add(new ApiGatewayAccountCfnProvisioner(apiGatewayService)); + } + if (autoScalingService != null) { + discovered.add(new AutoScalingLifecycleHookCfnProvisioner(autoScalingService)); + } + if (lambdaService != null) { + discovered.add(new LambdaAddressingCfnProvisioner(lambdaService)); + discovered.add(new LambdaVersionAliasCfnProvisioner(lambdaService)); + } + if (flowLogService != null) { + discovered.add(new Ec2FlowLogCfnProvisioner(flowLogService)); + } + if (lambdaMicrovmsService != null) { + discovered.add(new LambdaMicrovmsCfnProvisioner(lambdaMicrovmsService)); + } + if (awsConfigService != null) { + discovered.add(new ConfigCfnProvisioner(awsConfigService)); + } + if (organizationsService != null) { + discovered.add(new OrganizationsCfnProvisioner(organizationsService)); + } + if (sqsService != null) { + discovered.add(new SqsCfnProvisioner(sqsService)); + } + if (wafV2Service != null) { + discovered.add(new WafV2CfnProvisioner(wafV2Service)); + } + if (ec2Service != null) { + discovered.add(new Ec2VpcCfnProvisioner(ec2Service)); + discovered.add(new Ec2VpcEndpointCfnProvisioner(ec2Service)); + discovered.add(new Ec2VpcGatewayAttachmentCfnProvisioner(ec2Service)); + discovered.add(new Ec2NetworkAclCfnProvisioner(ec2Service)); + discovered.add(new Ec2SecurityGroupRuleCfnProvisioner(ec2Service)); + discovered.add(new Ec2LaunchTemplateCfnProvisioner(ec2Service)); + } + return discovered; + } + public Builder s3(S3Service v) { this.s3Service = v; return this; @@ -271,8 +414,39 @@ final class CfnProvisionerFixture { return this; } + public Builder flowLog(FlowLogService v) { + this.flowLogService = v; + return this; + } + + public Builder lambdaMicrovms(LambdaMicrovmsService v) { + this.lambdaMicrovmsService = v; + return this; + } + + public Builder awsConfig(AwsConfigService v) { + this.awsConfigService = v; + return this; + } + + public Builder organizations(OrganizationsService v) { + this.organizationsService = v; + return this; + } + + public Builder sqs(SqsService v) { + this.sqsService = v; + return this; + } + + public Builder wafV2(WafV2Service v) { + this.wafV2Service = v; + return this; + } + public Builder registry(CloudFormationResourceRegistry v) { this.resourceRegistry = v; + this.registryChosenByTest = true; return this; } @@ -286,7 +460,17 @@ final class CfnProvisionerFixture { return this; } + /** The registry {@link #build()} would use: explicit if the test chose one, else inferred. */ + CloudFormationResourceRegistry buildRegistry() { + return registryChosenByTest + ? resourceRegistry + : new CloudFormationResourceRegistry(inferredProvisioners()); + } + public CloudFormationResourceProvisioner build() { + if (!registryChosenByTest) { + resourceRegistry = buildRegistry(); + } if (dynamicReferences == null) { // Wire it from the services already named, the way CDI does in production, so a // test resolving {{resolve:ssm:...}} or {{resolve:secretsmanager:...}} does not diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java new file mode 100644 index 0000000000..3b9698a056 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java @@ -0,0 +1,170 @@ +package io.github.hectorvent.floci.services.cloudformation; + +import com.fasterxml.jackson.databind.JsonNode; +import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnResourceProvisioner; +import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry; +import io.github.hectorvent.floci.services.cloudformation.provisioners.ProvisionContext; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService; +import io.github.hectorvent.floci.services.ec2.Ec2Service; +import io.github.hectorvent.floci.services.sns.SnsService; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +/** + * The fixture wires provisioners from the services a test names. + * + *

This is what keeps a test honest across a migration. Before it, a test that named a service + * and provisioned one of its types silently kept passing when that type moved into a provisioner: + * the empty registry sent it to the dispatcher's stub arm, which reports CREATE_COMPLETE with a + * synthetic physical id, so every assertion ran against a resource nothing had provisioned. + */ +class CfnProvisionerFixtureTest { + + /** + * Provisioners the fixture cannot build from a single service, so they must be passed to + * {@code provisioners(...)} explicitly. Both take collaborators beyond one service + * ({@code RegionResolver} and an {@code ObjectMapper}), which the Builder does not model. + */ + private static final Set NOT_INFERABLE = + Set.of("CodeBuildCfnProvisioner", "CodePipelineCfnProvisioner"); + + /** + * Builder setters that replace the inferred registry outright rather than naming a service. + * Driving them in the sweep below would discard everything inference produced. + */ + private static final Set REGISTRY_OVERRIDES = Set.of("registry", "provisioners"); + + /** Reaches the registry the same way the dispatcher does. */ + private static boolean serves(CfnProvisionerFixture.Builder builder, String type) { + return builder.buildRegistry().forType(type).isPresent(); + } + + @Test + void namingAServiceWiresItsProvisioner() { + CfnProvisionerFixture.Builder fixture = CfnProvisionerFixture.builder() + .logs(mock(CloudWatchLogsService.class)); + + assertTrue(serves(fixture, "AWS::Logs::LogGroup")); + } + + @Test + void anUnnamedServiceIsNotWired() { + CfnProvisionerFixture.Builder fixture = CfnProvisionerFixture.builder() + .logs(mock(CloudWatchLogsService.class)); + + assertFalse(serves(fixture, "AWS::SNS::Topic"), + "a test that never named SNS should not get its provisioner"); + } + + /** One service can back several provisioners, as it does under CDI. */ + @Test + void oneServiceCanWireSeveralProvisioners() { + CfnProvisionerFixture.Builder fixture = CfnProvisionerFixture.builder() + .ec2(mock(Ec2Service.class)); + + for (String type : Set.of("AWS::EC2::VPC", "AWS::EC2::VPCEndpoint", "AWS::EC2::NetworkAcl", + "AWS::EC2::LaunchTemplate", "AWS::EC2::SecurityGroupIngress")) { + assertTrue(serves(fixture, type), type + " should be wired from Ec2Service"); + } + } + + /** CDK::Metadata backs no service, so it is always available. */ + @Test + void theServicelessProvisionerIsAlwaysWired() { + assertTrue(serves(CfnProvisionerFixture.builder(), "AWS::CDK::Metadata")); + } + + /** + * Every provisioner on disk is reachable by driving the Builder's own public setters, or is + * explicitly exempt. + * + *

Deliberately behavioural rather than textual. An earlier version of this gate matched + * {@code new XCfnProvisioner(} against the fixture's source, which scored a construction arm as + * wired even when its field had no setter and could never be non-null. That is the same silent + * gap this fixture exists to close, so the check has to run the wiring instead of reading it: + * everything below reaches the registry only through the public API a test has, which means a + * field no setter assigns cannot be satisfied and shows up here as unreachable. + * + *

Mirrors {@code CfnResourceInventoryTest}, which compares the CDI-resolved production + * registry for the same reason. + */ + @Test + void everyProvisionerIsReachableThroughThePublicBuilderOrExplicitlyExempt() throws Exception { + Path provisioners = Path.of( + "src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners"); + Set onDisk; + try (var files = Files.list(provisioners)) { + onDisk = files.map(f -> f.getFileName().toString()) + .filter(n -> n.endsWith("CfnProvisioner.java")) + .map(n -> n.replace(".java", "")) + .collect(Collectors.toCollection(TreeSet::new)); + } + + CfnProvisionerFixture.Builder builder = CfnProvisionerFixture.builder(); + for (Method setter : CfnProvisionerFixture.Builder.class.getDeclaredMethods()) { + boolean namesOneCollaborator = Modifier.isPublic(setter.getModifiers()) + && setter.getReturnType() == CfnProvisionerFixture.Builder.class + && setter.getParameterCount() == 1 + && !setter.isVarArgs() + && !REGISTRY_OVERRIDES.contains(setter.getName()); + if (namesOneCollaborator) { + setter.invoke(builder, mock(setter.getParameterTypes()[0])); + } + } + + CloudFormationResourceRegistry registry = builder.buildRegistry(); + Set reachable = registry.registeredTypes().stream() + .map(registry::ownerOf) + .collect(Collectors.toCollection(TreeSet::new)); + + Set unreachable = new TreeSet<>(onDisk); + unreachable.removeAll(reachable); + unreachable.removeAll(NOT_INFERABLE); + + assertTrue(unreachable.isEmpty(), + "These provisioners cannot be reached through any public Builder setter, so a " + + "fixture test naming their service would silently hit the stub arm. Give " + + "them a service field AND a setter in CfnProvisionerFixture, or add them " + + "to NOT_INFERABLE with a reason: " + unreachable); + + Set staleExemptions = new TreeSet<>(NOT_INFERABLE); + staleExemptions.removeAll(onDisk); + assertTrue(staleExemptions.isEmpty(), + "NOT_INFERABLE names provisioners that no longer exist: " + staleExemptions); + } + + @Test + void anExplicitProvisionerSetReplacesTheInferredOne() { + CfnResourceProvisioner only = new CfnResourceProvisioner() { + @Override + public Set resourceTypes() { + return Set.of("AWS::Test::Only"); + } + + @Override + public void provision(StackResource r, JsonNode props, ProvisionContext ctx) { + } + }; + + CfnProvisionerFixture.Builder fixture = CfnProvisionerFixture.builder() + .sns(mock(SnsService.class)) + .provisioners(only); + + assertTrue(serves(fixture, "AWS::Test::Only")); + assertFalse(serves(fixture, "AWS::SNS::Topic"), + "an explicit set replaces inference rather than adding to it"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogGroupProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogGroupProvisionerTest.java index 9e2c81cec8..504862439b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogGroupProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogGroupProvisionerTest.java @@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.cloudformation; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; -import io.github.hectorvent.floci.services.cloudformation.provisioners.LogsCfnProvisioner; import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -40,14 +39,12 @@ class CloudFormationLogGroupProvisionerTest { @BeforeEach void setUp() { logsService = mock(CloudWatchLogsService.class); - // Registering the provisioner is load-bearing, not decoration: AWS::Logs::LogGroup now lives - // in LogsCfnProvisioner, so with an empty registry these calls would fall through to the - // dispatcher's stub arm and assert against a synthetic id instead of the real logic. Going - // through the dispatcher rather than the provisioner directly also covers the plumbing this - // migration changed, that existingPhysicalId and existingAttributes reach ProvisionContext. + // Naming the service is enough: the fixture wires LogsCfnProvisioner from it, the way CDI + // does. Testing through the dispatcher rather than the provisioner directly also covers the + // plumbing that carries existingPhysicalId and existingAttributes into ProvisionContext. provisioner = CfnProvisionerFixture.builder() .objectMapper(mapper) - .provisioners(new LogsCfnProvisioner(logsService)) + .logs(logsService) .build(); }