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

fix: Port binding is not respected for SSH port #1043

Merged
merged 8 commits into from
Jun 5, 2024
Merged
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 @@ -278,7 +278,11 @@
}
final Ports portBindings = hostConfig.getPortBindings();
if (portBindings != null) {
portBindings.add(sshPortBinding);
final Ports.Binding[] portBinding = portBindings.getBindings().get(sshPortBinding.getExposedPort());
// Only add default ssh port Binding when it is not already configured
if (portBinding == null || portBinding.length == 0) {

Check warning on line 283 in src/main/java/io/jenkins/docker/connector/DockerComputerSSHConnector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 283 is only partially covered, one branch is missing
portBindings.add(sshPortBinding);
}
hostConfig.withPortBindings(portBindings);
} else {
hostConfig.withPortBindings(sshPortBinding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,46 @@ public void testPortBinding() throws IOException, InterruptedException {
final String actualHostPortSpecForPort22 = actualBindingsForPort22[0].getHostPortSpec();
Assert.assertNull(actualHostPortSpecForPort22);
}

@Test
public void testPortBindingPort22() throws IOException, InterruptedException {
// Given
DockerComputerSSHConnector connector =
new DockerComputerSSHConnector(Mockito.mock(DockerComputerSSHConnector.SSHKeyStrategy.class));
CreateContainerCmdImpl cmd = new CreateContainerCmdImpl(
Mockito.mock(CreateContainerCmd.Exec.class), Mockito.mock(AuthConfig.class), "");
HostConfig hostConfig = cmd.getHostConfig();
if (hostConfig == null) {
hostConfig = new HostConfig();
cmd.withHostConfig(hostConfig);
}
final PortBinding exportContainerPort42 = PortBinding.parse("42:42");
final PortBinding exportContainerPort22 = PortBinding.parse("3022:22");
hostConfig.withPortBindings(exportContainerPort42, exportContainerPort22);
final ExposedPort port42 = new ExposedPort(42);
final ExposedPort port22 = new ExposedPort(22);

// When
connector.setPort(22);
connector.beforeContainerCreated(Mockito.mock(DockerAPI.class), "/workdir", cmd);

// Then
final Ports actualPortBindings = cmd.getHostConfig().getPortBindings();
Assert.assertNotNull(actualPortBindings);
final Map<ExposedPort, Ports.Binding[]> actualBindingMap = actualPortBindings.getBindings();
Assert.assertNotNull(actualBindingMap);
Assert.assertEquals(2, actualBindingMap.size());

final Ports.Binding[] actualBindingsForPort42 = actualBindingMap.get(port42);
Assert.assertNotNull(actualBindingsForPort42);
Assert.assertEquals(1, actualBindingsForPort42.length);
final String actualHostPortSpecForPort42 = actualBindingsForPort42[0].getHostPortSpec();
Assert.assertEquals("42", actualHostPortSpecForPort42);

final Ports.Binding[] actualBindingsForPort22 = actualBindingMap.get(port22);
Assert.assertNotNull(actualBindingsForPort22);
Assert.assertEquals(1, actualBindingsForPort22.length);
final String actualHostPortSpecForPort22 = actualBindingsForPort22[0].getHostPortSpec();
Assert.assertEquals("3022", actualHostPortSpecForPort22);
}
}