Repository navigation
Expand file tree
/
Copy pathlifecycle.test.ts
More file actions
133 lines (120 loc) · 4.71 KB
/
Copy pathlifecycle.test.ts
File metadata and controls
133 lines (120 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { describe, expect, it } from 'vitest'
import { ensureProductionComposeFile } from './compose-asset'
import {
composeInstallFromDirectory,
composeServiceQuery,
composeServiceState,
type Install,
serviceStatusRows,
} from './lifecycle'
describe('setup lifecycle', () => {
it('does not duplicate a running install restored from its directory', () => {
const root = mkdtempSync(path.join(tmpdir(), 'sim-setup-lifecycle-'))
try {
const file = ensureProductionComposeFile({ kind: 'standalone', root, existing: false })
const active = [{ kind: 'compose', file, dir: root, project: 'sim-live' }] as const
expect(composeInstallFromDirectory(root, active)).toBeNull()
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('reads a Compose service as running when any replica is running', () => {
expect(composeServiceState('running')).toEqual({ state: 'running' })
expect(composeServiceState('exited\nrunning\n')).toEqual({ state: 'running' })
})
it('reads a Compose service with only stopped containers as stopped', () => {
expect(composeServiceState('exited')).toEqual({ state: 'stopped' })
expect(composeServiceState('created\nexited')).toEqual({ state: 'stopped' })
})
it('reads an empty Compose query as absent and a failed one as unknown', () => {
expect(composeServiceState('')).toBeNull()
expect(composeServiceState('\n \n')).toBeNull()
expect(composeServiceState(null)).toBe('unknown')
})
})
describe('status service rows', () => {
const compose = (project: string): Install => ({
kind: 'compose',
file: `/srv/${project}/docker-compose.prod.yml`,
dir: `/srv/${project}`,
project,
})
const dev: Install = { kind: 'dev', postgres: true, redis: true }
const standalone = { db: { state: 'running' as const }, redis: null }
/** Answers `docker ps` by the project/service labels in the query, like Docker would. */
const fakeDocker =
(states: Record<string, string | null>) =>
(args: string[]): string | null => {
const project = args.find((a) => a.startsWith('label=com.docker.compose.project='))
const service = args.find((a) => a.startsWith('label=com.docker.compose.service='))
const key = `${project?.split('=').pop()}/${service?.split('=').pop()}`
return key in states ? states[key]! : ''
}
it('queries Compose services by project and service label', () => {
expect(composeServiceQuery('sim-abc', 'db')).toEqual([
'ps',
'-a',
'--filter',
'label=com.docker.compose.project=sim-abc',
'--filter',
'label=com.docker.compose.service=db',
'--format',
'{{.State}}',
])
})
it('shows only Compose rows for a Compose install', () => {
const rows = serviceStatusRows(
[compose('sim-abc')],
standalone,
fakeDocker({ 'sim-abc/db': 'running', 'sim-abc/redis': 'exited' })
)
expect(rows).toEqual([
{ label: 'postgres (compose db)', state: { state: 'running' } },
{ label: 'redis (compose redis)', state: { state: 'stopped' } },
])
})
it('reports a failed Compose query as unknown, not absent', () => {
const rows = serviceStatusRows([compose('sim-abc')], standalone, () => null)
expect(rows.map((row) => row.state)).toEqual(['unknown', 'unknown'])
})
it('keeps standalone rows when a dev install is present', () => {
const rows = serviceStatusRows(
[dev, compose('sim-abc')],
standalone,
fakeDocker({ 'sim-abc/db': 'running', 'sim-abc/redis': 'running' })
)
expect(rows.map((row) => row.label)).toEqual([
'postgres (sim-postgres)',
'redis (sim-redis)',
'postgres (compose db)',
'redis (compose redis)',
])
expect(rows[0]!.state).toEqual({ state: 'running' })
expect(rows[1]!.state).toBeNull()
})
it('never queries Compose labels without a Compose install', () => {
let queried = false
const rows = serviceStatusRows([dev], standalone, () => {
queried = true
return ''
})
expect(queried).toBe(false)
expect(rows.map((row) => row.label)).toEqual(['postgres (sim-postgres)', 'redis (sim-redis)'])
})
it('prefixes rows with the project when several Compose stacks run', () => {
const rows = serviceStatusRows(
[compose('sim-a'), compose('sim-b')],
standalone,
fakeDocker({ 'sim-a/db': 'running', 'sim-b/db': '' })
)
expect(rows.map((row) => [row.label, row.state])).toEqual([
['sim-a postgres (compose db)', { state: 'running' }],
['sim-a redis (compose redis)', null],
['sim-b postgres (compose db)', null],
['sim-b redis (compose redis)', null],
])
})
})