mirror of
https://github.com/floci-io/floci.git
synced 2026-11-25 14:57:54 -05:00
chore(cloudformation): move the custom resource stack tests onto CustomResourceProbe (#6931)
This commit is contained in:
1 parent
606ed3cf8b
commit
c3fdacc3ce
5 files changed
+67
-156
No files matched your search
+14
-51
@@ -5,8 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
@@ -14,20 +12,14 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
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.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A stack update that changes a custom resource and then fails on a later resource rolls the custom
|
||||
@@ -45,9 +37,7 @@ class CloudFormationCustomResourceRollbackIntegrationTest {
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:rollback-probe";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final List<JsonNode> events = new CopyOnWriteArrayList<>();
|
||||
private volatile String failingRequest;
|
||||
private volatile String failureReason;
|
||||
private CustomResourceProbe probe;
|
||||
|
||||
@InjectMock
|
||||
LambdaService lambdaService;
|
||||
@@ -59,13 +49,7 @@ class CloudFormationCustomResourceRollbackIntegrationTest {
|
||||
|
||||
@BeforeEach
|
||||
void stubHandler() {
|
||||
when(lambdaService.invoke(any(), eq(SERVICE_TOKEN), any(), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(inv -> {
|
||||
JsonNode event = mapper.readTree((byte[]) inv.getArgument(2));
|
||||
events.add(event);
|
||||
putResponse(event);
|
||||
return new InvokeResult(200, null, "null".getBytes(StandardCharsets.UTF_8), null, "req-1");
|
||||
});
|
||||
probe = CustomResourceProbe.stub(lambdaService, SERVICE_TOKEN, "rollback-probe");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,10 +63,10 @@ class CloudFormationCustomResourceRollbackIntegrationTest {
|
||||
awaitStackStatus(stackId, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
assertEquals(List.of("Create:1", "Update:2", "Update:1"), requests());
|
||||
assertProperties(events.get(0), "1", null);
|
||||
assertProperties(events.get(1), "2", "1");
|
||||
assertProperties(events.get(2), "1", "2");
|
||||
assertEquals(physicalId, events.get(2).get("PhysicalResourceId").asText());
|
||||
assertProperties(probe.events().get(0), "1", null);
|
||||
assertProperties(probe.events().get(1), "2", "1");
|
||||
assertProperties(probe.events().get(2), "1", "2");
|
||||
assertEquals(physicalId, probe.events().get(2).get("PhysicalResourceId").asText());
|
||||
assertEquals(physicalId, probePhysicalId(stackName));
|
||||
assertFalse(describeStackEvents(stackId).contains("Rollback is not implemented"));
|
||||
}
|
||||
@@ -120,10 +104,10 @@ class CloudFormationCustomResourceRollbackIntegrationTest {
|
||||
awaitStackStatus(stackId, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
assertEquals(List.of("Create:1", "Update:3", "Update:1"), requests());
|
||||
assertProperties(events.get(0), "1", null);
|
||||
assertProperties(events.get(1), "3", "1");
|
||||
assertProperties(events.get(2), "1", "3");
|
||||
assertEquals(physicalId, events.get(2).get("PhysicalResourceId").asText());
|
||||
assertProperties(probe.events().get(0), "1", null);
|
||||
assertProperties(probe.events().get(1), "3", "1");
|
||||
assertProperties(probe.events().get(2), "1", "3");
|
||||
assertEquals(physicalId, probe.events().get(2).get("PhysicalResourceId").asText());
|
||||
assertEquals(physicalId, probePhysicalId(stackName));
|
||||
List<String> probeStatuses = XmlParser.extractGroups(describeStackEvents(stackId), "member")
|
||||
.reversed()
|
||||
@@ -149,8 +133,8 @@ class CloudFormationCustomResourceRollbackIntegrationTest {
|
||||
awaitStackStatus(stackId, "DELETE_COMPLETE");
|
||||
|
||||
assertEquals(List.of("Create:1", "Update:2", "Update:1", "Delete:1"), requests());
|
||||
assertProperties(events.get(3), "1", null);
|
||||
assertEquals(physicalId, events.get(3).get("PhysicalResourceId").asText());
|
||||
assertProperties(probe.events().get(3), "1", null);
|
||||
assertEquals(physicalId, probe.events().get(3).get("PhysicalResourceId").asText());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,8 +158,7 @@ class CloudFormationCustomResourceRollbackIntegrationTest {
|
||||
}
|
||||
|
||||
private void failOn(String request, String reason) {
|
||||
failingRequest = request;
|
||||
failureReason = reason;
|
||||
probe.failOn(event -> request(event).equals(request), reason);
|
||||
}
|
||||
|
||||
private static String template(String revision, boolean withFailingBucket) {
|
||||
@@ -206,33 +189,13 @@ class CloudFormationCustomResourceRollbackIntegrationTest {
|
||||
|
||||
/** Each request the handler saw, as its RequestType and the Revision it was asked to apply. */
|
||||
private List<String> requests() {
|
||||
return events.stream().map(CloudFormationCustomResourceRollbackIntegrationTest::request).toList();
|
||||
return probe.events().stream().map(CloudFormationCustomResourceRollbackIntegrationTest::request).toList();
|
||||
}
|
||||
|
||||
private static String request(JsonNode event) {
|
||||
return event.get("RequestType").asText() + ":" + event.get("ResourceProperties").get("Revision").asText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Answers the request the way a handler does: a PUT of its result document to ResponseURL. The
|
||||
* request the test named with {@link #failOn} is answered FAILED.
|
||||
*/
|
||||
private void putResponse(JsonNode event) {
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
if (request(event).equals(failingRequest)) {
|
||||
response.put("Status", "FAILED");
|
||||
response.put("Reason", failureReason);
|
||||
} else {
|
||||
response.put("Status", "SUCCESS");
|
||||
}
|
||||
response.put("RequestId", event.get("RequestId").asText());
|
||||
response.put("StackId", event.get("StackId").asText());
|
||||
response.put("LogicalResourceId", event.get("LogicalResourceId").asText());
|
||||
response.put("PhysicalResourceId", "rollback-probe");
|
||||
String path = URI.create(event.get("ResponseURL").asText()).getPath();
|
||||
given().body(response.toString()).when().put(path).then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String createStack(String stackName, String template) {
|
||||
String xml = cfn(Map.of("Action", "CreateStack", "StackName", stackName, "TemplateBody", template));
|
||||
return XmlParser.extractFirst(xml, "StackId", null);
|
||||
|
||||
+9
-38
@@ -1,12 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
@@ -14,19 +10,13 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Every request CloudFormation sends a custom resource carries the {@code StackId} of the stack that
|
||||
@@ -43,8 +33,7 @@ class CloudFormationCustomResourceStackIdIntegrationTest {
|
||||
private static final String SERVICE_TOKEN =
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:stack-id-probe";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final List<JsonNode> events = new CopyOnWriteArrayList<>();
|
||||
private CustomResourceProbe probe;
|
||||
|
||||
@InjectMock
|
||||
LambdaService lambdaService;
|
||||
@@ -56,13 +45,8 @@ class CloudFormationCustomResourceStackIdIntegrationTest {
|
||||
|
||||
@BeforeEach
|
||||
void stubHandler() {
|
||||
when(lambdaService.invoke(any(), eq(SERVICE_TOKEN), any(), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(inv -> {
|
||||
JsonNode event = mapper.readTree((byte[]) inv.getArgument(2));
|
||||
events.add(event);
|
||||
putResponse(event);
|
||||
return new InvokeResult(200, null, "null".getBytes(StandardCharsets.UTF_8), null, "req-1");
|
||||
});
|
||||
probe = CustomResourceProbe.stub(lambdaService, SERVICE_TOKEN, "stack-id-probe")
|
||||
.data(event -> Map.of("EventStackId", event.get("StackId").asText()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,13 +67,13 @@ class CloudFormationCustomResourceStackIdIntegrationTest {
|
||||
awaitStackStatus(stackId, "DELETE_COMPLETE");
|
||||
|
||||
assertEquals(List.of("Create", "Update", "Delete"),
|
||||
events.stream().map(e -> e.get("RequestType").asText()).toList());
|
||||
for (JsonNode event : events) {
|
||||
probe.events().stream().map(e -> e.get("RequestType").asText()).toList());
|
||||
for (JsonNode event : probe.events()) {
|
||||
assertEquals(stackId, event.get("StackId").asText(), event.toString());
|
||||
}
|
||||
// What a handler compares: the property built from Ref AWS::StackId against the event.
|
||||
assertEquals(events.get(0).get("StackId").asText(),
|
||||
events.get(0).get("ResourceProperties").get("OwningStackArn").asText());
|
||||
assertEquals(probe.events().get(0).get("StackId").asText(),
|
||||
probe.events().get(0).get("ResourceProperties").get("OwningStackArn").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,8 +107,8 @@ class CloudFormationCustomResourceStackIdIntegrationTest {
|
||||
assertEquals(childStackId, outputValue(parent, "ChildEventStackId"));
|
||||
String child = awaitStackStatus(childStackId, "CREATE_COMPLETE");
|
||||
assertEquals(childStackId, outputValue(child, "RefStackId"));
|
||||
assertEquals(1, events.size());
|
||||
assertEquals(childStackId, events.get(0).get("StackId").asText());
|
||||
assertEquals(1, probe.events().size());
|
||||
assertEquals(childStackId, probe.events().get(0).get("StackId").asText());
|
||||
}
|
||||
|
||||
private static String probeTemplate(String revision) {
|
||||
@@ -148,19 +132,6 @@ class CloudFormationCustomResourceStackIdIntegrationTest {
|
||||
""".formatted(SERVICE_TOKEN, revision);
|
||||
}
|
||||
|
||||
/** Answers the request the way a handler does: a PUT of its result document to ResponseURL. */
|
||||
private void putResponse(JsonNode event) {
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
response.put("Status", "SUCCESS");
|
||||
response.put("RequestId", event.get("RequestId").asText());
|
||||
response.put("StackId", event.get("StackId").asText());
|
||||
response.put("LogicalResourceId", event.get("LogicalResourceId").asText());
|
||||
response.put("PhysicalResourceId", "stack-id-probe");
|
||||
response.putObject("Data").put("EventStackId", event.get("StackId").asText());
|
||||
String path = URI.create(event.get("ResponseURL").asText()).getPath();
|
||||
given().body(response.toString()).when().put(path).then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String createStack(String stackName, String template) {
|
||||
String xml = cfn(Map.of("Action", "CreateStack", "StackName", stackName, "TemplateBody", template));
|
||||
return XmlParser.extractFirst(xml, "StackId", null);
|
||||
|
||||
+1
-29
@@ -1,12 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
@@ -15,8 +10,6 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
@@ -24,9 +17,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A custom resource keeps the physical id of each resource its definition names, through a
|
||||
@@ -43,8 +33,6 @@ class CloudFormationCustomResourceUsesIntegrationTest {
|
||||
private static final String SERVICE_TOKEN = "arn:aws:lambda:us-east-1:000000000000:function:uses-probe";
|
||||
private static final String USES = "__FlociUsesResource.";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@InjectMock
|
||||
LambdaService lambdaService;
|
||||
|
||||
@@ -58,11 +46,7 @@ class CloudFormationCustomResourceUsesIntegrationTest {
|
||||
|
||||
@BeforeEach
|
||||
void stubHandler() {
|
||||
when(lambdaService.invoke(any(), eq(SERVICE_TOKEN), any(), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(inv -> {
|
||||
putResponse(mapper.readTree((byte[]) inv.getArgument(2)));
|
||||
return new InvokeResult(200, null, "null".getBytes(StandardCharsets.UTF_8), null, "req-1");
|
||||
});
|
||||
CustomResourceProbe.stub(lambdaService, SERVICE_TOKEN, "uses-probe");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -190,18 +174,6 @@ class CloudFormationCustomResourceUsesIntegrationTest {
|
||||
return XmlParser.extractFirst(xml, "PhysicalResourceId", null);
|
||||
}
|
||||
|
||||
/** Answers the request the way a handler does: a PUT of a SUCCESS result to ResponseURL. */
|
||||
private void putResponse(JsonNode event) {
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
response.put("Status", "SUCCESS");
|
||||
response.put("RequestId", event.get("RequestId").asText());
|
||||
response.put("StackId", event.get("StackId").asText());
|
||||
response.put("LogicalResourceId", event.get("LogicalResourceId").asText());
|
||||
response.put("PhysicalResourceId", "uses-probe");
|
||||
String path = URI.create(event.get("ResponseURL").asText()).getPath();
|
||||
given().body(response.toString()).when().put(path).then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String createStack(String stackName, String template) {
|
||||
String xml = cfn(Map.of("Action", "CreateStack", "StackName", stackName, "TemplateBody", template));
|
||||
return XmlParser.extractFirst(xml, "StackId", null);
|
||||
|
||||
+6
-33
@@ -5,8 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.testing.CfnStackWaits;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
@@ -15,18 +13,12 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@code AWS::NoValue} is an omission wherever a template property can hold it: the properties a
|
||||
@@ -45,7 +37,7 @@ class CloudFormationNoValueOmissionIntegrationTest {
|
||||
private static final String NO_VALUE_IF = "{\"Fn::If\": [\"Off\", \"unused\", {\"Ref\": \"AWS::NoValue\"}]}";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final List<JsonNode> events = new CopyOnWriteArrayList<>();
|
||||
private CustomResourceProbe probe;
|
||||
|
||||
@InjectMock
|
||||
LambdaService lambdaService;
|
||||
@@ -57,13 +49,7 @@ class CloudFormationNoValueOmissionIntegrationTest {
|
||||
|
||||
@BeforeEach
|
||||
void stubHandler() {
|
||||
when(lambdaService.invoke(any(), eq(SERVICE_TOKEN), any(), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(inv -> {
|
||||
JsonNode event = mapper.readTree((byte[]) inv.getArgument(2));
|
||||
events.add(event);
|
||||
putResponse(event);
|
||||
return new InvokeResult(200, null, "null".getBytes(StandardCharsets.UTF_8), null, "req-1");
|
||||
});
|
||||
probe = CustomResourceProbe.stub(lambdaService, SERVICE_TOKEN, "no-value-probe");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -126,10 +112,10 @@ class CloudFormationNoValueOmissionIntegrationTest {
|
||||
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack).status());
|
||||
|
||||
assertEquals(List.of("Create", "Update"),
|
||||
events.stream().map(e -> e.get("RequestType").asText()).toList());
|
||||
assertEquals(expected("one"), events.get(0).get("ResourceProperties"));
|
||||
assertEquals(expected("two"), events.get(1).get("ResourceProperties"));
|
||||
assertEquals(expected("one"), events.get(1).get("OldResourceProperties"));
|
||||
probe.events().stream().map(e -> e.get("RequestType").asText()).toList());
|
||||
assertEquals(expected("one"), probe.events().get(0).get("ResourceProperties"));
|
||||
assertEquals(expected("two"), probe.events().get(1).get("ResourceProperties"));
|
||||
assertEquals(expected("one"), probe.events().get(1).get("OldResourceProperties"));
|
||||
}
|
||||
|
||||
private JsonNode expected(String revision) {
|
||||
@@ -213,19 +199,6 @@ class CloudFormationNoValueOmissionIntegrationTest {
|
||||
.extract().jsonPath().getString("Description");
|
||||
}
|
||||
|
||||
/** Answers the request the way a handler does: a PUT of its result document to ResponseURL. */
|
||||
private void putResponse(JsonNode event) {
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
response.put("Status", "SUCCESS");
|
||||
response.put("RequestId", event.get("RequestId").asText());
|
||||
response.put("StackId", event.get("StackId").asText());
|
||||
response.put("LogicalResourceId", event.get("LogicalResourceId").asText());
|
||||
response.put("PhysicalResourceId", "no-value-probe");
|
||||
response.putObject("Data");
|
||||
String path = URI.create(event.get("ResponseURL").asText()).getPath();
|
||||
given().body(response.toString()).when().put(path).then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String unique(String prefix) {
|
||||
return prefix + "-" + Long.toString(System.nanoTime(), 36);
|
||||
}
|
||||
|
||||
+37
-5
@@ -10,7 +10,10 @@ import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -20,7 +23,8 @@ import static org.mockito.Mockito.when;
|
||||
/**
|
||||
* The handler of a custom resource in a stack test. It stands in for the ServiceToken Lambda on a
|
||||
* mocked {@link LambdaService}, keeps every request it is sent, and answers each the way a handler
|
||||
* does: a PUT of a SUCCESS result to the event's {@code ResponseURL}.
|
||||
* does: a PUT of its result to the event's {@code ResponseURL}, SUCCESS unless {@link #failOn} names
|
||||
* the request.
|
||||
*/
|
||||
final class CustomResourceProbe {
|
||||
|
||||
@@ -28,6 +32,9 @@ final class CustomResourceProbe {
|
||||
|
||||
private final List<JsonNode> events = new CopyOnWriteArrayList<>();
|
||||
private final String physicalId;
|
||||
private volatile Function<JsonNode, Map<String, String>> data;
|
||||
private volatile Predicate<JsonNode> failing = event -> false;
|
||||
private volatile String failureReason;
|
||||
|
||||
private CustomResourceProbe(String physicalId) {
|
||||
this.physicalId = physicalId;
|
||||
@@ -39,24 +46,49 @@ final class CustomResourceProbe {
|
||||
.thenAnswer(invocation -> {
|
||||
JsonNode event = MAPPER.readTree((byte[]) invocation.getArgument(2));
|
||||
probe.events.add(event);
|
||||
probe.succeed(event);
|
||||
probe.answer(event);
|
||||
return new InvokeResult(200, null, "null".getBytes(StandardCharsets.UTF_8), null, "req-1");
|
||||
});
|
||||
return probe;
|
||||
}
|
||||
|
||||
/** Answers each request with the Data {@code data} builds from it. */
|
||||
CustomResourceProbe data(Function<JsonNode, Map<String, String>> data) {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Answers the requests {@code request} matches FAILED, with {@code reason}. */
|
||||
void failOn(Predicate<JsonNode> request, String reason) {
|
||||
failureReason = reason;
|
||||
failing = request;
|
||||
}
|
||||
|
||||
/** Every request so far, in the order it was sent. */
|
||||
List<JsonNode> events() {
|
||||
return events;
|
||||
}
|
||||
|
||||
/** The first request of {@code requestType}, such as Create. */
|
||||
JsonNode request(String requestType) {
|
||||
return events.stream().filter(event -> requestType.equals(event.path("RequestType").asText()))
|
||||
.findFirst().orElseThrow(() -> new AssertionError("No " + requestType + " request in " + events));
|
||||
}
|
||||
|
||||
private void succeed(JsonNode event) {
|
||||
ObjectNode response = MAPPER.createObjectNode().put("Status", "SUCCESS")
|
||||
.put("RequestId", event.get("RequestId").asText())
|
||||
private void answer(JsonNode event) {
|
||||
ObjectNode response = MAPPER.createObjectNode();
|
||||
if (failing.test(event)) {
|
||||
response.put("Status", "FAILED").put("Reason", failureReason);
|
||||
} else {
|
||||
response.put("Status", "SUCCESS");
|
||||
}
|
||||
response.put("RequestId", event.get("RequestId").asText())
|
||||
.put("StackId", event.get("StackId").asText())
|
||||
.put("LogicalResourceId", event.get("LogicalResourceId").asText())
|
||||
.put("PhysicalResourceId", physicalId);
|
||||
if (data != null) {
|
||||
response.set("Data", MAPPER.valueToTree(data.apply(event)));
|
||||
}
|
||||
given().body(response.toString()).when().put(URI.create(event.get("ResponseURL").asText()).getPath())
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user