fix(sagemaker): filter ListModels by CreationTimeAfter and CreationTimeBefore (#8766)

ListModels ignored CreationTimeAfter and CreationTimeBefore and returned every model. Both now filter before paging, beside NameContains: CreationTimeAfter keeps the models created at or after its time and CreationTimeBefore those created before it, as the ListModels reference describes, and a bound that is not a number is a SerializationException. The page token records both bounds, so a token reused with a changed bound is refused with a ValidationException, as ListTrainingJobs does for its filters.

Closes #7627
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-17 16:23:37 -04:00
1 parent 8eda666f82
commit 0e782b62f2
4 files changed
+158 -9

No files matched your search

@@ -0,0 +1,72 @@
package io.floci.aws.services.sagemaker;
import io.floci.aws.testing.AwsTestClients;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sagemaker.SageMakerClient;
import software.amazon.awssdk.services.sagemaker.model.ListModelsRequest;
import software.amazon.awssdk.services.sagemaker.model.ListModelsResponse;
import software.amazon.awssdk.services.sagemaker.model.ModelSummary;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** {@code ListModels} returns only the models created inside its {@code CreationTimeAfter}/{@code Before} window. */
@QuarkusTest
class SageMakerListModelsCreationTimeIntegrationTest {
private static final String ACCOUNT = "000000000000";
@Test
void creationTimeFiltersKeepOnlyTheModelsCreatedInTheirWindow() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
List<String> created = List.of("window-a-" + suffix, "window-b-" + suffix);
try (SageMakerClient sagemaker = AwsTestClients.create(SageMakerClient.builder(), ACCOUNT, Region.US_EAST_1)) {
try {
created.forEach(name -> createModel(sagemaker, name));
Instant createdAt = sagemaker.describeModel(r -> r.modelName(created.getFirst())).creationTime();
Instant before = createdAt.minus(Duration.ofHours(1));
Instant after = createdAt.plus(Duration.ofHours(1));
assertEquals(List.of(), names(sagemaker, suffix, r -> r.creationTimeBefore(Instant.EPOCH)));
assertEquals(List.of(), names(sagemaker, suffix, r -> r.creationTimeAfter(after)));
assertEquals(created, names(sagemaker, suffix, r -> r.creationTimeAfter(before)));
assertEquals(created, names(sagemaker, suffix, r -> r.creationTimeBefore(after)));
assertEquals(created, names(sagemaker, suffix,
r -> r.creationTimeAfter(before).creationTimeBefore(after)));
} finally {
created.forEach(name -> sagemaker.deleteModel(r -> r.modelName(name)));
}
}
}
/** The names of the models whose name holds {@code suffix}, by name, one page at a time. */
private static List<String> names(SageMakerClient sagemaker, String suffix,
Consumer<ListModelsRequest.Builder> filter) {
List<String> names = new ArrayList<>();
String token = null;
do {
String next = token;
ListModelsResponse page = sagemaker.listModels(r -> {
filter.accept(r.nameContains(suffix).sortBy("Name").sortOrder("Ascending").maxResults(1));
r.nextToken(next);
});
page.models().stream().map(ModelSummary::modelName).forEach(names::add);
token = page.nextToken();
} while (token != null);
return names;
}
private static void createModel(SageMakerClient sagemaker, String name) {
sagemaker.createModel(r -> r.modelName(name)
.executionRoleArn("arn:aws:iam::" + ACCOUNT + ":role/sagemaker")
.primaryContainer(c -> c.image("123456789012.dkr.ecr.us-east-1.amazonaws.com/model:latest")));
}
}
@@ -131,7 +131,7 @@ class SageMakerListSortServiceTest {
second.trainingJobStatus = "InProgress";
jobs.put(REGION + "::b-job", first);
jobs.put(REGION + "::a-job", second);
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), jobs,
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), jobs,
mapperMutatingBeforeRender(() -> first.trainingJobStatus = "Completed"));
ObjectNode request = mapper.createObjectNode()
.put("SortBy", "Status").put("SortOrder", "Descending").put("MaxResults", 1);
@@ -153,8 +153,8 @@ class SageMakerListSortServiceTest {
EndpointResource second = endpoint("a-endpoint");
endpoints.put(REGION + "::b-endpoint", first);
endpoints.put(REGION + "::a-endpoint", second);
SageMakerService service = service(new InMemoryStorage<>(), endpoints, new InMemoryStorage<>(),
mapperMutatingBeforeRender(() -> first.endpointStatus = "Creating"));
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), endpoints,
new InMemoryStorage<>(), mapperMutatingBeforeRender(() -> first.endpointStatus = "Creating"));
ObjectNode request = mapper.createObjectNode()
.put("SortBy", "Status").put("SortOrder", "Descending").put("MaxResults", 1);
@@ -311,6 +311,62 @@ class SageMakerListSortServiceTest {
assertEquals("ValidationException", changedStatus.getErrorCode());
}
@Test
void listModelsCursorRejectsChangedCreationTimeFilters() throws Exception {
SageMakerService service = modelService(model("older", 1000), model("newer", 3000));
JsonNode first = service.listModels(mapper.createObjectNode()
.put("CreationTimeAfter", 0.5).put("MaxResults", 1), REGION);
String cursor = first.path("NextToken").asText();
JsonNode sameFilter = service.listModels(mapper.createObjectNode()
.put("CreationTimeAfter", 0.5).put("NextToken", cursor), REGION);
assertEquals(List.of("older"), names(sameFilter, "Models", "ModelName"));
AwsException changedAfter = assertThrows(AwsException.class, () -> service.listModels(
mapper.createObjectNode().put("CreationTimeAfter", 0.6).put("NextToken", cursor), REGION));
assertEquals("ValidationException", changedAfter.getErrorCode());
AwsException addedBefore = assertThrows(AwsException.class, () -> service.listModels(
mapper.createObjectNode().put("CreationTimeAfter", 0.5).put("CreationTimeBefore", 9.0)
.put("NextToken", cursor), REGION));
assertEquals("ValidationException", addedBefore.getErrorCode());
}
@Test
void listModelsCreationTimeAfterIncludesAModelCreatedAtItsTimeAndBeforeExcludesIt() throws Exception {
SageMakerService service = modelService(model("exact", 1000));
JsonNode after = service.listModels(mapper.createObjectNode().put("CreationTimeAfter", 1.0), REGION);
JsonNode before = service.listModels(mapper.createObjectNode().put("CreationTimeBefore", 1.0), REGION);
assertEquals(List.of("exact"), names(after, "Models", "ModelName"));
assertEquals(List.of(), names(before, "Models", "ModelName"));
}
@Test
void listModelsRejectsACreationTimeThatIsNotATimestamp() throws Exception {
SageMakerService service = modelService(model("any", 1000));
AwsException e = assertThrows(AwsException.class, () -> service.listModels(
mapper.createObjectNode().put("CreationTimeBefore", "not-a-timestamp"), REGION));
assertEquals("SerializationException", e.getErrorCode());
}
private SageMakerService modelService(ModelResource... models) {
InMemoryStorage<String, ModelResource> store = new InMemoryStorage<>();
for (ModelResource m : models) {
store.put(REGION + "::" + m.modelName, m);
}
return service(store, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), mapper);
}
private static ModelResource model(String name, long creationTime) {
ModelResource m = new ModelResource();
m.modelName = name;
m.modelArn = "arn:aws:sagemaker:us-east-1:000000000000:model/" + name;
m.creationTime = creationTime;
return m;
}
@Test
void listRejectsUnmodeledSortValues() throws Exception {
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
@@ -355,14 +411,15 @@ class SageMakerListSortServiceTest {
private SageMakerService service(InMemoryStorage<String, EndpointConfigResource> configs,
InMemoryStorage<String, EndpointResource> endpoints,
InMemoryStorage<String, TrainingJobResource> jobs) {
return service(configs, endpoints, jobs, mapper);
return service(new InMemoryStorage<>(), configs, endpoints, jobs, mapper);
}
private SageMakerService service(InMemoryStorage<String, EndpointConfigResource> configs,
private SageMakerService service(InMemoryStorage<String, ModelResource> models,
InMemoryStorage<String, EndpointConfigResource> configs,
InMemoryStorage<String, EndpointResource> endpoints,
InMemoryStorage<String, TrainingJobResource> jobs,
ObjectMapper serviceMapper) {
return new SageMakerService(new InMemoryStorage<String, ModelResource>(), configs, endpoints, jobs,
return new SageMakerService(models, configs, endpoints, jobs,
new RegionResolver(REGION, "000000000000"), serviceMapper,
mock(SageMakerEndpointManager.class), mock(SageMakerTrainingRunner.class));
}