diff options
| author | Bruce Ashfield <bruce.ashfield@gmail.com> | 2026-01-05 14:24:37 +0000 |
|---|---|---|
| committer | Bruce Ashfield <bruce.ashfield@gmail.com> | 2026-02-09 03:32:52 +0000 |
| commit | f9f8e294e5d870f28dd65f13ddb43c224be958fc (patch) | |
| tree | 1da13e95dbb2fc3f5ef9388b0484b84d299b6961 /tests | |
| parent | 1165c61f5ab8ada644c7def03e991890c4d380ca (diff) | |
| download | meta-virtualization-f9f8e294e5d870f28dd65f13ddb43c224be958fc.tar.gz | |
tests: add port forwarding test for vdkr
Add TestPortForwarding class with test_port_forward_nginx that:
- Starts memres with port forwarding (-p 8080:80)
- Runs nginx with --network=host
- Verifies accessibility from host via curl
Also adds port_forwards parameter to memres_start() in both
VdkrRunner and VpdmnRunner helper classes.
Signed-off-by: Bruce Ashfield <bruce.ashfield@gmail.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 30 | ||||
| -rw-r--r-- | tests/test_vdkr.py | 60 |
2 files changed, 84 insertions, 6 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 39d92322..a9452410 100644 --- a/tests/conftest.py +++ b/tests/conftest.py | |||
| @@ -299,9 +299,18 @@ class VdkrRunner: | |||
| 299 | raise AssertionError(error_msg) | 299 | raise AssertionError(error_msg) |
| 300 | return result | 300 | return result |
| 301 | 301 | ||
| 302 | def memres_start(self, timeout=120): | 302 | def memres_start(self, timeout=120, port_forwards=None): |
| 303 | """Start memory resident mode.""" | 303 | """Start memory resident mode. |
| 304 | return self.run("memres", "start", timeout=timeout) | 304 | |
| 305 | Args: | ||
| 306 | timeout: Command timeout in seconds | ||
| 307 | port_forwards: List of port forwards, e.g., ["8080:80", "2222:22"] | ||
| 308 | """ | ||
| 309 | args = ["memres", "start"] | ||
| 310 | if port_forwards: | ||
| 311 | for pf in port_forwards: | ||
| 312 | args.extend(["-p", pf]) | ||
| 313 | return self.run(*args, timeout=timeout) | ||
| 305 | 314 | ||
| 306 | def memres_stop(self, timeout=30): | 315 | def memres_stop(self, timeout=30): |
| 307 | """Stop memory resident mode.""" | 316 | """Stop memory resident mode.""" |
| @@ -506,9 +515,18 @@ class VpdmnRunner: | |||
| 506 | raise AssertionError(error_msg) | 515 | raise AssertionError(error_msg) |
| 507 | return result | 516 | return result |
| 508 | 517 | ||
| 509 | def memres_start(self, timeout=120): | 518 | def memres_start(self, timeout=120, port_forwards=None): |
| 510 | """Start memory resident mode.""" | 519 | """Start memory resident mode. |
| 511 | return self.run("memres", "start", timeout=timeout) | 520 | |
| 521 | Args: | ||
| 522 | timeout: Command timeout in seconds | ||
| 523 | port_forwards: List of port forwards, e.g., ["8080:80", "2222:22"] | ||
| 524 | """ | ||
| 525 | args = ["memres", "start"] | ||
| 526 | if port_forwards: | ||
| 527 | for pf in port_forwards: | ||
| 528 | args.extend(["-p", pf]) | ||
| 529 | return self.run(*args, timeout=timeout) | ||
| 512 | 530 | ||
| 513 | def memres_stop(self, timeout=30): | 531 | def memres_stop(self, timeout=30): |
| 514 | """Stop memory resident mode.""" | 532 | """Stop memory resident mode.""" |
diff --git a/tests/test_vdkr.py b/tests/test_vdkr.py index 07715a66..6ce19036 100644 --- a/tests/test_vdkr.py +++ b/tests/test_vdkr.py | |||
| @@ -76,6 +76,66 @@ class TestMemresBasic: | |||
| 76 | assert vdkr.is_memres_running() | 76 | assert vdkr.is_memres_running() |
| 77 | 77 | ||
| 78 | 78 | ||
| 79 | class TestPortForwarding: | ||
| 80 | """Test port forwarding with memres. | ||
| 81 | |||
| 82 | Port forwarding allows access to services running in containers from the host. | ||
| 83 | Containers must use --network=host because Docker bridge networking is not | ||
| 84 | available inside the QEMU VM. | ||
| 85 | """ | ||
| 86 | |||
| 87 | @pytest.mark.network | ||
| 88 | @pytest.mark.slow | ||
| 89 | def test_port_forward_nginx(self, vdkr): | ||
| 90 | """Test port forwarding with nginx. | ||
| 91 | |||
| 92 | This test: | ||
| 93 | 1. Starts memres with port forward 8080:80 | ||
| 94 | 2. Runs nginx with --network=host | ||
| 95 | 3. Verifies nginx is accessible from host via curl | ||
| 96 | """ | ||
| 97 | import subprocess | ||
| 98 | import time | ||
| 99 | |||
| 100 | # Stop any running memres first | ||
| 101 | vdkr.memres_stop() | ||
| 102 | |||
| 103 | # Start memres with port forwarding | ||
| 104 | result = vdkr.memres_start(timeout=180, port_forwards=["8080:80"]) | ||
| 105 | assert result.returncode == 0, f"memres start failed: {result.stderr}" | ||
| 106 | |||
| 107 | try: | ||
| 108 | # Pull nginx:alpine if not present | ||
| 109 | vdkr.run("pull", "nginx:alpine", timeout=300) | ||
| 110 | |||
| 111 | # Run nginx with host networking | ||
| 112 | result = vdkr.run("run", "-d", "--rm", "--network=host", "nginx:alpine", timeout=60) | ||
| 113 | assert result.returncode == 0, f"nginx run failed: {result.stderr}" | ||
| 114 | |||
| 115 | # Give nginx time to start | ||
| 116 | time.sleep(3) | ||
| 117 | |||
| 118 | # Test access from host | ||
| 119 | curl_result = subprocess.run( | ||
| 120 | ["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", "http://localhost:8080"], | ||
| 121 | capture_output=True, | ||
| 122 | text=True, | ||
| 123 | timeout=10 | ||
| 124 | ) | ||
| 125 | assert curl_result.stdout == "200", f"Expected HTTP 200, got {curl_result.stdout}" | ||
| 126 | |||
| 127 | finally: | ||
| 128 | # Clean up: stop all containers | ||
| 129 | vdkr.run("ps", "-q", check=False) | ||
| 130 | ps_result = vdkr.run("ps", "-q", check=False) | ||
| 131 | if ps_result.stdout.strip(): | ||
| 132 | for container_id in ps_result.stdout.strip().split('\n'): | ||
| 133 | vdkr.run("stop", container_id, timeout=10, check=False) | ||
| 134 | |||
| 135 | # Stop memres | ||
| 136 | vdkr.memres_stop() | ||
| 137 | |||
| 138 | |||
| 79 | class TestImages: | 139 | class TestImages: |
| 80 | """Test image management commands.""" | 140 | """Test image management commands.""" |
| 81 | 141 | ||
