|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package org.hiero.otter.fixtures.container.logging; |
| 3 | + |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.hiero.otter.fixtures.logging.LogMessageParser.extractLogLevel; |
| 6 | +import static org.hiero.otter.fixtures.logging.LogMessageParser.isLogMessage; |
| 7 | + |
| 8 | +import java.io.ByteArrayOutputStream; |
| 9 | +import java.io.PrintStream; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.time.Duration; |
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.core.LoggerContext; |
| 14 | +import org.hiero.otter.fixtures.Network; |
| 15 | +import org.hiero.otter.fixtures.TestEnvironment; |
| 16 | +import org.hiero.otter.fixtures.TimeManager; |
| 17 | +import org.hiero.otter.fixtures.container.ContainerTestEnvironment; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +/** |
| 21 | + * Tests to verify console output in the Container-based test environment. This test validates the output on |
| 22 | + * the host machine that runs the test, not the output inside the containers. |
| 23 | + */ |
| 24 | +class ContainerConsoleOutputTest { |
| 25 | + |
| 26 | + /** |
| 27 | + * Test basic console output capturing and log message verification. |
| 28 | + * The output should contain expected log messages from the Otter framework and user log statements. |
| 29 | + */ |
| 30 | + @Test |
| 31 | + void testBasicConsoleOutput() { |
| 32 | + // Capture console output |
| 33 | + final ByteArrayOutputStream consoleCapture = new ByteArrayOutputStream(); |
| 34 | + final PrintStream originalOut = System.out; |
| 35 | + final PrintStream captureOut = new PrintStream(consoleCapture, true, StandardCharsets.UTF_8); |
| 36 | + |
| 37 | + try { |
| 38 | + // Redirect System.out BEFORE creating the test environment |
| 39 | + System.setOut(captureOut); |
| 40 | + |
| 41 | + // Force Log4j to reconfigure so ConsoleAppender picks up the new System.out |
| 42 | + final LoggerContext context = (LoggerContext) LogManager.getContext(false); |
| 43 | + context.reconfigure(); |
| 44 | + |
| 45 | + final TestEnvironment env = new ContainerTestEnvironment(); |
| 46 | + try { |
| 47 | + final Network network = env.network(); |
| 48 | + final TimeManager timeManager = env.timeManager(); |
| 49 | + |
| 50 | + // Start a 4-node network |
| 51 | + network.addNodes(4); |
| 52 | + network.start(); |
| 53 | + |
| 54 | + System.out.println("Hello Otter!"); |
| 55 | + LogManager.getLogger().info("Hello Hiero!"); |
| 56 | + LogManager.getLogger("com.acme.ExternalOtterTest").info("Hello World!"); |
| 57 | + |
| 58 | + // Wait 5 seconds |
| 59 | + timeManager.waitFor(Duration.ofSeconds(5L)); |
| 60 | + |
| 61 | + } finally { |
| 62 | + env.destroy(); |
| 63 | + } |
| 64 | + |
| 65 | + // Restore System.out before examining captured output |
| 66 | + System.setOut(originalOut); |
| 67 | + |
| 68 | + // Get the captured console output |
| 69 | + final String consoleOutput = consoleCapture.toString(StandardCharsets.UTF_8); |
| 70 | + |
| 71 | + // Verify that the console output contains expected log messages |
| 72 | + assertThat(consoleOutput) |
| 73 | + .as("Console output should contain 'Random seed:' entry") |
| 74 | + .doesNotContain("Random seed:"); // Turtle environment only |
| 75 | + assertThat(consoleOutput) |
| 76 | + .as("Console output should contain 'testcontainers' entry") |
| 77 | + .contains("testcontainers"); // Container environment only |
| 78 | + |
| 79 | + // Verify presence of key log messages from Otter framework |
| 80 | + assertThat(consoleOutput) |
| 81 | + .as("Console output should contain 'Starting network...' message") |
| 82 | + .contains("Starting network..."); |
| 83 | + assertThat(consoleOutput) |
| 84 | + .as("Console output should contain 'Network started.' message") |
| 85 | + .contains("Network started."); |
| 86 | + assertThat(consoleOutput) |
| 87 | + .as("Console output should contain 'Waiting for PT5S' message") |
| 88 | + .contains("Waiting for PT5S"); |
| 89 | + assertThat(consoleOutput) |
| 90 | + .as("Console output should contain 'Destroying network...' message") |
| 91 | + .contains("Destroying network..."); |
| 92 | + |
| 93 | + // Verify presence of user log messages |
| 94 | + assertThat(consoleOutput) |
| 95 | + .as("Console output should contain 'Hello Otter!' message") |
| 96 | + .contains("Hello Otter!"); |
| 97 | + assertThat(consoleOutput) |
| 98 | + .as("Console output should contain 'Hello Hiero!' message") |
| 99 | + .contains("Hello Hiero!"); |
| 100 | + assertThat(consoleOutput) |
| 101 | + .as("Console output should contain 'Hello World!' message") |
| 102 | + .contains("Hello World!"); |
| 103 | + |
| 104 | + // Parse each line and verify log messages follow the expected pattern |
| 105 | + final String[] lines = consoleOutput.split("\n"); |
| 106 | + for (final String line : lines) { |
| 107 | + if (line.trim().isEmpty()) { |
| 108 | + continue; // Skip empty lines |
| 109 | + } |
| 110 | + |
| 111 | + // Check if this line is a log message (matches the log pattern) |
| 112 | + // Pattern: yyyy-MM-dd HH:mm:ss.SSS [thread] [optional marker] LEVEL logger - message |
| 113 | + // Example: 2025-10-22 16:29:13.345 [Test worker] INFO org.hiero.otter.fixtures... - ... |
| 114 | + if (isLogMessage(line)) { |
| 115 | + // Extract log level and logger name |
| 116 | + final String logLevel = extractLogLevel(line); |
| 117 | + |
| 118 | + // Verify log level is INFO or more critical (not DEBUG or TRACE) |
| 119 | + assertThat(logLevel) |
| 120 | + .as("Log message should have INFO or higher level: %s", line) |
| 121 | + .isIn("INFO", "WARN", "ERROR", "FATAL"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + } finally { |
| 126 | + // Ensure System.out is always restored even if an exception occurs |
| 127 | + System.setOut(originalOut); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments