Files
dsh_shenxian/test/worker-provision.test.mjs

39 lines
1.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* `ensureOsAccount`(`src/worker/agent.ts`)—— 集群化后「建号」落点的守卫。
*
* 本套件只覆盖**参数校验**(纯逻辑,任何平台都能跑)。
* 真实建号(`useradd` + `chown`)需要 Linux root ⇒ 在各节点上冒烟验证,不进本套件。
*
* 背景:单机时代的 `dsh-provision.path` 靠**本地控制面库**算 uid,集群下算不出来
* (实测 `hashUid` 兜底会把 guest 算成 184656 ≠ 真实 100002,而 PG 是 Manager 专属)
* ⇒ 改为「Manager 随 `POST /launch` 投递 uid,agent 在 spawn 前确保账号存在」。
* 见 `agent.ts` 中该函数的注释。
*/
import test from 'node:test'
import assert from 'node:assert/strict'
import { ensureOsAccount } from '../lib/worker/agent.js'
const config = { baseUid: 100000, dataRoot: '/nonexistent-dshs-root' }
const VALID = '4092b965-2f68-4977-9989-68b3966f7df0'
test('ensureOsAccount:拒绝非 UUID 形状的 userId', () => {
assert.throws(() => ensureOsAccount(config, 'guest', 100002), /invalid userId/)
})
test('ensureOsAccount:拒绝低于 baseUid 的 uid', () => {
assert.throws(() => ensureOsAccount(config, VALID, 999), /invalid uid/)
})
test('ensureOsAccount:拒绝非整数 uid', () => {
assert.throws(() => ensureOsAccount(config, VALID, 100000.5), /invalid uid/)
})
test('ensureOsAccount:拒绝负数 uid', () => {
assert.throws(() => ensureOsAccount(config, VALID, -1), /invalid uid/)
})
test('ensureOsAccount:非 Linux 平台在参数合法时静默返回(不抛错、不建号)', () => {
if (process.platform === 'linux') return // 该用例只在非 Linux 上有意义
assert.doesNotThrow(() => ensureOsAccount(config, VALID, 100002))
})