droboticflow/tests/backend/render_manifests_test.py
2026-08-31 13:27:18 +08:00

256 lines
11 KiB
Python

import os
import pathlib
import subprocess
ROOT = pathlib.Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "render-manifests.sh"
CONFIG_ENV_KEYS = {
"AI_API_BASE_URL",
"AI_API_KEY",
"AI_MODEL",
"API_IMAGE",
"BOS_ENDPOINT",
"MONGO_CONNECT_RETRIES",
"MONGO_CONNECT_RETRY_DELAY_MS",
"WORKER_POLL_INTERVAL_MS",
"API_NODE_PORT",
"API_REPLICAS",
"WORKER_REPLICAS",
"API_PUBLIC_LOAD_BALANCER",
"OPERATOR_JOB_TTL_SECONDS",
"DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED",
"DROBOTICFLOW_QWEN_NODE_CACHE_PVC",
"DROBOTICFLOW_QWEN_NODE_CACHE_MOUNT_PATH",
"DROBOTICFLOW_QWEN_NODE_CACHE_NODE_SELECTOR",
"DROBOTICFLOW_QWEN_NODE_CACHE_STALE_STAGING_SECONDS",
"OPERATOR_ALLOW_SHELL_COMMANDS",
"DROBOTICFLOW_REQUIRE_SESSION_AUTH",
"DROBOTICFLOW_SESSION_COOKIE_SECURE",
"DROBOTICFLOW_BUILD_COMMIT",
"DROBOTICFLOW_BUILD_TIME",
"DROSYNC_COORDINATOR_IMAGE",
"DEPLOY_ENV",
"IMAGE_TAG",
"K8S_IMAGE_PULL_SECRET_NAME",
"K8S_NAMESPACE",
"OPERATOR_COMMAND_ALLOWLIST_REGEX",
"OPERATOR_COMMAND_DENYLIST_REGEX",
"OPERATOR_GPU_TOLERATIONS_JSON",
"OPERATOR_RUNNER_IMAGE",
"OPERATOR_RUNNER_BUILD_COMMIT",
"OPERATOR_RUNNER_BUILD_TIME",
"REGISTRY_HOST",
"REGISTRY_NAMESPACE",
"WORKER_API_BASE_URL",
"WORKER_IMAGE",
"WORKSPACE_MOUNT_PATH",
"WORKSPACE_PVC_NAME",
}
DEFAULT_IMAGE_ENV = {
"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",
}
def run_render_manifests(extra_env: dict[str, str], *, include_default_images: bool = True) -> subprocess.CompletedProcess:
env = os.environ.copy()
for key in CONFIG_ENV_KEYS:
env.pop(key, None)
env["DROBOTICFLOW_DEPLOY_ENV_LOADED"] = "test"
if include_default_images:
env.update(DEFAULT_IMAGE_ENV)
env.update(extra_env)
return subprocess.run(["bash", str(SCRIPT)], cwd=ROOT, env=env, text=True, capture_output=True, check=False)
def assert_render_validation_failure(result: subprocess.CompletedProcess, message: str) -> None:
assert result.returncode == 1, result.stderr + result.stdout
assert message in result.stderr
assert "apiVersion:" not in result.stdout
def test_render_manifests_accepts_valid_startup_configuration():
result = run_render_manifests(
{
"MONGO_CONNECT_RETRIES": "2",
"MONGO_CONNECT_RETRY_DELAY_MS": "100",
"WORKER_POLL_INTERVAL_MS": "500",
"API_REPLICAS": "1",
"WORKER_REPLICAS": "0",
"API_NODE_PORT": "30081",
"API_PUBLIC_LOAD_BALANCER": "false",
"BOS_ENDPOINT": "https://gz.bcebos.com",
"OPERATOR_JOB_TTL_SECONDS": "0",
"OPERATOR_ALLOW_SHELL_COMMANDS": "off",
"DROBOTICFLOW_REQUIRE_SESSION_AUTH": "yes",
"DROBOTICFLOW_SESSION_COOKIE_SECURE": "no",
"DROBOTICFLOW_BUILD_COMMIT": "api-worker-commit",
"DROBOTICFLOW_BUILD_TIME": "2026-07-10T08:00:00Z",
"OPERATOR_RUNNER_BUILD_COMMIT": "explicit-runner-commit",
"OPERATOR_RUNNER_BUILD_TIME": "2026-07-10T07:59:00Z",
}
)
assert result.returncode == 0, result.stderr + result.stdout
assert 'BOS_ENDPOINT: "https://gz.bcebos.com"' in result.stdout
assert 'WORKER_POLL_INTERVAL_MS: "500"' in result.stdout
assert 'nodePort: 30081' in result.stdout
assert 'OPERATOR_JOB_TTL_SECONDS: "0"' in result.stdout
assert 'DROBOTICFLOW_REQUIRE_SESSION_AUTH: "yes"' in result.stdout
assert 'DROBOTICFLOW_BUILD_COMMIT: "api-worker-commit"' in result.stdout
assert 'OPERATOR_RUNNER_BUILD_COMMIT: "explicit-runner-commit"' in result.stdout
assert 'OPERATOR_RUNNER_BUILD_TIME: "2026-07-10T07:59:00Z"' in result.stdout
assert "- name: OPERATOR_RUNNER_BUILD_COMMIT" in result.stdout
assert "key: OPERATOR_RUNNER_BUILD_TIME" in result.stdout
assert result.stdout.count("- name: DROSYNC_COORDINATOR_PORT") == 3
assert "name: droboticflow-worker-node-reader-droboticflow" in result.stdout
assert 'resources: ["nodes"]' in result.stdout
assert 'verbs: ["get", "list"]' in result.stdout
assert "kind: ClusterRoleBinding" in result.stdout
def test_render_manifests_derives_images_from_explicit_registry_configuration():
result = run_render_manifests(
{
"REGISTRY_HOST": "registry.example.com",
"REGISTRY_NAMESPACE": "dataflow-poc",
"IMAGE_TAG": "release-20260617",
},
include_default_images=False,
)
assert result.returncode == 0, result.stderr + result.stdout
assert "image: registry.example.com/dataflow-poc/droboticflow-api:release-20260617" in result.stdout
assert "image: registry.example.com/dataflow-poc/droboticflow-worker:release-20260617" in result.stdout
assert 'OPERATOR_RUNNER_IMAGE: "registry.example.com/dataflow-poc/droboticflow-operator-runner:release-20260617"' in result.stdout
def test_render_manifests_requires_explicit_images_or_registry_configuration():
result = run_render_manifests({}, include_default_images=False)
assert_render_validation_failure(
result,
"API_IMAGE is required unless REGISTRY_HOST and REGISTRY_NAMESPACE are both set",
)
def test_render_manifests_allows_empty_optional_global_runtime_config():
result = run_render_manifests({})
assert result.returncode == 0, result.stderr + result.stdout
assert 'BOS_ENDPOINT: ""' in result.stdout
assert 'AI_API_BASE_URL: ""' in result.stdout
assert 'AI_MODEL: ""' in result.stdout
assert 'DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED: "false"' in result.stdout
assert 'DROBOTICFLOW_QWEN_NODE_CACHE_PVC: ""' in result.stdout
def test_render_manifests_wires_default_disabled_qwen_node_cache_to_worker_only():
result = run_render_manifests(
{
"DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED": "true",
"DROBOTICFLOW_QWEN_NODE_CACHE_PVC": "qwen-node-cache",
"DROBOTICFLOW_QWEN_NODE_CACHE_MOUNT_PATH": "/qwen-node-cache",
"DROBOTICFLOW_QWEN_NODE_CACHE_NODE_SELECTOR": "kubernetes.io/hostname=192.168.80.136",
"DROBOTICFLOW_QWEN_NODE_CACHE_STALE_STAGING_SECONDS": "3600",
}
)
assert result.returncode == 0, result.stderr + result.stdout
assert 'DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED: "true"' in result.stdout
assert 'DROBOTICFLOW_QWEN_NODE_CACHE_PVC: "qwen-node-cache"' in result.stdout
assert result.stdout.count("- name: DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED") == 1
assert "key: DROBOTICFLOW_QWEN_NODE_CACHE_NODE_SELECTOR" in result.stdout
def test_render_manifests_rejects_incomplete_enabled_qwen_node_cache():
result = run_render_manifests({"DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED": "true"})
assert_render_validation_failure(result, "DROBOTICFLOW_QWEN_NODE_CACHE_PVC must be a lowercase DNS subdomain")
def test_render_manifests_rejects_invalid_startup_numeric_configuration():
cases = [
({"MONGO_CONNECT_RETRIES": "0"}, "MONGO_CONNECT_RETRIES must be greater than or equal to 1"),
({"MONGO_CONNECT_RETRY_DELAY_MS": "abc"}, "MONGO_CONNECT_RETRY_DELAY_MS must be an integer"),
({"WORKER_POLL_INTERVAL_MS": "0"}, "WORKER_POLL_INTERVAL_MS must be greater than or equal to 1"),
({"OPERATOR_JOB_TTL_SECONDS": "-1"}, "OPERATOR_JOB_TTL_SECONDS must be greater than or equal to 0"),
({"API_REPLICAS": "-1"}, "API_REPLICAS must be greater than or equal to 0"),
({"WORKER_REPLICAS": "x"}, "WORKER_REPLICAS must be an integer"),
({"API_NODE_PORT": "80"}, "API_NODE_PORT must be greater than or equal to 30000"),
({"API_NODE_PORT": "32768"}, "API_NODE_PORT must be less than or equal to 32767"),
]
for env, message in cases:
result = run_render_manifests(env)
assert_render_validation_failure(result, message)
def test_render_manifests_indents_multiline_block_scalar_values():
result = run_render_manifests(
{
"OPERATOR_GPU_TOLERATIONS_JSON": '[\n {"key":"nvidia.com/gpu","operator":"Exists"}\n]',
"OPERATOR_COMMAND_ALLOWLIST_REGEX": "^python3 .*$\n^bash .*$",
"OPERATOR_COMMAND_DENYLIST_REGEX": "rm -rf\ncurl .*$",
}
)
assert result.returncode == 0, result.stderr + result.stdout
assert 'OPERATOR_GPU_TOLERATIONS_JSON: |-\n [\n {"key":"nvidia.com/gpu","operator":"Exists"}\n ]' in result.stdout
assert "OPERATOR_COMMAND_ALLOWLIST_REGEX: |-\n ^python3 .*$\n ^bash .*$" in result.stdout
assert "OPERATOR_COMMAND_DENYLIST_REGEX: |-\n rm -rf\n curl .*$" in result.stdout
def test_render_manifests_rejects_invalid_boolean_configuration():
cases = [
({"API_PUBLIC_LOAD_BALANCER": "maybe"}, "API_PUBLIC_LOAD_BALANCER must be a boolean"),
({"OPERATOR_ALLOW_SHELL_COMMANDS": "maybe"}, "OPERATOR_ALLOW_SHELL_COMMANDS must be a boolean"),
({"DROBOTICFLOW_REQUIRE_SESSION_AUTH": "maybe"}, "DROBOTICFLOW_REQUIRE_SESSION_AUTH must be a boolean"),
({"DROBOTICFLOW_SESSION_COOKIE_SECURE": "maybe"}, "DROBOTICFLOW_SESSION_COOKIE_SECURE must be a boolean"),
]
for env, message in cases:
result = run_render_manifests(env)
assert_render_validation_failure(result, message)
def test_render_manifests_rejects_invalid_kubernetes_identity_configuration():
cases = [
({"K8S_NAMESPACE": "Droboticflow"}, "K8S_NAMESPACE must be a lowercase DNS label"),
({"K8S_IMAGE_PULL_SECRET_NAME": "registry_secret"}, "K8S_IMAGE_PULL_SECRET_NAME must be a lowercase DNS subdomain"),
({"WORKSPACE_PVC_NAME": "workspace_pvc"}, "WORKSPACE_PVC_NAME must be a lowercase DNS subdomain"),
({"WORKSPACE_MOUNT_PATH": "workspace"}, "WORKSPACE_MOUNT_PATH must be an absolute container path"),
]
for env, message in cases:
result = run_render_manifests(env)
assert_render_validation_failure(result, message)
def test_render_manifests_rejects_invalid_runtime_reference_configuration():
cases = [
({"API_IMAGE": "registry.example.com/dataflow/api:latest bad"}, "API_IMAGE must be a non-empty container image reference"),
({"WORKER_IMAGE": "registry.example.com/dataflow/worker:latest bad"}, "WORKER_IMAGE must be a non-empty container image reference"),
(
{"OPERATOR_RUNNER_IMAGE": "registry.example.com/dataflow/operator runner:latest"},
"OPERATOR_RUNNER_IMAGE must be a non-empty container image reference",
),
({"WORKER_API_BASE_URL": "droboticflow-worker:3002"}, "WORKER_API_BASE_URL must be an absolute http(s) URL"),
({"AI_API_BASE_URL": "https://example.com/with space"}, "AI_API_BASE_URL must be an absolute http(s) URL"),
({"AI_MODEL": "model x"}, "AI_MODEL must not contain whitespace"),
({"AI_API_KEY": "secret", "AI_MODEL": "model-x"}, "AI_API_BASE_URL is required when AI_API_KEY is configured"),
(
{"AI_API_KEY": "secret", "AI_API_BASE_URL": "https://ai.example/v1"},
"AI_MODEL is required when AI_API_KEY is configured",
),
({"BOS_ENDPOINT": "bj.bcebos.com"}, "BOS_ENDPOINT must be an absolute http(s) URL"),
]
for env, message in cases:
result = run_render_manifests(env)
assert_render_validation_failure(result, message)