package org.testcontainers.containers.output;

import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
import static org.testcontainers.containers.output.OutputFrame.OutputType.STDERR;
import static org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT;

public class ContainerLogsTest {

    @Test
    public void getLogsReturnsAllLogsToDate() {
        try (GenericContainer container = shortLivedContainer()) {
            container.start();

            // docsGetAllLogs {
            final String logs = container.getLogs();
            // }
            assertEquals("stdout and stderr are reflected in the returned logs", "stdout\nstderr", logs);
        }
    }

    @Test
    public void getLogsReturnsStdOutToDate() {
        try (GenericContainer container = shortLivedContainer()) {
            container.start();

            // docsGetStdOut {
            final String logs = container.getLogs(STDOUT);
            // }
            assertEquals("stdout and stderr are reflected in the returned logs", "stdout", logs);
        }
    }

    @Test
    public void getLogsReturnsStdErrToDate() {
        try (GenericContainer container = shortLivedContainer()) {
            container.start();

            // docsGetStdErr {
            final String logs = container.getLogs(STDERR);
            // }
            assertEquals("stdout and stderr are reflected in the returned logs", "stderr", logs);
        }
    }

    @Test
    public void getLogsForLongRunningContainer() throws InterruptedException {
        try (GenericContainer container = longRunningContainer()) {
            container.start();

            Thread.sleep(1000L);

            final String logs = container.getLogs(STDOUT);
            assertTrue("stdout is reflected in the returned logs for a running container", logs.contains("seq=0"));
        }
    }

    private static GenericContainer shortLivedContainer() {
        return new GenericContainer("alpine:3.3")
            .withCommand("/bin/sh", "-c", "echo -n 'stdout' && echo -n 'stderr' 1>&2")
            .withStartupCheckStrategy(new OneShotStartupCheckStrategy());
    }

    private static GenericContainer longRunningContainer() {
        return new GenericContainer("alpine:3.3")
            .withCommand("ping -c 100 127.0.0.1");
    }
}

@@ -40,16 +40,41 @@
 utput;%0A%0A
+import org.junit.Ignore;%0A
 import o
@@ -572,32 +572,104 @@
 st %7B%0A%0A    @Test%0A
+    @Ignore(%22fails due to the timing of the shell's decision to flush%22)%0A
     public void 
@@ -791,32 +791,368 @@
 ainer.start();%0A%0A
+            final String logs = container.getLogs();%0A            assertEquals(%22stdout and stderr are reflected in the returned logs%22, %22stdout%5Cnstderr%22, logs);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void getLogsContainsBothOutputTypes() %7B%0A        try (GenericContainer container = shortLivedContainer()) %7B%0A            container.start();%0A%0A
             // d
@@ -1247,38 +1247,36 @@
           assert
-Equals
+True
 (%22stdout and std
@@ -1264,38 +1264,119 @@
 rue(%22stdout 
-and 
+is reflected in the returned logs%22, logs.contains(%22stdout%22));%0A            assertTrue(%22
 stderr 
-are
+is
  reflected i
@@ -1397,38 +1397,39 @@
  logs%22, 
-%22stdout%5Cn
+logs.contains(%22
 stderr%22
-, logs
+)
 );%0A     
@@ -1713,38 +1713,36 @@
           assert
-Equals
+True
 (%22stdout and std
@@ -1730,38 +1730,26 @@
 rue(%22stdout 
-and stderr are
+is
  reflected i
@@ -1770,24 +1770,38 @@
  logs%22, 
+logs.contains(
 %22stdout%22
 , logs);
@@ -1792,22 +1792,17 @@
 %22stdout%22
-, logs
+)
 );%0A     
@@ -2098,37 +2098,23 @@
 sert
-Equals(%22stdout and 
+True(%22
 stderr 
-are
+is
  ref
@@ -2143,24 +2143,38 @@
  logs%22, 
+logs.contains(
 %22stderr%22
 , logs);
@@ -2161,30 +2161,25 @@
 ins(%22stderr%22
-, logs
+)
 );%0A        %7D

Make test resilient to timing issues (#1375)