"""Verify the native release in an isolated directory on the workspace filesystem.""" import http.cookiejar import json import os from pathlib import Path import secrets import shutil import socket import sqlite3 import subprocess import tarfile import tempfile import time import urllib.request project = Path(__file__).resolve().parent.parent stage = Path(tempfile.mkdtemp(prefix=".bluebird-smoke-", dir=project)) server = None try: with tarfile.open(project / "build/bluebird-linux-amd64.tar.gz") as archive: archive.extractall(stage, filter="data") with socket.socket() as sock: sock.bind(("127.0.0.1", 0)) port = sock.getsockname()[1] base = f"http://127.0.0.1:{port}" config = stage / "config.toml" source = (config.read_text().replace("127.0.0.1:8080", f"127.0.0.1:{port}") .replace("http://localhost:8080", base) .replace("min_free_bytes = 1073741824", "min_free_bytes = 0") .replace("encryption = false", "encryption = true")) config.write_text(source) binary = str(stage / "bluebird") password = secrets.token_urlsafe(24) env = os.environ.copy() env["BLUEBIRD_ADMIN_PASSWORD"] = password subprocess.run([binary, "init", "--config", str(config)], env=env, check=True, capture_output=True, timeout=15) server = subprocess.Popen([binary, "serve", "--config", str(config)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) opener = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())) def call(method, path, body=None, raw=False): headers = {} if body is not None: if not raw: body = json.dumps(body).encode() headers = {"Origin": base, "Content-Type": "application/octet-stream" if raw else "application/json"} request = urllib.request.Request(base + "/api/v1" + path, data=body, headers=headers, method=method) with opener.open(request, timeout=10) as response: return json.loads(response.read()) for _ in range(50): try: assert call("GET", "/health")["ready"] break except (OSError, AssertionError): time.sleep(.1) else: raise AssertionError("Release server did not start") with opener.open(base + "/", timeout=10) as response: assert b'