deploy-remote-script.test.js 10 KB

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