@whyour/qinglong 2.22.0 → 2.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/back/protos/cron.proto +4 -0
  2. package/docs/development/node-cron-migration.md +49 -0
  3. package/docs/development/task-startup.md +78 -0
  4. package/docs/releases/2.22.0.md +8 -0
  5. package/docs/releases/2.23.0.md +33 -0
  6. package/docs/script-history.md +53 -0
  7. package/package.json +9 -8
  8. package/scripts/benchmark-script-history.cjs +39 -0
  9. package/scripts/publish-static.sh +34 -0
  10. package/scripts/sync-static.sh +20 -0
  11. package/scripts/wait-for-mirror.sh +214 -0
  12. package/shell/api.sh +56 -70
  13. package/shell/node_path_cache.sh +10 -2
  14. package/shell/otask.sh +7 -9
  15. package/shell/preload/sitecustomize.js +6 -2
  16. package/shell/preload/sitecustomize.py +4 -2
  17. package/shell/share.sh +1 -1
  18. package/shell/task-timeout.sh +63 -0
  19. package/shell/task.sh +8 -6
  20. package/shell/update.sh +40 -95
  21. package/shell/upgrade.cjs +8 -0
  22. package/static/build/api/config.js +2 -6
  23. package/static/build/api/env.js +15 -14
  24. package/static/build/api/open.js +2 -1
  25. package/static/build/api/script.js +155 -9
  26. package/static/build/api/subscription.js +3 -6
  27. package/static/build/api/system.js +35 -3
  28. package/static/build/config/grpcCerts.js +89 -40
  29. package/static/build/config/subscription.js +19 -11
  30. package/static/build/config/util.js +6 -4
  31. package/static/build/loaders/express.js +2 -0
  32. package/static/build/loaders/initData.js +20 -30
  33. package/static/build/loaders/initTask.js +11 -1
  34. package/static/build/middlewares/uniqueConstraintError.js +15 -0
  35. package/static/build/protos/cron.js +63 -2
  36. package/static/build/schedule/addCron.js +45 -63
  37. package/static/build/schedule/client.js +19 -1
  38. package/static/build/schedule/delCron.js +1 -1
  39. package/static/build/schedule/setConcurrency.js +27 -0
  40. package/static/build/services/config.js +2 -3
  41. package/static/build/services/cron.js +36 -10
  42. package/static/build/services/env.js +1 -1
  43. package/static/build/services/grpc.js +2 -1
  44. package/static/build/services/log.js +1 -1
  45. package/static/build/services/open.js +106 -31
  46. package/static/build/services/schedule.js +20 -10
  47. package/static/build/services/script.js +1 -1
  48. package/static/build/services/scriptHistory.js +30 -0
  49. package/static/build/services/sshKey.js +39 -14
  50. package/static/build/services/subscription.js +104 -53
  51. package/static/build/services/system.js +67 -32
  52. package/static/build/services/user.js +15 -10
  53. package/static/build/shared/cronCommand.js +17 -0
  54. package/static/build/shared/cronSchedule.js +101 -0
  55. package/static/build/shared/cronScheduler.js +73 -0
  56. package/static/build/shared/i18n.js +3 -2
  57. package/static/build/shared/logPath.js +15 -0
  58. package/static/build/shared/loginThrottle.js +45 -0
  59. package/static/build/shared/pLimit.js +19 -6
  60. package/static/build/shared/runCron.js +13 -4
  61. package/static/build/shared/scriptHistory.js +445 -0
  62. package/static/build/shared/shellQuote.js +11 -0
  63. package/static/build/shared/subscriptionMutationLock.js +34 -0
  64. package/static/build/shared/subscriptionPath.js +41 -0
  65. package/static/build/shared/subscriptionStorage.js +98 -0
  66. package/static/build/shared/utils.js +2 -1
  67. package/static/build/token.js +3 -14
  68. package/static/build/validation/schedule.js +3 -18
  69. package/static/dist/292.264757f0.async.js +1 -0
  70. package/static/dist/3512.95e41255.async.js +1 -0
  71. package/static/dist/index.html +2 -2
  72. package/static/dist/preload_helper.01fcd534.js +1 -0
  73. package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
  74. package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
  75. package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
  76. package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
  77. package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
  78. package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
  79. package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
  80. package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
  81. package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
  82. package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
  83. package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
  84. package/static/dist/{umi.fc019050.js → umi.3a24a9fa.js} +1 -1
  85. package/test/back/advisory-input-security.test.cjs +416 -0
  86. package/test/back/code-scanning-security.test.cjs +189 -0
  87. package/test/back/cron-macros.test.cjs +51 -0
  88. package/test/back/cron-schedule-compatibility.test.cjs +69 -0
  89. package/test/back/cron-scheduler-routing.test.cjs +59 -0
  90. package/test/back/draft-path-security.test.cjs +235 -0
  91. package/test/back/env-import-security.test.cjs +91 -0
  92. package/test/back/execution-lifecycle.test.cjs +1 -1
  93. package/test/back/file-access-scope.test.cjs +180 -0
  94. package/test/back/grpc-mtls-security.test.cjs +136 -0
  95. package/test/back/http-security.test.cjs +105 -5
  96. package/test/back/legacy-cron-recovery.test.cjs +76 -0
  97. package/test/back/linux-mirror-result.test.cjs +176 -0
  98. package/test/back/listen-security.test.cjs +1 -0
  99. package/test/back/mirror-status.test.cjs +257 -0
  100. package/test/back/node-cron-migration.test.cjs +65 -0
  101. package/test/back/node-path-cache.test.cjs +29 -0
  102. package/test/back/node-path-lock.test.cjs +6 -2
  103. package/test/back/preload-lazy-notify.test.cjs +168 -0
  104. package/test/back/queued-cron-invalidation.test.cjs +98 -0
  105. package/test/back/restart-instance-recovery.test.cjs +116 -0
  106. package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
  107. package/test/back/scheduler-concurrency.test.cjs +411 -0
  108. package/test/back/scheduler-reconciliation.test.cjs +58 -4
  109. package/test/back/script-file-access.test.cjs +210 -0
  110. package/test/back/script-history-api.test.cjs +551 -0
  111. package/test/back/script-history.test.cjs +383 -0
  112. package/test/back/shell-api-parsing.test.cjs +4 -4
  113. package/test/back/shell-translation-output.test.cjs +16 -0
  114. package/test/back/shell-upgrade.test.cjs +312 -0
  115. package/test/back/shellApiToken.test.cjs +151 -0
  116. package/test/back/ssh-config-home.test.cjs +113 -0
  117. package/test/back/static-mirror-trigger.test.cjs +441 -0
  118. package/test/back/static-mirror.test.cjs +179 -0
  119. package/test/back/stop-race.test.cjs +5 -1
  120. package/test/back/subscription-mutation-lock.test.cjs +83 -0
  121. package/test/back/subscription-review-regression.test.cjs +607 -0
  122. package/test/back/subscription-storage-regression.test.cjs +553 -0
  123. package/test/back/system-dependence-cache.test.cjs +1 -0
  124. package/test/back/system-export-history.test.cjs +119 -0
  125. package/test/back/systemTokenRenewal.test.cjs +497 -0
  126. package/test/back/task-shell-lifecycle.test.cjs +194 -0
  127. package/test/back/unique-constraint-error.test.cjs +72 -0
  128. package/test/back/upgrade-guards.test.cjs +285 -0
  129. package/test/back/upgrade-lifecycle.test.cjs +288 -0
  130. package/test/back/upgrade-transaction.test.cjs +133 -0
  131. package/test/back/user-security.test.cjs +102 -1
  132. package/test/back/wpush-grpc.test.cjs +1 -0
  133. package/test/fixtures/legacy-cron.json +1371 -0
  134. package/test/front/diff-file-routing.test.cjs +126 -0
  135. package/test/front/script-history-save.test.cjs +121 -0
  136. package/test/helpers/upgrade-fixture.cjs +30 -0
  137. package/version.yaml +14 -19
  138. package/static/dist/2096.6d98fd12.async.js +0 -1
  139. package/static/dist/4859.97d6653c.async.js +0 -1
  140. package/static/dist/6707.801b886b.async.js +0 -1
  141. package/static/dist/preload_helper.cf7de3fa.js +0 -1
  142. package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +0 -1
  143. package/static/dist/src__pages__diff__index.1714b941.async.js +0 -1
  144. package/static/dist/src__pages__script__editModal.22acdf7c.async.js +0 -1
  145. package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +0 -1
  146. package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
  147. package/static/dist/src__pages__script__index.a18b2692.async.js +0 -1
  148. package/static/dist/src__pages__script__saveModal.8965b74c.async.js +0 -1
  149. package/static/dist/src__pages__subscription__modal.38991e1e.async.js +0 -1
  150. /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
@@ -0,0 +1,136 @@
1
+ const assert = require('node:assert/strict');
2
+ const test = require('node:test');
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const os = require('node:os');
6
+ const childProcess = require('node:child_process');
7
+ const grpc = require('@grpc/grpc-js');
8
+ const load = require('../helpers/load-security-module.cjs');
9
+ const logger = { info() {}, debug() {}, warn() {}, error() {} };
10
+
11
+ function fixture(t, extra = {}) {
12
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "ql-grpc's-"));
13
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
14
+ const module = load(path.resolve('back/config/grpcCerts.ts'), {
15
+ './index': { configPath: root },
16
+ './util': { fileExist: async (p) => fs.existsSync(p) },
17
+ '../loaders/logger': logger,
18
+ ...extra,
19
+ });
20
+ return { root, module };
21
+ }
22
+
23
+ test('gRPC keys use a private reserved workspace and clean up all OpenSSL side files', async (t) => {
24
+ const workspaces = new Set();
25
+ const { root, module } = fixture(t, {
26
+ child_process: {
27
+ ...childProcess,
28
+ execFileSync: (file, args, opts) => {
29
+ const temporary = args.find((arg) => arg.includes('/.generate-'));
30
+ const workspace = path.dirname(temporary);
31
+ workspaces.add(workspace);
32
+ assert.equal(fs.statSync(workspace).mode & 0o777, 0o700);
33
+ return childProcess.execFileSync(file, args, opts);
34
+ },
35
+ },
36
+ });
37
+ const certs = await module.initGrpcCerts();
38
+ assert.match(certs.serverCert, /BEGIN CERTIFICATE/);
39
+ assert.equal(workspaces.size, 1);
40
+ for (const dir of workspaces) assert.equal(fs.existsSync(dir), false);
41
+ assert.deepEqual(fs.readdirSync(path.join(root, 'grpc')).sort(), [
42
+ 'ca.crt',
43
+ 'ca.key',
44
+ 'client.crt',
45
+ 'client.key',
46
+ 'server.crt',
47
+ 'server.key',
48
+ ]);
49
+ for (const file of ['ca.key', 'client.key', 'server.key']) {
50
+ assert.equal(
51
+ fs.statSync(path.join(root, 'grpc', file)).mode & 0o777,
52
+ 0o600,
53
+ );
54
+ }
55
+ assert.strictEqual(await module.initGrpcCerts(), certs);
56
+ });
57
+
58
+ test('gRPC certificate generation cleans partial files on OpenSSL errors', async (t) => {
59
+ const { root, module } = fixture(t, {
60
+ child_process: {
61
+ execFileSync: (_file, args) => {
62
+ fs.writeFileSync(args[2], 'partial private key');
63
+ throw new Error('openssl failed');
64
+ },
65
+ },
66
+ });
67
+ await assert.rejects(module.initGrpcCerts(), /openssl failed/);
68
+ assert.deepEqual(fs.readdirSync(path.join(root, 'grpc')), []);
69
+ });
70
+
71
+ test('real gRPC server rejects plaintext and TLS without client certificates, accepts mTLS', async (t) => {
72
+ const { module } = fixture(t);
73
+ const certs = await module.initGrpcCerts();
74
+ let port;
75
+ let checks = 0;
76
+ const Grpc = load(path.resolve('back/services/grpc.ts'), {
77
+ '../config': { bindHostGrpc: '127.0.0.1', grpcPort: 0 },
78
+ '../config/grpcCerts': { initGrpcCerts: async () => certs },
79
+ '../loaders/logger': logger,
80
+ './metrics': { metricsService: { record() {} } },
81
+ typedi: { Service: () => (x) => x },
82
+ '@grpc/grpc-js': {
83
+ ...grpc,
84
+ Server: class extends grpc.Server {
85
+ bindAsync(address, creds, cb) {
86
+ super.bindAsync(address, creds, (e, boundPort) => {
87
+ port = boundPort;
88
+ cb(e, boundPort);
89
+ });
90
+ }
91
+ },
92
+ },
93
+ '../schedule/addCron': {},
94
+ '../schedule/delCron': {},
95
+ '../schedule/setConcurrency': {},
96
+ '../schedule/api': {},
97
+ '../schedule/health': {
98
+ check: (_call, cb) => {
99
+ checks++;
100
+ cb(null, { status: 1 });
101
+ },
102
+ },
103
+ }).GrpcServerService;
104
+ const server = new Grpc();
105
+ await server.initialize();
106
+ t.after(() => server.shutdown());
107
+ const HealthClient = load(path.resolve('back/protos/health.ts')).HealthClient;
108
+ const request = async (credentials) => {
109
+ const client = new HealthClient(`localhost:${port}`, credentials, {
110
+ 'grpc.enable_http_proxy': 0,
111
+ });
112
+ try {
113
+ return await new Promise((resolve, reject) => {
114
+ client.check({}, { deadline: Date.now() + 2000 }, (e, reply) =>
115
+ e ? reject(e) : resolve(reply),
116
+ );
117
+ });
118
+ } finally {
119
+ client.close();
120
+ }
121
+ };
122
+ await assert.rejects(request(grpc.credentials.createInsecure()));
123
+ await assert.rejects(
124
+ request(grpc.credentials.createSsl(Buffer.from(certs.caCert))),
125
+ );
126
+ assert.equal(checks, 0);
127
+ const reply = await request(
128
+ grpc.credentials.createSsl(
129
+ Buffer.from(certs.caCert),
130
+ Buffer.from(certs.clientKey),
131
+ Buffer.from(certs.clientCert),
132
+ ),
133
+ );
134
+ assert.equal(reply.status, 1);
135
+ assert.equal(checks, 1);
136
+ });
@@ -9,7 +9,7 @@ const load = require('../helpers/load-security-module.cjs');
9
9
 
10
10
  test('HTTP authentication protects init, scopes, expired sessions and config secrets', async (t) => {
11
11
  const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-http-security-'));
12
- for (const dir of ['config/grpc', 'scripts', 'upload', 'tmp'])
12
+ for (const dir of ['config/grpc', 'scripts', 'upload', 'tmp', 'bak'])
13
13
  fs.mkdirSync(path.join(tmp, dir), { recursive: true });
14
14
  fs.writeFileSync(path.join(tmp, 'config/grpc/client.key'), 'SENTINEL');
15
15
  fs.writeFileSync(path.join(tmp, 'config/normal.txt'), 'normal');
@@ -36,10 +36,13 @@ test('HTTP authentication protects init, scopes, expired sessions and config sec
36
36
  ),
37
37
  jwt: { secret },
38
38
  rootPath: tmp,
39
+ dataPath: tmp,
39
40
  configPath: path.join(tmp, 'config/'),
40
41
  scriptPath: path.join(tmp, 'scripts/'),
41
42
  uploadPath: path.join(tmp, 'upload'),
42
43
  tmpPath: path.join(tmp, 'tmp'),
44
+ bakPath: path.join(tmp, 'bak'),
45
+ writePathList: [path.join(tmp, 'scripts')],
43
46
  blackFileList: ['auth.json', 'grpc'],
44
47
  baseUrl: '/panel',
45
48
  };
@@ -48,6 +51,16 @@ test('HTTP authentication protects init, scopes, expired sessions and config sec
48
51
  scopes: ['configs'],
49
52
  tokens: [{ value: 'config-app', expiration: Date.now() / 1000 + 3600 }],
50
53
  },
54
+ {
55
+ scopes: ['configs', 'scripts'],
56
+ tokens: [
57
+ { value: 'script-config-app', expiration: Date.now() / 1000 + 3600 },
58
+ ],
59
+ },
60
+ {
61
+ scopes: ['scripts'],
62
+ tokens: [{ value: 'script-app', expiration: Date.now() / 1000 + 3600 }],
63
+ },
51
64
  {
52
65
  scopes: ['envs'],
53
66
  tokens: [{ value: 'env-app', expiration: Date.now() / 1000 + 3600 }],
@@ -68,6 +81,8 @@ test('HTTP authentication protects init, scopes, expired sessions and config sec
68
81
  getToken: (r) => (r.headers.authorization || '').replace(/^Bearer /, ''),
69
82
  getPlatform: () => 'desktop',
70
83
  getFileContentByName: (p) => fs.promises.readFile(p, 'utf8'),
84
+ fileExist: (p) => fs.existsSync(p),
85
+ rmPath: (p) => fs.promises.rm(p),
71
86
  },
72
87
  '../shared/i18n': { t: (x) => x },
73
88
  '../shared/store': {
@@ -75,11 +90,10 @@ test('HTTP authentication protects init, scopes, expired sessions and config sec
75
90
  },
76
91
  '../config/serverEnv': { serveEnv: (_req, res) => res.end() },
77
92
  '../services/user': User,
93
+ '../services/script': class {},
78
94
  '../data/open': {},
79
95
  '../data/system': {},
80
- '../shared/utils': {
81
- writeFileWithLock: (p, content) => fs.promises.writeFile(p, content),
82
- },
96
+ typedi: { Service: () => (x) => x },
83
97
  };
84
98
  const Config = load(path.join(__dirname, '../../back/services/config.ts'), {
85
99
  ...mocks,
@@ -87,8 +101,18 @@ test('HTTP authentication protects init, scopes, expired sessions and config sec
87
101
  }).default;
88
102
  const configService = new Config();
89
103
  mocks['../services/config'] = Config;
104
+ const historyCache = new Map();
105
+ const History = load(
106
+ path.join(__dirname, '../../back/services/scriptHistory.ts'),
107
+ mocks,
108
+ historyCache,
109
+ ).default;
110
+ const history = new History();
90
111
  mocks.typedi = {
91
- Container: { get: (x) => (x === User ? user : configService) },
112
+ ...mocks.typedi,
113
+ Container: {
114
+ get: (x) => (x === User ? user : x === History ? history : configService),
115
+ },
92
116
  };
93
117
  mocks['../api'] = () => {
94
118
  const router = express.Router();
@@ -96,6 +120,11 @@ test('HTTP authentication protects init, scopes, expired sessions and config sec
96
120
  load(path.join(__dirname, '../../back/api/config.ts'), mocks).default(
97
121
  router,
98
122
  );
123
+ load(
124
+ path.join(__dirname, '../../back/api/script.ts'),
125
+ mocks,
126
+ historyCache,
127
+ ).default(router);
99
128
  router.get('/envs', (_req, res) => res.json({ code: 200 }));
100
129
  return router;
101
130
  };
@@ -210,4 +239,75 @@ test('HTTP authentication protects init, scopes, expired sessions and config sec
210
239
  ).status,
211
240
  400,
212
241
  );
242
+ const scriptName = 'data/scripts/test.js';
243
+ for (const prefix of ['/open', '/panel/open']) {
244
+ const denied = await request(
245
+ `${prefix}/configs/save`,
246
+ 'config-app',
247
+ 'POST',
248
+ {
249
+ name: scriptName,
250
+ content: 'overwrite',
251
+ },
252
+ );
253
+ assert.equal(denied.body.code, 403);
254
+ assert.equal(fs.existsSync(path.join(tmp, 'scripts/test.js')), false);
255
+ }
256
+ assert.equal(
257
+ (
258
+ await request('/open/configs/save', 'script-config-app', 'POST', {
259
+ name: scriptName,
260
+ content: 'authorized script',
261
+ })
262
+ ).body.code,
263
+ 403,
264
+ );
265
+ assert.equal(fs.existsSync(path.join(tmp, 'scripts/test.js')), false);
266
+ assert.equal(
267
+ (await request(`/open/configs/detail?path=${scriptName}`, 'config-app'))
268
+ .body.code,
269
+ 403,
270
+ );
271
+ assert.equal(
272
+ (
273
+ await request('/open/configs/save', 'config-app', 'POST', {
274
+ name: 'normal.txt',
275
+ content: 'normal config',
276
+ })
277
+ ).body.code,
278
+ 200,
279
+ );
280
+ assert.equal(
281
+ (
282
+ await request('/api/configs/save', valid, 'POST', {
283
+ name: scriptName,
284
+ content: 'owner script',
285
+ })
286
+ ).body.code,
287
+ 403,
288
+ );
289
+ for (const prefix of ['/open', '/panel/open']) {
290
+ assert.equal(
291
+ (
292
+ await request(`${prefix}/scripts`, 'config-app', 'POST', {
293
+ filename: 'test.js',
294
+ content: 'unauthorized',
295
+ })
296
+ ).status,
297
+ 401,
298
+ );
299
+ assert.equal(
300
+ (
301
+ await request(`${prefix}/scripts`, 'script-app', 'POST', {
302
+ filename: 'test.js',
303
+ content: 'authorized script',
304
+ })
305
+ ).body.code,
306
+ 200,
307
+ );
308
+ assert.equal(
309
+ fs.readFileSync(path.join(tmp, 'scripts/test.js'), 'utf8'),
310
+ 'authorized script',
311
+ );
312
+ }
213
313
  });
@@ -0,0 +1,76 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const { format } = require('node:util');
4
+ const load = require('../helpers/load-security-module.cjs');
5
+ const { SchedulerReadiness } = require('../../back/shared/schedulerReadiness');
6
+
7
+ const { getTasks } = require('node-cron');
8
+
9
+ const legacySchedules = [
10
+ '* * * * *',
11
+ '*/5 * * * *',
12
+ '0 */10 * * * ?',
13
+ '0 */2 * * * ?',
14
+ '0 */5 * * * ?',
15
+ '0 0/30 * * * ?',
16
+ '0 0/5 * * * ?',
17
+ '0 0 12 ? * MON',
18
+ ];
19
+
20
+ test('API accepts legacy question-mark schedules but rejects malformed cron', () => {
21
+ const { scheduleSchema } = load('back/validation/schedule.ts', {
22
+ '../config': { logPath: '/ql/data/log' },
23
+ });
24
+ for (const schedule of [...legacySchedules, '@once', '@boot']) {
25
+ assert.equal(scheduleSchema.validate(schedule).error, undefined, schedule);
26
+ }
27
+ for (const schedule of ['@bogus', '0 /5 * * * ?', '0 70 * * * ?', 'not a cron']) {
28
+ assert.ok(scheduleSchema.validate(schedule).error, schedule);
29
+ }
30
+ });
31
+
32
+ test('legacy main and extra schedules restore real jobs and healthy readiness', async (t) => {
33
+ const stacks = new Map();
34
+ t.after(() => {
35
+ for (const jobs of stacks.values()) for (const job of jobs) job.cancel();
36
+ });
37
+ const { addCron } = load('back/schedule/addCron.ts', {
38
+ './data': { scheduleStacks: stacks },
39
+ '../shared/runCron': { runCron() {} },
40
+ '../loaders/logger': { info() {}, warn() {} },
41
+ '../shared/i18n': { tf: format },
42
+ });
43
+ const crons = legacySchedules.map((schedule, index) => ({
44
+ id: `legacy-${index}`, name: 'legacy', schedule, command: 'true',
45
+ extra_schedules: [{ schedule: '0 0/30 * * * ?' }],
46
+ }));
47
+ const register = (rows) => new Promise((resolve, reject) => {
48
+ addCron({ request: { replace: true, crons: rows } }, (error) =>
49
+ error ? reject(error) : resolve());
50
+ });
51
+ const readiness = new SchedulerReadiness(async () => {}, 60000);
52
+ t.after(() => clearTimeout(readiness.retry));
53
+ readiness.configure(() => register(crons));
54
+ assert.equal(await readiness.recover(), true);
55
+ assert.equal(await readiness.check(), true);
56
+ assert.equal(stacks.size, crons.length);
57
+ for (const jobs of stacks.values()) {
58
+ assert.equal(jobs.length, 2);
59
+ assert.ok(jobs.every((job) => typeof job.cancel === 'function'));
60
+ }
61
+ const nativeJobs = [...getTasks().values()];
62
+ assert.equal(nativeJobs.length, crons.length * 2);
63
+ assert.ok(nativeJobs.every((job) => job.getStatus() !== 'stopped'));
64
+ const previousJobs = [...stacks.values()].flat();
65
+ for (const schedule of ['@bogus', '0 /5 * * * ?', '0 70 * * * ?']) {
66
+ for (const invalid of [
67
+ { ...crons[0], schedule },
68
+ { ...crons[0], extra_schedules: [{ schedule }] },
69
+ ]) {
70
+ await assert.rejects(register([invalid]), (error) => error.code === 3);
71
+ assert.deepEqual([...stacks.values()].flat(), previousJobs);
72
+ assert.deepEqual([...getTasks().values()], nativeJobs);
73
+ assert.ok(nativeJobs.every((job) => job.getStatus() !== 'stopped'));
74
+ }
75
+ }
76
+ });
@@ -0,0 +1,176 @@
1
+ const assert = require('node:assert/strict');
2
+ const test = require('node:test');
3
+ const { spawn, spawnSync } = require('node:child_process');
4
+ const fs = require('node:fs/promises');
5
+ const os = require('node:os');
6
+ const path = require('node:path');
7
+ const load = require('../helpers/load-security-module.cjs');
8
+
9
+ for (const scenario of [
10
+ {
11
+ name: 'stderr warning with exit zero',
12
+ code: 'process.stderr.write("sudo: warning\\n")',
13
+ saved: true,
14
+ },
15
+ {
16
+ name: 'nonzero exit without stderr',
17
+ code: 'process.exitCode = 7',
18
+ saved: false,
19
+ },
20
+ {
21
+ name: 'signal termination',
22
+ code: 'process.kill(process.pid, "SIGTERM")',
23
+ saved: false,
24
+ },
25
+ { name: 'spawn failure', shell: '/nonexistent-ql-test-shell', saved: false },
26
+ ]) {
27
+ test(`Linux mirror persistence follows the process result: ${scenario.name}`, async () => {
28
+ const logger = { info() {}, error() {} };
29
+ const ScheduleService = load('back/services/schedule.ts', {
30
+ '../shared/cronScheduler': {},
31
+ '../shared/pLimit': { runWithSystemLimit: async (_params, fn) => fn() },
32
+ 'cross-spawn': {
33
+ spawn: () =>
34
+ spawn(
35
+ process.execPath,
36
+ ['-e', scenario.code || ''],
37
+ scenario.shell ? { shell: scenario.shell } : {},
38
+ ),
39
+ },
40
+ }).default;
41
+ const schedule = new ScheduleService(logger);
42
+ let completion;
43
+ const runTask = schedule.runTask.bind(schedule);
44
+ schedule.runTask = (...args) => (completion = runTask(...args));
45
+ const messages = [],
46
+ writes = [];
47
+ const SystemService = load('back/services/system.ts', {
48
+ os: { platform: () => 'linux' },
49
+ '../config': {},
50
+ '../config/const': {},
51
+ '../config/util': { updateLinuxMirrorFile: async () => 'fixture' },
52
+ '../data/dependence': {},
53
+ '../data/notify': {},
54
+ '../data/system': {},
55
+ '../schedule/client': {},
56
+ '../shared/pLimit': {},
57
+ '../shared/i18n': {},
58
+ '../shared/schedulerMutationLock': {},
59
+ './notify': class {},
60
+ './schedule': ScheduleService,
61
+ './sock': class {},
62
+ }).default;
63
+ const service = new SystemService(logger, schedule, {
64
+ sendMessage: (message) => messages.push(message),
65
+ });
66
+ service.getSystemConfig = async () => ({ id: 1, info: { lang: 'en-US' } });
67
+ service.updateAuthDb = async (doc) => writes.push(doc);
68
+ let ended = 0;
69
+ await service.updateLinuxMirror(
70
+ { linuxMirror: 'https://mirror.example' },
71
+ undefined,
72
+ () => ended++,
73
+ );
74
+ const result = await completion;
75
+ assert.equal(ended, 1);
76
+ assert.equal(writes.length, scenario.saved ? 1 : 0);
77
+ assert.ok(messages.some((message) => message.status === 'completed'));
78
+ if (scenario.saved) {
79
+ assert.equal(result.code, 0);
80
+ assert.ok(
81
+ messages.some((message) => message.message.includes('sudo: warning')),
82
+ );
83
+ assert.deepEqual(writes[0].info, {
84
+ lang: 'en-US',
85
+ linuxMirror: 'https://mirror.example',
86
+ });
87
+ }
88
+ });
89
+ }
90
+
91
+ const hasApt = spawnSync('apt-get', ['--version']).status === 0;
92
+ for (const distribution of ['Debian', 'Ubuntu']) {
93
+ for (const existingMirror of [false, true]) {
94
+ test(
95
+ `${distribution} mirror update rejects failed index downloads with ${
96
+ existingMirror ? 'an existing' : 'an empty'
97
+ } source`,
98
+ { skip: !hasApt },
99
+ async (t) => {
100
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-apt-result-'));
101
+ t.after(() => fs.rm(root, { recursive: true, force: true }));
102
+ const parts = path.join(root, 'parts');
103
+ await fs.mkdir(parts);
104
+ await fs.mkdir(path.join(root, 'lists', 'partial'), {
105
+ recursive: true,
106
+ });
107
+ const source = path.join(root, 'sources.list');
108
+ await fs.writeFile(
109
+ source,
110
+ 'deb http://127.0.0.1:1/debian bookworm main\n',
111
+ );
112
+ // Load only fixture configuration, including all writable APT paths.
113
+ const aptConfig = path.join(root, 'apt.conf');
114
+ await fs.writeFile(
115
+ aptConfig,
116
+ `Dir::Etc::main "/dev/null";
117
+ Dir::Etc::parts "${parts}";
118
+ Dir::Etc::sourcelist "${source}";
119
+ Dir::Etc::sourceparts "${parts}";
120
+ Dir::State::lists "${path.join(root, 'lists')}";
121
+ Dir::Cache "${path.join(root, 'cache')}";
122
+ Dir::Log "${path.join(root, 'logs')}";
123
+ Acquire::Retries "0";
124
+ Acquire::http::Proxy "DIRECT";
125
+ Acquire::http::Timeout "1";
126
+ `,
127
+ );
128
+ const util = load('back/config/util.ts', {
129
+ 'fs/promises': {
130
+ readFile: async (file) =>
131
+ file === '/etc/os-release'
132
+ ? distribution
133
+ : existingMirror
134
+ ? 'URIs: http://old.example/debian\n'
135
+ : '',
136
+ },
137
+ os: { platform: () => 'linux' },
138
+ './index': {},
139
+ './const': {},
140
+ './share': {},
141
+ './container': { isInContainer: () => false },
142
+ '../loaders/logger': {},
143
+ '../shared/utils': {},
144
+ '../shared/fileAccess': {},
145
+ '../data/dependence': {},
146
+ });
147
+ const previousOS = process.env.QL_OS_TYPE;
148
+ let command;
149
+ try {
150
+ process.env.QL_OS_TYPE = distribution.toLowerCase();
151
+ command = await util.updateLinuxMirrorFile('http://127.0.0.1:1');
152
+ } finally {
153
+ if (previousOS === undefined) delete process.env.QL_OS_TYPE;
154
+ else process.env.QL_OS_TYPE = previousOS;
155
+ }
156
+ // Exercise the generated updater without modifying host source files.
157
+ const [program, ...args] = command
158
+ .slice(command.lastIndexOf('&&') + 2)
159
+ .trim()
160
+ .split(/\s+/);
161
+ assert.equal(program, 'apt-get');
162
+ const options = {
163
+ encoding: 'utf8',
164
+ timeout: 10000,
165
+ env: { ...process.env, APT_CONFIG: aptConfig },
166
+ };
167
+ const baseline = spawnSync('apt-get', ['update'], options);
168
+ assert.equal(baseline.status, 0, baseline.stderr);
169
+ assert.match(baseline.stderr, /Failed to fetch/);
170
+ const result = spawnSync(program, args, options);
171
+ assert.equal(result.status, 100, result.stderr);
172
+ assert.match(result.stderr, /Failed to fetch/);
173
+ },
174
+ );
175
+ }
176
+ }
@@ -48,6 +48,7 @@ for (const host of ['127.0.0.1', '::1', '192.0.2.1', '::']) {
48
48
  '../protos/api': { ApiService: {} },
49
49
  '../schedule/addCron': {},
50
50
  '../schedule/delCron': {},
51
+ '../schedule/setConcurrency': {},
51
52
  '../schedule/health': {},
52
53
  '../schedule/api': {},
53
54
  '../config/grpcCerts': {