deploy-remote-script.test.js 9.9 KB

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