mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 18:48:00 -05:00
feat(bedrock-agentcore): keep runtime endpoint create tags and support tagging them (#7597)
This commit is contained in:
1 parent
0cb45a86df
commit
c1a88b8ab8
8 files changed
+305
-8
No files matched your search
+83
@@ -15,10 +15,12 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@@ -432,6 +434,87 @@ class BedrockAgentCoreControlServiceTest {
|
||||
() -> service.createEndpoint(id, "DEFAULT", null, null, null, REGION)).getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointKeepsItsCreateTagsApartFromTheRuntimes() {
|
||||
AgentRuntime rt = service.createAgentRuntime("myAgent", artifact(), network(),
|
||||
"arn:aws:iam::000000000000:role/agent", "desc", null, null, null,
|
||||
Map.of("owner", "runtime"), null, REGION);
|
||||
String id = rt.getAgentRuntimeId();
|
||||
AgentRuntimeEndpoint ep = service.createEndpoint(id, "prod", null, null,
|
||||
Map.of("env", "prod"), null, REGION);
|
||||
String endpointArn = service.endpointArn(ep, REGION);
|
||||
|
||||
assertEquals(Map.of("env", "prod"), service.getEndpointTagsByArn(REGION, endpointArn));
|
||||
|
||||
service.tagEndpointByArn(REGION, endpointArn, Map.of("team", "ml"));
|
||||
service.untagEndpointByArn(REGION, endpointArn, List.of("env"));
|
||||
|
||||
assertEquals(Map.of("team", "ml"), service.getEndpointTagsByArn(REGION, endpointArn));
|
||||
assertEquals(Map.of("owner", "runtime"),
|
||||
service.getTagsByArn(REGION, service.arn(rt, "1", REGION)));
|
||||
assertTrue(service.getEndpoint(id, "DEFAULT", REGION).getTags().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointTagSnapshotsStayStableWhileTagsAreWrittenConcurrently() throws Exception {
|
||||
AgentRuntime rt = create("myAgent");
|
||||
String id = rt.getAgentRuntimeId();
|
||||
AgentRuntimeEndpoint ep = service.createEndpoint(id, "prod", null, null,
|
||||
Map.of("fixed", "1"), null, REGION);
|
||||
String endpointArn = service.endpointArn(ep, REGION);
|
||||
AtomicInteger inconsistent = new AtomicInteger();
|
||||
try (ExecutorService executor = Executors.newFixedThreadPool(3)) {
|
||||
Future<?> writer = executor.submit(() -> {
|
||||
for (int i = 0; i < 2000; i++) {
|
||||
service.tagEndpointByArn(REGION, endpointArn, Map.of("a", "x", "b", "x"));
|
||||
service.untagEndpointByArn(REGION, endpointArn, List.of("a", "b"));
|
||||
}
|
||||
service.tagEndpointByArn(REGION, endpointArn, Map.of("final", "yes"));
|
||||
});
|
||||
List<Future<?>> readers = List.of(
|
||||
executor.submit(() -> readEndpointTagSnapshots(endpointArn, writer, inconsistent)),
|
||||
executor.submit(() -> readEndpointTagSnapshots(endpointArn, writer, inconsistent)));
|
||||
writer.get(60, TimeUnit.SECONDS);
|
||||
for (Future<?> reader : readers) {
|
||||
reader.get(60, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
assertEquals(0, inconsistent.get(), "a snapshot must never show a partial tag write");
|
||||
assertEquals(Map.of("fixed", "1", "final", "yes"), service.getEndpointTagsByArn(REGION, endpointArn));
|
||||
}
|
||||
|
||||
private void readEndpointTagSnapshots(String endpointArn, Future<?> writer, AtomicInteger inconsistent) {
|
||||
while (!writer.isDone()) {
|
||||
Map<String, String> snapshot = service.getEndpointTagsByArn(REGION, endpointArn);
|
||||
int size = 0;
|
||||
for (String ignored : snapshot.keySet()) {
|
||||
size++;
|
||||
}
|
||||
if (snapshot.containsKey("a") != snapshot.containsKey("b") || !"1".equals(snapshot.get("fixed"))
|
||||
|| size != snapshot.size()) {
|
||||
inconsistent.incrementAndGet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointTagsAreRemovedWithTheEndpoint() {
|
||||
AgentRuntime rt = create("myAgent");
|
||||
String id = rt.getAgentRuntimeId();
|
||||
AgentRuntimeEndpoint ep = service.createEndpoint(id, "prod", null, null,
|
||||
Map.of("env", "prod"), null, REGION);
|
||||
String endpointArn = service.endpointArn(ep, REGION);
|
||||
|
||||
service.deleteEndpoint(id, "prod", null, REGION);
|
||||
|
||||
assertEquals(404, assertThrows(AwsException.class,
|
||||
() -> service.getEndpointTagsByArn(REGION, endpointArn)).getHttpStatus());
|
||||
assertEquals(404, assertThrows(AwsException.class,
|
||||
() -> service.tagEndpointByArn(REGION, endpointArn, Map.of("a", "b"))).getHttpStatus());
|
||||
assertEquals(404, assertThrows(AwsException.class,
|
||||
() -> service.untagEndpointByArn(REGION, endpointArn, List.of("a"))).getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteEndpointIsIdempotentByClientToken() {
|
||||
AgentRuntime rt = create("myAgent");
|
||||
|
||||
+98
@@ -10,6 +10,7 @@ import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@@ -136,4 +137,101 @@ class BedrockAgentCoreEndpointIntegrationTest {
|
||||
.when().put("/runtimes/missing-abcdefghij/runtime-endpoints/")
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointTagsFromCreateCanBeListedTaggedAndUntaggedOnTheEndpointArn() {
|
||||
String id = given().contentType("application/json").body(CREATE_RUNTIME.replace("epAgent", "epTagAgent"))
|
||||
.when().put("/runtimes/").then().statusCode(202).extract().path("agentRuntimeId");
|
||||
String runtimeArn = given().contentType("application/json")
|
||||
.when().get("/runtimes/" + id + "/")
|
||||
.then().statusCode(200).extract().path("agentRuntimeArn");
|
||||
String endpointArn = given().contentType("application/json")
|
||||
.body("{\"name\":\"tagged\",\"tags\":{\"env\":\"prod\"}}")
|
||||
.when().put("/runtimes/" + id + "/runtime-endpoints/")
|
||||
.then().statusCode(202).extract().path("agentRuntimeEndpointArn");
|
||||
try {
|
||||
given().when().get("/tags/" + endpointArn)
|
||||
.then().statusCode(200).body("tags.env", equalTo("prod"));
|
||||
|
||||
given().contentType("application/json").body("{\"tags\":{\"team\":\"ml\"}}")
|
||||
.when().post("/tags/" + endpointArn).then().statusCode(204);
|
||||
given().when().delete("/tags/" + endpointArn + "?tagKeys=env").then().statusCode(204);
|
||||
given().when().get("/tags/" + endpointArn)
|
||||
.then().statusCode(200)
|
||||
.body("tags.team", equalTo("ml"))
|
||||
.body("tags.env", nullValue());
|
||||
|
||||
given().when().get("/tags/" + runtimeArn)
|
||||
.then().statusCode(200).body("tags.team", nullValue());
|
||||
} finally {
|
||||
given().when().delete("/runtimes/" + id + "/").then().statusCode(202);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointTagsAreRemovedWithTheEndpointAndRejectForeignOrMissingArns() {
|
||||
String id = given().contentType("application/json").body(CREATE_RUNTIME.replace("epAgent", "epTagAgent2"))
|
||||
.when().put("/runtimes/").then().statusCode(202).extract().path("agentRuntimeId");
|
||||
String endpointArn = given().contentType("application/json")
|
||||
.body("{\"name\":\"gone\",\"tags\":{\"env\":\"prod\"}}")
|
||||
.when().put("/runtimes/" + id + "/runtime-endpoints/")
|
||||
.then().statusCode(202).extract().path("agentRuntimeEndpointArn");
|
||||
try {
|
||||
for (String foreign : new String[] {endpointArn.replace(":000000000000:", ":222222222222:"),
|
||||
endpointArn.replace(":us-east-1:", ":eu-west-1:"),
|
||||
endpointArn.replace("arn:aws:", "arn:aws-cn:")}) {
|
||||
given().when().get("/tags/" + foreign)
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
given().contentType("application/json").body("{\"tags\":{\"env\":\"stolen\"}}")
|
||||
.when().post("/tags/" + foreign).then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
given().when().delete("/tags/" + foreign + "?tagKeys=env")
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
given().when().get("/tags/" + endpointArn).then().statusCode(200).body("tags.env", equalTo("prod"));
|
||||
|
||||
given().when().delete("/runtimes/" + id + "/runtime-endpoints/gone/").then().statusCode(202);
|
||||
given().when().get("/tags/" + endpointArn)
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
given().contentType("application/json").body("{\"tags\":{\"a\":\"b\"}}")
|
||||
.when().post("/tags/" + endpointArn).then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
given().when().delete("/tags/" + endpointArn + "?tagKeys=a")
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
|
||||
String missingRuntimeArn = endpointArn.replaceAll("agentEndpoint/.*", "agent/missing0000-AbCdEfGhIj");
|
||||
String existingRuntimeArn = endpointArn.replaceAll("agentEndpoint/.*", "agent/" + id);
|
||||
for (String runtimeTarget : new String[] {missingRuntimeArn,
|
||||
existingRuntimeArn.replace(":000000000000:", ":222222222222:"),
|
||||
existingRuntimeArn.replace(":us-east-1:", ":eu-west-1:"),
|
||||
existingRuntimeArn.replace("arn:aws:", "arn:aws-cn:")}) {
|
||||
given().when().get("/tags/" + runtimeTarget)
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
given().contentType("application/json").body("{\"tags\":{\"a\":\"b\"}}")
|
||||
.when().post("/tags/" + runtimeTarget).then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
given().when().delete("/tags/" + runtimeTarget + "?tagKeys=a")
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
} finally {
|
||||
given().when().delete("/runtimes/" + id + "/").then().statusCode(202);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEndpointRejectsInvalidTags() {
|
||||
String id = given().contentType("application/json").body(CREATE_RUNTIME.replace("epAgent", "epTagAgent3"))
|
||||
.when().put("/runtimes/").then().statusCode(202).extract().path("agentRuntimeId");
|
||||
try {
|
||||
given().contentType("application/json").body("{\"name\":\"bad\",\"tags\":{\"k\":\"v!\"}}")
|
||||
.when().put("/runtimes/" + id + "/runtime-endpoints/").then().statusCode(400);
|
||||
given().contentType("application/json").body("{\"name\":\"bad\",\"tags\":{\"\":\"v\"}}")
|
||||
.when().put("/runtimes/" + id + "/runtime-endpoints/").then().statusCode(400);
|
||||
given().contentType("application/json")
|
||||
.when().get("/runtimes/" + id + "/runtime-endpoints/bad/")
|
||||
.then().statusCode(404);
|
||||
} finally {
|
||||
given().when().delete("/runtimes/" + id + "/").then().statusCode(202);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -54,7 +54,7 @@ class BedrockAgentCorePersistenceTest {
|
||||
String id = created.getAgentRuntimeId();
|
||||
svc.updateAgentRuntime(id, artifact, network,
|
||||
"arn:aws:iam::000000000000:role/agent", "v2", Map.of("K", "V"), authorizer, null, REGION);
|
||||
svc.createEndpoint(id, "prod", "1", "prod ep", null, REGION);
|
||||
svc.createEndpoint(id, "prod", "1", "prod ep", Map.of("stage", "live"), null, REGION);
|
||||
AgentRuntime before = svc.getAgentRuntime(id, REGION);
|
||||
svc.tagByArn(REGION, svc.arn(before, "1", REGION), Map.of("env", "prod"));
|
||||
before = svc.getAgentRuntime(id, REGION);
|
||||
@@ -86,6 +86,8 @@ class BedrockAgentCorePersistenceTest {
|
||||
assertEquals("prod", loaded.getTags().get("env"));
|
||||
assertEquals(2, loaded.getEndpoints().size());
|
||||
assertTrue(loaded.getEndpoints().stream().anyMatch(e -> "prod".equals(e.getName())));
|
||||
assertEquals(Map.of("stage", "live"), loaded.getEndpoints().stream()
|
||||
.filter(e -> "prod".equals(e.getName())).findFirst().orElseThrow().getTags());
|
||||
assertTrue(loaded.getEndpoints().stream().anyMatch(e -> "DEFAULT".equals(e.getName())));
|
||||
}
|
||||
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class BedrockAgentCoreTagHandlerTest {
|
||||
|
||||
private static final String CUSTOM_REGION = "xx-custom-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
|
||||
private final BedrockAgentCoreControlService runtimeService = mock(BedrockAgentCoreControlService.class);
|
||||
private final BedrockAgentCoreTagHandler handler = new BedrockAgentCoreTagHandler(runtimeService,
|
||||
mock(BedrockAgentCoreGatewayService.class), mock(BedrockAgentCoreMemoryService.class),
|
||||
mock(BedrockAgentCoreHarnessRegistry.class),
|
||||
new RegionResolver(CUSTOM_REGION, ACCOUNT, "aws-cn"));
|
||||
|
||||
private static String endpointArn(String partition) {
|
||||
return "arn:" + partition + ":bedrock-agentcore:" + CUSTOM_REGION + ":" + ACCOUNT
|
||||
+ ":agentEndpoint/abc123";
|
||||
}
|
||||
|
||||
@Test
|
||||
void customRegionOnChinaDeploymentResolvesItsOwnArn() {
|
||||
String arn = endpointArn("aws-cn");
|
||||
when(runtimeService.getEndpointTagsByArn(CUSTOM_REGION, arn)).thenReturn(Map.of("env", "prod"));
|
||||
|
||||
assertEquals(Map.of("env", "prod"), handler.listTags(CUSTOM_REGION, arn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void customRegionRejectsAnArnOfAnotherPartition() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> handler.listTags(CUSTOM_REGION, endpointArn("aws")));
|
||||
|
||||
assertEquals("ResourceNotFoundException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user