package worker import ( "testing" "droboticflow/internal/domain" "droboticflow/internal/k8s" ) func qwenCacheTestNode(operatorID string, handler string) domain.WorkflowNode { return domain.WorkflowNode{ ID: "rough-cut", Type: "operator", Data: domain.WorkflowNodeData{ OperatorID: operatorID, RuntimeContract: map[string]any{"handler": handler}, }, } } func TestQwenNodeCacheAppliesOnlyToExactBuiltin(t *testing.T) { config, err := newQwenNodeCacheConfig(true, "qwen-node-cache", "/qwen-node-cache", "kubernetes.io/hostname=192.168.80.136", 3600) if err != nil { t.Fatalf("newQwenNodeCacheConfig: %v", err) } qwen := qwenCacheTestNode(qwenBehaviorRoughCutOperatorID, "builtin_qwen_behavior_rough_cut") env, mounts, selector, err := config.apply(qwen, map[string]string{}, nil) if err != nil { t.Fatalf("apply qwen cache: %v", err) } if env["DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED"] != "true" || env["DROBOTICFLOW_QWEN_NODE_CACHE_ROOT"] != "/qwen-node-cache" || env["DROBOTICFLOW_QWEN_NODE_CACHE_STALE_STAGING_SECONDS"] != "3600" { t.Fatalf("unexpected Qwen cache env: %#v", env) } if len(mounts) != 1 || mounts[0] != (k8s.PVCMount{Name: "qwen-node-cache", PVCName: "qwen-node-cache", MountPath: "/qwen-node-cache"}) { t.Fatalf("unexpected Qwen cache mounts: %#v", mounts) } if selector["kubernetes.io/hostname"] != "192.168.80.136" { t.Fatalf("unexpected Qwen cache selector: %#v", selector) } for _, node := range []domain.WorkflowNode{ qwenCacheTestNode("operator-builtin-qwen-temporal-localization", "builtin_qwen_temporal_localization"), qwenCacheTestNode(qwenBehaviorRoughCutOperatorID, "builtin_qwen_temporal_localization"), {ID: "not-operator", Type: "task", Data: domain.WorkflowNodeData{OperatorID: qwenBehaviorRoughCutOperatorID, RuntimeContract: map[string]any{"handler": "builtin_qwen_behavior_rough_cut"}}}, } { env, mounts, selector, err := config.apply(node, map[string]string{}, nil) if err != nil { t.Fatalf("non-Qwen cache apply: %v", err) } if len(env) != 0 || len(mounts) != 0 || len(selector) != 0 { t.Fatalf("cache must not leak to node %#v: env=%#v mounts=%#v selector=%#v", node, env, mounts, selector) } } } func TestQwenNodeCacheDisabledAndInvalidConfigurationFailSafe(t *testing.T) { disabled, err := newQwenNodeCacheConfig(false, "", "", "", 0) if err != nil { t.Fatalf("disabled cache must not require configuration: %v", err) } env, mounts, selector, err := disabled.apply(qwenCacheTestNode(qwenBehaviorRoughCutOperatorID, "builtin_qwen_behavior_rough_cut"), map[string]string{}, nil) if err != nil || len(env) != 0 || len(mounts) != 0 || len(selector) != 0 { t.Fatalf("disabled cache must preserve legacy launch: env=%#v mounts=%#v selector=%#v err=%v", env, mounts, selector, err) } cases := []struct { name string pvc string mount string selector string stale int }{ {name: "missing pvc", mount: "/qwen-node-cache", selector: "kubernetes.io/hostname=node", stale: 1}, {name: "root mount", pvc: "qwen-node-cache", mount: "/", selector: "kubernetes.io/hostname=node", stale: 1}, {name: "missing selector", pvc: "qwen-node-cache", mount: "/qwen-node-cache", stale: 1}, {name: "invalid selector", pvc: "qwen-node-cache", mount: "/qwen-node-cache", selector: "not-a-selector", stale: 1}, {name: "negative stale", pvc: "qwen-node-cache", mount: "/qwen-node-cache", selector: "kubernetes.io/hostname=node", stale: -1}, } for _, testCase := range cases { t.Run(testCase.name, func(t *testing.T) { if _, err := newQwenNodeCacheConfig(true, testCase.pvc, testCase.mount, testCase.selector, testCase.stale); err == nil { t.Fatalf("expected invalid cache config to fail") } }) } } func TestQwenNodeCacheRejectsPVCMountCollision(t *testing.T) { config, err := newQwenNodeCacheConfig(true, "qwen-node-cache", "/qwen-node-cache", "kubernetes.io/hostname=node", 1) if err != nil { t.Fatalf("newQwenNodeCacheConfig: %v", err) } _, _, _, err = config.apply( qwenCacheTestNode(qwenBehaviorRoughCutOperatorID, "builtin_qwen_behavior_rough_cut"), map[string]string{}, []k8s.PVCMount{{Name: "other", PVCName: "other", MountPath: "/qwen-node-cache"}}, ) if err == nil { t.Fatalf("expected mount collision to fail closed") } }