deploy-remote-script.test.js 9.5 KB

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