deploy-remote-script.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import assert from 'node:assert/strict';
  2. import {
  3. chmodSync,
  4. mkdirSync,
  5. mkdtempSync,
  6. readFileSync,
  7. rmSync,
  8. writeFileSync
  9. } from 'node:fs';
  10. import os from 'node:os';
  11. import path from 'node:path';
  12. import { spawnSync } from 'node:child_process';
  13. import { test } from 'node:test';
  14. import { fileURLToPath } from 'node:url';
  15. const scriptPath = fileURLToPath(new URL('../scripts/deploy-remote.sh', import.meta.url));
  16. const scriptSource = readFileSync(scriptPath, 'utf8');
  17. const canRun = process.platform !== 'win32';
  18. test('checks Maildir access with Dovecot mail worker uid instead of container root', () => {
  19. assert.match(scriptSource, /compose exec -T --user 1000:1000 dovecot <\/dev\/null sh/);
  20. assert.match(scriptSource, /fs\.statSync\(probe\)\.isFile\(\)/);
  21. assert.doesNotMatch(scriptSource, /trap .*rm -f -- \"\$probe\"/);
  22. });
  23. test('keeps the Maildir migration as an explicit deployment option', () => {
  24. assert.match(scriptSource, /run_maildir_migration="\$\{MAILHUB_DEPLOY_RUN_MAILDIR_MIGRATION:-0\}"/);
  25. assert.match(scriptSource, /if \[\[ "\$\{run_maildir_migration\}" == "1" \]\]; then/);
  26. assert.match(
  27. scriptSource,
  28. /docker compose run --rm --no-deps -T app <\/dev\/null node scripts\/migrate-sqlite-maildir\.js/
  29. );
  30. assert.match(scriptSource, /Skipping Maildir migration check; set MAILHUB_DEPLOY_RUN_MAILDIR_MIGRATION=1 to run it\./);
  31. });
  32. test('isolates runtime probes and setup scripts from the SSH heredoc stdin', () => {
  33. assert.match(scriptSource, /docker compose exec -T app <\/dev\/null node -e/);
  34. assert.match(scriptSource, /\.\/scripts\/prepare-dovecot\.sh <\/dev\/null/);
  35. assert.equal(
  36. scriptSource.match(/\.\/scripts\/sync-tls-certificate\.sh <\/dev\/null/g)?.length,
  37. 3
  38. );
  39. });
  40. test('checks the Dovecot IMAPS authentication path through Lua passdb', () => {
  41. assert.match(scriptSource, /tls\.connect\(\{[\s\S]*host: "dovecot"[\s\S]*port: 31993/);
  42. assert.match(scriptSource, /setTimeout\(\(\) => finish\(false\), 15000\)/);
  43. assert.match(scriptSource, /mailhub-healthcheck@invalid\.invalid/);
  44. assert.match(scriptSource, /temporary authentication failure\|unavailable/);
  45. assert.match(scriptSource, /Dovecot IMAPS authentication path check failed\./);
  46. assert.match(scriptSource, /wait_for_compose_health app postfix\s+docker compose restart dovecot <\/dev\/null/);
  47. });
  48. test('waits for app and postfix before restarting Dovecot and certificate synchronization', { skip: !canRun }, (t) => {
  49. const fixture = createFixture(t);
  50. const result = runDeploy(fixture);
  51. assert.equal(result.status, 0, result.stderr);
  52. const events = readEvents(fixture.logFile);
  53. assert.deepEqual(
  54. events.filter((event) => event.startsWith('health:') || event.startsWith('sync:')),
  55. [
  56. 'sync:0',
  57. 'health:app-container',
  58. 'health:postfix-container',
  59. 'health:app-container',
  60. 'health:postfix-container',
  61. 'health:dovecot-container',
  62. 'sync:1',
  63. 'health:app-container',
  64. 'health:postfix-container',
  65. 'health:dovecot-container'
  66. ]
  67. );
  68. assert.equal(events.filter((event) => event === 'runtime:app').length, 6);
  69. assert.equal(events.filter((event) => event === 'runtime:dovecot').length, 2);
  70. });
  71. test('continues after remote tools actively consume their stdin', { skip: !canRun }, (t) => {
  72. const fixture = createFixture(t);
  73. const result = runDeploy(fixture);
  74. assert.equal(result.status, 0, result.stderr);
  75. const events = readEvents(fixture.logFile);
  76. assert.equal(events.includes('migrate'), false, events.join('\n'));
  77. assert.ok(events.includes('sync:1'), events.join('\n'));
  78. assert.equal(events.at(-1), 'final:ps', events.join('\n'));
  79. });
  80. test('skips the Maildir migration maintenance window by default', { skip: !canRun }, (t) => {
  81. const fixture = createFixture(t);
  82. const result = runDeploy(fixture);
  83. assert.equal(result.status, 0, result.stderr);
  84. const events = readEvents(fixture.logFile);
  85. const offlineSync = events.indexOf('sync:0');
  86. const pull = events.indexOf('pull:dovecot');
  87. const up = events.indexOf('up');
  88. const dovecotRestart = events.indexOf('compose-restart:dovecot');
  89. assert.equal(events.includes('stop:app,dovecot'), false, events.join('\n'));
  90. assert.equal(events.includes('migrate'), false, events.join('\n'));
  91. assert.ok(offlineSync >= 0 && offlineSync < up, events.join('\n'));
  92. assert.ok(pull >= 0 && pull < up, events.join('\n'));
  93. assert.ok(up >= 0 && up < dovecotRestart, events.join('\n'));
  94. });
  95. test('runs the opt-in Maildir migration without consuming the SSH heredoc stdin', { skip: !canRun }, (t) => {
  96. const fixture = createFixture(t);
  97. const result = runDeploy(fixture, { runMaildirMigration: '1' });
  98. assert.equal(result.status, 0, result.stderr);
  99. const events = readEvents(fixture.logFile);
  100. const offlineSync = events.indexOf('sync:0');
  101. const pull = events.indexOf('pull:dovecot');
  102. const stop = events.indexOf('stop:app,dovecot');
  103. const migrate = events.indexOf('migrate');
  104. const up = events.indexOf('up');
  105. assert.ok(offlineSync >= 0 && offlineSync < stop, events.join('\n'));
  106. assert.ok(pull >= 0 && pull < stop, events.join('\n'));
  107. assert.ok(stop >= 0 && stop < migrate, events.join('\n'));
  108. assert.ok(migrate >= 0 && migrate < up, events.join('\n'));
  109. });
  110. test('reports the previous revision when deployment fails', { skip: !canRun }, (t) => {
  111. const fixture = createFixture(t);
  112. const result = runDeploy(fixture, { syncStatus: '19' });
  113. assert.equal(result.status, 19);
  114. assert.match(
  115. result.stderr,
  116. /Deployment failed\. Previous revision was previous-revision; inspect the running containers before recovery\./
  117. );
  118. assert.doesNotMatch(result.stderr, /Maildir cutover is already committed/);
  119. const events = readEvents(fixture.logFile);
  120. assert.equal(events.filter((event) => event === 'sync:0').length, 1);
  121. assert.equal(events.filter((event) => event === 'sync:1').length, 1);
  122. assert.equal(events.filter((event) => event === 'health:app-container').length, 2);
  123. assert.equal(events.filter((event) => event === 'health:postfix-container').length, 2);
  124. assert.equal(events.filter((event) => event === 'health:dovecot-container').length, 1);
  125. assert.equal(events.includes('stop:app,dovecot'), false, events.join('\n'));
  126. assert.equal(events.at(-1), 'final:ps', events.join('\n'));
  127. });
  128. test('does not apply Maildir cutover protection to default runtime probe failures', { skip: !canRun }, (t) => {
  129. const fixture = createFixture(t);
  130. const result = runDeploy(fixture, { runtimeStatus: '29' });
  131. assert.equal(result.status, 1);
  132. assert.match(
  133. result.stderr,
  134. /Deployment failed\. Previous revision was previous-revision; inspect the running containers before recovery\./
  135. );
  136. assert.doesNotMatch(result.stderr, /Maildir cutover is already committed/);
  137. const events = readEvents(fixture.logFile);
  138. assert.equal(events.includes('migrate'), false, events.join('\n'));
  139. assert.equal(events.includes('stop:app,dovecot'), false, events.join('\n'));
  140. assert.ok(events.includes('compose-restart:dovecot'), events.join('\n'));
  141. assert.equal(events.at(-1), 'final:ps', events.join('\n'));
  142. });
  143. test('stops the app for migration and restarts the previous container if migration fails', { skip: !canRun }, (t) => {
  144. const fixture = createFixture(t);
  145. const result = runDeploy(fixture, { migrationStatus: '23', runMaildirMigration: '1' });
  146. assert.equal(result.status, 23);
  147. assert.match(result.stderr, /Restarting the pre-migration MailHub mail services\./);
  148. assert.deepEqual(readEvents(fixture.logFile), [
  149. 'sync:0',
  150. 'pull:dovecot',
  151. 'stop:app,dovecot',
  152. 'migrate',
  153. 'restart:app-container',
  154. 'restart:dovecot-container',
  155. 'final:ps'
  156. ]);
  157. });
  158. test('keeps the maintenance window explicit after cutover health checks fail', { skip: !canRun }, (t) => {
  159. const fixture = createFixture(t);
  160. const result = runDeploy(fixture, { runtimeStatus: '29', runMaildirMigration: '1' });
  161. assert.notEqual(result.status, 0);
  162. assert.match(result.stderr, /Maildir cutover is already committed; legacy mail services will not be restarted/);
  163. assert.match(result.stderr, /maintenance window remains active/);
  164. assert.equal(readEvents(fixture.logFile).some((event) => event.startsWith('restart:')), false);
  165. assert.equal(readEvents(fixture.logFile).filter((event) => event === 'stop:app,dovecot').length, 2);
  166. });
  167. function createFixture(t) {
  168. const root = mkdtempSync(path.join(os.tmpdir(), 'mailhub-deploy-script-'));
  169. const fakeBin = path.join(root, 'bin');
  170. const remoteDir = path.join(root, 'remote');
  171. const remoteScriptsDir = path.join(remoteDir, 'scripts');
  172. const logFile = path.join(root, 'events.log');
  173. mkdirSync(fakeBin, { recursive: true });
  174. mkdirSync(remoteScriptsDir, { recursive: true });
  175. writeFileSync(logFile, '');
  176. writeExecutable(path.join(fakeBin, 'git'), `#!/bin/sh
  177. if [ "$1" = "remote" ] && [ "$2" = "get-url" ]; then
  178. printf '%s\\n' 'ssh://git.example.test/mailhub.git'
  179. elif [ "$1" = "status" ]; then
  180. :
  181. elif [ "$1" = "rev-parse" ] && [ "$2" = "HEAD" ]; then
  182. if [ "$PWD" = "$MAILHUB_DEPLOY_TEST_REMOTE_DIR" ]; then
  183. printf '%s\\n' 'previous-revision'
  184. else
  185. printf '%s\\n' 'pushed-revision'
  186. fi
  187. elif [ "$1" = "rev-parse" ]; then
  188. printf '%s\\n' 'pushed-revision'
  189. fi
  190. `);
  191. writeExecutable(path.join(fakeBin, 'ssh'), `#!/bin/sh
  192. while [ "$#" -gt 0 ]; do
  193. case "$1" in
  194. -o) shift 2 ;;
  195. *) break ;;
  196. esac
  197. done
  198. [ "$#" -gt 0 ] && shift
  199. [ "$#" -gt 0 ] && shift
  200. [ "\${1:-}" = "--" ] && shift
  201. exec bash -s -- "$@"
  202. `);
  203. writeExecutable(path.join(fakeBin, 'docker'), `#!/bin/sh
  204. if [ "$1" = "compose" ] && [ "$2" = "ps" ] && [ "\${3:-}" = "--all" ]; then
  205. printf '%s-container\\n' "$5"
  206. exit 0
  207. fi
  208. if [ "$1" = "compose" ] && [ "$2" = "stop" ]; then
  209. printf 'stop:%s,%s\\n' "$3" "$4" >> "$MAILHUB_DEPLOY_TEST_LOG"
  210. exit 0
  211. fi
  212. if [ "$1" = "compose" ] && [ "$2" = "run" ]; then
  213. cat >/dev/null
  214. printf '%s\\n' 'migrate' >> "$MAILHUB_DEPLOY_TEST_LOG"
  215. exit "\${MAILHUB_DEPLOY_TEST_MIGRATION_STATUS:-0}"
  216. fi
  217. if [ "$1" = "compose" ] && [ "$2" = "pull" ]; then
  218. printf 'pull:%s\\n' "$3" >> "$MAILHUB_DEPLOY_TEST_LOG"
  219. exit 0
  220. fi
  221. if [ "$1" = "compose" ] && [ "$2" = "up" ]; then
  222. printf '%s\\n' 'up' >> "$MAILHUB_DEPLOY_TEST_LOG"
  223. exit 0
  224. fi
  225. if [ "$1" = "compose" ] && [ "$2" = "restart" ]; then
  226. printf 'compose-restart:%s\\n' "$3" >> "$MAILHUB_DEPLOY_TEST_LOG"
  227. exit 0
  228. fi
  229. if [ "$1" = "compose" ] && [ "$2" = "exec" ]; then
  230. cat >/dev/null
  231. service=""
  232. for argument in "$@"; do
  233. if [ "$argument" = "app" ] || [ "$argument" = "dovecot" ]; then
  234. service="$argument"
  235. break
  236. fi
  237. done
  238. printf 'runtime:%s\\n' "$service" >> "$MAILHUB_DEPLOY_TEST_LOG"
  239. if [ "$service" = "app" ] && [ "\${MAILHUB_DEPLOY_TEST_RUNTIME_STATUS:-0}" != "0" ]; then
  240. exit "$MAILHUB_DEPLOY_TEST_RUNTIME_STATUS"
  241. fi
  242. exit 0
  243. fi
  244. if [ "$1" = "compose" ] && [ "$2" = "ps" ] && [ -z "\${3:-}" ]; then
  245. printf '%s\\n' 'final:ps' >> "$MAILHUB_DEPLOY_TEST_LOG"
  246. exit 0
  247. fi
  248. if [ "$1" = "start" ]; then
  249. printf 'restart:%s\\n' "$2" >> "$MAILHUB_DEPLOY_TEST_LOG"
  250. exit 0
  251. fi
  252. if [ "$1" = "inspect" ]; then
  253. container=''
  254. for argument in "$@"; do
  255. container="$argument"
  256. done
  257. printf 'health:%s\\n' "$container" >> "$MAILHUB_DEPLOY_TEST_LOG"
  258. printf '%s\\n' 'running healthy'
  259. fi
  260. `);
  261. writeExecutable(path.join(remoteScriptsDir, 'sync-tls-certificate.sh'), `#!/bin/sh
  262. cat >/dev/null
  263. printf 'sync:%s\\n' "\${MAILHUB_CERT_RESTART:-}" >> "$MAILHUB_DEPLOY_TEST_LOG"
  264. if [ "\${MAILHUB_CERT_RESTART:-0}" = "1" ]; then
  265. exit "\${MAILHUB_DEPLOY_TEST_SYNC_STATUS:-0}"
  266. fi
  267. exit 0
  268. `);
  269. writeExecutable(path.join(remoteScriptsDir, 'prepare-dovecot.sh'), `#!/bin/sh
  270. cat >/dev/null
  271. exit 0
  272. `);
  273. t.after(() => rmSync(root, { recursive: true, force: true }));
  274. return { root, fakeBin, remoteDir, logFile };
  275. }
  276. function runDeploy(
  277. fixture,
  278. { syncStatus = '0', migrationStatus = '0', runtimeStatus = '0', runMaildirMigration = '0' } = {}
  279. ) {
  280. return spawnSync('bash', [scriptPath], {
  281. cwd: fixture.root,
  282. encoding: 'utf8',
  283. env: {
  284. ...process.env,
  285. PATH: `${fixture.fakeBin}${path.delimiter}${process.env.PATH ?? ''}`,
  286. MAILHUB_DEPLOY_REMOTE: 'deploy@example.test',
  287. MAILHUB_DEPLOY_DIR: fixture.remoteDir,
  288. MAILHUB_DEPLOY_BRANCH: 'main',
  289. MAILHUB_DEPLOY_GIT_URL: 'ssh://git.example.test/mailhub.git',
  290. MAILHUB_DEPLOY_TEST_LOG: fixture.logFile,
  291. MAILHUB_DEPLOY_TEST_REMOTE_DIR: fixture.remoteDir,
  292. MAILHUB_DEPLOY_TEST_SYNC_STATUS: syncStatus,
  293. MAILHUB_DEPLOY_TEST_MIGRATION_STATUS: migrationStatus,
  294. MAILHUB_DEPLOY_TEST_RUNTIME_STATUS: runtimeStatus,
  295. MAILHUB_DEPLOY_RUN_MAILDIR_MIGRATION: runMaildirMigration
  296. }
  297. });
  298. }
  299. function writeExecutable(filePath, contents) {
  300. writeFileSync(filePath, contents);
  301. chmodSync(filePath, 0o755);
  302. }
  303. function readEvents(logFile) {
  304. return readFileSync(logFile, 'utf8').trim().split('\n').filter(Boolean);
  305. }