mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test(cloudformation): record and ratchet what a dropped property does (#7599)
This commit is contained in:
1 parent
a884257311
commit
fd29b4aa32
8 files changed
+367
-19
No files matched your search
+80
@@ -0,0 +1,80 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* The committed {@code drop-policy.tsv}: what an update does with a property the previous template
|
||||
* declared and the new one drops, one row per writable property that a provisioner arm calling
|
||||
* {@code ProvisionContext.droppedProperties} reads. Shared by {@link CfnSchemaCoverageTest}, which
|
||||
* checks the rows against the sources, and by the AppSync provisioner test, which checks them
|
||||
* against {@code AppSyncCfnProvisioner.dropPolicy}.
|
||||
*
|
||||
* <p>Columns, tab separated, no header: type, property, policy, and an optional reason. A policy is
|
||||
* {@code clear}, {@code default:<value>}, {@code keep:<reason>} or {@code create-only}. {@code create-only}
|
||||
* marks a create-only property: a drop is skipped or refused, never cleared.
|
||||
*/
|
||||
public final class CfnDropPolicyTable {
|
||||
|
||||
public static final Path FILE = Path.of("src/test/resources/cloudformation/drop-policy.tsv");
|
||||
|
||||
/** One row: the policy as written, and the optional fourth column. */
|
||||
public record Row(String policy, String reason) {
|
||||
|
||||
/** {@code clear}, {@code default}, {@code keep} or {@code create-only}. */
|
||||
public String kind() {
|
||||
int colon = policy.indexOf(':');
|
||||
return colon < 0 ? policy : policy.substring(0, colon);
|
||||
}
|
||||
|
||||
/** The text after the colon of {@code default:} and {@code keep:}, empty otherwise. */
|
||||
public String detail() {
|
||||
int colon = policy.indexOf(':');
|
||||
return colon < 0 ? "" : policy.substring(colon + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private CfnDropPolicyTable() {
|
||||
}
|
||||
|
||||
/** "type<TAB>property" to its row. */
|
||||
public static Map<String, Row> load() {
|
||||
try {
|
||||
return parse(Files.readAllLines(FILE, StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Cannot read " + FILE, e);
|
||||
}
|
||||
}
|
||||
|
||||
/** A repeated key, a wrong field count or an unknown policy is refused with the line named. */
|
||||
public static Map<String, Row> parse(List<String> lines) {
|
||||
Map<String, Row> rows = new TreeMap<>();
|
||||
for (String line : lines) {
|
||||
if (line.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String[] parts = line.split("\t", -1);
|
||||
if (parts.length < 3 || parts.length > 4) {
|
||||
throw new AssertionError("Expected 'type<TAB>property<TAB>policy[<TAB>reason]' in " + FILE
|
||||
+ " but found: " + line);
|
||||
}
|
||||
Row row = new Row(parts[2], parts.length == 4 ? parts[3] : "");
|
||||
if (!List.of("clear", "default", "keep", "create-only").contains(row.kind())) {
|
||||
throw new AssertionError("Unknown policy '" + parts[2] + "' in " + FILE + ": " + line
|
||||
+ ". Use clear, default:<value>, keep:<reason> or create-only.");
|
||||
}
|
||||
String key = parts[0] + "\t" + parts[1];
|
||||
if (rows.put(key, row) != null) {
|
||||
throw new AssertionError("Two rows for " + parts[0] + " " + parts[1] + " in " + FILE
|
||||
+ "; the later would replace the earlier.");
|
||||
}
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
+122
@@ -51,6 +51,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
* <p>Known limit: a property counts as read when its name appears as a literal in a read call or in
|
||||
* a helper's arguments in the type's reachable code, so the scan can credit a property that is not
|
||||
* applied. It is a ratchet on what the sources mention, not proof that a value takes effect.
|
||||
*
|
||||
* <p>The same scan pins {@code drop-policy.tsv}: one row for each writable property that an arm
|
||||
* calling {@code droppedProperties} reads, saying what an update that drops it does. Only the
|
||||
* presence of a row and the {@code create-only} rows are checked here; the AppSync values
|
||||
* are checked against {@code AppSyncCfnProvisioner.dropPolicy} in {@code AppSyncCfnProvisionerTest},
|
||||
* and the EC2 values are not checked against the code.
|
||||
*/
|
||||
class CfnSchemaCoverageTest {
|
||||
|
||||
@@ -141,6 +147,8 @@ class CfnSchemaCoverageTest {
|
||||
"requestBody(props, ctx)",
|
||||
"request(props, ctx)");
|
||||
private static final String ALL_PROPERTIES = "*";
|
||||
/** A call that makes an arm decide what a dropped property means, see {@code drop-policy.tsv}. */
|
||||
private static final String DROPPED_PROPERTIES_CALL = "droppedProperties(";
|
||||
|
||||
/** What the registry schemas declare for one resource type, see {@link #schemaSurface}. */
|
||||
private record SchemaSurface(Set<String> readOnly, Set<String> writable) { }
|
||||
@@ -253,6 +261,84 @@ class CfnSchemaCoverageTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The drop-policy rows are the writable properties that the arms calling {@code droppedProperties}
|
||||
* read, one row each, no more and no fewer. Same scan as the property gaps, so a provisioner that
|
||||
* starts reading a property in such an arm, or a new arm that calls it, needs a row.
|
||||
*/
|
||||
@Test
|
||||
void dropPolicyRowsMatchTheArmsThatCallDroppedProperties() {
|
||||
assertSameLines(render(recordedDropPolicyKeys()), render(actualDropPolicyKeys()),
|
||||
"The set of properties with a recorded drop policy changed.",
|
||||
"(an arm that calls droppedProperties reads a property, or a new such arm arrived): "
|
||||
+ "add a row with its policy to " + CfnDropPolicyTable.FILE,
|
||||
"(no longer read in an arm that calls droppedProperties): delete their rows from "
|
||||
+ CfnDropPolicyTable.FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyDropPolicyRowIsStillReal() {
|
||||
Map<String, String> actual = actualDropPolicyKeys();
|
||||
List<String> stale = CfnDropPolicyTable.load().keySet().stream()
|
||||
.filter(key -> !actual.containsKey(key))
|
||||
.sorted()
|
||||
.toList();
|
||||
|
||||
if (!stale.isEmpty()) {
|
||||
throw new AssertionError("These properties are no longer read by an arm that calls "
|
||||
+ "droppedProperties, so their rows in " + CfnDropPolicyTable.FILE
|
||||
+ " are stale and should be deleted: " + stale);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyKeepPolicyNamesItsReason() {
|
||||
List<String> unexplained = CfnDropPolicyTable.load().entrySet().stream()
|
||||
.filter(e -> "keep".equals(e.getValue().kind())
|
||||
&& (e.getValue().detail().isBlank() || e.getValue().detail().startsWith("TODO")))
|
||||
.map(Map.Entry::getKey)
|
||||
.sorted()
|
||||
.toList();
|
||||
|
||||
if (!unexplained.isEmpty()) {
|
||||
throw new AssertionError("A 'keep' policy in " + CfnDropPolicyTable.FILE
|
||||
+ " must say why the value stays, as keep:<reason>: " + unexplained);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@code create-only} is the class: a drop there is skipped or refused, never cleared. */
|
||||
@Test
|
||||
void everyCreateOnlyPolicyIsACreateOnlyProperty() {
|
||||
List<String> wrong = CfnDropPolicyTable.load().entrySet().stream()
|
||||
.filter(e -> "create-only".equals(e.getValue().kind()))
|
||||
.map(Map.Entry::getKey)
|
||||
.filter(key -> {
|
||||
String[] parts = key.split("\t");
|
||||
return !CfnCreateOnlyProperties.isCreateOnly(parts[0], parts[1]);
|
||||
})
|
||||
.sorted()
|
||||
.toList();
|
||||
|
||||
if (!wrong.isEmpty()) {
|
||||
throw new AssertionError("These 'create-only' rows in " + CfnDropPolicyTable.FILE
|
||||
+ " are not create-only in create-only-properties.json: " + wrong);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDuplicateDropPolicyRowIsRefused() {
|
||||
AssertionError e = assertThrows(AssertionError.class, () -> CfnDropPolicyTable.parse(List.of(
|
||||
"AWS::EC2::Subnet\tTags\tclear", "AWS::EC2::Subnet\tTags\tclear")));
|
||||
assertTrue(e.getMessage().contains("AWS::EC2::Subnet Tags"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownDropPolicyIsRefused() {
|
||||
AssertionError e = assertThrows(AssertionError.class,
|
||||
() -> CfnDropPolicyTable.parse(List.of("AWS::EC2::Subnet\tTags\tremove")));
|
||||
assertTrue(e.getMessage().contains("remove"), e.getMessage());
|
||||
}
|
||||
|
||||
/** An entry no provisioner contains credits nothing: a renamed helper must not leave it behind. */
|
||||
@Test
|
||||
void everyWholesaleCallAndHelperReadStillOccursInAProvisioner() {
|
||||
@@ -348,6 +434,36 @@ class CfnSchemaCoverageTest {
|
||||
return gaps;
|
||||
}
|
||||
|
||||
/** "AWS::EC2::Subnet\tTags" for each row of {@code drop-policy.tsv}. */
|
||||
private static Map<String, String> recordedDropPolicyKeys() {
|
||||
Map<String, String> keys = new TreeMap<>();
|
||||
CfnDropPolicyTable.load().keySet().forEach(key -> keys.put(key, ""));
|
||||
return keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* The writable properties read by the arms whose reachable code calls {@code droppedProperties},
|
||||
* keyed as in {@link CfnDropPolicyTable}.
|
||||
*/
|
||||
private static Map<String, String> actualDropPolicyKeys() {
|
||||
Map<String, ProvisionerScan> scans = scanProvisioners();
|
||||
Map<String, String> keys = new TreeMap<>();
|
||||
for (Map.Entry<String, String> entry : inventory().entrySet()) {
|
||||
String type = entry.getKey();
|
||||
ProvisionerScan scan = scans.get(entry.getValue());
|
||||
if (scan == null || !scan.reaches(type, DROPPED_PROPERTIES_CALL)) {
|
||||
continue;
|
||||
}
|
||||
Set<String> read = scan.propertiesReadBy(type);
|
||||
for (String property : schemaWritableProperties(type)) {
|
||||
if (read.contains(ALL_PROPERTIES) || read.contains(property)) {
|
||||
keys.put(type + "\t" + property, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** The gaps computed from the schemas and the provisioner sources, keyed the same way. */
|
||||
private static Map<String, String> actualGaps() {
|
||||
Map<String, ProvisionerScan> scans = scanProvisioners();
|
||||
@@ -432,6 +548,12 @@ class CfnSchemaCoverageTest {
|
||||
return read;
|
||||
}
|
||||
|
||||
/** Whether the code reachable from the type's arm contains {@code call}. */
|
||||
boolean reaches(String resourceType, String call) {
|
||||
String code = codeByType.isEmpty() ? classCode : codeByType.get(resourceType);
|
||||
return code != null && code.contains(call);
|
||||
}
|
||||
|
||||
Set<String> attributesFor(String resourceType) {
|
||||
if (byType.isEmpty()) {
|
||||
return classWide;
|
||||
|
||||
+28
@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.services.appsync.model.Resolver;
|
||||
import io.github.hectorvent.floci.services.appsync.model.ResolverKind;
|
||||
import io.github.hectorvent.floci.services.appsync.model.SchemaCreationStatus;
|
||||
import io.github.hectorvent.floci.services.appsync.model.SchemaCreationStatusType;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CfnDropPolicyTable;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -26,6 +27,7 @@ import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -1517,4 +1519,30 @@ class AppSyncCfnProvisionerTest {
|
||||
"AWS::AppSync::Resolver", "AWS::AppSync::ApiKey"),
|
||||
provisioner.resourceTypes());
|
||||
}
|
||||
|
||||
/**
|
||||
* The AppSync rows of drop-policy.tsv are what {@code clearDropped} acts on. A {@code keep} row
|
||||
* carries its reason after the colon, which the accessor does not repeat.
|
||||
*/
|
||||
@Test
|
||||
void appSyncDropPolicyRowsMatchWhatClearDroppedActsOn() {
|
||||
List<String> mismatched = new ArrayList<>();
|
||||
int checked = 0;
|
||||
for (Map.Entry<String, CfnDropPolicyTable.Row> entry : CfnDropPolicyTable.load().entrySet()) {
|
||||
String[] key = entry.getKey().split("\t");
|
||||
if (!key[0].startsWith("AWS::AppSync::")) {
|
||||
continue;
|
||||
}
|
||||
checked++;
|
||||
CfnDropPolicyTable.Row row = entry.getValue();
|
||||
String expected = AppSyncCfnProvisioner.dropPolicy(key[0], key[1]);
|
||||
String recorded = "keep".equals(row.kind()) ? "keep" : row.policy();
|
||||
if (!expected.equals(recorded)) {
|
||||
mismatched.add(key[0] + " " + key[1] + ": file says " + row.policy() + ", code says " + expected);
|
||||
}
|
||||
}
|
||||
assertTrue(checked > 0, "no AppSync rows in " + CfnDropPolicyTable.FILE);
|
||||
assertTrue(mismatched.isEmpty(), "Rows in " + CfnDropPolicyTable.FILE
|
||||
+ " that differ from AppSyncCfnProvisioner.dropPolicy: " + mismatched);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user