@mzzsfy/dsh-maintain 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,67 +0,0 @@
1
- import assert from 'node:assert/strict'
2
- import { test } from 'node:test'
3
- import { detectRuntimeEnv, RUNTIME_KINDS } from '../src/runtime.mjs'
4
-
5
- const POSIX = 'linux'
6
- const WIN32 = 'win32'
7
- const BOTH_TTY = { stdin: true, stdout: true }
8
- const NO_TTY = { stdin: false, stdout: false }
9
- const noProbes = { existsImpl: async () => false, readFileImpl: async () => { throw new Error('ENOENT') } }
10
-
11
- test('环境检测:env 覆盖最高优先', async () => {
12
- const managed = await detectRuntimeEnv({ env: { DSH_MAINTAIN_RUNTIME_ENV: 'managed', pm_id: '0' }, platform: POSIX, isTTY: BOTH_TTY, ...noProbes })
13
- assert.deepEqual(managed, { kind: RUNTIME_KINDS.DECLARED_MANAGED, declared: true })
14
- const manual = await detectRuntimeEnv({ env: { DSH_MAINTAIN_RUNTIME_ENV: 'manual', pm_id: '0' }, platform: POSIX, isTTY: NO_TTY, ...noProbes })
15
- assert.deepEqual(manual, { kind: RUNTIME_KINDS.MANUAL_START, declared: false })
16
- // 非法覆盖值不生效,继续走常规判定
17
- const bogus = await detectRuntimeEnv({ env: { DSH_MAINTAIN_RUNTIME_ENV: 'sideways' }, platform: POSIX, isTTY: NO_TTY, ...noProbes })
18
- assert.deepEqual(bogus, { kind: RUNTIME_KINDS.UNKNOWN, declared: false })
19
- })
20
-
21
- test('环境检测:env 表命中各托管形态', async () => {
22
- const table = [
23
- [{ pm_id: '0' }, RUNTIME_KINDS.PM2],
24
- [{ PM2_HOME: 'C:/pm2' }, RUNTIME_KINDS.PM2],
25
- [{ pm_uptime: '123' }, RUNTIME_KINDS.PM2],
26
- [{ INVOCATION_ID: 'x' }, RUNTIME_KINDS.SYSTEMD],
27
- [{ JOURNAL_STREAM: '9' }, RUNTIME_KINDS.SYSTEMD],
28
- [{ NOTIFY_SOCKET: '/run/notify' }, RUNTIME_KINDS.SYSTEMD],
29
- [{ SUPERVISOR_ENABLED: '1' }, RUNTIME_KINDS.SUPERVISORD],
30
- [{ SUPERVISOR_PROCESS_NAME: 'dsh' }, RUNTIME_KINDS.SUPERVISORD],
31
- [{ KUBERNETES_SERVICE_HOST: '10.0.0.1' }, RUNTIME_KINDS.KUBERNETES],
32
- ]
33
- for (const [env, kind] of table) {
34
- const result = await detectRuntimeEnv({ env, platform: POSIX, isTTY: NO_TTY, ...noProbes })
35
- assert.deepEqual(result, { kind, declared: true }, JSON.stringify(env))
36
- }
37
- // 空串环境值不构成声明
38
- const empty = await detectRuntimeEnv({ env: { pm_id: '' }, platform: POSIX, isTTY: NO_TTY, ...noProbes })
39
- assert.deepEqual(empty, { kind: RUNTIME_KINDS.UNKNOWN, declared: false })
40
- })
41
-
42
- test('环境检测:POSIX 容器探测', async () => {
43
- const docker = await detectRuntimeEnv({ env: {}, platform: POSIX, isTTY: NO_TTY, existsImpl: async (p) => p === '/.dockerenv', readFileImpl: async () => { throw new Error('ENOENT') } })
44
- assert.deepEqual(docker, { kind: RUNTIME_KINDS.DOCKER, declared: true })
45
- for (const [marker, kind] of [['/docker/', RUNTIME_KINDS.DOCKER], ['kubepods', RUNTIME_KINDS.KUBERNETES], ['containerd', RUNTIME_KINDS.CONTAINER], ['lxc', RUNTIME_KINDS.CONTAINER], ['podman', RUNTIME_KINDS.CONTAINER]]) {
46
- const result = await detectRuntimeEnv({ env: {}, platform: POSIX, isTTY: NO_TTY, existsImpl: async () => false, readFileImpl: async () => '12:pids:/sys/fs/cgroup/' + marker + 'abc' })
47
- assert.deepEqual(result, { kind, declared: true }, marker)
48
- }
49
- // 探测器异常不致命,继续后续判定
50
- const broken = await detectRuntimeEnv({ env: {}, platform: POSIX, isTTY: NO_TTY, existsImpl: async () => { throw new Error('EACCES') }, readFileImpl: async () => { throw new Error('EACCES') } })
51
- assert.deepEqual(broken, { kind: RUNTIME_KINDS.UNKNOWN, declared: false })
52
- })
53
-
54
- test('环境检测:win32 跳过 POSIX 探测', async () => {
55
- const win = await detectRuntimeEnv({ env: {}, platform: WIN32, isTTY: NO_TTY, existsImpl: async () => true, readFileImpl: async () => '/docker/xyz' })
56
- assert.deepEqual(win, { kind: RUNTIME_KINDS.UNKNOWN, declared: false })
57
- })
58
-
59
- test('环境检测:双 TTY 判手动直跑,单 TTY 不构成', async () => {
60
- const both = await detectRuntimeEnv({ env: {}, platform: POSIX, isTTY: BOTH_TTY, ...noProbes })
61
- assert.deepEqual(both, { kind: RUNTIME_KINDS.MANUAL_START, declared: false })
62
- const stdinOnly = await detectRuntimeEnv({ env: {}, platform: POSIX, isTTY: { stdin: true, stdout: false }, ...noProbes })
63
- assert.deepEqual(stdinOnly, { kind: RUNTIME_KINDS.UNKNOWN, declared: false })
64
- // 托管声明优先于 TTY
65
- const declaredWithTty = await detectRuntimeEnv({ env: { pm_id: '0' }, platform: POSIX, isTTY: BOTH_TTY, ...noProbes })
66
- assert.deepEqual(declaredWithTty, { kind: RUNTIME_KINDS.PM2, declared: true })
67
- })