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