Testcontainers-java: ToStringConsumer adds spurious newline to execInContainer outputs

Created on 10 Sep 2019  路  5Comments  路  Source: testcontainers/testcontainers-java

When fetching stdout from an execInContainer Result object, an extra newline is added at the end of each frame. In our case, every 4097th char is a newline, which may break up a word.

The problem exists because ExecInContainerPattern.execInContainer() uses ToStringConsumer to collect stdout and stderr. Said classes adds a newline to before all but the first frame of data.

We need the exact output for our tests, and would appreciate a fix, or at least make this behavior configurable.

arelogging resolutioacknowledged typbreaking-api-change typbug

Most helpful comment

We see line breaks again in version 1.15.0-rc2. (Tested with Docker/Mac 2.3.0.5 and 2.4.0.0)

Testcase:

@Testcontainers
public class NewLineContainerTest {

    static class AlpineContainer extends GenericContainer<AlpineContainer> {
        public AlpineContainer() {
            super("alpine");
        }

        @Override
        protected void configure() {
            super.configure();
            this.withCommand("sleep", "2m");
        }
    }

    @Container
    private static final AlpineContainer container = new AlpineContainer();

    @Test
    public void test() throws Exception {
        String largeString = RandomStringUtils.randomAlphabetic(100000);
        System.out.println(largeString.contains("\n")); // returns false
        ExecResult build = container.execInContainer("echo", largeString);
        System.out.println(build.getStdout());
        System.out.println(build.getStdout().contains("\n")); // returns true
    }
}

All 5 comments

Hey @weiminyu thanks for already investigating this, your description sounds valid.
With regards to #1763 it would be nice, if we could solve at a single point, to a bit more DRY.

Would you like to provide a PR for it?

@rnorth @bsideup Anything to add? It would be a "soft" breaking API change in case users are currently depending on this flawed behavior (which might very well be the case).

Update: the issue seems to have gone away by itself. Now I see the entire stdout passed as one OutputFrame to ToStringConsumer, and newline becomes a non-issue.

We did not upgrade testcontainer nor made any obviously related changes. Maybe this is a transitive dependency change wrt Docker?

Our output is less than 8K bytes. I'm unclear if the issue is fixed for all use cases However, for us this is no longer a pressing issue. Please feel free to close.

Thanks.

We see line breaks again in version 1.15.0-rc2. (Tested with Docker/Mac 2.3.0.5 and 2.4.0.0)

Testcase:

@Testcontainers
public class NewLineContainerTest {

    static class AlpineContainer extends GenericContainer<AlpineContainer> {
        public AlpineContainer() {
            super("alpine");
        }

        @Override
        protected void configure() {
            super.configure();
            this.withCommand("sleep", "2m");
        }
    }

    @Container
    private static final AlpineContainer container = new AlpineContainer();

    @Test
    public void test() throws Exception {
        String largeString = RandomStringUtils.randomAlphabetic(100000);
        System.out.println(largeString.contains("\n")); // returns false
        ExecResult build = container.execInContainer("echo", largeString);
        System.out.println(build.getStdout());
        System.out.println(build.getStdout().contains("\n")); // returns true
    }
}

Same here, upgrading from 1.14.3 to 1.15.1 makes a lot of our tests fail because of newline in the output that we never had before.

Thanks for the test case @ReuDa. I used it to create a unit test in this MR where I fixed this: https://github.com/testcontainers/testcontainers-java/pull/3752 (I hope this was fine with you, assuming the snippet you posted could be considered public domain).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

naderghanbari picture naderghanbari  路  3Comments

itudoben picture itudoben  路  3Comments

dabraham02124 picture dabraham02124  路  3Comments

ParafeniukMikalaj picture ParafeniukMikalaj  路  3Comments

chomhanks picture chomhanks  路  3Comments