mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(docker): run container as host user with bind-mounted workspace
This commit is contained in:
1 parent
a73ac19bbc
commit
4cdd3d7d0b
10 files changed
+716
-4
No files matched your search
@@ -0,0 +1,327 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { spawnSync } from 'child_process'
|
||||
import { mkdirSync, writeFileSync, rmSync, chmodSync, existsSync, readFileSync } from 'fs'
|
||||
import { join } from 'path'
|
||||
import { tmpdir } from 'os'
|
||||
import { repoRoot } from '../helpers/repo-root'
|
||||
|
||||
const libPath = join(repoRoot, 'scripts/lib/container-user.sh')
|
||||
|
||||
let stubDir: string
|
||||
let logPath: string
|
||||
|
||||
const writeStub = (name: string, body: string) => {
|
||||
const file = join(stubDir, name)
|
||||
writeFileSync(file, `#!/bin/bash\n${body}\n`)
|
||||
chmodSync(file, 0o755)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
stubDir = join(tmpdir(), `ocm-container-user-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
mkdirSync(stubDir, { recursive: true })
|
||||
logPath = join(stubDir, 'calls.log')
|
||||
|
||||
writeStub('id', `
|
||||
case "$1" in
|
||||
-u) echo "${'${OCM_STUB_NODE_UID:-1000}'}" ;;
|
||||
-g) echo "${'${OCM_STUB_NODE_GID:-1000}'}" ;;
|
||||
*) exit 1 ;;
|
||||
esac`)
|
||||
|
||||
writeStub('getent', `
|
||||
if [ "$1" = "group" ] && [ "$2" = "${'${OCM_STUB_GID_KEY:-__none__}'}" ]; then
|
||||
echo "${'${OCM_STUB_GID_HOLDER}'}:x:$2:"
|
||||
exit 0
|
||||
fi
|
||||
if [ "$1" = "passwd" ] && [ "$2" = "${'${OCM_STUB_UID_KEY:-__none__}'}" ]; then
|
||||
echo "${'${OCM_STUB_UID_HOLDER}'}:x:$2:$2::/nonexistent:/bin/false"
|
||||
exit 0
|
||||
fi
|
||||
exit 2`)
|
||||
|
||||
writeStub('groupmod', `echo "groupmod $*" >> "$OCM_STUB_LOG"`)
|
||||
writeStub('usermod', `echo "usermod $*" >> "$OCM_STUB_LOG"`)
|
||||
writeStub('stat', `echo "${'${OCM_STUB_OWNER_UID:-0}'}"`)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(stubDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
const runScript = (snippet: string, env: Record<string, string> = {}) =>
|
||||
spawnSync('bash', ['-c', `set -e\nsource "${libPath}"\n${snippet}`], {
|
||||
encoding: 'utf-8',
|
||||
env: {
|
||||
...process.env,
|
||||
PUID: '',
|
||||
PGID: '',
|
||||
PATH: `${stubDir}:${process.env.PATH}`,
|
||||
OCM_STUB_LOG: logPath,
|
||||
...env,
|
||||
},
|
||||
})
|
||||
|
||||
const stubCalls = () => {
|
||||
if (!existsSync(logPath)) return []
|
||||
return readFileSync(logPath, 'utf-8').split('\n').filter(Boolean)
|
||||
}
|
||||
|
||||
describe('resolve_target_ids', () => {
|
||||
it('defaults PUID/PGID to 1000 when unset', () => {
|
||||
const res = runScript('unset PUID PGID; resolve_target_ids; echo "uid=$OCM_TARGET_UID gid=$OCM_TARGET_GID"')
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stdout).toContain('uid=1000 gid=1000')
|
||||
})
|
||||
|
||||
it('honors explicit PUID and PGID values', () => {
|
||||
const res = runScript('resolve_target_ids; echo "uid=$OCM_TARGET_UID gid=$OCM_TARGET_GID"', {
|
||||
PUID: '1001',
|
||||
PGID: '1002',
|
||||
})
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stdout).toContain('uid=1001 gid=1002')
|
||||
})
|
||||
|
||||
it('rejects non-numeric PUID with the offending value in stderr', () => {
|
||||
const res = runScript('resolve_target_ids', { PUID: 'abc' })
|
||||
expect(res.status).not.toBe(0)
|
||||
expect(res.stderr).toMatch(/PUID must be a numeric user id/)
|
||||
expect(res.stderr).toContain('abc')
|
||||
})
|
||||
|
||||
it('rejects non-numeric PGID', () => {
|
||||
const res = runScript('resolve_target_ids', { PGID: '1x0' })
|
||||
expect(res.status).not.toBe(0)
|
||||
expect(res.stderr).toMatch(/PGID must be a numeric group id/)
|
||||
})
|
||||
|
||||
it('falls back to 1000 when PUID is explicitly empty', () => {
|
||||
const res = runScript('resolve_target_ids; echo "uid=$OCM_TARGET_UID"', { PUID: '' })
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stdout).toContain('uid=1000')
|
||||
})
|
||||
})
|
||||
|
||||
describe('align_container_user', () => {
|
||||
it('is a no-op when ids already match', () => {
|
||||
const res = runScript(
|
||||
'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"',
|
||||
{ OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1000', PGID: '1000' },
|
||||
)
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toEqual([])
|
||||
expect(res.stdout).toContain('uidChanged=0 gidChanged=0')
|
||||
})
|
||||
|
||||
it('aligns both ids and records the change, group before user', () => {
|
||||
const res = runScript(
|
||||
'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"',
|
||||
{ OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002' },
|
||||
)
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toEqual(['groupmod -g 1002 node', 'usermod -u 1001 node'])
|
||||
expect(res.stdout).toContain('uidChanged=1 gidChanged=1')
|
||||
})
|
||||
|
||||
it('aligns only the gid when the uid already matches', () => {
|
||||
const res = runScript(
|
||||
'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"',
|
||||
{ OCM_STUB_NODE_UID: '1001', PUID: '1001', PGID: '1002' },
|
||||
)
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toEqual(['groupmod -g 1002 node'])
|
||||
expect(res.stdout).toContain('uidChanged=0 gidChanged=1')
|
||||
})
|
||||
|
||||
it('aligns only the uid when the gid already matches', () => {
|
||||
const res = runScript(
|
||||
'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"',
|
||||
{ OCM_STUB_NODE_GID: '1002', PUID: '1001', PGID: '1002' },
|
||||
)
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toEqual(['usermod -u 1001 node'])
|
||||
expect(res.stdout).toContain('uidChanged=1 gidChanged=0')
|
||||
})
|
||||
|
||||
it('defaults the account name to node', () => {
|
||||
const res = runScript('align_container_user', { PUID: '1001' })
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls().some((c) => c.endsWith(' node'))).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects malformed ids before touching accounts', () => {
|
||||
const res = runScript('align_container_user node', { PUID: 'abc' })
|
||||
expect(res.status).not.toBe(0)
|
||||
expect(stubCalls()).toEqual([])
|
||||
})
|
||||
|
||||
it('logs what it is doing', () => {
|
||||
const res = runScript('align_container_user node', { PUID: '1001', PGID: '1002' })
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stdout).toMatch(/Aligning node group to gid 1002/)
|
||||
expect(res.stdout).toMatch(/Aligning node user to uid 1001/)
|
||||
})
|
||||
|
||||
it('propagates groupmod failure without setting the gid change flag', () => {
|
||||
writeStub('groupmod', `echo "groupmod $*" >> "$OCM_STUB_LOG"\nexit 1`)
|
||||
const res = runScript(
|
||||
'align_container_user node || echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"',
|
||||
{ OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002' },
|
||||
)
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toEqual(['groupmod -g 1002 node'])
|
||||
expect(res.stdout).toContain('uidChanged=0 gidChanged=0')
|
||||
})
|
||||
|
||||
it('propagates usermod failure without setting the uid change flag', () => {
|
||||
writeStub('usermod', `echo "usermod $*" >> "$OCM_STUB_LOG"\nexit 1`)
|
||||
const res = runScript(
|
||||
'align_container_user node || echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"',
|
||||
{ OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002' },
|
||||
)
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toEqual(['groupmod -g 1002 node', 'usermod -u 1001 node'])
|
||||
expect(res.stdout).toContain('uidChanged=0 gidChanged=1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('align_container_user id collisions', () => {
|
||||
it('aborts before mutating anything when the gid is held by another group', () => {
|
||||
const res = runScript('align_container_user node', {
|
||||
OCM_STUB_NODE_UID: '1000',
|
||||
OCM_STUB_NODE_GID: '1000',
|
||||
PUID: '1001',
|
||||
PGID: '100',
|
||||
OCM_STUB_GID_KEY: '100',
|
||||
OCM_STUB_GID_HOLDER: 'users',
|
||||
})
|
||||
expect(res.status).not.toBe(0)
|
||||
expect(res.stderr).toContain('PGID 100')
|
||||
expect(res.stderr).toContain('users')
|
||||
expect(res.stderr).toContain('id -g')
|
||||
expect(stubCalls()).toEqual([])
|
||||
})
|
||||
|
||||
it('aborts after a successful gid alignment when the uid is held by another user', () => {
|
||||
const res = runScript('align_container_user node', {
|
||||
OCM_STUB_NODE_UID: '1000',
|
||||
OCM_STUB_NODE_GID: '1000',
|
||||
PUID: '4',
|
||||
PGID: '1002',
|
||||
OCM_STUB_UID_KEY: '4',
|
||||
OCM_STUB_UID_HOLDER: 'sync',
|
||||
})
|
||||
expect(res.status).not.toBe(0)
|
||||
expect(res.stderr).toContain('PUID 4')
|
||||
expect(res.stderr).toContain('sync')
|
||||
expect(stubCalls()).toEqual(['groupmod -g 1002 node'])
|
||||
})
|
||||
|
||||
it('treats a gid already owned by the target account as a no-collision', () => {
|
||||
const res = runScript('align_container_user node', {
|
||||
OCM_STUB_NODE_GID: '1000',
|
||||
PGID: '1002',
|
||||
OCM_STUB_GID_KEY: '1002',
|
||||
OCM_STUB_GID_HOLDER: 'node',
|
||||
})
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toContain('groupmod -g 1002 node')
|
||||
})
|
||||
|
||||
it('treats a uid already owned by the target account as a no-collision', () => {
|
||||
const res = runScript('align_container_user node', {
|
||||
OCM_STUB_NODE_UID: '1000',
|
||||
PUID: '1001',
|
||||
PGID: '1000',
|
||||
OCM_STUB_UID_KEY: '1001',
|
||||
OCM_STUB_UID_HOLDER: 'node',
|
||||
})
|
||||
expect(res.status).toBe(0)
|
||||
expect(stubCalls()).toEqual(['usermod -u 1001 node'])
|
||||
})
|
||||
|
||||
it('emits an actionable remediation hint in the collision message', () => {
|
||||
const gidCollision = runScript('align_container_user node', {
|
||||
OCM_STUB_NODE_UID: '1000',
|
||||
OCM_STUB_NODE_GID: '1000',
|
||||
PUID: '1001',
|
||||
PGID: '100',
|
||||
OCM_STUB_GID_KEY: '100',
|
||||
OCM_STUB_GID_HOLDER: 'users',
|
||||
})
|
||||
expect(gidCollision.status).not.toBe(0)
|
||||
expect(gidCollision.stderr).toMatch(/Pick a different PGID/)
|
||||
|
||||
const uidCollision = runScript('align_container_user node', {
|
||||
OCM_STUB_NODE_UID: '1000',
|
||||
OCM_STUB_NODE_GID: '1000',
|
||||
PUID: '4',
|
||||
PGID: '1002',
|
||||
OCM_STUB_UID_KEY: '4',
|
||||
OCM_STUB_UID_HOLDER: 'sync',
|
||||
})
|
||||
expect(uidCollision.status).not.toBe(0)
|
||||
expect(uidCollision.stderr).toMatch(/Pick a different PUID/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('warn_if_workspace_owner_differs', () => {
|
||||
it('is silent when the path does not exist', () => {
|
||||
const res = runScript(`warn_if_workspace_owner_differs "${join(stubDir, 'does-not-exist')}" 1000`)
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stderr).toBe('')
|
||||
})
|
||||
|
||||
it('is silent when the directory is empty', () => {
|
||||
const ws = join(stubDir, 'ws')
|
||||
mkdirSync(ws, { recursive: true })
|
||||
const res = runScript(`warn_if_workspace_owner_differs "${ws}" 1000`, { OCM_STUB_OWNER_UID: '0' })
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stderr).toBe('')
|
||||
})
|
||||
|
||||
it('is silent when the directory is non-empty and the owner matches', () => {
|
||||
const ws = join(stubDir, 'ws')
|
||||
mkdirSync(ws, { recursive: true })
|
||||
writeFileSync(join(ws, 'repo.txt'), 'data')
|
||||
const res = runScript(`warn_if_workspace_owner_differs "${ws}" 1000`, { OCM_STUB_OWNER_UID: '1000' })
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stderr).toBe('')
|
||||
})
|
||||
|
||||
it('warns non-fatally when a non-empty directory has a mismatched owner', () => {
|
||||
const ws = join(stubDir, 'ws')
|
||||
mkdirSync(ws, { recursive: true })
|
||||
writeFileSync(join(ws, 'repo.txt'), 'data')
|
||||
const res = runScript(`warn_if_workspace_owner_differs "${ws}" 1000`, { OCM_STUB_OWNER_UID: '1001' })
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stderr).toContain('1001')
|
||||
expect(res.stderr).toContain('1000')
|
||||
expect(res.stderr).toContain('WARNING')
|
||||
expect(res.stderr).toContain(ws)
|
||||
expect(res.stderr).toMatch(/rewriting ownership/)
|
||||
})
|
||||
|
||||
it('names the id -u / id -g remediation in the warning', () => {
|
||||
const ws = join(stubDir, 'ws')
|
||||
mkdirSync(ws, { recursive: true })
|
||||
writeFileSync(join(ws, 'repo.txt'), 'data')
|
||||
const res = runScript(`warn_if_workspace_owner_differs "${ws}" 1000`, { OCM_STUB_OWNER_UID: '1001' })
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stderr).toMatch(/id -u/)
|
||||
expect(res.stderr).toMatch(/id -g/)
|
||||
})
|
||||
|
||||
it('stays silent and returns 0 when stat fails under set -e', () => {
|
||||
const ws = join(stubDir, 'ws')
|
||||
mkdirSync(ws, { recursive: true })
|
||||
writeFileSync(join(ws, 'repo.txt'), 'data')
|
||||
writeStub('stat', `exit 1`)
|
||||
const res = runScript(`warn_if_workspace_owner_differs "${ws}" 1000; echo "status=$?"`, {
|
||||
OCM_STUB_OWNER_UID: '1001',
|
||||
})
|
||||
expect(res.status).toBe(0)
|
||||
expect(res.stderr).toBe('')
|
||||
expect(res.stdout).toContain('status=0')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,202 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { readFileSync, mkdtempSync, mkdirSync, writeFileSync, rmSync, readdirSync, statSync, existsSync } from 'fs'
|
||||
import { execSync } from 'child_process'
|
||||
import { join } from 'path'
|
||||
import { tmpdir } from 'os'
|
||||
import { repoRoot } from '../helpers/repo-root'
|
||||
|
||||
const entrypointPath = join(repoRoot, 'scripts/docker-entrypoint.sh')
|
||||
const dockerfilePath = join(repoRoot, 'Dockerfile')
|
||||
const composePath = join(repoRoot, 'docker-compose.yml')
|
||||
const envExamplePath = join(repoRoot, '.env.example')
|
||||
const dockerDocsPath = join(repoRoot, 'docs/configuration/docker.md')
|
||||
const installationDocsPath = join(repoRoot, 'docs/getting-started/installation.md')
|
||||
|
||||
const read = (path: string) => readFileSync(path, 'utf-8')
|
||||
|
||||
const SOURCE_PATH_RE = /^\s*(?:source|\.)\s+(\/\S+)/m
|
||||
const COPY_LIB_RE = /^COPY\s+scripts\/lib\/container-user\.sh\s+(\S+)/m
|
||||
|
||||
describe('entrypoint library wiring', () => {
|
||||
it('sources the path the Dockerfile installs', () => {
|
||||
const entrypoint = read(entrypointPath)
|
||||
const dockerfile = read(dockerfilePath)
|
||||
|
||||
const sourceMatch = entrypoint.match(SOURCE_PATH_RE)
|
||||
const copyMatch = dockerfile.match(COPY_LIB_RE)
|
||||
|
||||
expect(sourceMatch, 'entrypoint must source the container-user library').not.toBeNull()
|
||||
expect(copyMatch, 'Dockerfile must COPY the container-user library').not.toBeNull()
|
||||
expect(sourceMatch![1]).toBe(copyMatch![1])
|
||||
})
|
||||
|
||||
it('aborts explicitly on alignment failure', () => {
|
||||
const entrypoint = read(entrypointPath)
|
||||
expect(entrypoint).toMatch(/if ! align_container_user node; then/)
|
||||
const blockStart = entrypoint.indexOf('align_container_user node; then')
|
||||
const blockEnd = entrypoint.indexOf('fi', blockStart)
|
||||
const block = entrypoint.slice(blockStart, blockEnd)
|
||||
expect(block).toMatch(/exit 1/)
|
||||
})
|
||||
|
||||
it('warns before chowning the workspace', () => {
|
||||
const entrypoint = read(entrypointPath)
|
||||
const warnIndex = entrypoint.indexOf('warn_if_workspace_owner_differs /workspace')
|
||||
const workspaceChownMatch = entrypoint.match(/chown -R node:node [^\n]*\/workspace/)
|
||||
expect(workspaceChownMatch, 'entrypoint must chown the workspace').not.toBeNull()
|
||||
const chownIndex = entrypoint.indexOf(workspaceChownMatch![0])
|
||||
expect(warnIndex).toBeGreaterThan(-1)
|
||||
expect(chownIndex).toBeGreaterThan(-1)
|
||||
expect(warnIndex).toBeLessThan(chownIndex)
|
||||
})
|
||||
|
||||
it('realigns /app only when ids changed', () => {
|
||||
const entrypoint = read(entrypointPath)
|
||||
|
||||
expect(entrypoint).toContain('OCM_UID_CHANGED')
|
||||
expect(entrypoint).toContain('OCM_GID_CHANGED')
|
||||
|
||||
const conditionalBlockMatch = entrypoint.match(
|
||||
/if \[ "\$OCM_UID_CHANGED" = "1" \] \|\| \[ "\$OCM_GID_CHANGED" = "1" \]; then[\s\S]*?chown -R node:node \/app\nfi/,
|
||||
)
|
||||
expect(conditionalBlockMatch, 'conditional /app realign block must exist').not.toBeNull()
|
||||
|
||||
const workspaceChownMatch = entrypoint.match(/chown -R node:node [^\n]*\/workspace/)
|
||||
expect(workspaceChownMatch, 'pre-existing unconditional workspace chown must remain').not.toBeNull()
|
||||
const conditionalChownIndex = entrypoint.indexOf('chown -R node:node /app\n', conditionalBlockMatch!.index!)
|
||||
const workspaceChownIndex = entrypoint.indexOf(workspaceChownMatch![0])
|
||||
expect(conditionalChownIndex).toBeGreaterThan(workspaceChownIndex)
|
||||
|
||||
expect(entrypoint).toContain('mkdir -p /app/data /workspace /home/node/.cache /home/node/.opencode')
|
||||
})
|
||||
|
||||
it('does not mark the library executable in the image', () => {
|
||||
const dockerfile = read(dockerfilePath)
|
||||
expect(dockerfile).not.toMatch(/chmod \+x \/usr\/local\/lib\/ocm\/container-user\.sh/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('workspace ownership configuration', () => {
|
||||
it('exposes PUID and PGID environment defaults in docker-compose.yml', () => {
|
||||
const compose = read(composePath)
|
||||
expect(compose).toContain('- PUID=${PUID:-1000}')
|
||||
expect(compose).toContain('- PGID=${PGID:-1000}')
|
||||
})
|
||||
|
||||
it('overrides the workspace mount source in docker-compose.yml', () => {
|
||||
const compose = read(composePath)
|
||||
expect(compose).toContain('${OCM_WORKSPACE_HOST_PATH:-opencode-workspace}:/workspace')
|
||||
})
|
||||
|
||||
it('does not declare the bare service workspace mount in docker-compose.yml', () => {
|
||||
const compose = read(composePath)
|
||||
expect(compose).not.toContain('- opencode-workspace:/workspace')
|
||||
})
|
||||
|
||||
it('keeps the opencode-workspace named volume declared at top level', () => {
|
||||
const compose = read(composePath)
|
||||
expect(compose).toMatch(/^volumes:\n(?:.*\n)*?\s+opencode-workspace:/m)
|
||||
})
|
||||
|
||||
it('keeps the docker docs compose snippet in sync with docker-compose.yml', () => {
|
||||
const compose = read(composePath)
|
||||
const docs = read(dockerDocsPath)
|
||||
|
||||
const fenceStart = docs.indexOf('```yaml\nservices:')
|
||||
expect(fenceStart, 'docs must contain a fenced compose yaml block').toBeGreaterThan(-1)
|
||||
const contentStart = fenceStart + '```yaml\n'.length
|
||||
const fenceEnd = docs.indexOf('\n```\n', contentStart)
|
||||
expect(fenceEnd, 'docs compose yaml block must be closed').toBeGreaterThan(-1)
|
||||
const docsBlock = docs.slice(contentStart, fenceEnd)
|
||||
|
||||
const normalize = (s: string) => s.replace(/\s+$/, '').split('\n').map((l) => l.replace(/\s+$/, '')).join('\n')
|
||||
expect(normalize(docsBlock)).toBe(normalize(compose))
|
||||
})
|
||||
|
||||
it('documents the Accessing Repositories From the Host subsection', () => {
|
||||
const docs = read(dockerDocsPath)
|
||||
expect(docs).toContain('#### Accessing Repositories From the Host')
|
||||
})
|
||||
|
||||
it('documents the new workspace ownership env vars in .env.example', () => {
|
||||
const envExample = read(envExamplePath)
|
||||
expect(envExample).toContain('OCM_WORKSPACE_HOST_PATH')
|
||||
expect(envExample).toContain('# PUID=1000')
|
||||
expect(envExample).toContain('# PGID=1000')
|
||||
})
|
||||
|
||||
it('links the host-access subsection from the installation guide', () => {
|
||||
const installation = read(installationDocsPath)
|
||||
expect(installation).toContain('configuration/docker.md#accessing-repositories-from-the-host')
|
||||
})
|
||||
|
||||
it('documents the migration empty-destination guard and quoted host path', () => {
|
||||
const docs = read(dockerDocsPath)
|
||||
expect(docs).toContain('if [ -n "$(ls -A "<host path>")" ]; then')
|
||||
expect(docs).toContain('mkdir -p "<host path>"')
|
||||
expect(docs).toContain('-v "<host path>":/to')
|
||||
expect(docs).toContain('chown -R "$(id -u):$(id -g)" "<host path>"')
|
||||
})
|
||||
})
|
||||
|
||||
describe('named-volume migration recipe', () => {
|
||||
const runMigrationShell = (src: string, dst: string) => {
|
||||
const scriptDir = mkdtempSync(join(tmpdir(), 'migrate-script-'))
|
||||
const scriptPath = join(scriptDir, 'migrate.sh')
|
||||
writeFileSync(
|
||||
scriptPath,
|
||||
`set -eu
|
||||
src=${JSON.stringify(src)}
|
||||
dst=${JSON.stringify(dst)}
|
||||
mkdir -p "$dst"
|
||||
if [ -n "$(ls -A "$dst")" ]; then
|
||||
echo "destination '$dst' is not empty; aborting migration" >&2
|
||||
exit 1
|
||||
fi
|
||||
cp -a "$src/." "$dst/"
|
||||
chown -R "$(id -u):$(id -g)" "$dst"
|
||||
`,
|
||||
)
|
||||
try {
|
||||
return execSync(`bash ${JSON.stringify(scriptPath)}`, { stdio: 'pipe' })
|
||||
} finally {
|
||||
rmSync(scriptDir, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
it('aborts without modification when the destination is non-empty', () => {
|
||||
const dst = mkdtempSync(join(tmpdir(), 'migrate-dst-'))
|
||||
writeFileSync(join(dst, 'existing.txt'), 'keep me')
|
||||
const src = mkdtempSync(join(tmpdir(), 'migrate-src-'))
|
||||
mkdirSync(join(src, 'repo'))
|
||||
writeFileSync(join(src, 'repo', 'file.txt'), 'volume data')
|
||||
|
||||
let threw = false
|
||||
try {
|
||||
runMigrationShell(src, dst)
|
||||
} catch {
|
||||
threw = true
|
||||
}
|
||||
|
||||
expect(threw, 'recipe must abort when destination is non-empty').toBe(true)
|
||||
expect(readdirSync(dst)).toEqual(['existing.txt'])
|
||||
expect(existsSync(join(dst, 'repo'))).toBe(false)
|
||||
rmSync(dst, { recursive: true, force: true })
|
||||
rmSync(src, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('copies volume contents to the root of an empty destination with spaces in the path', () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'migrate-root-'))
|
||||
const dst = join(root, 'My Repositories')
|
||||
const src = mkdtempSync(join(tmpdir(), 'migrate-src-'))
|
||||
mkdirSync(join(src, 'repo'))
|
||||
writeFileSync(join(src, 'repo', 'file.txt'), 'volume data')
|
||||
|
||||
runMigrationShell(src, dst)
|
||||
|
||||
expect(statSync(join(dst, 'repo', 'file.txt')).isFile()).toBe(true)
|
||||
expect(readdirSync(dst)).toEqual(['repo'])
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
rmSync(src, { recursive: true, force: true })
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user