From 4353e4232b192f96d5239355008b166ca97bf1b9 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 16 Oct 2026 12:16:10 +0100 Subject: [PATCH] feat(rds): DescribeDBLogFiles and DownloadDBLogFilePortion (#5741) * feat(rds): DescribeDBLogFiles and DownloadDBLogFilePortion Both answered UnsupportedOperation, so an instance's log could only be read with docker logs, outside the AWS API. An instance's log is now read from its database container and cut into files the way RDS names them: hourly error/postgresql.log.* files for PostgreSQL, error/mysql-error.log for MySQL and MariaDB, and log/ERROR for SQL Server. DescribeDBLogFiles filters and pages them, and DownloadDBLogFilePortion follows the API reference's marker rules with a 1 MB block cap. Control characters XML cannot carry come back as U+FFFD. In mock RDS mode an instance has no log files. Closes #5733 * fix(rds): bound and validate log file reads A log read now takes the most recent 100,000 lines instead of the whole history, fails when Docker does not finish in time instead of returning part of the log, and, when Docker no longer knows the container, finds or starts the backend once before answering DBInstanceNotReady rather than an empty log. A single line over 1 MB is cut at 1 MB like the rest of the block. Negative FileSize, FileLastWritten and Marker values are InvalidParameterValue; the shared offset paging refuses a negative marker instead of starting over. * fix(rds): close the Docker log stream when a log read times out A timed-out read now closes its callback before failing, so the stream stops and nothing writes into the buffer after the read gives up; the buffer is also written and read under one lock. * refactor(rds): keep portion's Javadoc on portion truncateUtf8 had landed between portion's comment and portion itself. It now follows portion. * test(rds): read log files on the first call, and import class names CreateDBInstance returns once the database accepts connections, so the startup log is already in the container's output. The Docker and SDK log tests now assert on the first DescribeDBLogFiles and DownloadDBLogFilePortion call instead of polling. The timeout test and RdsLogFilesTest import the classes they had written out in full. --- .../com/floci/test/RdsControlPlaneTest.java | 30 +++ docs/services/index.md | 2 +- docs/services/rds.md | 22 ++ .../floci/core/common/AwsQueryController.java | 1 + .../floci/services/rds/RdsLogFiles.java | 197 ++++++++++++++++++ .../floci/services/rds/RdsQueryHandler.java | 51 +++++ .../floci/services/rds/RdsService.java | 78 ++++++- .../rds/container/RdsContainerManager.java | 54 +++++ .../rds/RdsLogFilesDockerIntegrationTest.java | 88 ++++++++ .../rds/RdsLogFilesIntegrationTest.java | 93 +++++++++ .../floci/services/rds/RdsLogFilesTest.java | 134 ++++++++++++ .../floci/services/rds/RdsServiceTest.java | 31 +++ .../container/RdsContainerManagerTest.java | 41 ++++ 13 files changed, 820 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/rds/RdsLogFiles.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesDockerIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesTest.java diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java index 33592de3a6..f649c1598c 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java @@ -34,14 +34,17 @@ import software.amazon.awssdk.services.rds.model.DbClusterRoleNotFoundException; import software.amazon.awssdk.services.rds.model.DbClusterSnapshotNotFoundException; import software.amazon.awssdk.services.rds.model.DbInstanceRoleAlreadyExistsException; import software.amazon.awssdk.services.rds.model.DbInstanceRoleNotFoundException; +import software.amazon.awssdk.services.rds.model.DbLogFileNotFoundException; import software.amazon.awssdk.services.rds.model.DbSnapshotAlreadyExistsException; import software.amazon.awssdk.services.rds.model.DbSnapshotNotFoundException; +import software.amazon.awssdk.services.rds.model.DescribeDBLogFilesDetails; import software.amazon.awssdk.services.rds.model.DescribeDbProxiesResponse; import software.amazon.awssdk.services.rds.model.DescribeDbProxyTargetGroupsResponse; import software.amazon.awssdk.services.rds.model.DescribeDbProxyTargetsResponse; import software.amazon.awssdk.services.rds.model.DescribeDbSubnetGroupsResponse; import software.amazon.awssdk.services.rds.model.DescribeOptionGroupsResponse; import software.amazon.awssdk.services.rds.model.DescribeOrderableDbInstanceOptionsResponse; +import software.amazon.awssdk.services.rds.model.DownloadDbLogFilePortionResponse; import software.amazon.awssdk.services.rds.model.EngineDefaults; import software.amazon.awssdk.services.rds.model.InvalidDbInstanceStateException; import software.amazon.awssdk.services.rds.model.InvalidOptionGroupStateException; @@ -791,6 +794,33 @@ class RdsControlPlaneTest { } } + @Test + @DisplayName("Log files: list an instance's log files and download a block") + void sdkReadsInstanceLogFiles() { + String instanceName = TestFixtures.uniqueName("rds-log-files"); + try { + createDbInstance(rds, instanceName, "log-files-secret"); + List files = + rds.describeDBLogFiles(b -> b.dbInstanceIdentifier(instanceName)).describeDBLogFiles(); + assertThat(files).isNotEmpty(); + assertThat(files).allSatisfy(f -> { + assertThat(f.logFileName()).startsWith("error/postgresql.log."); + assertThat(f.size()).isPositive(); + assertThat(f.lastWritten()).isPositive(); + }); + String name = files.get(0).logFileName(); + DownloadDbLogFilePortionResponse portion = rds.downloadDBLogFilePortion(b -> b + .dbInstanceIdentifier(instanceName).logFileName(name).marker("0").numberOfLines(1)); + assertThat(portion.logFileData()).isNotEmpty(); + assertThat(portion.marker()).isEqualTo("1"); + assertThatThrownBy(() -> rds.downloadDBLogFilePortion(b -> b + .dbInstanceIdentifier(instanceName).logFileName("error/no-such.log"))) + .isInstanceOf(DbLogFileNotFoundException.class); + } finally { + deleteDbInstance(rds, instanceName); + } + } + @Test @DisplayName("IAM role associations: add, describe and remove for a cluster and an instance") void sdkRoundTripsIamRoleAssociations() { diff --git a/docs/services/index.md b/docs/services/index.md index ae7e0918ae..879ee3e9c1 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -63,7 +63,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [Amazon Data Lifecycle Manager](dlm.md) | `/policies`, `/policies/{policyId}`, `/tags/{resourceArn}` | REST JSON | 8 | | [ElastiCache](elasticache.md) | `POST /` with `Action=` param + TCP proxy | Query + RESP | 30 | | [MemoryDB](memorydb.md) | `POST /` + `X-Amz-Target: AmazonMemoryDB.*` + TCP proxy | JSON 1.1 + RESP | 13 | -| [RDS](rds.md) | `POST /` with `Action=` param + TCP proxy | Query + wire | 100 | +| [RDS](rds.md) | `POST /` with `Action=` param + TCP proxy | Query + wire | 102 | | [RDS Data API](rds-data.md) | `/Execute`, `/BeginTransaction`, `/CommitTransaction`, `/RollbackTransaction` | REST JSON | 4 | | [Timestream for InfluxDB](timestream-influxdb.md) | `POST /` + `X-Amz-Target: AmazonTimestreamInfluxDB.*` + InfluxDB container | JSON 1.0 + InfluxDB HTTP | 24 | | [MSK](msk.md) | `/v1/clusters/...`, `/api/v2/clusters/...` + Redpanda broker | REST JSON + Kafka | 8 | diff --git a/docs/services/rds.md b/docs/services/rds.md index 35fb26bbf3..91bd66a613 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -73,6 +73,8 @@ RDS Data API (`rds-data`) is documented separately because it uses REST JSON rou | `DeleteDBClusterEndpoint` | Delete a custom endpoint | | `EnableHttpEndpoint` | Turn on an Aurora cluster's Data API endpoint by ARN; see [Data API endpoint](#data-api-endpoint) | | `DisableHttpEndpoint` | Turn off a cluster's Data API endpoint by ARN | +| `DescribeDBLogFiles` | List an instance's log files, read from its database container; see [Log files](#log-files) | +| `DownloadDBLogFilePortion` | Read a block of a log file, from the start or the most recent lines | | `AddRoleToDBCluster` | Associate an IAM role with a cluster, optionally for a feature; see [IAM role associations](#iam-role-associations) | | `RemoveRoleFromDBCluster` | Remove a role from a cluster | | `AddRoleToDBInstance` | Associate an IAM role with an instance for a feature | @@ -631,6 +633,26 @@ it off is always accepted. An ARN that names no cluster in the caller's account `ResourceNotFoundFault`, and a cluster that is not available is `InvalidResourceStateFault`. The flag is recorded and reported only: the Data API (`rds-data`) does not refuse a cluster with it off. +## Log files + +`DescribeDBLogFiles` and `DownloadDBLogFilePortion` read an instance's log from its database +container (`docker logs`, the most recent 100,000 lines), cut into files the way RDS names them: PostgreSQL rotates hourly into +`error/postgresql.log.YYYY-MM-DD-HH` (UTC), MySQL and MariaDB keep `error/mysql-error.log`, and SQL +Server keeps `log/ERROR`. An Aurora member reads its cluster's container. `LastWritten` is the time +Docker recorded for the file's last line, and `Size` the bytes of its lines. + +`DescribeDBLogFiles` takes `FilenameContains`, `FileLastWritten` (files written at or after it) and +`FileSize` (files larger than it), and pages by `MaxRecords` / `Marker`. `DownloadDBLogFilePortion` +follows the API reference: without a `Marker` it returns the most recent `NumberOfLines` lines (the +whole file, up to 10000 lines, without either), `Marker` `0` starts at the first line, and the +returned `Marker` continues until `AdditionalDataPending` is false. A block stops at 1 MB, and a +single line longer than that is cut at 1 MB, as the API reference says the block is truncated. +Control characters XML cannot carry (a terminal colour code) come back as U+FFFD. + +An instance that is not available, or whose database container cannot be found or started, is +`DBInstanceNotReady`, and an unknown file name `DBLogFileNotFoundFault`. Negative `FileSize`, +`FileLastWritten` or `Marker` values are `InvalidParameterValue`. In mock RDS mode there is no container, so an instance has no log files. + ## IAM role associations `AddRoleToDBCluster` and `AddRoleToDBInstance` associate an IAM role with an available cluster or diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java index 3458ddbbd2..e951a6af0d 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java @@ -567,6 +567,7 @@ public class AwsQueryController { "CreateDBCluster", "DescribeDBClusters", "DeleteDBCluster", "ModifyDBCluster", "AddRoleToDBCluster", "RemoveRoleFromDBCluster", "AddRoleToDBInstance", "RemoveRoleFromDBInstance", "EnableHttpEndpoint", "DisableHttpEndpoint", + "DescribeDBLogFiles", "DownloadDBLogFilePortion", "DescribeGlobalClusters", "CreateDBParameterGroup", "DescribeDBParameterGroups", "DeleteDBParameterGroup", "ModifyDBParameterGroup", "DescribeDBParameters", diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsLogFiles.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsLogFiles.java new file mode 100644 index 0000000000..0118bc4e3a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsLogFiles.java @@ -0,0 +1,197 @@ +package io.github.hectorvent.floci.services.rds; + +import io.github.hectorvent.floci.core.common.AwsException; + +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.TreeMap; + +/** + * An instance's log files, cut from its database container's log as RDS names and rotates them, + * and the paging {@code DownloadDBLogFilePortion} applies to one file. + */ +final class RdsLogFiles { + + /** One line of the container's log, with the time Docker recorded for it. */ + record LogLine(Instant time, String text) {} + + /** A log file: its name, its last line's time, its size in bytes and its lines in order. */ + record LogFile(String name, Instant lastWritten, long size, List lines) {} + + /** What one {@code DownloadDBLogFilePortion} call returns. */ + record Portion(String data, String marker, boolean additionalDataPending) {} + + /** The most lines a download returns, and its size cap, as the API reference gives them. */ + static final int MAX_LINES = 10_000; + static final int MAX_BYTES = 1024 * 1024; + + private static final DateTimeFormatter HOUR = + DateTimeFormatter.ofPattern("yyyy-MM-dd-HH").withZone(ZoneOffset.UTC); + + private RdsLogFiles() { + } + + /** + * Docker's timestamped lines as {@link LogLine}s. A line whose timestamp does not parse (a + * continuation Docker split) takes the previous line's time. + */ + static List parseDockerLines(List raw) { + List lines = new ArrayList<>(raw.size()); + Instant last = Instant.EPOCH; + for (String line : raw) { + int space = line.indexOf(' '); + Instant time = null; + if (space > 0) { + try { + time = Instant.parse(line.substring(0, space)); + } catch (DateTimeParseException ignored) { + // not a timestamp: the whole line is text + } + } + if (time != null) { + last = time; + lines.add(new LogLine(time, xmlSafe(line.substring(space + 1)))); + } else { + lines.add(new LogLine(last, xmlSafe(line))); + } + } + return lines; + } + + /** + * Replaces the control characters XML 1.0 cannot carry (a terminal colour code, a stray NUL) + * with U+FFFD, so {@code LogFileData} always parses; tabs stay. + */ + static String xmlSafe(String text) { + StringBuilder out = null; + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); + boolean bad = (c < 0x20 && c != '\t') || c == 0xFFFE || c == 0xFFFF; + if (bad && out == null) { + out = new StringBuilder(text.length()); + out.append(text, 0, i); + } + if (out != null) { + out.append(bad ? '\uFFFD' : c); + } + } + return out != null ? out.toString() : text; + } + + /** + * The files an engine's log is cut into. PostgreSQL rotates hourly into + * {@code error/postgresql.log.YYYY-MM-DD-HH} (UTC); MySQL and MariaDB keep + * {@code error/mysql-error.log}; SQL Server keeps {@code log/ERROR}. + */ + static List files(String engine, List lines) { + Map> byName = new TreeMap<>(); + for (LogLine line : lines) { + byName.computeIfAbsent(fileName(engine, line.time()), name -> new ArrayList<>()).add(line); + } + List files = new ArrayList<>(); + byName.forEach((name, fileLines) -> { + long size = 0; + List texts = new ArrayList<>(fileLines.size()); + for (LogLine line : fileLines) { + texts.add(line.text()); + size += line.text().getBytes(StandardCharsets.UTF_8).length + 1; + } + files.add(new LogFile(name, fileLines.get(fileLines.size() - 1).time(), size, List.copyOf(texts))); + }); + return files; + } + + private static String fileName(String engine, Instant time) { + String name = engine == null ? "postgres" : engine.toLowerCase(Locale.ROOT); + if (name.contains("postgres")) { + return "error/postgresql.log." + HOUR.format(time); + } + if (name.startsWith("sqlserver")) { + return "log/ERROR"; + } + return "error/mysql-error.log"; + } + + /** + * The files {@code DescribeDBLogFiles} lists: names containing {@code filenameContains}, written + * at or after {@code fileLastWritten} (epoch milliseconds), larger than {@code fileSize} bytes. + */ + static List matching(List files, String filenameContains, Long fileLastWritten, Long fileSize) { + return files.stream() + .filter(f -> filenameContains == null || filenameContains.isEmpty() + || f.name().contains(filenameContains)) + .filter(f -> fileLastWritten == null || f.lastWritten().toEpochMilli() >= fileLastWritten) + .filter(f -> fileSize == null || f.size() > fileSize) + .toList(); + } + + /** + * One block of a file, by the API reference's rules. Without a marker the most recent lines + * come back (all of them, up to {@link #MAX_LINES}, without {@code numberOfLines}); marker + * {@code 0} starts at the first line, and any other marker is the line to continue from, as a + * previous call returned it. A block stops at 1 MB. + */ + static Portion portion(LogFile file, String marker, Integer numberOfLines) { + if (numberOfLines != null && numberOfLines < 1) { + throw new AwsException("InvalidParameterValue", "NumberOfLines must be greater than 0.", 400); + } + int wanted = numberOfLines == null ? MAX_LINES : Math.min(numberOfLines, MAX_LINES); + List lines = file.lines(); + int start; + if (marker == null || marker.isEmpty()) { + start = Math.max(0, lines.size() - wanted); + } else { + try { + start = Integer.parseInt(marker); + } catch (NumberFormatException e) { + throw new AwsException("InvalidParameterValue", "Invalid marker: " + marker, 400); + } + if (start < 0) { + throw new AwsException("InvalidParameterValue", "Invalid marker: " + marker, 400); + } + start = Math.min(start, lines.size()); + } + StringBuilder data = new StringBuilder(); + int bytes = 0; + int end = start; + while (end < lines.size() && end - start < wanted) { + String line = lines.get(end) + "\n"; + int lineBytes = line.getBytes(StandardCharsets.UTF_8).length; + if (end > start && bytes + lineBytes > MAX_BYTES) { + break; + } + if (lineBytes > MAX_BYTES) { + // A single line over 1 MB is cut there, as the API reference says the block is. + line = truncateUtf8(lines.get(end), MAX_BYTES - 1) + "\n"; + lineBytes = line.getBytes(StandardCharsets.UTF_8).length; + } + data.append(line); + bytes += lineBytes; + end++; + } + return new Portion(data.toString(), Integer.toString(end), end < lines.size()); + } + + /** The longest prefix of {@code text} that is at most {@code maxBytes} in UTF-8, cut between characters. */ + static String truncateUtf8(String text, int maxBytes) { + int bytes = 0; + int i = 0; + while (i < text.length()) { + int codePoint = text.codePointAt(i); + int size = codePoint < 0x80 ? 1 : codePoint < 0x800 ? 2 : codePoint < 0x10000 ? 3 : 4; + if (bytes + size > maxBytes) { + break; + } + bytes += size; + i += Character.charCount(codePoint); + } + return text.substring(0, i); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java index 27a8aba9e7..a857be2cb2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java @@ -115,6 +115,8 @@ public class RdsQueryHandler { case "DeleteDBClusterEndpoint" -> handleDeleteDbClusterEndpoint(params, region); case "EnableHttpEndpoint" -> handleSetHttpEndpoint("EnableHttpEndpoint", params, region, true); case "DisableHttpEndpoint" -> handleSetHttpEndpoint("DisableHttpEndpoint", params, region, false); + case "DescribeDBLogFiles" -> handleDescribeDbLogFiles(params, region); + case "DownloadDBLogFilePortion" -> handleDownloadDbLogFilePortion(params, region); case "AddRoleToDBCluster" -> handleAddRoleToDbCluster(params, region); case "RemoveRoleFromDBCluster" -> handleRemoveRoleFromDbCluster(params, region); case "AddRoleToDBInstance" -> handleAddRoleToDbInstance(params, region); @@ -380,6 +382,55 @@ public class RdsQueryHandler { return Response.ok(AwsQueryResponse.envelope(action, AwsNamespaces.RDS, result)).build(); } + private Response handleDescribeDbLogFiles(MultivaluedMap params, String region) { + RdsService.LogFilePage page = service.describeDbLogFiles(region, + params.getFirst("DBInstanceIdentifier"), + params.getFirst("FilenameContains"), + optionalLong(params, "FileLastWritten"), + optionalLong(params, "FileSize"), + optionalInt(params.getFirst("MaxRecords")), + params.getFirst("Marker")); + XmlBuilder xml = new XmlBuilder().start("DescribeDBLogFiles"); + for (RdsLogFiles.LogFile file : page.files()) { + xml.start("DescribeDBLogFilesDetails") + .elem("LogFileName", file.name()) + .elem("LastWritten", file.lastWritten().toEpochMilli()) + .elem("Size", file.size()) + .end("DescribeDBLogFilesDetails"); + } + xml.end("DescribeDBLogFiles"); + if (page.marker() != null) { + xml.elem("Marker", page.marker()); + } + return Response.ok(AwsQueryResponse.envelope("DescribeDBLogFiles", AwsNamespaces.RDS, xml.build())).build(); + } + + private Response handleDownloadDbLogFilePortion(MultivaluedMap params, String region) { + RdsLogFiles.Portion portion = service.downloadDbLogFilePortion(region, + params.getFirst("DBInstanceIdentifier"), + params.getFirst("LogFileName"), + params.getFirst("Marker"), + optionalInt(params.getFirst("NumberOfLines"))); + String result = new XmlBuilder() + .elem("LogFileData", portion.data()) + .elem("Marker", portion.marker()) + .elem("AdditionalDataPending", portion.additionalDataPending()) + .build(); + return Response.ok(AwsQueryResponse.envelope("DownloadDBLogFilePortion", AwsNamespaces.RDS, result)).build(); + } + + private static Long optionalLong(MultivaluedMap params, String name) { + String value = params.getFirst(name); + if (value == null || value.isBlank()) { + return null; + } + try { + return Long.parseLong(value.trim()); + } catch (NumberFormatException e) { + throw new AwsException("InvalidParameterValue", name + " must be a number: " + value, 400); + } + } + private Response handleAddRoleToDbCluster(MultivaluedMap params, String region) { service.addRoleToDbCluster(region, params.getFirst("DBClusterIdentifier"), params.getFirst("RoleArn"), params.getFirst("FeatureName")); diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index 38b0c7f6ce..843d7063c8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -9550,9 +9550,12 @@ public class RdsService implements Resettable, ResourceProvider { } catch (NumberFormatException e) { throw new AwsException("InvalidParameterValue", "Invalid marker: " + marker, 400); } + if (from < 0) { + throw new AwsException("InvalidParameterValue", "Invalid marker: " + marker, 400); + } } int limit = maxRecords == null ? DEFAULT_MAX_RECORDS : maxRecords; - int start = Math.min(Math.max(from, 0), matching.size()); + int start = Math.min(from, matching.size()); int to = Math.min(matching.size(), start + limit); return new OffsetPage<>(matching.subList(start, to), to < matching.size() ? Integer.toString(to) : null); } @@ -9826,6 +9829,79 @@ public class RdsService implements Resettable, ResourceProvider { return cluster; } + // ── Log files ───────────────────────────────────────────────────────────── + + /** One page of an instance's log files, plus the marker to continue from. */ + public record LogFilePage(List files, String marker) {} + + /** + * The instance's log files, read from its database container's log and named as RDS names them. + * An instance without a container (mock RDS) has none. + */ + public LogFilePage describeDbLogFiles(String region, String instanceId, String filenameContains, + Long fileLastWritten, Long fileSize, Integer maxRecords, String marker) { + requireParameter(instanceId, "DBInstanceIdentifier"); + requireValidMaxRecords(maxRecords); + if (fileLastWritten != null && fileLastWritten < 0) { + throw new AwsException("InvalidParameterValue", "FileLastWritten must not be negative.", 400); + } + if (fileSize != null && fileSize < 0) { + throw new AwsException("InvalidParameterValue", "FileSize must not be negative.", 400); + } + List files = RdsLogFiles.matching(logFiles(region, instanceId), + filenameContains, fileLastWritten, fileSize); + OffsetPage page = offsetPage(files, maxRecords, marker); + return new LogFilePage(page.items(), page.marker()); + } + + /** One block of a log file; see {@link RdsLogFiles#portion}. */ + public RdsLogFiles.Portion downloadDbLogFilePortion(String region, String instanceId, String logFileName, + String marker, Integer numberOfLines) { + requireParameter(instanceId, "DBInstanceIdentifier"); + requireParameter(logFileName, "LogFileName"); + RdsLogFiles.LogFile file = logFiles(region, instanceId).stream() + .filter(f -> f.name().equals(logFileName)) + .findFirst() + .orElseThrow(() -> new AwsException("DBLogFileNotFoundFault", + "DBLogFile " + logFileName + " not found for DB instance " + instanceId + ".", 404)); + return RdsLogFiles.portion(file, marker, numberOfLines); + } + + private List logFiles(String region, String instanceId) { + String effectiveRegion = effectiveRegion(region); + DbInstance instance = getDbInstance(instanceId, effectiveRegion); + if (instance.getStatus() != null && instance.getStatus() != DbInstanceStatus.AVAILABLE) { + throw new AwsException("DBInstanceNotReady", + "DB instance " + instanceId + " is not in available state.", 400); + } + if (config.services().rds().mock()) { + return List.of(); + } + // Container ids are not persisted, and a container can be removed outside Floci: when there is + // no id, or Docker no longer knows it, the backend is found or started once before giving up. + String containerId = instance.getContainerId(); + List raw = containerId != null ? readContainerLog(containerId) : null; + if (raw == null) { + containerId = ensureInstanceBackend(instanceId, effectiveRegion).getContainerId(); + raw = containerId != null ? readContainerLog(containerId) : null; + } + if (raw == null) { + throw new AwsException("DBInstanceNotReady", + "DB instance " + instanceId + " has no running database container.", 400); + } + String engine = !isBlank(instance.getEngineIdentifier()) ? instance.getEngineIdentifier() + : instance.getEngine() != null ? instance.getEngine().name() : null; + return RdsLogFiles.files(engine, RdsLogFiles.parseDockerLines(raw)); + } + + private List readContainerLog(String containerId) { + try { + return containerManager.readLog(containerId); + } catch (IllegalStateException e) { + throw new AwsException("InternalFailure", e.getMessage(), 500); + } + } + // ── IAM role associations ───────────────────────────────────────────────── /** diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java index 4cba52b0ac..9ff2c130dc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java @@ -1,6 +1,9 @@ package io.github.hectorvent.floci.services.rds.container; +import com.github.dockerjava.api.async.ResultCallback; import com.github.dockerjava.api.command.InspectContainerResponse; +import com.github.dockerjava.api.exception.NotFoundException; +import com.github.dockerjava.api.model.Frame; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -885,6 +888,57 @@ public class RdsContainerManager implements RdsBackendGate, Resettable { throw new IllegalStateException("Timed out initializing " + description + " in " + containerName + ": " + lastOutput); } + /** The most recent lines {@link #readLog} reads, so one call never holds an unbounded history. */ + public static final int MAX_LOG_LINES = 100_000; + + /** + * The container's log as Docker keeps it, stdout and stderr, at most the last + * {@link #MAX_LOG_LINES} lines, each with Docker's RFC 3339 timestamp in front. Null when Docker + * no longer knows the container, so the caller can tell a missing backend from an empty log. A + * read that does not finish in time fails rather than returning part of the log. + */ + public List readLog(String containerId) { + return readLog(containerId, Duration.ofSeconds(10)); + } + + List readLog(String containerId, Duration timeout) { + ByteArrayOutputStream raw = new ByteArrayOutputStream(); + ResultCallback.Adapter callback = new ResultCallback.Adapter<>() { + @Override + public void onNext(Frame frame) { + synchronized (raw) { + raw.writeBytes(frame.getPayload()); + } + } + }; + try (callback) { + boolean completed = lifecycleManager.getDockerClient().logContainerCmd(containerId) + .withStdOut(true).withStdErr(true).withTimestamps(true).withTail(MAX_LOG_LINES) + .exec(callback) + .awaitCompletion(timeout.toMillis(), TimeUnit.MILLISECONDS); + if (!completed) { + // Closing the callback (try-with-resources) stops the stream, so nothing writes into + // the buffer after this read gives up. + throw new IllegalStateException("Timed out reading the log of container " + containerId); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted while reading the log of container " + containerId, e); + } catch (NotFoundException e) { + return null; + } catch (IOException e) { + throw new IllegalStateException("Could not close the log stream of container " + containerId, e); + } + String text; + synchronized (raw) { + text = raw.toString(StandardCharsets.UTF_8); + } + if (text.isEmpty()) { + return List.of(); + } + return List.of(text.split("\r?\n")); + } + public String createPostgresSnapshot(String containerId, String masterUsername) { String effectiveUser = (masterUsername != null && !masterUsername.isBlank()) ? masterUsername : "postgres"; String[] cmd = { diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesDockerIntegrationTest.java new file mode 100644 index 0000000000..4271eba47f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesDockerIntegrationTest.java @@ -0,0 +1,88 @@ +package io.github.hectorvent.floci.services.rds; + +import com.github.dockerjava.api.DockerClient; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.xml.XmlPath; +import io.restassured.specification.RequestSpecification; +import jakarta.inject.Inject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static io.restassured.http.ContentType.URLENC; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** + * A real PostgreSQL instance's log, read through {@code DescribeDBLogFiles} and + * {@code DownloadDBLogFilePortion}: the container's startup line lands in an hourly + * {@code error/postgresql.log.*} file. Skipped without Docker. + */ +@QuarkusTest +@Tag("docker") +class RdsLogFilesDockerIntegrationTest { + + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=test/20261015/us-east-1/rds/aws4_request, " + + "SignedHeaders=content-type;host, Signature=test"; + + @Inject + DockerClient dockerClient; + + @BeforeEach + void checkDocker() { + try { + dockerClient.pingCmd().exec(); + } catch (Exception e) { + assumeTrue(false, "Docker is not available: " + e.getMessage()); + } + } + + private static RequestSpecification query(String action) { + return given().header("Authorization", AUTH) + .contentType(URLENC) + .formParam("Action", action) + .formParam("Version", "2014-10-31"); + } + + @Test + void aPostgresInstancesLogIsListedAndDownloaded() { + String id = "log-files-pg-" + Long.toString(System.nanoTime(), 36); + try { + query("CreateDBInstance") + .formParam("DBInstanceIdentifier", id) + .formParam("Engine", "postgres") + .formParam("DBInstanceClass", "db.t3.micro") + .formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "password123") + .formParam("AllocatedStorage", "20") + .when().post("/").then().statusCode(200); + + // CreateDBInstance returns once the database accepts connections, so its startup log is + // already in the container's output: the first call must see it. + List names = query("DescribeDBLogFiles").formParam("DBInstanceIdentifier", id) + .when().post("/").then().statusCode(200).extract().xmlPath() + .getList("**.findAll { it.name() == 'LogFileName' }", String.class); + assertFalse(names.isEmpty(), "the instance should have a log file"); + assertTrue(names.stream().allMatch(n -> n.matches("error/postgresql\\.log\\.\\d{4}-\\d{2}-\\d{2}-\\d{2}")), + names.toString()); + + StringBuilder all = new StringBuilder(); + for (String name : names) { + all.append(query("DownloadDBLogFilePortion").formParam("DBInstanceIdentifier", id) + .formParam("LogFileName", name).formParam("Marker", "0") + .when().post("/").then().statusCode(200).extract().xmlPath() + .getString("DownloadDBLogFilePortionResponse.DownloadDBLogFilePortionResult.LogFileData")); + } + String data = all.toString(); + assertTrue(data.contains("ready to accept connections"), data); + } finally { + query("DeleteDBInstance").formParam("DBInstanceIdentifier", id).formParam("SkipFinalSnapshot", "true") + .when().post("/"); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesIntegrationTest.java new file mode 100644 index 0000000000..48ce5da7c0 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesIntegrationTest.java @@ -0,0 +1,93 @@ +package io.github.hectorvent.floci.services.rds; + +import io.github.hectorvent.floci.testing.RdsMockProfile; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static io.restassured.http.ContentType.URLENC; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +/** + * {@code DescribeDBLogFiles} and {@code DownloadDBLogFilePortion} over the Query protocol in mock + * RDS mode, where an instance has no container and so no log files, and the errors the API + * reference lists. {@code RdsLogFilesDockerIntegrationTest} reads a real container's log. + */ +@QuarkusTest +@TestProfile(RdsMockProfile.class) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class RdsLogFilesIntegrationTest { + + private static final String INSTANCE = "log-files-db"; + + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=test/20261015/us-east-1/rds/aws4_request, " + + "SignedHeaders=content-type;host, Signature=test"; + + private static RequestSpecification query(String action) { + return given().header("Authorization", AUTH) + .contentType(URLENC) + .formParam("Action", action) + .formParam("Version", "2014-10-31"); + } + + @Test + @Order(1) + void anInstanceWithoutAContainerHasNoLogFiles() { + query("CreateDBInstance") + .formParam("DBInstanceIdentifier", INSTANCE) + .formParam("Engine", "postgres") + .formParam("DBInstanceClass", "db.t3.micro") + .formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "password123") + .formParam("AllocatedStorage", "20") + .when().post("/").then().statusCode(200); + + query("DescribeDBLogFiles").formParam("DBInstanceIdentifier", INSTANCE) + .when().post("/").then().statusCode(200) + .body(containsString("")); + query("DownloadDBLogFilePortion").formParam("DBInstanceIdentifier", INSTANCE) + .formParam("LogFileName", "error/postgresql.log.2026-10-15-10") + .when().post("/").then().statusCode(404) + .body("ErrorResponse.Error.Code", equalTo("DBLogFileNotFoundFault")); + } + + @Test + @Order(2) + void theApiReferencesErrors() { + query("DescribeDBLogFiles").formParam("DBInstanceIdentifier", "no-such-db") + .when().post("/").then().statusCode(404) + .body("ErrorResponse.Error.Code", equalTo("DBInstanceNotFound")); + query("DescribeDBLogFiles") + .when().post("/").then().statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("MissingParameter")); + query("DownloadDBLogFilePortion").formParam("DBInstanceIdentifier", INSTANCE) + .when().post("/").then().statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("MissingParameter")); + query("DescribeDBLogFiles").formParam("DBInstanceIdentifier", INSTANCE).formParam("MaxRecords", "5") + .when().post("/").then().statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue")); + query("DescribeDBLogFiles").formParam("DBInstanceIdentifier", INSTANCE).formParam("FileSize", "big") + .when().post("/").then().statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue")); + for (String[] bad : new String[][] {{"FileSize", "-1"}, {"FileLastWritten", "-1"}, {"Marker", "-1"}}) { + query("DescribeDBLogFiles").formParam("DBInstanceIdentifier", INSTANCE).formParam(bad[0], bad[1]) + .when().post("/").then().statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue")); + } + + query("StopDBInstance").formParam("DBInstanceIdentifier", INSTANCE) + .when().post("/").then().statusCode(200); + query("DescribeDBLogFiles").formParam("DBInstanceIdentifier", INSTANCE) + .when().post("/").then().statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("DBInstanceNotReady")); + query("StartDBInstance").formParam("DBInstanceIdentifier", INSTANCE) + .when().post("/").then().statusCode(200); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesTest.java new file mode 100644 index 0000000000..0a9d64966f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsLogFilesTest.java @@ -0,0 +1,134 @@ +package io.github.hectorvent.floci.services.rds; + +import io.github.hectorvent.floci.core.common.AwsException; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class RdsLogFilesTest { + + private static List docker(String... lines) { + return List.of(lines); + } + + @Test + void postgresRotatesHourlyAndMysqlKeepsOneErrorLog() { + List lines = RdsLogFiles.parseDockerLines(docker( + "2026-10-15T09:59:59.000000001Z LOG: database system is ready", + "2026-10-15T10:00:00.5Z LOG: checkpoint starting", + "2026-10-15T10:30:00Z ERROR: relation \"t\" does not exist")); + + List postgres = RdsLogFiles.files("postgres", lines); + assertEquals(List.of("error/postgresql.log.2026-10-15-09", "error/postgresql.log.2026-10-15-10"), + postgres.stream().map(RdsLogFiles.LogFile::name).toList()); + RdsLogFiles.LogFile tenOClock = postgres.get(1); + assertEquals(Instant.parse("2026-10-15T10:30:00Z"), tenOClock.lastWritten()); + assertEquals(List.of("LOG: checkpoint starting", "ERROR: relation \"t\" does not exist"), tenOClock.lines()); + assertEquals("LOG: checkpoint starting\n".length() + "ERROR: relation \"t\" does not exist\n".length(), + tenOClock.size()); + + assertEquals(List.of("error/mysql-error.log"), + RdsLogFiles.files("mariadb", lines).stream().map(RdsLogFiles.LogFile::name).toList()); + assertEquals(List.of("log/ERROR"), + RdsLogFiles.files("sqlserver-se", lines).stream().map(RdsLogFiles.LogFile::name).toList()); + assertEquals(2, RdsLogFiles.files("aurora-postgresql", lines).size()); + } + + @Test + void aLineWithoutATimestampTakesThePreviousOnesTimeAndControlCharactersAreReplaced() { + List lines = RdsLogFiles.parseDockerLines(docker( + "2026-10-15T10:00:00Z first", + "continued\u001b[0m\ttab")); + assertEquals(Instant.parse("2026-10-15T10:00:00Z"), lines.get(1).time()); + assertEquals("continued�[0m\ttab", lines.get(1).text()); + } + + @Test + void describeFiltersByNameTimeAndSize() { + List files = RdsLogFiles.files("postgres", RdsLogFiles.parseDockerLines(docker( + "2026-10-15T09:00:00Z a", + "2026-10-15T10:00:00Z bbbbbbbbbb"))); + assertEquals(1, RdsLogFiles.matching(files, "-09", null, null).size()); + long tenOClock = Instant.parse("2026-10-15T10:00:00Z").toEpochMilli(); + assertEquals("error/postgresql.log.2026-10-15-10", + RdsLogFiles.matching(files, null, tenOClock, null).get(0).name()); + // Larger than the size given: "a\n" is 2 bytes, "bbbbbbbbbb\n" 11. + assertEquals(1, RdsLogFiles.matching(files, null, null, 2L).size()); + assertEquals(2, RdsLogFiles.matching(files, "", null, null).size()); + } + + private static RdsLogFiles.LogFile fileOf(int lines) { + List texts = new ArrayList<>(); + for (int i = 0; i < lines; i++) { + texts.add("line " + i); + } + return new RdsLogFiles.LogFile("error/mysql-error.log", Instant.EPOCH, 0, texts); + } + + @Test + void downloadFollowsTheApiReferencesMarkerRules() { + RdsLogFiles.LogFile file = fileOf(5); + + // No marker: the most recent lines. + RdsLogFiles.Portion tail = RdsLogFiles.portion(file, null, 2); + assertEquals("line 3\nline 4\n", tail.data()); + assertFalse(tail.additionalDataPending()); + assertEquals("5", tail.marker()); + + // Marker "0": from the start, in blocks, until nothing is pending. + RdsLogFiles.Portion first = RdsLogFiles.portion(file, "0", 2); + assertEquals("line 0\nline 1\n", first.data()); + assertTrue(first.additionalDataPending()); + RdsLogFiles.Portion second = RdsLogFiles.portion(file, first.marker(), 2); + assertEquals("line 2\nline 3\n", second.data()); + RdsLogFiles.Portion last = RdsLogFiles.portion(file, second.marker(), 2); + assertEquals("line 4\n", last.data()); + assertFalse(last.additionalDataPending()); + + // Neither: the whole file (up to 10000 lines). + assertEquals(5, RdsLogFiles.portion(file, null, null).data().split("\n").length); + // A marker past the end is an empty block. + assertEquals("", RdsLogFiles.portion(file, "99", 2).data()); + + assertThrows(AwsException.class, () -> RdsLogFiles.portion(file, "next", 2)); + assertThrows(AwsException.class, () -> RdsLogFiles.portion(file, "-1", 2)); + assertThrows(AwsException.class, () -> RdsLogFiles.portion(file, "0", 0)); + } + + @Test + void aBlockStopsAtOneMegabyteAndAtTenThousandLines() { + List big = new ArrayList<>(); + String kb = "x".repeat(1023); + for (int i = 0; i < 2000; i++) { + big.add(kb); + } + RdsLogFiles.LogFile file = new RdsLogFiles.LogFile("log/ERROR", Instant.EPOCH, 0, big); + RdsLogFiles.Portion portion = RdsLogFiles.portion(file, "0", 5000); + assertEquals(1024, portion.data().split("\n").length); + assertTrue(portion.additionalDataPending()); + assertEquals("1024", portion.marker()); + + assertEquals("10000", RdsLogFiles.portion(fileOf(12_000), "0", 50_000).marker()); + } + + @Test + void aSingleLineOverOneMegabyteIsCutAtOneMegabyte() { + String huge = "\u00e9".repeat(RdsLogFiles.MAX_BYTES); + RdsLogFiles.LogFile file = new RdsLogFiles.LogFile("log/ERROR", Instant.EPOCH, 0, List.of(huge, "next")); + RdsLogFiles.Portion portion = RdsLogFiles.portion(file, "0", 10); + int bytes = portion.data().getBytes(StandardCharsets.UTF_8).length; + assertTrue(bytes <= RdsLogFiles.MAX_BYTES, Integer.toString(bytes)); + assertTrue(portion.data().endsWith("\u00e9\n"), "cut between characters"); + assertEquals("1", portion.marker()); + assertTrue(portion.additionalDataPending()); + assertEquals("next\n", RdsLogFiles.portion(file, portion.marker(), 10).data()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java index 95064fe529..a8ec8c1017 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java @@ -510,6 +510,37 @@ class RdsServiceTest { } } + @Test + void logFilesRecoverAMissingContainerOnceThenReportTheInstanceNotReady() { + when(rdsConfig.mock()).thenReturn(true); + rdsService.createDbInstance("logs-db", "postgres", "16.3", "admin", "password", "appdb", + "db.t3.micro", 20, false, null, null, null, null, false, false, null, + Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults()); + when(rdsConfig.mock()).thenReturn(false); + RdsService service = spy(rdsService); + DbInstance recovered = new DbInstance(); + recovered.setContainerId("fresh-container"); + doReturn(recovered).when(service).ensureInstanceBackend(eq("logs-db"), any()); + + // No container id on file: the backend is found once and its log read. + when(containerManager.readLog("fresh-container")) + .thenReturn(List.of("2026-10-15T10:00:00Z LOG: database system is ready to accept connections")); + assertEquals(List.of("error/postgresql.log.2026-10-15-10"), + service.describeDbLogFiles(null, "logs-db", null, null, null, null, null).files().stream() + .map(RdsLogFiles.LogFile::name).toList()); + + // Docker does not know the recovered container either: the instance is not ready. + when(containerManager.readLog("fresh-container")).thenReturn(null); + AwsException notReady = assertThrows(AwsException.class, + () -> service.describeDbLogFiles(null, "logs-db", null, null, null, null, null)); + assertEquals("DBInstanceNotReady", notReady.getErrorCode()); + + // A read that times out is a failure, not a short log. + when(containerManager.readLog("fresh-container")).thenThrow(new IllegalStateException("Timed out")); + assertEquals("InternalFailure", assertThrows(AwsException.class, + () -> service.describeDbLogFiles(null, "logs-db", null, null, null, null, null)).getErrorCode()); + } + @Test void replacedClusterCannotReceiveAnOldMemberPublicationAndRetryReusesThePort() throws Exception { when(rdsConfig.mock()).thenReturn(false); diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java index 862f1265fb..de7f99a5ef 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java @@ -1,10 +1,14 @@ package io.github.hectorvent.floci.services.rds.container; import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.async.ResultCallback; import com.github.dockerjava.api.command.CopyArchiveToContainerCmd; +import com.github.dockerjava.api.command.LogContainerCmd; import com.github.dockerjava.api.command.PauseContainerCmd; import com.github.dockerjava.api.command.UnpauseContainerCmd; import com.github.dockerjava.api.model.Bind; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.api.model.StreamType; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.ServiceConfigAccess; @@ -24,9 +28,12 @@ import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mockito; +import java.io.Closeable; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Duration; import java.time.Instant; import java.util.List; import java.util.Map; @@ -240,6 +247,40 @@ class RdsContainerManagerTest { verify(lifecycleManager, never()).getDockerClient(); } + @Test + void aLogReadThatTimesOutFailsAndClosesTheStream() throws Exception { + EmulatorConfig config = config(tempDir.resolve("host-root")); + ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class); + DockerClient dockerClient = mock(DockerClient.class); + when(lifecycleManager.getDockerClient()).thenReturn(dockerClient); + LogContainerCmd logCmd = mock(LogContainerCmd.class); + when(dockerClient.logContainerCmd("slow")).thenReturn(logCmd); + when(logCmd.withStdOut(true)).thenReturn(logCmd); + when(logCmd.withStdErr(true)).thenReturn(logCmd); + when(logCmd.withTimestamps(true)).thenReturn(logCmd); + when(logCmd.withTail(RdsContainerManager.MAX_LOG_LINES)).thenReturn(logCmd); + Closeable stream = mock(Closeable.class); + // Docker starts the stream and sends one frame, then never completes. + when(logCmd.exec(any())).thenAnswer(invocation -> { + ResultCallback.Adapter callback = invocation.getArgument(0); + callback.onStart(stream); + callback.onNext(new Frame(StreamType.STDOUT, + "2026-10-15T10:00:00Z partial\n".getBytes(StandardCharsets.UTF_8))); + return callback; + }); + + RdsContainerManager manager = new RdsContainerManager( + new ContainerBuilder(config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class)), + lifecycleManager, mock(ContainerLogStreamer.class), mock(ContainerDetector.class), config, + new RegionResolver("us-east-1", "000000000000"), + mock(ServiceConfigAccess.class)); + + IllegalStateException timedOut = assertThrows(IllegalStateException.class, + () -> manager.readLog("slow", Duration.ofMillis(50))); + assertTrue(timedOut.getMessage().contains("Timed out"), timedOut.getMessage()); + verify(stream).close(); + } + @Test void mysqlNonRootMasterStartInstallsGrantInitScriptBeforeStart() throws Exception { EmulatorConfig config = config(tempDir.resolve("host-root"));