deploy-remote-script.test.js 8.4 KB

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