droboticflow/tests/frontend/gpu-capacity.test.mjs
2026-06-26 03:17:17 +08:00

140 lines
4.4 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
getWorkflowGPUCapacityBlocker,
getWorkflowGPURequestSummary,
gpuCapacityStatus,
hasPositiveGPURequest,
} from "../../src/lib/gpu-capacity.js";
test("gpu capacity status reports ready when schedulable gpu exists", () => {
const status = gpuCapacityStatus({
available: true,
maxAllocatable: 2,
schedulableNodeCount: 1,
nodes: [{ name: "gpu-node", allocatable: 2 }],
});
assert.equal(status.tone, "ready");
assert.equal(status.label, "可调度");
assert.match(status.detail, /单节点最多 2 张 GPU/);
});
test("gpu capacity status reports blocked for cpu-only poc cluster", () => {
const status = gpuCapacityStatus({
available: false,
maxAllocatable: 0,
schedulableNodeCount: 0,
nodes: [
{ name: "192.168.80.249", allocatable: 0 },
{ name: "192.168.81.47", allocatable: 0 },
],
});
assert.equal(status.tone, "blocked");
assert.equal(status.label, "无可调度 GPU");
assert.match(status.detail, /2 个节点/);
});
test("positive gpu request parser rejects lossy or empty values", () => {
assert.equal(hasPositiveGPURequest("1"), true);
assert.equal(hasPositiveGPURequest(" 8 "), true);
assert.equal(hasPositiveGPURequest(""), false);
assert.equal(hasPositiveGPURequest("0"), false);
assert.equal(hasPositiveGPURequest("1.5"), false);
assert.equal(hasPositiveGPURequest("gpu"), false);
});
test("workflow gpu request summary ignores non-positive gpu values", () => {
const summary = getWorkflowGPURequestSummary([
{ id: "cpu", data: { label: "CPU node", gpu: "" } },
{ id: "zero", data: { label: "Zero GPU", gpu: "0" } },
{ id: "detector", data: { label: "Detector", gpu: "2" } },
{ id: "segmenter", data: { name: "Segmenter", gpu: "1" } },
]);
assert.equal(summary.requested, true);
assert.equal(summary.nodeCount, 2);
assert.equal(summary.totalRequested, 3);
assert.equal(summary.maxRequested, 2);
assert.deepEqual(
summary.nodes.map((node) => [node.id, node.label, node.gpu]),
[
["detector", "Detector", 2],
["segmenter", "Segmenter", 1],
],
);
});
test("workflow gpu run blocker allows cpu-only workflows before capacity loads", () => {
const blocker = getWorkflowGPUCapacityBlocker({
nodes: [{ id: "cpu", data: { label: "CPU node", gpu: "" } }],
});
assert.equal(blocker.blocked, false);
assert.equal(blocker.summary.requested, false);
});
test("workflow gpu run blocker blocks poc cpu-only clusters", () => {
const blocker = getWorkflowGPUCapacityBlocker({
nodes: [{ id: "detect", data: { label: "YOLO 检测", gpu: "1" } }],
gpu: {
available: false,
maxAllocatable: 0,
schedulableNodeCount: 0,
nodes: [
{ name: "192.168.80.249", allocatable: 0 },
{ name: "192.168.81.47", allocatable: 0 },
],
},
});
assert.equal(blocker.blocked, true);
assert.equal(blocker.reason, "gpuUnavailable");
assert.match(blocker.message, /YOLO 检测 请求 1 张 GPU/);
assert.match(blocker.message, /无可调度 GPU/);
});
test("workflow gpu run blocker blocks single node gpu requests above cluster capacity", () => {
const blocker = getWorkflowGPUCapacityBlocker({
nodes: [{ id: "train", data: { label: "训练", gpu: "4" } }],
gpu: {
available: true,
maxAllocatable: 2,
schedulableNodeCount: 1,
nodes: [{ name: "gpu-node", allocatable: 2 }],
},
});
assert.equal(blocker.blocked, true);
assert.equal(blocker.reason, "gpuInsufficient");
assert.match(blocker.message, /单节点最多只有 2 张/);
});
test("workflow gpu run blocker allows matching gpu capacity", () => {
const blocker = getWorkflowGPUCapacityBlocker({
nodes: [{ id: "train", data: { label: "训练", gpu: "2" } }],
gpu: {
available: true,
maxAllocatable: 4,
schedulableNodeCount: 1,
nodes: [{ name: "gpu-node", allocatable: 4 }],
},
});
assert.equal(blocker.blocked, false);
assert.equal(blocker.summary.maxRequested, 2);
});
test("workflow gpu run blocker blocks when capacity lookup fails", () => {
const blocker = getWorkflowGPUCapacityBlocker({
nodes: [{ id: "train", data: { label: "训练", gpu: "1" } }],
capacityError: "worker offline",
});
assert.equal(blocker.blocked, true);
assert.equal(blocker.reason, "gpuCapacityUnavailable");
assert.match(blocker.message, /worker offline/);
});