Skip to content

ToStringConsumer adds spurious newline to execInContainer outputs #1854

Closed

Description

@weiminyu

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.

Activity

  1. kiview commented on Dec 7, 2019

    @kiview
    Member

    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).

  2. weiminyu commented on Jan 8, 2020

    @weiminyu
    Author

    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.

  3. ReuDa commented on Oct 5, 2020

    @ReuDa

    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
        }
    }
    
  4. gaeljw commented on Dec 18, 2020

    @gaeljw

    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.

  5. perlun commented on Feb 2, 2021

    @perlun
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions