1
0
Fork 0

Refactor E2E tests: add wait_for_json_response helper for robust JSON response handling

- Replaced direct `curl` calls with `wait_for_json_response` to improve retry logic and error handling.
- Simplified tests for header validation and backend readiness verification.
This commit is contained in:
Joao Gilberto Magalhaes 2026-02-17 16:13:40 -05:00
parent 491f4a8c09
commit 3805100244

View file

@ -893,6 +893,44 @@ def wait_for_easyhaproxy_discovery(kubectl_cmd: str, expected_host: str, timeout
return True return True
def wait_for_json_response(host: str, extra_headers: list = None, timeout: int = 30) -> dict:
"""
Wait until the given host returns a valid JSON response via HAProxy.
Retries until JSON is parseable or timeout is reached.
Returns the parsed JSON dict, or raises AssertionError with debug info.
"""
start = time.time()
last_stdout = ""
last_stderr = ""
last_returncode = None
while time.time() - start < timeout:
cmd = ["curl", "-s", "-H", f"Host: {host}", f"http://localhost:{HTTP_PORT}"]
if extra_headers:
for h in extra_headers:
cmd += ["-H", h]
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
last_returncode = result.returncode
last_stdout = result.stdout
last_stderr = result.stderr
if result.returncode == 0 and result.stdout.strip():
try:
return json.loads(result.stdout)
except json.JSONDecodeError:
pass
except Exception:
pass
time.sleep(1)
raise AssertionError(
f"No valid JSON response from '{host}' within {timeout}s. "
f"Last returncode={last_returncode}, "
f"stdout={repr(last_stdout)}, stderr={repr(last_stderr)}"
)
# ============================================================================= # =============================================================================
# Test: service.yml - Basic Service # Test: service.yml - Basic Service
# ============================================================================= # =============================================================================
@ -1665,19 +1703,8 @@ class TestCloudflare:
assert wait_for_easyhaproxy_discovery(kubectl, "myapp.example.local", timeout=30), \ assert wait_for_easyhaproxy_discovery(kubectl, "myapp.example.local", timeout=30), \
"EasyHAProxy did not become ready for myapp.example.local within 30 seconds" "EasyHAProxy did not become ready for myapp.example.local within 30 seconds"
# Test HTTP request # Wait for a valid JSON response (retries until backend is ready)
result = subprocess.run( data = wait_for_json_response("myapp.example.local", timeout=30)
["curl", "-s", "-H", "Host: myapp.example.local",
f"http://localhost:{HTTP_PORT}"],
capture_output=True,
text=True,
timeout=10
)
assert result.returncode == 0, f"Curl failed with return code {result.returncode}"
# Parse JSON response
data = json.loads(result.stdout)
# Verify JSON structure # Verify JSON structure
assert "headers" in data, "Response should contain 'headers' field" assert "headers" in data, "Response should contain 'headers' field"
@ -1692,23 +1719,14 @@ class TestCloudflare:
assert wait_for_easyhaproxy_discovery(kubectl, "myapp.example.local", timeout=30), \ assert wait_for_easyhaproxy_discovery(kubectl, "myapp.example.local", timeout=30), \
"EasyHAProxy did not become ready within 30 seconds" "EasyHAProxy did not become ready within 30 seconds"
# Send request with CF-Connecting-IP header # Send request with CF-Connecting-IP header, wait for valid JSON response
test_ip = "203.0.113.50" test_ip = "203.0.113.50"
result = subprocess.run( data = wait_for_json_response(
["curl", "-s", "myapp.example.local",
"-H", "Host: myapp.example.local", extra_headers=[f"CF-Connecting-IP: {test_ip}"],
"-H", f"CF-Connecting-IP: {test_ip}", timeout=30
f"http://localhost:{HTTP_PORT}"],
capture_output=True,
text=True,
timeout=10
) )
assert result.returncode == 0, f"Curl failed"
# Parse JSON response from header-echo server
data = json.loads(result.stdout)
# VERIFY: X-Forwarded-For was set to the CF-Connecting-IP value # VERIFY: X-Forwarded-For was set to the CF-Connecting-IP value
# This proves the Cloudflare plugin actually works, not just that config exists # This proves the Cloudflare plugin actually works, not just that config exists
assert data['x_forwarded_for'] == test_ip, \ assert data['x_forwarded_for'] == test_ip, \