Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter mapped ports when port numbers are not specified #3948

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.DockerClientFactory;
Expand Down Expand Up @@ -181,7 +182,9 @@ default List<Integer> getBoundPortNumbers() {
.map(PortBinding::getBinding)
.map(Ports.Binding::getHostPortSpec)
.filter(Objects::nonNull)
.filter(NumberUtils::isNumber)
.map(Integer::valueOf)
.filter(port -> port.compareTo(0) > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason why port > 0 cannot be used?

.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.testcontainers.containers;

import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.List;

import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.when;
import static org.rnorth.visibleassertions.VisibleAssertions.*;

public class ContainerStateTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please make this test parameterized?


@Mock
ContainerState containerState;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
doCallRealMethod().when(containerState).getBoundPortNumbers();
}

void testSet(String... args) {
when(containerState.getPortBindings()).thenReturn(
Lists.list(args)
);
}

@Test
public void testGetBoundPortNumbers() {
testSet("80:8080/tcp");
List<Integer> res = containerState.getBoundPortNumbers();
assertEquals(null,1,res.size());
assertEquals("regular mapping works",80, res.stream().findFirst().get());
}

@Test
public void testGetBoundPortNumbersFull() {
testSet("127.0.0.1:80:8080/tcp");
List<Integer> res = containerState.getBoundPortNumbers();
assertEquals(null,1,res.size());
assertEquals("regular mapping with host works",80, res.stream().findFirst().get());
}

@Test
public void testGetBoundPortNumbersFullExplicitZero() {
testSet(":0:8080/tcp");
List<Integer> res = containerState.getBoundPortNumbers();
assertEquals("zero port with host is ignored",0,res.size());
}

@Test
public void testGetBoundPortNumbersFullImplicitZero() {
testSet("0.0.0.0::8080/tcp");
List<Integer> res = containerState.getBoundPortNumbers();
assertEquals("missing port with host is ignored",0,res.size());
}

@Test
public void testGetBoundPortNumbersExplicitZero() {
testSet("0:8080/tcp");
List<Integer> res = containerState.getBoundPortNumbers();
assertEquals("zero port (synthetic case) is ignored",0,res.size());
}

@Test
public void testGetBoundPortNumbersImplicitZero() {
testSet(":8080/tcp");
List<Integer> res = containerState.getBoundPortNumbers();
assertEquals("missing port is ignored",0,res.size());
}
}