deploy-remote-script.test.js 8.2 KB

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