Spaces:
Running
Running
File size: 8,271 Bytes
f24e94d 3fac9f1 f24e94d c125dfd f24e94d | 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | from __future__ import annotations
import os
import shutil
import subprocess
import tempfile
import textwrap
from pathlib import Path
import unittest
class StartServerScriptTests(unittest.TestCase):
def test_conda_env_uvicorn_is_preferred_on_macos_when_available(self):
repo_root = Path(__file__).resolve().parent.parent
source_script = repo_root / "start-server.sh"
with tempfile.TemporaryDirectory() as tmp:
temp_root = Path(tmp)
shutil.copy(source_script, temp_root / "start-server.sh")
os.chmod(temp_root / "start-server.sh", 0o755)
fake_bin = temp_root / "bin"
fake_bin.mkdir()
fake_uname = fake_bin / "uname"
fake_uname.write_text("#!/usr/bin/env bash\nprintf 'Darwin\\n'\n", encoding="utf-8")
os.chmod(fake_uname, 0o755)
conda_uvicorn = temp_root / ".conda-env" / "bin" / "uvicorn"
conda_uvicorn.parent.mkdir(parents=True)
conda_uvicorn.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
set -euo pipefail
printf 'UVICORN=conda\n'
printf 'ARGS=%s\n' "$*"
"""
),
encoding="utf-8",
)
os.chmod(conda_uvicorn, 0o755)
venv_uvicorn = temp_root / ".venv" / "bin" / "uvicorn"
venv_uvicorn.parent.mkdir(parents=True)
venv_uvicorn.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
set -euo pipefail
printf 'UVICORN=venv\n'
"""
),
encoding="utf-8",
)
os.chmod(venv_uvicorn, 0o755)
result = subprocess.run(
[str(temp_root / "start-server.sh"), "dev"],
cwd=temp_root,
env={**os.environ, "PATH": f"{fake_bin}{os.pathsep}{os.environ['PATH']}"},
capture_output=True,
text=True,
check=False,
)
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertIn("UVICORN=conda", result.stdout)
self.assertNotIn("UVICORN=venv", result.stdout)
self.assertIn("ARGS=app:app --host 127.0.0.1 --port 8000 --reload", result.stdout)
def test_venv_uvicorn_is_preferred_on_linux_even_when_conda_env_exists(self):
repo_root = Path(__file__).resolve().parent.parent
source_script = repo_root / "start-server.sh"
with tempfile.TemporaryDirectory() as tmp:
temp_root = Path(tmp)
shutil.copy(source_script, temp_root / "start-server.sh")
os.chmod(temp_root / "start-server.sh", 0o755)
fake_bin = temp_root / "bin"
fake_bin.mkdir()
fake_uname = fake_bin / "uname"
fake_uname.write_text("#!/usr/bin/env bash\nprintf 'Linux\\n'\n", encoding="utf-8")
os.chmod(fake_uname, 0o755)
conda_uvicorn = temp_root / ".conda-env" / "bin" / "uvicorn"
conda_uvicorn.parent.mkdir(parents=True)
conda_uvicorn.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
set -euo pipefail
printf 'UVICORN=conda\n'
"""
),
encoding="utf-8",
)
os.chmod(conda_uvicorn, 0o755)
venv_uvicorn = temp_root / ".venv" / "bin" / "uvicorn"
venv_uvicorn.parent.mkdir(parents=True)
venv_uvicorn.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
set -euo pipefail
printf 'UVICORN=venv\n'
printf 'ARGS=%s\n' "$*"
"""
),
encoding="utf-8",
)
os.chmod(venv_uvicorn, 0o755)
result = subprocess.run(
[str(temp_root / "start-server.sh"), "dev"],
cwd=temp_root,
env={**os.environ, "PATH": f"{fake_bin}{os.pathsep}{os.environ['PATH']}"},
capture_output=True,
text=True,
check=False,
)
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertIn("UVICORN=venv", result.stdout)
self.assertNotIn("UVICORN=conda", result.stdout)
self.assertIn("ARGS=app:app --host 127.0.0.1 --port 8000 --reload", result.stdout)
def test_model_weights_path_flag_is_forwarded_to_bucklakeai(self):
repo_root = Path(__file__).resolve().parent.parent
source_script = repo_root / "start-server.sh"
with tempfile.TemporaryDirectory() as tmp:
temp_root = Path(tmp)
shutil.copy(source_script, temp_root / "start-server.sh")
os.chmod(temp_root / "start-server.sh", 0o755)
fake_uvicorn = temp_root / ".venv" / "bin" / "uvicorn"
fake_uvicorn.parent.mkdir(parents=True)
fake_uvicorn.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
set -euo pipefail
printf 'BL_MODEL_WEIGHTS_PATH=%s\n' "${BL_MODEL_WEIGHTS_PATH:-}"
printf 'BL_MODEL_VERSION=%s\n' "${BL_MODEL_VERSION:-}"
printf 'ARGS=%s\n' "$*"
"""
),
encoding="utf-8",
)
os.chmod(fake_uvicorn, 0o755)
result = subprocess.run(
[
str(temp_root / "start-server.sh"),
"--model-weights-path",
"/tmp/local/stock_prediction_model_anchored-path-v1_best.keras",
"dev",
],
cwd=temp_root,
capture_output=True,
text=True,
check=False,
)
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertIn(
"BL_MODEL_WEIGHTS_PATH=/tmp/local/stock_prediction_model_anchored-path-v1_best.keras",
result.stdout,
)
self.assertIn("BL_MODEL_VERSION=anchored-path-v1", result.stdout)
self.assertIn("ARGS=app:app --host 127.0.0.1 --port 8000 --reload", result.stdout)
def test_model_weights_path_flag_infers_anchored_path_v2(self):
repo_root = Path(__file__).resolve().parent.parent
source_script = repo_root / "start-server.sh"
with tempfile.TemporaryDirectory() as tmp:
temp_root = Path(tmp)
shutil.copy(source_script, temp_root / "start-server.sh")
os.chmod(temp_root / "start-server.sh", 0o755)
fake_uvicorn = temp_root / ".venv" / "bin" / "uvicorn"
fake_uvicorn.parent.mkdir(parents=True)
fake_uvicorn.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
set -euo pipefail
printf 'BL_MODEL_WEIGHTS_PATH=%s\n' "${BL_MODEL_WEIGHTS_PATH:-}"
printf 'BL_MODEL_VERSION=%s\n' "${BL_MODEL_VERSION:-}"
"""
),
encoding="utf-8",
)
os.chmod(fake_uvicorn, 0o755)
result = subprocess.run(
[
str(temp_root / "start-server.sh"),
"--model-weights-path",
"/tmp/local/stock_prediction_model_anchored-path-v2_best.keras",
"dev",
],
cwd=temp_root,
capture_output=True,
text=True,
check=False,
)
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertIn("BL_MODEL_VERSION=anchored-path-v2", result.stdout)
if __name__ == "__main__":
unittest.main()
|