|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +import subprocess |
| 4 | + |
| 5 | + |
| 6 | +SCRIPT = ".github/scripts/modal-custom-domain-smoke.sh" |
| 7 | + |
| 8 | + |
| 9 | +def test_modal_custom_domain_smoke_passes_when_versions_match(tmp_path): |
| 10 | + env = _smoke_env( |
| 11 | + tmp_path, |
| 12 | + gateway_versions='{"current":"1.691.1","frontier":"1.691.1"}', |
| 13 | + custom_versions='{"current":"1.691.1","frontier":"1.691.1"}', |
| 14 | + ) |
| 15 | + |
| 16 | + result = subprocess.run( |
| 17 | + ["bash", SCRIPT], |
| 18 | + capture_output=True, |
| 19 | + env=env, |
| 20 | + text=True, |
| 21 | + ) |
| 22 | + |
| 23 | + assert result.returncode == 0, result.stderr |
| 24 | + assert ( |
| 25 | + "Custom domain points at the deployed Modal gateway." in result.stdout |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def test_modal_custom_domain_smoke_fails_when_custom_domain_is_not_gateway( |
| 30 | + tmp_path, |
| 31 | +): |
| 32 | + env = _smoke_env( |
| 33 | + tmp_path, |
| 34 | + gateway_versions='{"current":"1.691.1","frontier":"1.691.1"}', |
| 35 | + custom_versions="OK", |
| 36 | + ) |
| 37 | + |
| 38 | + result = subprocess.run( |
| 39 | + ["bash", SCRIPT], |
| 40 | + capture_output=True, |
| 41 | + env=env, |
| 42 | + text=True, |
| 43 | + ) |
| 44 | + |
| 45 | + assert result.returncode == 1 |
| 46 | + assert "Custom domain /versions/us did not return JSON" in result.stderr |
| 47 | + |
| 48 | + |
| 49 | +def test_modal_custom_domain_smoke_fails_when_versions_differ(tmp_path): |
| 50 | + env = _smoke_env( |
| 51 | + tmp_path, |
| 52 | + gateway_versions='{"current":"1.691.1","frontier":"1.691.1"}', |
| 53 | + custom_versions='{"current":"1.690.0","frontier":"1.691.1"}', |
| 54 | + ) |
| 55 | + |
| 56 | + result = subprocess.run( |
| 57 | + ["bash", SCRIPT], |
| 58 | + capture_output=True, |
| 59 | + env=env, |
| 60 | + text=True, |
| 61 | + ) |
| 62 | + |
| 63 | + assert result.returncode == 1 |
| 64 | + assert "does not match the generated Modal gateway" in result.stderr |
| 65 | + |
| 66 | + |
| 67 | +def test_modal_custom_domain_smoke_skips_non_main_environments(tmp_path): |
| 68 | + curl_log = tmp_path / "curl.log" |
| 69 | + env = _smoke_env( |
| 70 | + tmp_path, |
| 71 | + gateway_versions='{"current":"1.691.1"}', |
| 72 | + custom_versions='{"current":"1.691.1"}', |
| 73 | + ) |
| 74 | + env["MODAL_ENVIRONMENT"] = "staging" |
| 75 | + env["CURL_LOG"] = str(curl_log) |
| 76 | + |
| 77 | + result = subprocess.run( |
| 78 | + ["bash", SCRIPT], |
| 79 | + capture_output=True, |
| 80 | + env=env, |
| 81 | + text=True, |
| 82 | + ) |
| 83 | + |
| 84 | + assert result.returncode == 0 |
| 85 | + assert "Skipping custom-domain smoke check" in result.stdout |
| 86 | + assert not curl_log.exists() |
| 87 | + |
| 88 | + |
| 89 | +def _smoke_env( |
| 90 | + tmp_path: Path, |
| 91 | + *, |
| 92 | + gateway_versions: str, |
| 93 | + custom_versions: str, |
| 94 | +) -> dict[str, str]: |
| 95 | + get_url_script = tmp_path / "modal-get-url.sh" |
| 96 | + get_url_script.write_text( |
| 97 | + "#!/usr/bin/env bash\n" |
| 98 | + "set -euo pipefail\n" |
| 99 | + "echo https://generated-modal.example\n" |
| 100 | + ) |
| 101 | + get_url_script.chmod(0o755) |
| 102 | + |
| 103 | + fake_curl = tmp_path / "curl" |
| 104 | + fake_curl.write_text( |
| 105 | + """#!/usr/bin/env bash |
| 106 | +set -euo pipefail |
| 107 | +url="${@: -1}" |
| 108 | +if [ -n "${CURL_LOG:-}" ]; then |
| 109 | + printf '%s\\n' "${url}" >> "${CURL_LOG}" |
| 110 | +fi |
| 111 | +
|
| 112 | +case "${url}" in |
| 113 | + https://generated-modal.example/liveness_check|https://custom-domain.example/liveness_check) |
| 114 | + echo OK |
| 115 | + ;; |
| 116 | + https://generated-modal.example/versions/us) |
| 117 | + printf '%s\\n' "${GATEWAY_VERSIONS}" |
| 118 | + ;; |
| 119 | + https://custom-domain.example/versions/us) |
| 120 | + printf '%s\\n' "${CUSTOM_VERSIONS}" |
| 121 | + ;; |
| 122 | + *) |
| 123 | + echo "Unexpected URL: ${url}" >&2 |
| 124 | + exit 22 |
| 125 | + ;; |
| 126 | +esac |
| 127 | +""" |
| 128 | + ) |
| 129 | + fake_curl.chmod(0o755) |
| 130 | + |
| 131 | + return { |
| 132 | + **os.environ, |
| 133 | + "PATH": f"{tmp_path}:{os.environ['PATH']}", |
| 134 | + "MODAL_ENVIRONMENT": "main", |
| 135 | + "MODAL_GET_URL_SCRIPT": str(get_url_script), |
| 136 | + "HOUSEHOLD_MODAL_GATEWAY_CUSTOM_DOMAIN_URL": ( |
| 137 | + "https://custom-domain.example" |
| 138 | + ), |
| 139 | + "GATEWAY_VERSIONS": gateway_versions, |
| 140 | + "CUSTOM_VERSIONS": custom_versions, |
| 141 | + } |
0 commit comments