diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_vdkr.py | 72 |
1 files changed, 70 insertions, 2 deletions
diff --git a/tests/test_vdkr.py b/tests/test_vdkr.py index 81715d16..c343e05f 100644 --- a/tests/test_vdkr.py +++ b/tests/test_vdkr.py | |||
| @@ -80,8 +80,9 @@ class TestPortForwarding: | |||
| 80 | """Test port forwarding with memres. | 80 | """Test port forwarding with memres. |
| 81 | 81 | ||
| 82 | Port forwarding allows access to services running in containers from the host. | 82 | Port forwarding allows access to services running in containers from the host. |
| 83 | --network=host is used by default for all containers since Docker bridge | 83 | Docker bridge networking (docker0, 172.17.0.0/16) is used by default. |
| 84 | networking is not available inside the QEMU VM. | 84 | Each container gets its own IP, enabling multiple containers to listen |
| 85 | on the same internal port with different host port mappings. | ||
| 85 | """ | 86 | """ |
| 86 | 87 | ||
| 87 | @pytest.mark.network | 88 | @pytest.mark.network |
| @@ -135,6 +136,73 @@ class TestPortForwarding: | |||
| 135 | # Stop memres | 136 | # Stop memres |
| 136 | vdkr.memres_stop() | 137 | vdkr.memres_stop() |
| 137 | 138 | ||
| 139 | @pytest.mark.network | ||
| 140 | @pytest.mark.slow | ||
| 141 | def test_multiple_containers_same_internal_port(self, vdkr): | ||
| 142 | """Test multiple containers listening on same internal port. | ||
| 143 | |||
| 144 | This tests the key benefit of bridge networking: | ||
| 145 | - nginx1 listens on container port 80, mapped to host port 8080 | ||
| 146 | - nginx2 listens on container port 80, mapped to host port 8081 | ||
| 147 | - Both should work simultaneously (impossible with --network=host) | ||
| 148 | """ | ||
| 149 | import subprocess | ||
| 150 | import time | ||
| 151 | |||
| 152 | # Stop any running memres first | ||
| 153 | vdkr.memres_stop() | ||
| 154 | |||
| 155 | # Start memres | ||
| 156 | result = vdkr.memres_start(timeout=180) | ||
| 157 | assert result.returncode == 0, f"memres start failed: {result.stderr}" | ||
| 158 | |||
| 159 | try: | ||
| 160 | # Pull nginx:alpine if not present | ||
| 161 | vdkr.run("pull", "nginx:alpine", timeout=300) | ||
| 162 | |||
| 163 | # Run first nginx on host:8080 -> container:80 | ||
| 164 | result1 = vdkr.run("run", "-d", "--name", "nginx1", "-p", "8080:80", | ||
| 165 | "nginx:alpine", timeout=60) | ||
| 166 | assert result1.returncode == 0, f"nginx1 run failed: {result1.stderr}" | ||
| 167 | |||
| 168 | # Run second nginx on host:8081 -> container:80 (same internal port!) | ||
| 169 | result2 = vdkr.run("run", "-d", "--name", "nginx2", "-p", "8081:80", | ||
| 170 | "nginx:alpine", timeout=60) | ||
| 171 | assert result2.returncode == 0, f"nginx2 run failed: {result2.stderr}" | ||
| 172 | |||
| 173 | # Give nginx time to start | ||
| 174 | time.sleep(3) | ||
| 175 | |||
| 176 | # Test both are accessible | ||
| 177 | curl1 = subprocess.run( | ||
| 178 | ["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", | ||
| 179 | "http://localhost:8080"], | ||
| 180 | capture_output=True, text=True, timeout=10 | ||
| 181 | ) | ||
| 182 | assert curl1.stdout == "200", f"nginx1: Expected HTTP 200, got {curl1.stdout}" | ||
| 183 | |||
| 184 | curl2 = subprocess.run( | ||
| 185 | ["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", | ||
| 186 | "http://localhost:8081"], | ||
| 187 | capture_output=True, text=True, timeout=10 | ||
| 188 | ) | ||
| 189 | assert curl2.stdout == "200", f"nginx2: Expected HTTP 200, got {curl2.stdout}" | ||
| 190 | |||
| 191 | # Verify ps shows both containers with their port mappings | ||
| 192 | ps_result = vdkr.run("ps") | ||
| 193 | assert "nginx1" in ps_result.stdout | ||
| 194 | assert "nginx2" in ps_result.stdout | ||
| 195 | assert "8080" in ps_result.stdout | ||
| 196 | assert "8081" in ps_result.stdout | ||
| 197 | |||
| 198 | finally: | ||
| 199 | # Clean up | ||
| 200 | vdkr.run("stop", "nginx1", timeout=10, check=False) | ||
| 201 | vdkr.run("stop", "nginx2", timeout=10, check=False) | ||
| 202 | vdkr.run("rm", "-f", "nginx1", check=False) | ||
| 203 | vdkr.run("rm", "-f", "nginx2", check=False) | ||
| 204 | vdkr.memres_stop() | ||
| 205 | |||
| 138 | 206 | ||
| 139 | class TestImages: | 207 | class TestImages: |
| 140 | """Test image management commands.""" | 208 | """Test image management commands.""" |
