1
0

feat: target 时间配置校验,interval 最小 10s,timeout 不大于 interval

在配置加载阶段新增通用 target 时间字段语义校验:
- interval 解析后不得小于 10s
- timeout 解析后不得大于同一 target 的 interval
- 默认值(30s / 10s)参与校验
- 变量引用先解析再校验
- 格式错误优先于关系错误,避免级联提示
This commit is contained in:
2026-05-25 17:48:51 +08:00
parent 77c6015b3a
commit c120690cf1
3 changed files with 169 additions and 18 deletions

View File

@@ -886,6 +886,144 @@ targets:
await expectConfigLoadError(configPath, "无效的时长格式");
});
test("interval 小于 10s 抛出错误", async () => {
const configPath = join(tempDir, "interval-too-small.yaml");
await writeFile(
configPath,
`targets:
- name: "t"
id: "t"
type: http
interval: "9s"
http:
url: "http://a.com"
`,
);
await expectConfigLoadError(configPath, "interval 不能小于 10s");
});
test("interval 9999ms 抛出错误", async () => {
const configPath = join(tempDir, "interval-9999ms.yaml");
await writeFile(
configPath,
`targets:
- name: "t"
id: "t"
type: http
interval: "9999ms"
http:
url: "http://a.com"
`,
);
await expectConfigLoadError(configPath, "interval 不能小于 10s");
});
test("interval 10s 通过", async () => {
const configPath = join(tempDir, "interval-10s.yaml");
await writeFile(
configPath,
`targets:
- name: "t"
id: "t"
type: http
interval: "10s"
http:
url: "http://a.com"
`,
);
const config = await loadConfig(configPath);
expect(config.targets[0]!.intervalMs).toBe(10000);
});
test("interval 10000ms 通过", async () => {
const configPath = join(tempDir, "interval-10000ms.yaml");
await writeFile(
configPath,
`targets:
- name: "t"
id: "t"
type: http
interval: "10000ms"
http:
url: "http://a.com"
`,
);
const config = await loadConfig(configPath);
expect(config.targets[0]!.intervalMs).toBe(10000);
});
test("timeout 大于 interval 抛出错误", async () => {
const configPath = join(tempDir, "timeout-gt-interval.yaml");
await writeFile(
configPath,
`targets:
- name: "t"
id: "t"
type: http
interval: "10s"
timeout: "30s"
http:
url: "http://a.com"
`,
);
await expectConfigLoadError(configPath, "timeout 不能大于 interval");
});
test("timeout 等于 interval 通过", async () => {
const configPath = join(tempDir, "timeout-eq-interval.yaml");
await writeFile(
configPath,
`targets:
- name: "t"
id: "t"
type: http
interval: "30s"
timeout: "30s"
http:
url: "http://a.com"
`,
);
const config = await loadConfig(configPath);
expect(config.targets[0]!.intervalMs).toBe(30000);
expect(config.targets[0]!.timeoutMs).toBe(30000);
});
test("变量解析后 interval 小于 10s 抛出错误", async () => {
const configPath = join(tempDir, "var-interval-too-small.yaml");
await writeFile(
configPath,
`variables:
check_interval: "5s"
targets:
- name: "t"
id: "t"
type: http
interval: "\${check_interval}"
http:
url: "http://a.com"
`,
);
await expectConfigLoadError(configPath, "interval 不能小于 10s");
});
test("变量解析后 timeout 大于 interval 抛出错误", async () => {
const configPath = join(tempDir, "var-timeout-gt-interval.yaml");
await writeFile(
configPath,
`variables:
check_timeout: "60s"
targets:
- name: "t"
id: "t"
type: http
timeout: "\${check_timeout}"
http:
url: "http://a.com"
`,
);
await expectConfigLoadError(configPath, "timeout 不能大于 interval");
});
test("解析 expect 配置", async () => {
const configPath = join(tempDir, "expect.yaml");
await writeFile(