|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +import os |
| 4 | +import re |
| 5 | +import signal |
| 6 | +import socket |
| 7 | +import subprocess |
| 8 | +import tempfile |
| 9 | +import time |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +cli = os.environ["CLI"] |
| 13 | +environment = { |
| 14 | + key: value |
| 15 | + for key, value in os.environ.items() |
| 16 | + if not key.startswith(("DATABRICKS_", "ARM_", "AZURE_", "AWS_", "GOOGLE_")) |
| 17 | +} |
| 18 | +with tempfile.TemporaryDirectory() as temporary: |
| 19 | + directory = Path(temporary) |
| 20 | + environment["HOME"] = temporary |
| 21 | + environment["DATABRICKS_CONFIG_FILE"] = str(directory / "missing-config") |
| 22 | + child_pid = directory / "child.pid" |
| 23 | + sshd = directory / "sshd" |
| 24 | + # A long-running child exercises cancellation without requiring system sshd. |
| 25 | + sshd.write_text('#!/bin/sh\nif [ "$1" = "-t" ]; then exit 0; fi\necho $$ > "' + str(child_pid) + '"\nexec cat\n') |
| 26 | + sshd.chmod(0o700) |
| 27 | + help_result = subprocess.run( |
| 28 | + [cli, "sandbox", "--help"], env=environment, capture_output=True, text=True, check=True |
| 29 | + ) |
| 30 | + assert not re.search(r"^\s+server\s", help_result.stdout, re.MULTILINE) |
| 31 | + print("Server remains hidden from sandbox help") |
| 32 | + process = subprocess.Popen( |
| 33 | + [cli, "sandbox", "server", "--port", "0", "--sshd", str(sshd), "--sshd-config", "prepared-config"], |
| 34 | + env=environment, |
| 35 | + stdout=subprocess.PIPE, |
| 36 | + stderr=subprocess.PIPE, |
| 37 | + text=True, |
| 38 | + ) |
| 39 | + try: |
| 40 | + ready = json.loads(process.stdout.readline()) |
| 41 | + assert 10000 <= ready["port"] <= 65535 |
| 42 | + assert ready["token"] |
| 43 | + print("Server starts without workspace credentials") |
| 44 | + with socket.create_connection(("127.0.0.1", ready["port"]), timeout=5) as connection: |
| 45 | + key = base64.b64encode(os.urandom(16)).decode() |
| 46 | + connection.sendall( |
| 47 | + ( |
| 48 | + "GET /ssh?id=acceptance HTTP/1.1\r\nHost: localhost\r\n" |
| 49 | + "Upgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\n" |
| 50 | + f"Sec-WebSocket-Key: {key}\r\nX-Sandbox-SSH-Token: {ready['token']}\r\n\r\n" |
| 51 | + ).encode() |
| 52 | + ) |
| 53 | + assert connection.recv(4096).startswith(b"HTTP/1.1 101") |
| 54 | + deadline = time.monotonic() + 5 |
| 55 | + while not child_pid.exists(): |
| 56 | + assert time.monotonic() < deadline, "sshd child did not start" |
| 57 | + time.sleep(0.01) |
| 58 | + pid = int(child_pid.read_text()) |
| 59 | + process.send_signal(signal.SIGTERM) |
| 60 | + assert process.wait(timeout=10) == 1 # context cancellation, rather than signal termination |
| 61 | + try: |
| 62 | + os.kill(pid, 0) |
| 63 | + except ProcessLookupError: |
| 64 | + pass |
| 65 | + else: |
| 66 | + raise AssertionError("sshd child survived server shutdown") |
| 67 | + print("SIGTERM stops the server and its SSH child") |
| 68 | + finally: |
| 69 | + if process.poll() is None: |
| 70 | + process.kill() |
| 71 | + process.wait() |
0 commit comments