|
1 | 1 | package org.testcontainers.containers; |
2 | 2 |
|
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import com.github.dockerjava.api.model.ExposedPort; |
| 5 | +import com.github.dockerjava.api.model.NetworkSettings; |
| 6 | +import com.github.dockerjava.api.model.Ports; |
| 7 | +import org.junit.jupiter.api.Test; |
3 | 8 | import org.junit.jupiter.params.ParameterizedTest; |
4 | 9 | import org.junit.jupiter.params.provider.MethodSource; |
5 | 10 |
|
6 | 11 | import java.util.Collections; |
| 12 | +import java.util.HashMap; |
7 | 13 | import java.util.List; |
| 14 | +import java.util.Map; |
8 | 15 |
|
9 | 16 | import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
10 | 18 | import static org.mockito.Mockito.doCallRealMethod; |
11 | 19 | import static org.mockito.Mockito.mock; |
12 | 20 | import static org.mockito.Mockito.when; |
@@ -35,4 +43,112 @@ void test(String name, String testSet, List<Integer> expectedResult) { |
35 | 43 | List<Integer> result = containerState.getBoundPortNumbers(); |
36 | 44 | assertThat(result).hasSameElementsAs(expectedResult); |
37 | 45 | } |
| 46 | + |
| 47 | + @Test |
| 48 | + void shouldGetMappedPortForUdpProtocol() { |
| 49 | + ContainerState containerState = mock(ContainerState.class); |
| 50 | + InspectContainerResponse containerInfo = mock(InspectContainerResponse.class); |
| 51 | + NetworkSettings networkSettings = mock(NetworkSettings.class); |
| 52 | + Ports ports = mock(Ports.class); |
| 53 | + |
| 54 | + // Set up the mock port bindings for UDP port 5353 |
| 55 | + Map<ExposedPort, Ports.Binding[]> bindings = new HashMap<>(); |
| 56 | + ExposedPort udpPort = new ExposedPort(5353, com.github.dockerjava.api.model.InternetProtocol.UDP); |
| 57 | + bindings.put(udpPort, new Ports.Binding[] { Ports.Binding.bindPort(12345) }); |
| 58 | + |
| 59 | + when(containerState.getContainerId()).thenReturn("test-container-id"); |
| 60 | + when(containerState.getContainerInfo()).thenReturn(containerInfo); |
| 61 | + when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); |
| 62 | + when(networkSettings.getPorts()).thenReturn(ports); |
| 63 | + when(ports.getBindings()).thenReturn(bindings); |
| 64 | + doCallRealMethod().when(containerState).getMappedPort(5353, InternetProtocol.UDP); |
| 65 | + |
| 66 | + Integer mappedPort = containerState.getMappedPort(5353, InternetProtocol.UDP); |
| 67 | + assertThat(mappedPort).isEqualTo(12345); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void shouldGetMappedPortForTcpUsingProtocol() { |
| 72 | + ContainerState containerState = mock(ContainerState.class); |
| 73 | + InspectContainerResponse containerInfo = mock(InspectContainerResponse.class); |
| 74 | + NetworkSettings networkSettings = mock(NetworkSettings.class); |
| 75 | + Ports ports = mock(Ports.class); |
| 76 | + |
| 77 | + // Set up the mock port bindings for TCP port 8080 |
| 78 | + Map<ExposedPort, Ports.Binding[]> bindings = new HashMap<>(); |
| 79 | + ExposedPort tcpPort = new ExposedPort(8080, com.github.dockerjava.api.model.InternetProtocol.TCP); |
| 80 | + bindings.put(tcpPort, new Ports.Binding[] { Ports.Binding.bindPort(54321) }); |
| 81 | + |
| 82 | + when(containerState.getContainerId()).thenReturn("test-container-id"); |
| 83 | + when(containerState.getContainerInfo()).thenReturn(containerInfo); |
| 84 | + when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); |
| 85 | + when(networkSettings.getPorts()).thenReturn(ports); |
| 86 | + when(ports.getBindings()).thenReturn(bindings); |
| 87 | + doCallRealMethod().when(containerState).getMappedPort(8080, InternetProtocol.TCP); |
| 88 | + doCallRealMethod().when(containerState).getMappedPort(8080); |
| 89 | + |
| 90 | + // Test with explicit TCP protocol |
| 91 | + Integer mappedPort = containerState.getMappedPort(8080, InternetProtocol.TCP); |
| 92 | + assertThat(mappedPort).isEqualTo(54321); |
| 93 | + |
| 94 | + // Test default getMappedPort (should also return same value since it defaults to TCP) |
| 95 | + Integer defaultMappedPort = containerState.getMappedPort(8080); |
| 96 | + assertThat(defaultMappedPort).isEqualTo(54321); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void shouldThrowForUnmappedUdpPort() { |
| 101 | + ContainerState containerState = mock(ContainerState.class); |
| 102 | + InspectContainerResponse containerInfo = mock(InspectContainerResponse.class); |
| 103 | + NetworkSettings networkSettings = mock(NetworkSettings.class); |
| 104 | + Ports ports = mock(Ports.class); |
| 105 | + |
| 106 | + // Set up the mock port bindings with only TCP port |
| 107 | + Map<ExposedPort, Ports.Binding[]> bindings = new HashMap<>(); |
| 108 | + ExposedPort tcpPort = new ExposedPort(8080, com.github.dockerjava.api.model.InternetProtocol.TCP); |
| 109 | + bindings.put(tcpPort, new Ports.Binding[] { Ports.Binding.bindPort(54321) }); |
| 110 | + |
| 111 | + when(containerState.getContainerId()).thenReturn("test-container-id"); |
| 112 | + when(containerState.getContainerInfo()).thenReturn(containerInfo); |
| 113 | + when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); |
| 114 | + when(networkSettings.getPorts()).thenReturn(ports); |
| 115 | + when(ports.getBindings()).thenReturn(bindings); |
| 116 | + doCallRealMethod().when(containerState).getMappedPort(8080, InternetProtocol.UDP); |
| 117 | + |
| 118 | + // Should throw when trying to get unmapped UDP port |
| 119 | + assertThatThrownBy(() -> containerState.getMappedPort(8080, InternetProtocol.UDP)) |
| 120 | + .isInstanceOf(IllegalArgumentException.class) |
| 121 | + .hasMessageContaining("8080/udp") |
| 122 | + .hasMessageContaining("is not mapped"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void shouldSupportBothTcpAndUdpOnSamePort() { |
| 127 | + ContainerState containerState = mock(ContainerState.class); |
| 128 | + InspectContainerResponse containerInfo = mock(InspectContainerResponse.class); |
| 129 | + NetworkSettings networkSettings = mock(NetworkSettings.class); |
| 130 | + Ports ports = mock(Ports.class); |
| 131 | + |
| 132 | + // Set up the mock port bindings with both TCP and UDP on the same port |
| 133 | + Map<ExposedPort, Ports.Binding[]> bindings = new HashMap<>(); |
| 134 | + ExposedPort tcpPort = new ExposedPort(5000, com.github.dockerjava.api.model.InternetProtocol.TCP); |
| 135 | + ExposedPort udpPort = new ExposedPort(5000, com.github.dockerjava.api.model.InternetProtocol.UDP); |
| 136 | + bindings.put(tcpPort, new Ports.Binding[] { Ports.Binding.bindPort(11111) }); |
| 137 | + bindings.put(udpPort, new Ports.Binding[] { Ports.Binding.bindPort(22222) }); |
| 138 | + |
| 139 | + when(containerState.getContainerId()).thenReturn("test-container-id"); |
| 140 | + when(containerState.getContainerInfo()).thenReturn(containerInfo); |
| 141 | + when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); |
| 142 | + when(networkSettings.getPorts()).thenReturn(ports); |
| 143 | + when(ports.getBindings()).thenReturn(bindings); |
| 144 | + doCallRealMethod().when(containerState).getMappedPort(5000, InternetProtocol.TCP); |
| 145 | + doCallRealMethod().when(containerState).getMappedPort(5000, InternetProtocol.UDP); |
| 146 | + |
| 147 | + // Both should be mapped to different host ports |
| 148 | + Integer tcpMappedPort = containerState.getMappedPort(5000, InternetProtocol.TCP); |
| 149 | + Integer udpMappedPort = containerState.getMappedPort(5000, InternetProtocol.UDP); |
| 150 | + |
| 151 | + assertThat(tcpMappedPort).isEqualTo(11111); |
| 152 | + assertThat(udpMappedPort).isEqualTo(22222); |
| 153 | + } |
38 | 154 | } |
0 commit comments