deploy-remote-script.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. });
  47. test('waits for app and postfix health before and after certificate synchronization', { skip: !canRun }, (t) => {
  48. const fixture = createFixture(t);
  49. const result = runDeploy(fixture);
  50. assert.equal(result.status, 0, result.stderr);
  51. const events = readEvents(fixture.logFile);
  52. assert.deepEqual(
  53. events.filter((event) => event.startsWith('health:') || event.startsWith('sync:')),
  54. [
  55. 'sync:0',
  56. 'health:app-container',
  57. 'health:postfix-container',
  58. 'health:dovecot-container',
  59. 'sync:1',
  60. 'health:app-container',
  61. 'health:postfix-container',
  62. 'health:dovecot-container'
  63. ]
  64. );
  65. assert.equal(events.filter((event) => event === 'runtime:app').length, 6);
  66. assert.equal(events.filter((event) => event === 'runtime:dovecot').length, 2);
  67. });
  68. test('continues after remote tools actively consume their stdin', { skip: !canRun }, (t) => {
  69. const fixture = createFixture(t);
  70. const result = runDeploy(fixture);
  71. assert.equal(result.status, 0, result.stderr);
  72. const events = readEvents(fixture.logFile);
  73. assert.equal(events.includes('migrate'), false, events.join('\n'));
  74. assert.ok(events.includes('sync:1'), events.join('\n'));
  75. assert.equal(events.at(-1), 'final:ps', events.join('\n'));
  76. });
  77. test('skips the Maildir migration maintenance window by default', { skip: !canRun }, (t) => {
  78. const fixture = createFixture(t);
  79. const result = runDeploy(fixture);
  80. assert.equal(result.status, 0, result.stderr);
  81. const events = readEvents(fixture.logFile);
  82. const offlineSync = events.indexOf('sync:0');
  83. const pull = events.indexOf('pull:dovecot');
  84. const up = events.indexOf('up');
  85. assert.equal(events.includes('stop:app,dovecot'), false, events.join('\n'));
  86. assert.equal(events.includes('migrate'), false, events.join('\n'));
  87. assert.ok(offlineSync >= 0 && offlineSync < up, events.join('\n'));
  88. assert.ok(pull >= 0 && pull < up, events.join('\n'));
  89. });
  90. test('runs the opt-in Maildir migration without consuming the SSH heredoc stdin', { skip: !canRun }, (t) => {
  91. const fixture = createFixture(t);
  92. const result = runDeploy(fixture, { runMaildirMigration: '1' });
  93. assert.equal(result.status, 0, result.stderr);
  94. const events = readEvents(fixture.logFile);
  95. const offlineSync = events.indexOf('sync:0');
  96. const pull = events.indexOf('pull:dovecot');
  97. const stop = events.indexOf('stop:app,dovecot');
  98. const migrate = events.indexOf('migrate');
  99. const up = events.indexOf('up');
  100. assert.ok(offlineSync >= 0 && offlineSync < stop, events.join('\n'));
  101. assert.ok(pull >= 0 && pull < stop, events.join('\n'));
  102. assert.ok(stop >= 0 && stop < migrate, events.join('\n'));
  103. assert.ok(migrate >= 0 && migrate < up, events.join('\n'));
  104. });
  105. test('reports the previous revision when deployment fails', { skip: !canRun }, (t) => {
  106. const fixture = createFixture(t);
  107. const result = runDeploy(fixture, { syncStatus: '19' });
  108. assert.equal(result.status, 19);
  109. assert.match(
  110. result.stderr,
  111. /Deployment failed\. Previous revision was previous-revision; inspect the running containers before recovery\./
  112. );
  113. assert.match(result.stderr, /Maildir cutover is already committed; legacy mail services will not be restarted/);
  114. const events = readEvents(fixture.logFile);
  115. assert.equal(events.filter((event) => event === 'sync:0').length, 1);
  116. assert.equal(events.filter((event) => event === 'sync:1').length, 1);
  117. assert.equal(events.filter((event) => event === 'health:app-container').length, 1);
  118. assert.equal(events.filter((event) => event === 'health:postfix-container').length, 1);
  119. assert.equal(events.filter((event) => event === 'health:dovecot-container').length, 1);
  120. });
  121. test('stops the app for migration and restarts the previous container if migration fails', { skip: !canRun }, (t) => {
  122. const fixture = createFixture(t);
  123. const result = runDeploy(fixture, { migrationStatus: '23', runMaildirMigration: '1' });
  124. assert.equal(result.status, 23);
  125. assert.match(result.stderr, /Restarting the pre-migration MailHub mail services\./);
  126. assert.deepEqual(readEvents(fixture.logFile), [
  127. 'sync:0',
  128. 'pull:dovecot',
  129. 'stop:app,dovecot',
  130. 'migrate',
  131. 'restart:app-container',
  132. 'restart:dovecot-container',
  133. 'final:ps'
  134. ]);
  135. });
  136. test('keeps the maintenance window explicit after cutover health checks fail', { skip: !canRun }, (t) => {
  137. const fixture = createFixture(t);
  138. const result = runDeploy(fixture, { runtimeStatus: '29', runMaildirMigration: '1' });
  139. assert.notEqual(result.status, 0);
  140. assert.match(result.stderr, /Maildir cutover is already committed; legacy mail services will not be restarted/);
  141. assert.match(result.stderr, /maintenance window remains active/);
  142. assert.equal(readEvents(fixture.logFile).some((event) => event.startsWith('restart:')), false);
  143. assert.equal(readEvents(fixture.logFile).filter((event) => event === 'stop:app,dovecot').length, 2);
  144. });
  145. function createFixture(t) {
  146. const root = mkdtempSync(path.join(os.tmpdir(), 'mailhub-deploy-script-'));
  147. const fakeBin = path.join(root, 'bin');
  148. const remoteDir = path.join(root, 'remote');
  149. const remoteScriptsDir = path.join(remoteDir, 'scripts');
  150. const logFile = path.join(root, 'events.log');
  151. mkdirSync(fakeBin, { recursive: true });
  152. mkdirSync(remoteScriptsDir, { recursive: true });
  153. writeFileSync(logFile, '');
  154. writeExecutable(path.join(fakeBin, 'git'), `#!/bin/sh
  155. if [ "$1" = "remote" ] && [ "$2" = "get-url" ]; then
  156. printf '%s\\n' 'ssh://git.example.test/mailhub.git'
  157. elif [ "$1" = "status" ]; then
  158. :
  159. elif [ "$1" = "rev-parse" ] && [ "$2" = "HEAD" ]; then
  160. if [ "$PWD" = "$MAILHUB_DEPLOY_TEST_REMOTE_DIR" ]; then
  161. printf '%s\\n' 'previous-revision'
  162. else
  163. printf '%s\\n' 'pushed-revision'
  164. fi
  165. elif [ "$1" = "rev-parse" ]; then
  166. printf '%s\\n' 'pushed-revision'
  167. fi
  168. `);
  169. writeExecutable(path.join(fakeBin, 'ssh'), `#!/bin/sh
  170. while [ "$#" -gt 0 ]; do
  171. case "$1" in
  172. -o) shift 2 ;;
  173. *) break ;;
  174. esac
  175. done
  176. [ "$#" -gt 0 ] && shift
  177. [ "$#" -gt 0 ] && shift
  178. [ "\${1:-}" = "--" ] && shift
  179. exec bash -s -- "$@"
  180. `);
  181. writeExecutable(path.join(fakeBin, 'docker'), `#!/bin/sh
  182. if [ "$1" = "compose" ] && [ "$2" = "ps" ] && [ "\${3:-}" = "--all" ]; then
  183. printf '%s-container\\n' "$5"
  184. exit 0
  185. fi
  186. if [ "$1" = "compose" ] && [ "$2" = "stop" ]; then
  187. printf 'stop:%s,%s\\n' "$3" "$4" >> "$MAILHUB_DEPLOY_TEST_LOG"
  188. exit 0
  189. fi
  190. if [ "$1" = "compose" ] && [ "$2" = "run" ]; then
  191. cat >/dev/null
  192. printf '%s\\n' 'migrate' >> "$MAILHUB_DEPLOY_TEST_LOG"
  193. exit "\${MAILHUB_DEPLOY_TEST_MIGRATION_STATUS:-0}"
  194. fi
  195. if [ "$1" = "compose" ] && [ "$2" = "pull" ]; then
  196. printf 'pull:%s\\n' "$3" >> "$MAILHUB_DEPLOY_TEST_LOG"
  197. exit 0
  198. fi
  199. if [ "$1" = "compose" ] && [ "$2" = "up" ]; then
  200. printf '%s\\n' 'up' >> "$MAILHUB_DEPLOY_TEST_LOG"
  201. exit 0
  202. fi
  203. if [ "$1" = "compose" ] && [ "$2" = "exec" ]; then
  204. cat >/dev/null
  205. service=""
  206. for argument in "$@"; do
  207. if [ "$argument" = "app" ] || [ "$argument" = "dovecot" ]; then
  208. service="$argument"
  209. break
  210. fi
  211. done
  212. printf 'runtime:%s\\n' "$service" >> "$MAILHUB_DEPLOY_TEST_LOG"
  213. if [ "$service" = "app" ] && [ "\${MAILHUB_DEPLOY_TEST_RUNTIME_STATUS:-0}" != "0" ]; then
  214. exit "$MAILHUB_DEPLOY_TEST_RUNTIME_STATUS"
  215. fi
  216. exit 0
  217. fi
  218. if [ "$1" = "compose" ] && [ "$2" = "ps" ] && [ -z "\${3:-}" ]; then
  219. printf '%s\\n' 'final:ps' >> "$MAILHUB_DEPLOY_TEST_LOG"
  220. exit 0
  221. fi
  222. if [ "$1" = "start" ]; then
  223. printf 'restart:%s\\n' "$2" >> "$MAILHUB_DEPLOY_TEST_LOG"
  224. exit 0
  225. fi
  226. if [ "$1" = "inspect" ]; then
  227. container=''
  228. for argument in "$@"; do
  229. container="$argument"
  230. done
  231. printf 'health:%s\\n' "$container" >> "$MAILHUB_DEPLOY_TEST_LOG"
  232. printf '%s\\n' 'running healthy'
  233. fi
  234. `);
  235. writeExecutable(path.join(remoteScriptsDir, 'sync-tls-certificate.sh'), `#!/bin/sh
  236. cat >/dev/null
  237. printf 'sync:%s\\n' "\${MAILHUB_CERT_RESTART:-}" >> "$MAILHUB_DEPLOY_TEST_LOG"
  238. if [ "\${MAILHUB_CERT_RESTART:-0}" = "1" ]; then
  239. exit "\${MAILHUB_DEPLOY_TEST_SYNC_STATUS:-0}"
  240. fi
  241. exit 0
  242. `);
  243. writeExecutable(path.join(remoteScriptsDir, 'prepare-dovecot.sh'), `#!/bin/sh
  244. cat >/dev/null
  245. exit 0
  246. `);
  247. t.after(() => rmSync(root, { recursive: true, force: true }));
  248. return { root, fakeBin, remoteDir, logFile };
  249. }
  250. function runDeploy(
  251. fixture,
  252. { syncStatus = '0', migrationStatus = '0', runtimeStatus = '0', runMaildirMigration = '0' } = {}
  253. ) {
  254. return spawnSync('bash', [scriptPath], {
  255. cwd: fixture.root,
  256. encoding: 'utf8',
  257. env: {
  258. ...process.env,
  259. PATH: `${fixture.fakeBin}${path.delimiter}${process.env.PATH ?? ''}`,
  260. MAILHUB_DEPLOY_REMOTE: 'deploy@example.test',
  261. MAILHUB_DEPLOY_DIR: fixture.remoteDir,
  262. MAILHUB_DEPLOY_BRANCH: 'main',
  263. MAILHUB_DEPLOY_GIT_URL: 'ssh://git.example.test/mailhub.git',
  264. MAILHUB_DEPLOY_TEST_LOG: fixture.logFile,
  265. MAILHUB_DEPLOY_TEST_REMOTE_DIR: fixture.remoteDir,
  266. MAILHUB_DEPLOY_TEST_SYNC_STATUS: syncStatus,
  267. MAILHUB_DEPLOY_TEST_MIGRATION_STATUS: migrationStatus,
  268. MAILHUB_DEPLOY_TEST_RUNTIME_STATUS: runtimeStatus,
  269. MAILHUB_DEPLOY_RUN_MAILDIR_MIGRATION: runMaildirMigration
  270. }
  271. });
  272. }
  273. function writeExecutable(filePath, contents) {
  274. writeFileSync(filePath, contents);
  275. chmodSync(filePath, 0o755);
  276. }
  277. function readEvents(logFile) {
  278. return readFileSync(logFile, 'utf8').trim().split('\n').filter(Boolean);
  279. }