@fishawack/lab-env 5.9.0-beta.4 → 5.9.0-beta.5

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.
@@ -0,0 +1,1205 @@
1
+ const assert = require("assert/strict");
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+ const vm = require("vm");
5
+ const { EventEmitter } = require("events");
6
+
7
+ const commandPath = path.join(
8
+ __dirname,
9
+ "../commands/create/cmds/force-managed-updates.js",
10
+ );
11
+ const helperPath = path.join(
12
+ __dirname,
13
+ "../commands/create/services/aws/elasticbeanstalk.js",
14
+ );
15
+
16
+ function load(file, dependencies, globals = {}) {
17
+ const module = { exports: {} };
18
+ vm.runInNewContext(
19
+ fs.readFileSync(file, "utf8"),
20
+ {
21
+ module,
22
+ require: (name) => {
23
+ assert.ok(
24
+ Object.hasOwn(dependencies, name),
25
+ `Unexpected dependency: ${name}`,
26
+ );
27
+ return dependencies[name];
28
+ },
29
+ ...globals,
30
+ },
31
+ { filename: file },
32
+ );
33
+ return module.exports;
34
+ }
35
+
36
+ function harness(options = {}) {
37
+ const calls = [];
38
+ const messages = [];
39
+ const colors = [];
40
+ const process = new EventEmitter();
41
+ let now = 0;
42
+ const environments = ["first", "second"].map((name) => ({
43
+ EnvironmentId: name,
44
+ EnvironmentName: name,
45
+ ApplicationName: "application",
46
+ Status: "Ready",
47
+ }));
48
+ const action = (environmentId) => ({
49
+ ActionId: `update-${environmentId}`,
50
+ ActionType: "PlatformUpdate",
51
+ ActionDescription: "Platform update",
52
+ Status: "Scheduled",
53
+ });
54
+ const eb = {
55
+ listAccountEnvironments: async () => {
56
+ calls.push("list");
57
+ return environments;
58
+ },
59
+ getManagedActions: async (environmentId) => {
60
+ calls.push(`actions:${environmentId}`);
61
+ return [
62
+ action(environmentId),
63
+ {
64
+ ...action(environmentId),
65
+ ActionId: "refresh",
66
+ ActionType: "InstanceRefresh",
67
+ },
68
+ ];
69
+ },
70
+ applyManagedAction: async (environmentId) =>
71
+ calls.push(`apply:${environmentId}`),
72
+ getManagedActionHistory: async (environmentId) => {
73
+ calls.push(`history:${environmentId}`);
74
+ return { Status: "Completed" };
75
+ },
76
+ ...options.eb,
77
+ };
78
+ const prompts = [];
79
+ const inquirer = {
80
+ prompt: async ([question]) => {
81
+ prompts.push(question);
82
+ calls.push(`prompt:${question.name}`);
83
+ if (question.name === "selected") {
84
+ return {
85
+ selected: options.none
86
+ ? []
87
+ : question.choices.map((choice) => choice.value),
88
+ };
89
+ }
90
+ if (question.name === "confirmed")
91
+ return { confirmed: options.confirmed !== false };
92
+ return { all: true };
93
+ },
94
+ };
95
+ class Spinner {
96
+ constructor() {
97
+ this.ora = Object.fromEntries(
98
+ ["succeed", "fail", "warn", "stop"].map((name) => [
99
+ name,
100
+ () => {},
101
+ ]),
102
+ );
103
+ }
104
+ }
105
+ const command = load(
106
+ commandPath,
107
+ {
108
+ "../../../globals.js": {
109
+ config: { preset: options.preset || "devops" },
110
+ },
111
+ inquirer,
112
+ semver: require("semver"),
113
+ "../services/aws/elasticbeanstalk.js": eb,
114
+ "../services/aws/misc.js": {
115
+ setAWSClientDefaults: (client, region) =>
116
+ calls.push(`scope:${client}:${region}`),
117
+ },
118
+ "../libs/utilities": {
119
+ Spinner,
120
+ colorize: (text, type) => {
121
+ colors.push({ text, type });
122
+ return text;
123
+ },
124
+ },
125
+ "../libs/prompts.js": {
126
+ client: { choices: ["account"] },
127
+ region: { choices: ["us-east-1", "eu-west-1"] },
128
+ },
129
+ },
130
+ {
131
+ process,
132
+ console: { log: (message) => messages.push(message) },
133
+ Date: class extends Date {
134
+ static now() {
135
+ return now;
136
+ }
137
+ },
138
+ setTimeout: (callback, duration) => {
139
+ now += duration;
140
+ callback();
141
+ },
142
+ },
143
+ );
144
+ return {
145
+ command,
146
+ eb,
147
+ calls,
148
+ messages,
149
+ colors,
150
+ prompts,
151
+ process,
152
+ environments,
153
+ action,
154
+ run: (argv = {}) =>
155
+ command[3]({
156
+ client: "account",
157
+ region: "us-east-1",
158
+ timeout: 120,
159
+ ...argv,
160
+ }),
161
+ };
162
+ }
163
+
164
+ function patchHarness(options = {}) {
165
+ const test = harness(options);
166
+ const current = {
167
+ PlatformArn: "current-platform",
168
+ PlatformVersion: "4.13.12",
169
+ PlatformBranchName: "PHP 8.2 running on 64bit Amazon Linux 2023",
170
+ PlatformBranchLifecycleState: "Supported",
171
+ PlatformStatus: "Ready",
172
+ PlatformOwner: "AWSElasticBeanstalk",
173
+ SolutionStackName: "64bit Amazon Linux 2023 v4.13.12 running PHP 8.2",
174
+ };
175
+ const version = (number, extra = {}) => ({
176
+ ...current,
177
+ PlatformArn: `platform-${number}`,
178
+ PlatformVersion: number,
179
+ ...extra,
180
+ });
181
+ const versions = [
182
+ version("4.13.9"),
183
+ version("4.13.13"),
184
+ version("4.13.15"),
185
+ version("4.14.0"),
186
+ version("5.0.0"),
187
+ ];
188
+ const polls = new Map();
189
+ const submitted = new Map();
190
+ test.environments.forEach((environment) => {
191
+ environment.PlatformArn = current.PlatformArn;
192
+ environment.SolutionStackName = current.SolutionStackName;
193
+ });
194
+ test.eb.getManagedActions = async () => [];
195
+ test.eb.describePlatformVersion = async (arn) =>
196
+ arn === current.PlatformArn
197
+ ? current
198
+ : versions.find((item) => item.PlatformArn === arn);
199
+ test.eb.listBranchPlatformVersions = async (branchName) => {
200
+ test.calls.push(`versions:${branchName}`);
201
+ return versions;
202
+ };
203
+ test.eb.updateEnvironmentPlatform = async (environmentId, platformArn) => {
204
+ test.calls.push(`direct:${environmentId}`);
205
+ submitted.set(environmentId, platformArn);
206
+ return { Status: "Updating", DateUpdated: new Date(0) };
207
+ };
208
+ test.eb.describeEnvironment = async (name) => {
209
+ const environment = test.environments.find(
210
+ (item) => item.EnvironmentName === name,
211
+ );
212
+ const count = (polls.get(environment.EnvironmentId) || 0) + 1;
213
+ polls.set(environment.EnvironmentId, count);
214
+ test.calls.push(`describe:${name}`);
215
+ if (count >= 3) {
216
+ environment.PlatformArn = submitted.get(environment.EnvironmentId);
217
+ test.calls.push(`finished:${name}`);
218
+ return {
219
+ Environments: [{ ...environment, DateUpdated: new Date(1) }],
220
+ };
221
+ }
222
+ return {
223
+ Environments: [
224
+ { ...environment, Status: count === 1 ? "Ready" : "Updating" },
225
+ ],
226
+ };
227
+ };
228
+ test.eb.getEnvironmentUpdateEvents = async (environmentId, started) => {
229
+ assert.equal(started.getTime(), 0);
230
+ return polls.get(environmentId) >= 3
231
+ ? [
232
+ {
233
+ Message: "Environment update completed successfully.",
234
+ Severity: "INFO",
235
+ EventDate: new Date(1),
236
+ },
237
+ ]
238
+ : [];
239
+ };
240
+ return { ...test, current, versions, version, polls, submitted };
241
+ }
242
+
243
+ describe("force-managed-updates (offline)", () => {
244
+ it("registers fmu and hides help outside devops", () => {
245
+ assert.deepEqual(Array.from(harness().command[0]), [
246
+ "force-managed-updates",
247
+ "fmu",
248
+ ]);
249
+ assert.equal(harness({ preset: "permanent" }).command[1], false);
250
+ });
251
+
252
+ it("validates monitoring timeout", () => {
253
+ let check;
254
+ harness().command[2]({
255
+ option() {},
256
+ check(callback) {
257
+ check = callback;
258
+ },
259
+ });
260
+ for (const timeout of [0, -1, NaN, Infinity])
261
+ assert.throws(() => check({ timeout }));
262
+ assert.equal(check({ timeout: 120 }), true);
263
+ assert.throws(() => check({ timeout: 120, filter: " " }));
264
+ });
265
+
266
+ it("filters current platform descriptions using literal, contiguous, case-insensitive phrases", async () => {
267
+ const cases = [
268
+ ["PHP", "64bit Amazon Linux 2023 v4.13.10 running PHP 8.2", true],
269
+ [
270
+ "php 8.2",
271
+ "64bit Amazon Linux 2023 v4.13.10 running PHP 8.2",
272
+ true,
273
+ ],
274
+ [
275
+ " PHP 8.2 ",
276
+ "64bit Amazon Linux 2023 v4.13.10 running PHP 8.2",
277
+ true,
278
+ ],
279
+ [
280
+ "PHP 8.2",
281
+ "64bit Amazon Linux 2023 v4.13.10 running PHP 8.1",
282
+ false,
283
+ ],
284
+ [
285
+ "PHP 8.2",
286
+ "64bit Amazon Linux 2023 v4.13.10 running PHP 8.20",
287
+ false,
288
+ ],
289
+ [
290
+ "PHP 8.2",
291
+ "64bit Amazon Linux 2023 v4.13.10 running PHP 8.2.1",
292
+ false,
293
+ ],
294
+ ["PHP 8.2", "64bit Amazon Linux 2023 running PHP 8x2", false],
295
+ ["PHP", "64bit Amazon Linux 2023 running NotPHP 8.2", false],
296
+ [
297
+ "Amazon Linux 2023",
298
+ "64bit Amazon Linux 2023 v4.13.10 running PHP 8.2",
299
+ true,
300
+ ],
301
+ [
302
+ "Amazon Linux 2023",
303
+ "64bit Amazon Linux 2 v3.8.0 running PHP 8.2 (build 023)",
304
+ false,
305
+ ],
306
+ [
307
+ "Amazon Linux 2",
308
+ "64bit Amazon Linux 2023 v4.13.10 running PHP 8.2",
309
+ false,
310
+ ],
311
+ [
312
+ "PHP 8.2",
313
+ "64bit Amazon Linux 2023 running PHP something 8.2",
314
+ false,
315
+ ],
316
+ ["PHP.*", "64bit Amazon Linux 2023 running PHP 8.2", false],
317
+ ];
318
+ for (const [filter, platform, expected] of cases) {
319
+ const test = harness({ none: true });
320
+ test.environments.forEach((environment) => {
321
+ environment.SolutionStackName = platform;
322
+ });
323
+ await test.run({ filter });
324
+ const selection = test.prompts.find(
325
+ (prompt) => prompt.name === "selected",
326
+ );
327
+ assert.equal(
328
+ Boolean(selection),
329
+ expected,
330
+ `${filter} against ${platform}`,
331
+ );
332
+ assert.equal(
333
+ test.messages.some((message) =>
334
+ message.includes("Environment scan report:"),
335
+ ),
336
+ expected,
337
+ );
338
+ if (expected) assert.equal(selection.choices.length, 2);
339
+ else
340
+ assert.ok(
341
+ !test.calls.some((call) => call.startsWith("actions:")),
342
+ );
343
+ }
344
+ });
345
+
346
+ it("uses platform metadata as a fallback without matching target update descriptions", async () => {
347
+ const test = harness({ none: true });
348
+ test.environments[0].PlatformArn = "php-platform";
349
+ test.environments[1].SolutionStackName =
350
+ "64bit Amazon Linux 2023 running Python 3.13";
351
+ test.eb.describePlatformVersion = async () => ({
352
+ SolutionStackName: "64bit Amazon Linux 2023 running PHP 8.2",
353
+ });
354
+ await test.run({ filter: "PHP 8.2" });
355
+ const selection = test.prompts.find(
356
+ (prompt) => prompt.name === "selected",
357
+ );
358
+ assert.equal(selection.choices.length, 1);
359
+ assert.equal(
360
+ selection.choices[0].value.environment.EnvironmentId,
361
+ "first",
362
+ );
363
+ assert.ok(
364
+ !test.messages.some((message) => message.includes(" / second")),
365
+ );
366
+ });
367
+
368
+ it("retains account-wide concurrency checks outside the platform filter", async () => {
369
+ const test = harness();
370
+ test.environments[0].SolutionStackName =
371
+ "64bit Amazon Linux 2023 running PHP 8.2";
372
+ test.environments[1].SolutionStackName =
373
+ "64bit Amazon Linux 2023 running Python 3.13";
374
+ const actions = test.eb.getManagedActions;
375
+ test.eb.getManagedActions = async (environmentId) =>
376
+ environmentId === "second"
377
+ ? [{ Status: "Running" }]
378
+ : actions(environmentId);
379
+ await test.run({ filter: "PHP 8.2" });
380
+ assert.ok(!test.calls.some((call) => call.startsWith("apply:")));
381
+ assert.ok(
382
+ test.messages.some((message) => message.includes("Skipped:")),
383
+ );
384
+ });
385
+
386
+ it("finishes the full scan before presenting unchecked platform updates", async () => {
387
+ const test = harness({ none: true });
388
+ await test.run({ client: undefined, region: undefined, all: true });
389
+ assert.equal(test.calls.filter((call) => call === "list").length, 2);
390
+ assert.equal(
391
+ test.calls.filter((call) => call.startsWith("actions:")).length,
392
+ 4,
393
+ );
394
+ const selection = test.prompts.find(
395
+ (prompt) => prompt.name === "selected",
396
+ );
397
+ assert.equal(selection.choices.length, 4);
398
+ assert.ok(
399
+ selection.choices.every(
400
+ (choice) =>
401
+ choice.checked === false &&
402
+ choice.value.action.ActionType === "PlatformUpdate",
403
+ ),
404
+ );
405
+ assert.ok(
406
+ test.calls.lastIndexOf("actions:second") <
407
+ test.calls.indexOf("prompt:selected"),
408
+ );
409
+ assert.ok(!test.calls.some((call) => call.startsWith("apply:")));
410
+ });
411
+
412
+ it("does not apply updates when confirmation is declined", async () => {
413
+ const test = harness({ confirmed: false });
414
+ await test.run();
415
+ assert.ok(!test.calls.some((call) => call.startsWith("apply:")));
416
+ });
417
+
418
+ it("reports recommended, outdated, retired, deprecated, and unverified platforms without offering unscheduled updates", async () => {
419
+ const cases = [
420
+ [
421
+ {
422
+ PlatformBranchLifecycleState: "Supported",
423
+ PlatformLifecycleState: "Recommended",
424
+ PlatformStatus: "Ready",
425
+ },
426
+ "Up to date (AWS recommended",
427
+ "success",
428
+ ],
429
+ [
430
+ {
431
+ PlatformBranchLifecycleState: "Supported",
432
+ PlatformStatus: "Ready",
433
+ },
434
+ "Not on AWS recommended",
435
+ "warning",
436
+ ],
437
+ [
438
+ {
439
+ PlatformBranchLifecycleState: "Retired",
440
+ PlatformLifecycleState: "Recommended",
441
+ },
442
+ "Retired platform branch; migration required",
443
+ "error",
444
+ ],
445
+ [
446
+ { PlatformBranchLifecycleState: "Deprecated" },
447
+ "Deprecated platform branch; migration recommended",
448
+ "warning",
449
+ ],
450
+ [
451
+ {
452
+ PlatformBranchLifecycleState: "Beta",
453
+ PlatformLifecycleState: "Recommended",
454
+ },
455
+ "platform currency unverified",
456
+ "warning",
457
+ ],
458
+ [null, "platform currency unverified", "warning"],
459
+ ];
460
+ for (const [platform, expected, color] of cases) {
461
+ const test = harness();
462
+ test.environments.forEach((environment) => {
463
+ environment.PlatformArn = "shared-platform";
464
+ });
465
+ let lookups = 0;
466
+ test.eb.describePlatformVersion = async () => {
467
+ lookups++;
468
+ return platform;
469
+ };
470
+ test.eb.getManagedActions = async () => [];
471
+ await test.run();
472
+ assert.equal(lookups, 1);
473
+ assert.equal(
474
+ test.messages.filter((message) => message.includes(expected))
475
+ .length,
476
+ 2,
477
+ );
478
+ assert.ok(
479
+ test.colors.some(
480
+ (entry) =>
481
+ entry.text.includes(expected) && entry.type === color,
482
+ ),
483
+ );
484
+ assert.ok(
485
+ !test.prompts.some((prompt) => prompt.name === "selected"),
486
+ );
487
+ assert.ok(!test.calls.some((call) => call.startsWith("apply:")));
488
+ }
489
+ });
490
+
491
+ it("reports platform lookup errors without hiding scheduled updates", async () => {
492
+ const test = harness({ none: true });
493
+ test.environments.forEach((environment) => {
494
+ environment.PlatformArn = "restricted-platform";
495
+ });
496
+ test.eb.describePlatformVersion = async () => {
497
+ throw new Error("Access denied");
498
+ };
499
+ await test.run();
500
+ assert.ok(
501
+ test.messages.some((message) =>
502
+ message.includes("Platform lookup failed: Access denied"),
503
+ ),
504
+ );
505
+ assert.ok(
506
+ test.colors.some(
507
+ (entry) =>
508
+ entry.text.includes(
509
+ "Platform lookup failed: Access denied",
510
+ ) && entry.type === "error",
511
+ ),
512
+ );
513
+ assert.equal(
514
+ test.prompts.find((prompt) => prompt.name === "selected").choices
515
+ .length,
516
+ 2,
517
+ );
518
+ assert.equal(test.process.exitCode, 1);
519
+ });
520
+
521
+ it("does not label running updates or missing platform metadata as up to date", async () => {
522
+ for (const active of [true, false]) {
523
+ const test = harness();
524
+ test.eb.getManagedActions = async () =>
525
+ active ? [{ Status: "Running" }] : [];
526
+ await test.run();
527
+ assert.ok(
528
+ test.messages.some((message) =>
529
+ message.includes(
530
+ active
531
+ ? "Managed action in progress"
532
+ : "platform currency unverified",
533
+ ),
534
+ ),
535
+ );
536
+ assert.ok(
537
+ !test.messages.some((message) =>
538
+ message.includes("Up to date (AWS recommended"),
539
+ ),
540
+ );
541
+ assert.ok(
542
+ test.colors.some(
543
+ (entry) =>
544
+ entry.text.includes(
545
+ active
546
+ ? "Managed action in progress"
547
+ : "platform currency unverified",
548
+ ) && entry.type === (active ? "info" : "warning"),
549
+ ),
550
+ );
551
+ }
552
+ });
553
+
554
+ it("waits for exact action completion before starting the next update", async () => {
555
+ const test = harness();
556
+ let polls = 0;
557
+ test.eb.getManagedActionHistory = async (environmentId, actionId) => {
558
+ assert.equal(actionId, `update-${environmentId}`);
559
+ test.calls.push(`history:${environmentId}`);
560
+ if (++polls === 1) return null;
561
+ return { Status: "Completed" };
562
+ };
563
+ await test.run();
564
+ const secondApply = test.calls.indexOf("apply:second");
565
+ assert.ok(secondApply > test.calls.lastIndexOf("history:first"));
566
+ assert.equal(
567
+ test.calls.filter((call) => call === "history:first").length,
568
+ 2,
569
+ );
570
+ assert.equal(test.process.listenerCount("SIGINT"), 0);
571
+ });
572
+
573
+ it("continues after a confirmed failure and reports AWS details", async () => {
574
+ const test = harness();
575
+ test.eb.getManagedActionHistory = async (environmentId) =>
576
+ environmentId === "first"
577
+ ? {
578
+ Status: "Failed",
579
+ FailureType: "RollbackSuccessful",
580
+ FailureDescription: "Quota exceeded",
581
+ }
582
+ : { Status: "Completed" };
583
+ await test.run();
584
+ assert.ok(test.calls.includes("apply:second"));
585
+ assert.ok(
586
+ test.messages.some((message) =>
587
+ message.includes("RollbackSuccessful: Quota exceeded"),
588
+ ),
589
+ );
590
+ assert.equal(test.process.exitCode, 1);
591
+ });
592
+
593
+ it("stops on timeout rather than treating a missing action as success", async () => {
594
+ const test = harness();
595
+ test.eb.getManagedActionHistory = async () => null;
596
+ await test.run({ timeout: 0.01 });
597
+ assert.ok(!test.calls.includes("apply:second"));
598
+ assert.ok(
599
+ test.messages.some((message) => message.includes("Uncertain:")),
600
+ );
601
+ assert.ok(
602
+ test.messages.some((message) => message.includes("Not started:")),
603
+ );
604
+ });
605
+
606
+ it("waits through temporary unknown history and active-action states before advancing", async () => {
607
+ for (const source of ["history", "active"]) {
608
+ const test = harness();
609
+ let polls = 0;
610
+ let applied = false;
611
+ const apply = test.eb.applyManagedAction;
612
+ const actions = test.eb.getManagedActions;
613
+ test.eb.applyManagedAction = async (environmentId) => {
614
+ applied = true;
615
+ return apply(environmentId);
616
+ };
617
+ test.eb.getManagedActions = async (environmentId) => {
618
+ if (
619
+ source === "active" &&
620
+ applied &&
621
+ environmentId === "first" &&
622
+ polls <= 2
623
+ ) {
624
+ return [
625
+ { ...test.action(environmentId), Status: "Unknown" },
626
+ ];
627
+ }
628
+ return actions(environmentId);
629
+ };
630
+ test.eb.getManagedActionHistory = async (environmentId) => {
631
+ test.calls.push(`history:${environmentId}`);
632
+ if (environmentId === "first" && ++polls <= 2) {
633
+ assert.ok(!test.calls.includes("apply:second"));
634
+ return source === "history" ? { Status: "Unknown" } : null;
635
+ }
636
+ return { Status: "Completed" };
637
+ };
638
+ await test.run();
639
+ assert.equal(polls, 3);
640
+ assert.ok(
641
+ test.calls.indexOf("apply:second") >
642
+ test.calls.lastIndexOf("history:first"),
643
+ );
644
+ assert.ok(
645
+ !test.messages.some((message) =>
646
+ message.includes("Uncertain:"),
647
+ ),
648
+ );
649
+ }
650
+ });
651
+
652
+ it("stops at the monitoring timeout if AWS status stays unknown", async () => {
653
+ const test = harness();
654
+ test.eb.getManagedActionHistory = async () => ({ Status: "Unknown" });
655
+ await test.run({ timeout: 0.5 });
656
+ assert.ok(!test.calls.includes("apply:second"));
657
+ assert.ok(
658
+ test.messages.some((message) =>
659
+ message.includes("Monitoring timed out"),
660
+ ),
661
+ );
662
+ assert.ok(
663
+ test.messages.some((message) => message.includes("Uncertain:")),
664
+ );
665
+ assert.equal(test.process.exitCode, 1);
666
+ });
667
+
668
+ it("stops if monitoring fails", async () => {
669
+ const test = harness();
670
+ test.eb.getManagedActionHistory = async () => {
671
+ throw new Error("Access denied");
672
+ };
673
+ await test.run();
674
+ assert.ok(!test.calls.includes("apply:second"));
675
+ assert.equal(test.process.exitCode, 1);
676
+ });
677
+
678
+ it("stops after an ambiguous apply error without retrying the mutation", async () => {
679
+ const test = harness();
680
+ test.eb.applyManagedAction = async () => {
681
+ throw new Error("Connection lost");
682
+ };
683
+ await test.run();
684
+ assert.ok(!test.calls.includes("apply:second"));
685
+ assert.ok(
686
+ test.messages.some((message) =>
687
+ message.includes("AWS may still be applying"),
688
+ ),
689
+ );
690
+ });
691
+
692
+ it("skips updates that are no longer scheduled or whose environment is busy", async () => {
693
+ for (const change of ["stale", "busy", "active"]) {
694
+ const test = harness();
695
+ let lists = 0;
696
+ const list = test.eb.listAccountEnvironments;
697
+ test.eb.listAccountEnvironments = async () => {
698
+ if (++lists > 1) {
699
+ if (change === "busy")
700
+ test.environments.forEach((environment) => {
701
+ environment.Status = "Updating";
702
+ });
703
+ else
704
+ test.eb.getManagedActions = async () =>
705
+ change === "stale" ? [] : [{ Status: "Running" }];
706
+ }
707
+ return list();
708
+ };
709
+ await test.run();
710
+ assert.ok(!test.calls.some((call) => call.startsWith("apply:")));
711
+ assert.ok(
712
+ test.messages.some((message) => message.includes("Skipped:")),
713
+ );
714
+ }
715
+ });
716
+
717
+ it("does not confuse a scan error with no updates available", async () => {
718
+ const test = harness({
719
+ eb: {
720
+ listAccountEnvironments: async () => {
721
+ throw new Error("Credentials expired");
722
+ },
723
+ },
724
+ });
725
+ await test.run();
726
+ assert.equal(test.process.exitCode, 1);
727
+ assert.ok(!test.prompts.some((prompt) => prompt.name === "selected"));
728
+ });
729
+
730
+ it("interrupts monitoring without starting another update", async () => {
731
+ const test = harness();
732
+ test.eb.getManagedActionHistory = async () => {
733
+ test.process.emit("SIGINT");
734
+ return null;
735
+ };
736
+ await test.run();
737
+ assert.ok(!test.calls.includes("apply:second"));
738
+ assert.equal(test.process.exitCode, 130);
739
+ assert.equal(test.process.listenerCount("SIGINT"), 0);
740
+ });
741
+ });
742
+
743
+ describe("direct patch platform updates (offline)", () => {
744
+ it("offers the latest patch only and reports major/minor upgrades separately", async () => {
745
+ const test = patchHarness({ none: true });
746
+ await test.run();
747
+ const choices = test.prompts.find(
748
+ (prompt) => prompt.name === "selected",
749
+ ).choices;
750
+ assert.equal(choices.length, 2);
751
+ assert.ok(
752
+ choices.every(
753
+ (choice) => choice.value.platform.PlatformVersion === "4.13.15",
754
+ ),
755
+ );
756
+ assert.ok(
757
+ choices.every(
758
+ (choice) =>
759
+ choice.name.startsWith("[Direct patch]") &&
760
+ choice.checked === false,
761
+ ),
762
+ );
763
+ assert.ok(
764
+ test.messages.some((message) =>
765
+ message.includes(
766
+ "Higher-risk minor/major update: 4.13.12 -> 5.0.0",
767
+ ),
768
+ ),
769
+ );
770
+ assert.equal(
771
+ test.calls.filter((call) => call.startsWith("versions:")).length,
772
+ 1,
773
+ );
774
+ assert.ok(!test.calls.some((call) => call.startsWith("direct:")));
775
+ });
776
+
777
+ it("does not offer risky-only, prerelease, unavailable, cross-branch, or different-owner versions", async () => {
778
+ const test = patchHarness();
779
+ test.versions.splice(
780
+ 0,
781
+ test.versions.length,
782
+ test.version("4.14.0"),
783
+ test.version("5.0.0"),
784
+ test.version("4.13.16-beta.1"),
785
+ test.version("4.13.17", { PlatformStatus: "Creating" }),
786
+ test.version("4.13.18", {
787
+ PlatformBranchName: "PHP 8.3 on Amazon Linux 2023",
788
+ }),
789
+ test.version("4.13.19", { PlatformOwner: "someone-else" }),
790
+ test.version("4.13.20", {
791
+ PlatformBranchLifecycleState: "Retired",
792
+ }),
793
+ test.version("4.13.9"),
794
+ );
795
+ await test.run();
796
+ assert.ok(!test.prompts.some((prompt) => prompt.name === "selected"));
797
+ assert.ok(
798
+ test.messages.some((message) =>
799
+ message.includes("Higher-risk minor/major update"),
800
+ ),
801
+ );
802
+ });
803
+
804
+ it("prefers scheduled managed updates instead of offering duplicates", async () => {
805
+ const test = patchHarness({ none: true });
806
+ test.eb.getManagedActions = async (environmentId) => [
807
+ test.action(environmentId),
808
+ ];
809
+ await test.run();
810
+ const choices = test.prompts.find(
811
+ (prompt) => prompt.name === "selected",
812
+ ).choices;
813
+ assert.equal(choices.length, 2);
814
+ assert.ok(choices.every((choice) => !choice.value.platform));
815
+ assert.ok(
816
+ choices.every((choice) =>
817
+ choice.name.startsWith("[Managed update]"),
818
+ ),
819
+ );
820
+ assert.ok(!test.calls.some((call) => call.startsWith("versions:")));
821
+ });
822
+
823
+ it("waits for deployment completion events and the target ARN before advancing", async () => {
824
+ const test = patchHarness();
825
+ await test.run();
826
+ assert.equal(test.polls.get("first"), 3);
827
+ assert.equal(test.polls.get("second"), 3);
828
+ assert.ok(
829
+ test.calls.indexOf("direct:second") >
830
+ test.calls.indexOf("finished:first"),
831
+ );
832
+ assert.ok(!test.calls.some((call) => call.startsWith("apply:")));
833
+ assert.ok(
834
+ !test.messages.some((message) => message.includes("Uncertain:")),
835
+ );
836
+ assert.ok(
837
+ test.colors.some(
838
+ (entry) =>
839
+ entry.type === "info" &&
840
+ entry.text.includes(
841
+ "configured configuration-update policy and health settings",
842
+ ) &&
843
+ entry.text.includes("does not inspect or change"),
844
+ ),
845
+ );
846
+ assert.ok(
847
+ !test.messages.some((message) =>
848
+ message.includes("Patch-only does not mean risk-free"),
849
+ ),
850
+ );
851
+ });
852
+
853
+ it("skips a destination that changed branch after selection", async () => {
854
+ const test = patchHarness();
855
+ const describe = test.eb.describePlatformVersion;
856
+ test.eb.describePlatformVersion = async (arn) => {
857
+ const platform = await describe(arn);
858
+ return arn === test.current.PlatformArn
859
+ ? platform
860
+ : { ...platform, PlatformBranchName: "different branch" };
861
+ };
862
+ await test.run();
863
+ assert.ok(!test.calls.some((call) => call.startsWith("direct:")));
864
+ assert.ok(
865
+ test.messages.some((message) =>
866
+ message.includes(
867
+ "no longer a verified same-branch patch-only update",
868
+ ),
869
+ ),
870
+ );
871
+ });
872
+
873
+ it("skips when the source platform changed or a managed update became scheduled", async () => {
874
+ for (const change of ["source", "scheduled"]) {
875
+ const test = patchHarness();
876
+ let lists = 0;
877
+ const list = test.eb.listAccountEnvironments;
878
+ test.eb.listAccountEnvironments = async () => {
879
+ if (++lists > 1) {
880
+ if (change === "source")
881
+ test.environments.forEach((environment) => {
882
+ environment.PlatformArn = "changed";
883
+ });
884
+ else
885
+ test.eb.getManagedActions = async (environmentId) => [
886
+ test.action(environmentId),
887
+ ];
888
+ }
889
+ return list();
890
+ };
891
+ await test.run();
892
+ assert.ok(!test.calls.some((call) => call.startsWith("direct:")));
893
+ }
894
+ });
895
+
896
+ it("reports rollbacks as failures and continues to the next environment", async () => {
897
+ const test = patchHarness();
898
+ test.eb.describeEnvironment = async (name) => ({
899
+ Environments: [
900
+ {
901
+ ...test.environments.find(
902
+ (environment) => environment.EnvironmentName === name,
903
+ ),
904
+ DateUpdated: new Date(1),
905
+ },
906
+ ],
907
+ });
908
+ test.eb.getEnvironmentUpdateEvents = async () => [
909
+ {
910
+ Message: "Environment update completed successfully.",
911
+ Severity: "INFO",
912
+ },
913
+ ];
914
+ await test.run();
915
+ assert.ok(test.calls.includes("direct:second"));
916
+ assert.ok(
917
+ test.messages.some((message) =>
918
+ message.includes("possibly rolled back"),
919
+ ),
920
+ );
921
+ assert.equal(test.process.exitCode, 1);
922
+ });
923
+
924
+ it("does not report success from Ready and target ARN alone", async () => {
925
+ const test = patchHarness();
926
+ test.eb.describeEnvironment = async (name) => ({
927
+ Environments: [
928
+ {
929
+ ...test.environments.find(
930
+ (environment) => environment.EnvironmentName === name,
931
+ ),
932
+ PlatformArn: "platform-4.13.15",
933
+ },
934
+ ],
935
+ });
936
+ test.eb.getEnvironmentUpdateEvents = async () => [];
937
+ await test.run({ timeout: 0.5 });
938
+ assert.ok(!test.calls.includes("direct:second"));
939
+ assert.ok(
940
+ test.messages.some((message) => message.includes("Uncertain:")),
941
+ );
942
+ assert.ok(
943
+ test.messages.some((message) =>
944
+ message.includes(
945
+ "direct platform update outcome is unconfirmed",
946
+ ),
947
+ ),
948
+ );
949
+ });
950
+
951
+ it("does not advance on a stale Ready response with nonterminal error events", async () => {
952
+ const test = patchHarness();
953
+ test.eb.describeEnvironment = async (name) => ({
954
+ Environments: [
955
+ {
956
+ ...test.environments.find(
957
+ (environment) => environment.EnvironmentName === name,
958
+ ),
959
+ DateUpdated: new Date(0),
960
+ },
961
+ ],
962
+ });
963
+ test.eb.getEnvironmentUpdateEvents = async () => [
964
+ { Message: "Instance health check failed", Severity: "ERROR" },
965
+ ];
966
+ await test.run({ timeout: 0.5 });
967
+ assert.ok(!test.calls.includes("direct:second"));
968
+ assert.ok(
969
+ test.messages.some((message) => message.includes("Uncertain:")),
970
+ );
971
+ });
972
+
973
+ it("reports settled deployment errors as confirmed failures", async () => {
974
+ const test = patchHarness();
975
+ test.eb.describeEnvironment = async (name) => ({
976
+ Environments: [
977
+ {
978
+ ...test.environments.find(
979
+ (environment) => environment.EnvironmentName === name,
980
+ ),
981
+ DateUpdated: new Date(1),
982
+ },
983
+ ],
984
+ });
985
+ test.eb.getEnvironmentUpdateEvents = async () => [
986
+ { Message: "Failed to deploy configuration", Severity: "ERROR" },
987
+ ];
988
+ await test.run();
989
+ assert.ok(test.calls.includes("direct:second"));
990
+ assert.ok(
991
+ test.messages.some((message) =>
992
+ message.includes("Failed to deploy configuration"),
993
+ ),
994
+ );
995
+ assert.equal(test.process.exitCode, 1);
996
+ });
997
+
998
+ it("stops on an ambiguous direct update error without retrying or advancing", async () => {
999
+ const test = patchHarness();
1000
+ let attempts = 0;
1001
+ test.eb.updateEnvironmentPlatform = async () => {
1002
+ attempts++;
1003
+ throw new Error("Connection lost");
1004
+ };
1005
+ await test.run();
1006
+ assert.equal(attempts, 1);
1007
+ assert.ok(!test.calls.includes("direct:second"));
1008
+ assert.ok(
1009
+ test.messages.some((message) =>
1010
+ message.includes("AWS may still be applying platform"),
1011
+ ),
1012
+ );
1013
+ });
1014
+
1015
+ it("reports version discovery permission errors without inventing a patch target", async () => {
1016
+ const test = patchHarness();
1017
+ test.eb.listBranchPlatformVersions = async () => {
1018
+ throw new Error("Access denied");
1019
+ };
1020
+ await test.run();
1021
+ assert.ok(!test.prompts.some((prompt) => prompt.name === "selected"));
1022
+ assert.ok(
1023
+ test.messages.some((message) =>
1024
+ message.includes(
1025
+ "Available version lookup failed: Access denied",
1026
+ ),
1027
+ ),
1028
+ );
1029
+ assert.equal(test.process.exitCode, 1);
1030
+ });
1031
+ });
1032
+
1033
+ describe("managed action AWS helpers (offline)", () => {
1034
+ function helpers(respond, configure = () => {}) {
1035
+ class Command {
1036
+ constructor(input) {
1037
+ this.input = input;
1038
+ }
1039
+ }
1040
+ const sdk = {
1041
+ ElasticBeanstalkClient: class {
1042
+ constructor(config) {
1043
+ configure(config);
1044
+ }
1045
+ async send(command) {
1046
+ return respond(command.input);
1047
+ }
1048
+ },
1049
+ DescribeEnvironmentsCommand: Command,
1050
+ DescribeEnvironmentManagedActionsCommand: Command,
1051
+ DescribeEnvironmentManagedActionHistoryCommand: Command,
1052
+ ApplyEnvironmentManagedActionCommand: Command,
1053
+ DescribePlatformVersionCommand: Command,
1054
+ ListPlatformVersionsCommand: Command,
1055
+ UpdateEnvironmentCommand: Command,
1056
+ DescribeEventsCommand: Command,
1057
+ };
1058
+ return load(helperPath, {
1059
+ "fs-extra": {},
1060
+ crypto: {},
1061
+ glob: {},
1062
+ inquirer: {},
1063
+ "@aws-sdk/client-elastic-beanstalk": sdk,
1064
+ "../../libs/utilities": {},
1065
+ "../../libs/vars": { awsClientConfig: {} },
1066
+ lodash: {},
1067
+ });
1068
+ }
1069
+
1070
+ it("paginates environment discovery and excludes terminating environments", async () => {
1071
+ const eb = helpers(async (input) =>
1072
+ input.NextToken
1073
+ ? {
1074
+ Environments: [
1075
+ { EnvironmentId: "second", Status: "Ready" },
1076
+ { Status: "Terminating" },
1077
+ ],
1078
+ }
1079
+ : {
1080
+ Environments: [
1081
+ { EnvironmentId: "first", Status: "Ready" },
1082
+ ],
1083
+ NextToken: "page2",
1084
+ },
1085
+ );
1086
+ const environments = await eb.listAccountEnvironments();
1087
+ assert.deepEqual(
1088
+ Array.from(
1089
+ environments,
1090
+ (environment) => environment.EnvironmentId,
1091
+ ),
1092
+ ["first", "second"],
1093
+ );
1094
+ });
1095
+
1096
+ it("finds the selected action across history pages", async () => {
1097
+ const eb = helpers(async (input) =>
1098
+ input.NextToken
1099
+ ? {
1100
+ ManagedActionHistoryItems: [
1101
+ { ActionId: "selected", Status: "Completed" },
1102
+ ],
1103
+ }
1104
+ : {
1105
+ ManagedActionHistoryItems: [
1106
+ { ActionId: "other", Status: "Completed" },
1107
+ ],
1108
+ NextToken: "page2",
1109
+ },
1110
+ );
1111
+ assert.equal(
1112
+ (await eb.getManagedActionHistory("env", "selected")).Status,
1113
+ "Completed",
1114
+ );
1115
+ assert.equal(await eb.getManagedActionHistory("env", "absent"), null);
1116
+ });
1117
+
1118
+ it("passes the selected environment and action IDs to apply", async () => {
1119
+ const eb = helpers(
1120
+ async (input) => {
1121
+ assert.equal(input.EnvironmentId, "environment");
1122
+ assert.equal(input.ActionId, "action");
1123
+ return { Status: "Pending" };
1124
+ },
1125
+ (config) => assert.equal(config.maxAttempts, 1),
1126
+ );
1127
+ assert.equal(
1128
+ (await eb.applyManagedAction("environment", "action")).Status,
1129
+ "Pending",
1130
+ );
1131
+ });
1132
+
1133
+ it("retrieves lifecycle metadata for the exact platform ARN", async () => {
1134
+ const eb = helpers(async (input) => {
1135
+ assert.equal(input.PlatformArn, "platform-arn");
1136
+ return {
1137
+ PlatformDescription: {
1138
+ PlatformBranchLifecycleState: "Retired",
1139
+ },
1140
+ };
1141
+ });
1142
+ assert.equal(
1143
+ (await eb.describePlatformVersion("platform-arn"))
1144
+ .PlatformBranchLifecycleState,
1145
+ "Retired",
1146
+ );
1147
+ });
1148
+
1149
+ it("paginates Ready versions in the exact platform branch", async () => {
1150
+ const eb = helpers(async (input) => {
1151
+ assert.equal(
1152
+ input.Filters[0].Values[0],
1153
+ "PHP 8.2 running on 64bit Amazon Linux 2023",
1154
+ );
1155
+ assert.equal(input.Filters[1].Values[0], "Ready");
1156
+ return input.NextToken
1157
+ ? { PlatformSummaryList: [{ PlatformVersion: "4.13.15" }] }
1158
+ : {
1159
+ PlatformSummaryList: [{ PlatformVersion: "4.13.12" }],
1160
+ NextToken: "page2",
1161
+ };
1162
+ });
1163
+ assert.equal(
1164
+ (
1165
+ await eb.listBranchPlatformVersions(
1166
+ "PHP 8.2 running on 64bit Amazon Linux 2023",
1167
+ )
1168
+ ).length,
1169
+ 2,
1170
+ );
1171
+ });
1172
+
1173
+ it("updates only the selected platform without SDK mutation retries", async () => {
1174
+ const eb = helpers(
1175
+ async (input) => {
1176
+ assert.equal(input.EnvironmentId, "environment");
1177
+ assert.equal(input.PlatformArn, "platform-arn");
1178
+ assert.equal(Object.keys(input).length, 2);
1179
+ return { Status: "Updating" };
1180
+ },
1181
+ (config) => assert.equal(config.maxAttempts, 1),
1182
+ );
1183
+ assert.equal(
1184
+ (await eb.updateEnvironmentPlatform("environment", "platform-arn"))
1185
+ .Status,
1186
+ "Updating",
1187
+ );
1188
+ });
1189
+
1190
+ it("paginates environment events from the update start time", async () => {
1191
+ const started = new Date();
1192
+ const eb = helpers(async (input) => {
1193
+ assert.equal(input.EnvironmentId, "environment");
1194
+ assert.equal(input.StartTime, started);
1195
+ return input.NextToken
1196
+ ? { Events: [{ Message: "second" }] }
1197
+ : { Events: [{ Message: "first" }], NextToken: "page2" };
1198
+ });
1199
+ assert.equal(
1200
+ (await eb.getEnvironmentUpdateEvents("environment", started))
1201
+ .length,
1202
+ 2,
1203
+ );
1204
+ });
1205
+ });