import os import pathlib import subprocess ROOT = pathlib.Path(__file__).resolve().parents[2] SCRIPT = ROOT / "scripts" / "deploy-k8s.sh" DEPLOY_ENV_KEYS = { "AI_API_BASE_URL", "AI_API_KEY", "AI_MODEL", "API_NODE_PORT", "API_PUBLIC_LOAD_BALANCER", "API_PUBLIC_SERVICE_NAME", "API_IMAGE", "BOS_ENDPOINT", "CREATE_REGISTRY_SECRET", "DEPLOY_ENV", "DROBOTICFLOW_API_TOKEN", "DROBOTICFLOW_REQUIRE_SESSION_AUTH", "DROBOTICFLOW_SESSION_COOKIE_SECURE", "DROBOTICFLOW_BUILD_COMMIT", "DROBOTICFLOW_BUILD_TIME", "DROSYNC_COORDINATOR_IMAGE", "IMAGE_TAG", "K8S_IMAGE_PULL_SECRET_NAME", "K8S_NAMESPACE", "MONGO_URI", "OPERATOR_RUNNER_IMAGE", "OPERATOR_RUNNER_BUILD_COMMIT", "OPERATOR_RUNNER_BUILD_TIME", "REGISTRY_HOST", "REGISTRY_PASSWORD", "REGISTRY_SERVER", "REGISTRY_NAMESPACE", "REGISTRY_USERNAME", "WORKER_API_BASE_URL", "WORKER_IMAGE", "WORKSPACE_MOUNT_PATH", "WORKSPACE_PVC_NAME", } def write_fake_kubectl(tmp_path: pathlib.Path, calls_path: pathlib.Path) -> pathlib.Path: fake_bin = tmp_path / "bin" fake_bin.mkdir() kubectl = fake_bin / "kubectl" kubectl.write_text( "#!/usr/bin/env python3\n" "import base64\n" "import os\n" "import pathlib\n" "import sys\n" f"calls_path = pathlib.Path({str(calls_path)!r})\n" "args = sys.argv[1:]\n" "with calls_path.open('a', encoding='utf-8') as handle:\n" " handle.write(' '.join(args) + '\\n')\n" "def strip_timeout(values):\n" " return values[1:] if values and values[0].startswith('--request-timeout=') else values\n" "def command_after_namespace(values):\n" " values = strip_timeout(values)\n" " return values[2:] if len(values) >= 2 and values[0] == '-n' else values\n" "def output_jsonpath(command, mapping):\n" " if '-o' not in command:\n" " print('ok')\n" " raise SystemExit(0)\n" " output = command[command.index('-o') + 1] if command.index('-o') + 1 < len(command) else ''\n" " for key, value in mapping.items():\n" " if output == f'jsonpath={{.data.{key}}}':\n" " print(value)\n" " raise SystemExit(0)\n" " raise SystemExit(1)\n" "if args[:2] == ['get', 'namespace']:\n" " raise SystemExit(0)\n" "if args and args[0] == 'apply':\n" " sys.stdin.read()\n" " print('applied')\n" " raise SystemExit(0)\n" "command = command_after_namespace(args)\n" "if len(command) >= 2:\n" " if command[:2] == ['create', 'secret']:\n" " print('apiVersion: v1\\nkind: Secret\\nmetadata:\\n name: fake')\n" " raise SystemExit(0)\n" " if command[:2] == ['get', 'secret']:\n" " name = command[2] if len(command) > 2 else ''\n" " if name == 'droboticflow-ai-secret' and os.environ.get('FAKE_EXISTING_AI_API_KEY'):\n" " encoded = base64.b64encode(os.environ['FAKE_EXISTING_AI_API_KEY'].encode('utf-8')).decode('ascii')\n" " output_jsonpath(command, {'AI_API_KEY': encoded})\n" " if name == 'droboticflow-auth-secret' and os.environ.get('FAKE_EXISTING_API_TOKEN'):\n" " encoded = base64.b64encode(os.environ['FAKE_EXISTING_API_TOKEN'].encode('utf-8')).decode('ascii')\n" " output_jsonpath(command, {'DROBOTICFLOW_API_TOKEN': encoded})\n" " raise SystemExit(1)\n" " if command[:2] == ['get', 'configmap']:\n" " name = command[2] if len(command) > 2 else ''\n" " if name == 'droboticflow-config':\n" " output_jsonpath(command, {\n" " 'AI_API_BASE_URL': os.environ.get('FAKE_EXISTING_AI_API_BASE_URL', ''),\n" " 'AI_MODEL': os.environ.get('FAKE_EXISTING_AI_MODEL', ''),\n" " })\n" " raise SystemExit(1)\n" " if command[:2] == ['rollout', 'status']:\n" " print('rollout ok')\n" " raise SystemExit(0)\n" " if command[:1] == ['get']:\n" " print('ok')\n" " raise SystemExit(0)\n" "print('unexpected kubectl call: ' + ' '.join(args), file=sys.stderr)\n" "raise SystemExit(97)\n", encoding="utf-8", ) kubectl.chmod(0o755) return fake_bin def run_deploy(tmp_path: pathlib.Path, extra_env: dict[str, str]) -> tuple[subprocess.CompletedProcess, pathlib.Path]: calls_path = tmp_path / "kubectl-calls.log" fake_bin = write_fake_kubectl(tmp_path, calls_path) env = os.environ.copy() for key in DEPLOY_ENV_KEYS: env.pop(key, None) env.update( { "PATH": f"{fake_bin}{os.pathsep}{env.get('PATH', '')}", "DROBOTICFLOW_DEPLOY_ENV_LOADED": "test", "K8S_NAMESPACE": "droboticflow-test", "MONGO_URI": "mongodb://mongo.example.net:27017/droboticflow", "API_IMAGE": "registry.example.com/dataflow/droboticflow-api:test", "WORKER_IMAGE": "registry.example.com/dataflow/droboticflow-worker:test", "DROSYNC_COORDINATOR_IMAGE": "registry.example.com/dataflow/drosync-coordinator:test", "OPERATOR_RUNNER_IMAGE": "registry.example.com/dataflow/droboticflow-operator-runner:test", } ) env.update(extra_env) result = subprocess.run(["bash", str(SCRIPT)], cwd=ROOT, env=env, text=True, capture_output=True, check=False) return result, calls_path def test_deploy_rejects_invalid_manifest_config_before_kubectl(tmp_path): result, calls_path = run_deploy( tmp_path, { "API_NODE_PORT": "80", "CREATE_REGISTRY_SECRET": "on", "REGISTRY_USERNAME": "robot", "REGISTRY_PASSWORD": "secret", }, ) assert result.returncode == 1, result.stderr + result.stdout assert "API_NODE_PORT must be greater than or equal to 30000" in result.stderr assert not calls_path.exists(), calls_path.read_text(encoding="utf-8") if calls_path.exists() else "" def test_deploy_rejects_invalid_public_service_name_before_kubectl(tmp_path): result, calls_path = run_deploy( tmp_path, { "API_PUBLIC_LOAD_BALANCER": "yes", "API_PUBLIC_SERVICE_NAME": "Droboticflow_Public", "CREATE_REGISTRY_SECRET": "off", "DROBOTICFLOW_API_TOKEN": "acceptance-token", }, ) assert result.returncode == 1, result.stderr + result.stdout assert "API_PUBLIC_SERVICE_NAME must be a lowercase DNS subdomain" in result.stderr assert not calls_path.exists(), calls_path.read_text(encoding="utf-8") if calls_path.exists() else "" def test_deploy_rejects_invalid_bos_endpoint_before_kubectl(tmp_path): result, calls_path = run_deploy( tmp_path, { "BOS_ENDPOINT": "bj.bcebos.com", "CREATE_REGISTRY_SECRET": "off", }, ) assert result.returncode == 1, result.stderr + result.stdout assert "BOS_ENDPOINT must be an absolute http(s) URL" in result.stderr assert not calls_path.exists(), calls_path.read_text(encoding="utf-8") if calls_path.exists() else "" def test_deploy_rejects_ai_key_without_explicit_runtime_before_kubectl(tmp_path): result, calls_path = run_deploy( tmp_path, { "AI_API_KEY": "secret", "AI_MODEL": "model-x", "CREATE_REGISTRY_SECRET": "off", }, ) assert result.returncode == 1, result.stderr + result.stdout assert "AI_API_BASE_URL is required when AI_API_KEY is configured" in result.stderr assert not calls_path.exists(), calls_path.read_text(encoding="utf-8") if calls_path.exists() else "" def test_deploy_reuses_existing_ai_secret_and_configmap_runtime(tmp_path): result, calls_path = run_deploy( tmp_path, { "CREATE_REGISTRY_SECRET": "off", "FAKE_EXISTING_AI_API_KEY": "existing-secret", "FAKE_EXISTING_AI_API_BASE_URL": "https://ai.example/v1", "FAKE_EXISTING_AI_MODEL": "model-x", }, ) assert result.returncode == 0, result.stderr + result.stdout calls = calls_path.read_text(encoding="utf-8") assert "--request-timeout=5s -n droboticflow-test get secret droboticflow-ai-secret -o jsonpath={.data.AI_API_KEY}" in calls assert "--request-timeout=5s -n droboticflow-test get configmap droboticflow-config -o jsonpath={.data.AI_API_BASE_URL}" in calls assert "--request-timeout=5s -n droboticflow-test get configmap droboticflow-config -o jsonpath={.data.AI_MODEL}" in calls def test_deploy_requires_explicit_images_or_registry_configuration_before_kubectl(tmp_path): result, calls_path = run_deploy( tmp_path, { "API_IMAGE": "", "WORKER_IMAGE": "", "OPERATOR_RUNNER_IMAGE": "", "CREATE_REGISTRY_SECRET": "off", }, ) assert result.returncode == 1, result.stderr + result.stdout assert "API_IMAGE is required unless REGISTRY_HOST and REGISTRY_NAMESPACE are both set" in result.stderr assert not calls_path.exists(), calls_path.read_text(encoding="utf-8") if calls_path.exists() else "" def test_deploy_derives_images_from_explicit_registry_configuration(tmp_path): result, calls_path = run_deploy( tmp_path, { "API_IMAGE": "", "WORKER_IMAGE": "", "OPERATOR_RUNNER_IMAGE": "", "REGISTRY_HOST": "registry.example.com", "REGISTRY_NAMESPACE": "dataflow-poc", "IMAGE_TAG": "release-20260617", "CREATE_REGISTRY_SECRET": "off", }, ) assert result.returncode == 0, result.stderr + result.stdout calls = calls_path.read_text(encoding="utf-8") assert "apply --dry-run=client -f" in calls assert "-n droboticflow-test get pods -l app in (droboticflow-api,droboticflow-worker,droboticflow-headpose)" in calls assert "\n-n droboticflow-test get pods\n" not in calls def test_deploy_treats_public_load_balancer_boolean_alias_as_true(tmp_path): result, calls_path = run_deploy( tmp_path, { "API_PUBLIC_LOAD_BALANCER": "yes", "API_PUBLIC_SERVICE_NAME": "droboticflow-api-public-test", "CREATE_REGISTRY_SECRET": "off", "DROBOTICFLOW_API_TOKEN": "acceptance-token", }, ) assert result.returncode == 0, result.stderr + result.stdout calls = calls_path.read_text(encoding="utf-8") assert "-n droboticflow-test get svc droboticflow-api-public-test" in calls def test_poc_preset_keeps_http_session_cookies_usable(tmp_path): env = os.environ.copy() for key in DEPLOY_ENV_KEYS | {"DROBOTICFLOW_DEPLOY_ENV_LOADED", "HOME", "XDG_CONFIG_HOME"}: env.pop(key, None) env.update( { "DEPLOY_ENV": "poc", "HOME": str(tmp_path / "home"), "XDG_CONFIG_HOME": str(tmp_path / "xdg"), } ) result = subprocess.run( ["bash", "-lc", "source scripts/load-deploy-env.sh && printf '%s' \"$DROBOTICFLOW_SESSION_COOKIE_SECURE\""], cwd=ROOT, env=env, text=True, capture_output=True, check=False, ) assert result.returncode == 0, result.stderr + result.stdout assert result.stdout == "false"