Repository navigation
Expand file tree
/
Copy pathe2eResourceEnv.test.js
More file actions
915 lines (870 loc) · 30.4 KB
/
Copy pathe2eResourceEnv.test.js
File metadata and controls
915 lines (870 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
const { execFileSync } = require('child_process');
const path = require('path');
const { createHermeticHost } = require('./e2eHermeticHost');
jest.setTimeout(30000);
const repoRoot = path.resolve(__dirname, '../../..');
const lib = path.join(repoRoot, 'scripts/e2e/lib/e2e-resource-env.sh');
const host = createHermeticHost('rnfb-resource-env-');
afterAll(function () {
host.cleanup();
});
function cleanEnv() {
const env = host.env();
for (const key of Object.keys(env)) {
if (
key.startsWith('RNFB_') ||
key.startsWith('JET_') ||
key === 'RCT_METRO_PORT' ||
key === 'ANDROID_SERIAL' ||
key === 'AVD_NAME' ||
key === 'E2E_PLATFORM_OVERRIDE' ||
key === 'E2E_MELLIFERA_FLAG' ||
key === 'E2E_SLOTTED_MAX' ||
key === 'E2E_MACOS_SLOTTED_MAX' ||
key === 'E2E_ALL_SLOTS' ||
key === 'E2E_SLOT_OVERRIDE'
) {
delete env[key];
}
}
env.E2E_SLOTTED_MAX = '2';
env.RNFB_E2E_RUN_RECORD_DIR = host.runRecordDir;
return env;
}
function bashSnippet(body, extra = {}) {
return execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
${body}`,
],
{ encoding: 'utf8', cwd: repoRoot, env: cleanEnv(), ...extra },
);
}
describe('temp root (${TMPDIR:-/tmp} without a trailing slash)', function () {
const cases = [
['/', '/tmp'],
['', '/tmp'],
[undefined, '/tmp'],
['/scratch/run/', '/scratch/run'],
['/scratch/run', '/scratch/run'],
];
function envWithTmpdir(tmpdir) {
const env = cleanEnv();
if (tmpdir === undefined) {
delete env.TMPDIR;
} else {
env.TMPDIR = tmpdir;
}
return env;
}
it.each(cases)('e2e_tmp_root maps TMPDIR=%j to %s', function (tmpdir, expected) {
expect(bashSnippet('e2e_tmp_root', { env: envWithTmpdir(tmpdir) }).trim()).toBe(expected);
});
const inlineScripts = [
'.github/workflows/scripts/test-expo-ios-launch-smoke.sh',
'.github/workflows/scripts/test-expo-ios-link.sh',
'.github/workflows/scripts/test-rn-bare-ios-build.sh',
'.github/workflows/scripts/test-rn-bare-ios-launch-smoke.sh',
'scripts/repro-android-build-flake.sh',
'scripts/tart/prewarm-caches.sh',
'scripts/tart/provision-toolchain.sh',
];
it.each(inlineScripts)('%s resolves its temp root the same way', function (script) {
const fs = require('fs');
const src = fs.readFileSync(path.join(repoRoot, script), 'utf8');
const lines = src.split('\n').filter(l => l.startsWith('TMP_ROOT='));
expect(lines).toHaveLength(3);
for (const [tmpdir, expected] of cases) {
const out = execFileSync('bash', ['-c', `${lines.join('\n')}\nprintf '%s' "$TMP_ROOT"`], {
encoding: 'utf8',
env: envWithTmpdir(tmpdir),
});
expect(out).toBe(expected);
}
});
it('scripts that source the e2e lib use e2e_tmp_root', function () {
const fs = require('fs');
for (const script of [
'scripts/run-full-tests.sh',
'scripts/e2e/release-e2e-resources.sh',
'scripts/e2e/start-packager.sh',
'.github/workflows/scripts/start-firebase-emulator.sh',
]) {
const code = fs
.readFileSync(path.join(repoRoot, script), 'utf8')
.split('\n')
.filter(l => !l.trimStart().startsWith('#'))
.join('\n');
expect(code).toContain('$(e2e_tmp_root)');
expect(code).not.toMatch(/TMPDIR:-\/tmp/);
}
});
});
describe('e2e_validate_platform_name', function () {
it('accepts android, ios, and macos', function () {
expect(bashSnippet('e2e_validate_platform_name android && echo OK_ANDROID')).toMatch(
/OK_ANDROID/,
);
expect(bashSnippet('e2e_validate_platform_name ios && echo OK_IOS')).toMatch(/OK_IOS/);
expect(bashSnippet('e2e_validate_platform_name macos && echo OK_MACOS')).toMatch(/OK_MACOS/);
});
it('rejects unknown platform with exit 2', function () {
let status = 0;
let err = '';
try {
bashSnippet('e2e_validate_platform_name foo');
} catch (e) {
status = e.status;
err = `${e.stderr || ''}${e.stdout || ''}${e.message || ''}`;
}
expect(status).toBe(2);
expect(err).toMatch(/platform must be android\|ios\|macos \(got foo\)/);
});
});
describe('check-e2e-resources --platform validation', function () {
it('rejects invalid --platform=foo with exit 2', function () {
const checkPath = path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh');
let status = 0;
let err = '';
try {
execFileSync('bash', [checkPath, '--platform=foo'], {
encoding: 'utf8',
cwd: repoRoot,
env: cleanEnv(),
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
status = e.status;
err = `${e.stderr || ''}${e.stdout || ''}${e.message || ''}`;
}
expect(status).toBe(2);
expect(err).toMatch(/platform must be android\|ios\|macos \(got foo\)/);
});
});
describe('e2e_slot_env_apply platform validation', function () {
it('rejects unknown platform with exit 1', function () {
const slotLib = path.join(repoRoot, 'scripts/e2e/lib/e2e-slot-env.sh');
let status = 0;
let err = '';
try {
execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(slotLib)}
e2e_slot_env_apply foo 0
`,
],
{ encoding: 'utf8', cwd: repoRoot, env: cleanEnv(), stdio: ['pipe', 'pipe', 'pipe'] },
);
} catch (e) {
status = e.status;
err = `${e.stderr || ''}${e.stdout || ''}${e.message || ''}`;
}
expect(status).toBe(1);
expect(err).toMatch(/platform must be android\|ios\|macos \(got foo\)/);
});
});
describe('E2E_SLOTTED_MAX default', function () {
it('defaults to 7 aligned with Detox slots 0-7', function () {
const slotLib = path.join(repoRoot, 'scripts/e2e/lib/e2e-slot-env.sh');
const env = { ...process.env };
delete env.E2E_SLOTTED_MAX;
delete env.E2E_MACOS_SLOTTED_MAX;
const out = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(slotLib)}
echo "$E2E_SLOTTED_MAX"
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(out.trim()).toBe('7');
});
});
describe('unscoped e2e_collect_targets leftover slots', function () {
it('serial default does not include leftover slotted hubs', function () {
const out = bashSnippet(`
e2e_collect_targets
e2e_print_collected_ports
`);
expect(out).not.toMatch(/emulator-hub:android-slot1/);
expect(out).not.toMatch(/metro:ios-slot2/);
expect(out).toMatch(/^4400 emulator-hub:global$/m);
});
it('includes android slot 1 hub 13005 and ios slot 2 metro 14107 with --all-slots', function () {
const env = cleanEnv();
env.E2E_ALL_SLOTS = '1';
const out = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_collect_targets
e2e_print_collected_ports
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(out).toMatch(/^13005 emulator-hub:android-slot1$/m);
expect(out).toMatch(/^14107 metro:ios-slot2$/m);
expect(out).toMatch(/^4400 emulator-hub:global$/m);
});
it('sweeps slot 7 and excludes slot 8 at the shared default ceiling', function () {
const env = cleanEnv();
env.E2E_ALL_SLOTS = '1';
env.E2E_SLOTTED_MAX = '7';
const out = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_collect_targets
e2e_print_collected_ports
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(out).toMatch(/^19005 emulator-hub:android-slot7$/m);
expect(out).toMatch(/^19107 metro:ios-slot7$/m);
expect(out).not.toMatch(/android-slot8|ios-slot8|macos-slot8/);
});
it('caps a configured max of 99 at slot 7 for every all-slot port sweep', function () {
const env = cleanEnv();
env.E2E_ALL_SLOTS = '1';
env.E2E_SLOTTED_MAX = '99';
const out = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_collect_targets
e2e_print_collected_ports
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(out).toMatch(/^19005 emulator-hub:android-slot7$/m);
expect(out).toMatch(/^19107 metro:ios-slot7$/m);
expect(out).not.toMatch(/android-slot8|ios-slot8|macos-slot8/);
});
it('honors a lower max for every all-slot port sweep', function () {
const env = cleanEnv();
env.E2E_ALL_SLOTS = '1';
env.E2E_SLOTTED_MAX = '2';
const out = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_collect_targets
e2e_print_collected_ports
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(out).toMatch(/android-slot2|ios-slot2|macos-slot2/);
expect(out).not.toMatch(/android-slot3|ios-slot3|macos-slot3/);
});
it('does not include leftover slotted hubs when slot env is loaded', function () {
const env = cleanEnv();
env.RNFB_E2E_SLOT = '1';
env.RNFB_E2E_HOST_SLOT = '1';
const out = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_collect_targets
e2e_print_collected_ports
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(out).not.toMatch(/emulator-hub:android-slot1/);
expect(out).toMatch(/^4400 emulator-hub:global$/m);
});
});
describe('e2e_qemu_avd_pgrep_pattern', function () {
it('does not match @TestingAVD-0 when looking for TestingAVD', function () {
const out = bashSnippet(`
pat=$(e2e_qemu_avd_pgrep_pattern TestingAVD)
serial='/sdk/qemu-system-aarch64 -avd TestingAVD @TestingAVD'
slot0='/sdk/qemu-system-aarch64 -avd TestingAVD-0 @TestingAVD-0'
[[ "$serial" =~ $pat ]] && echo MATCH_SERIAL || echo MISS_SERIAL
[[ "$slot0" =~ $pat ]] && echo MATCH_SLOT0 || echo MISS_SLOT0
echo "$pat"
`);
expect(out).toMatch(/MATCH_SERIAL/);
expect(out).toMatch(/MISS_SLOT0/);
expect(out).toContain('qemu-system.*@TestingAVD([[:space:]]|$)');
});
});
describe('unscoped device names', function () {
it('lists only serial TestingAVD / iPhone 17 by default', function () {
const avds = bashSnippet('e2e_android_avd_names_for_release');
expect(avds).toMatch(/^TestingAVD$/m);
expect(avds).not.toMatch(/^TestingAVD-0$/m);
const sims = bashSnippet('e2e_ios_simulator_names_for_release');
expect(sims).toMatch(/^iPhone 17$/m);
expect(sims).not.toMatch(/^RN E2E iOS slot-0$/m);
});
it('lists serial plus every exact neutral base through MAX with --all-slots', function () {
const env = cleanEnv();
env.E2E_ALL_SLOTS = '1';
env.E2E_SLOTTED_MAX = '7';
const avds = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_android_avd_names_for_release
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(avds).toMatch(/^TestingAVD$/m);
expect(avds).toMatch(/^TestingAVD-0$/m);
expect(avds).toMatch(/^TestingAVD-7$/m);
expect(avds).not.toMatch(/^TestingAVD-8$/m);
const sims = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_ios_simulator_names_for_release
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(sims).toMatch(/^iPhone 17$/m);
expect(sims).toMatch(/^RN E2E iOS slot-0$/m);
expect(sims).toMatch(/^RN E2E iOS slot-7$/m);
expect(sims).not.toMatch(/^RN E2E iOS slot-8$/m);
expect(sims).not.toMatch(/-Detox$/m);
expect(sims).not.toMatch(/^RNFB E2E iOS slot-/m);
});
it('caps configured 99 and honors lower max across Android, iOS, and macOS names', function () {
const renderNames = max => {
const env = cleanEnv();
env.E2E_ALL_SLOTS = '1';
env.E2E_SLOTTED_MAX = max;
return execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_android_avd_names_for_release
e2e_ios_simulator_names_for_release
e2e_macos_process_names_for_probe
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
};
const capped = renderNames('99');
expect(capped).toMatch(/^TestingAVD-7$/m);
expect(capped).toMatch(/^RN E2E iOS slot-7$/m);
expect(capped).toMatch(/^io\.invertase\.testing\.s7$/m);
expect(capped).not.toMatch(/TestingAVD-8|slot-8|\.s8$/m);
const lowered = renderNames('2');
expect(lowered).toMatch(/^TestingAVD-2$/m);
expect(lowered).toMatch(/^RN E2E iOS slot-2$/m);
expect(lowered).toMatch(/^io\.invertase\.testing\.s2$/m);
expect(lowered).not.toMatch(/TestingAVD-3|slot-3|\.s3$/m);
});
it('targets only the selected exact neutral base', function () {
const env = cleanEnv();
env.E2E_SLOT_OVERRIDE = '1';
const sims = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_ios_simulator_names_for_release
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(sims.trim().split('\n')).toEqual(['RN E2E iOS slot-1']);
});
});
describe('exact iOS simulator boot checks', function () {
it('distinguishes a neutral base from suffixed and substring-collision names', function () {
const out = bashSnippet(`
xcrun() {
cat <<'JSON'
{"devices":{"runtime":[
{"name":"RN E2E iOS slot-1-other","state":"Booted"},
{"name":"RN E2E iOS slot-10","state":"Booted"}
]}}
JSON
}
e2e_ios_sim_booted 'RN E2E iOS slot-1' && echo BASE_BUSY || echo BASE_CLEAR
`);
expect(out).toMatch(/^BASE_CLEAR$/m);
expect(out).not.toMatch(/^BASE_BUSY$/m);
});
});
describe('e2e_lsof_cache', function () {
it('e2e_port_listening and e2e_listener_pids read one lsof snapshot', function () {
const out = bashSnippet(`
E2E_LSOF_CACHE_LOADED=1
E2E_LSOF_CACHE_LINES='node 123 user 23u IPv4 0x0 0t0 TCP *:8081 (LISTEN)
java 456 user 44u IPv4 0x0 0t0 TCP 127.0.0.1:13005 (LISTEN)'
e2e_port_listening 8081 && echo YES8081
e2e_port_listening 8082 && echo YES8082 || echo NO8082
e2e_port_listening 13005 && echo YES13005
e2e_port_listening 130050 && echo BAD130050 || echo NO130050
e2e_listener_pids 8081
e2e_listener_pids 13005
`);
expect(out).toMatch(/YES8081/);
expect(out).toMatch(/NO8082/);
expect(out).toMatch(/YES13005/);
expect(out).toMatch(/NO130050/);
expect(out).toMatch(/^123$/m);
expect(out).toMatch(/^456$/m);
});
});
describe('e2e_sibling_metro_owns_root', function () {
it('e2e_cmdline_is_metro_packager accepts RN/metro and rejects qemu', function () {
const out = bashSnippet(`
e2e_cmdline_is_metro_packager 'node /repo/node_modules/react-native/cli.js start' && echo RN
e2e_cmdline_is_metro_packager 'yarn react-native start --port 12107' && echo YARN
e2e_cmdline_is_metro_packager 'node /repo/node_modules/metro/src/cli.js' && echo METRO
e2e_cmdline_is_metro_packager 'qemu-system-x86_64 -netdev user' && echo QEMU || echo NO_QEMU
e2e_cmdline_is_metro_packager 'netsimd --port 5554' && echo NET || echo NO_NET
`);
expect(out).toMatch(/^RN$/m);
expect(out).toMatch(/^YARN$/m);
expect(out).toMatch(/^METRO$/m);
expect(out).toMatch(/^NO_QEMU$/m);
expect(out).toMatch(/^NO_NET$/m);
});
it('returns true for sibling node metro cwd match (LISTEN not required)', function () {
const out = bashSnippet(`
e2e_listener_pids() { echo 100; }
e2e_metro_packager_candidate_pids() { echo 100; echo 200; }
e2e_process_cmdline() {
case "$1" in
100) echo 'node /x/react-native/cli.js start --port 8081' ;;
200) echo 'node /x/react-native/cli.js start --port 12107' ;;
esac
}
e2e_process_cwd() {
case "$1" in
100|200) echo /repo/tests ;;
esac
}
e2e_sibling_metro_owns_root /repo/tests 8081 && echo SIBLING || echo ALONE
`);
expect(out.trim()).toBe('SIBLING');
});
it('excludes our_port listeners even when cwd and cmdline match', function () {
const out = bashSnippet(`
e2e_listener_pids() { echo 100; }
e2e_metro_packager_candidate_pids() { echo 100; }
e2e_process_cmdline() { echo 'node /x/react-native/cli.js start'; }
e2e_process_cwd() { echo /repo/tests; }
e2e_sibling_metro_owns_root /repo/tests 8081 && echo SIBLING || echo ALONE
`);
expect(out.trim()).toBe('ALONE');
});
it('rejects qemu-like LISTEN cwd match without metro cmdline', function () {
const out = bashSnippet(`
e2e_listener_pids() { true; }
e2e_metro_packager_candidate_pids() { echo 999; }
e2e_process_cmdline() { echo 'qemu-system-x86_64 -cwd /repo/tests'; }
e2e_process_cwd() { echo /repo/tests; }
e2e_sibling_metro_owns_root /repo/tests 8081 && echo SIBLING || echo ALONE
`);
expect(out.trim()).toBe('ALONE');
});
it('returns false when alone (no candidates)', function () {
const out = bashSnippet(`
e2e_listener_pids() { true; }
e2e_metro_packager_candidate_pids() { true; }
e2e_sibling_metro_owns_root /repo/tests 8081 && echo SIBLING || echo ALONE
`);
expect(out.trim()).toBe('ALONE');
});
});
describe('e2e_abort_if_emulator_suite_ports_busy', function () {
it('exits 1 and names the busy hub when a mock listener is bound', function () {
let err = '';
try {
bashSnippet(
`
e2e_port_listening() { [[ "$1" == "13005" ]]; }
e2e_listener_pids() { echo 4242; }
e2e_abort_if_emulator_suite_ports_busy 13000 13001 13002 13003 13004 13005 13006
`,
{ stdio: ['pipe', 'pipe', 'pipe'] },
);
} catch (e) {
err = `${e.stderr || ''}${e.stdout || ''}${e.message || ''}`;
}
expect(err).toMatch(/hub :13005/);
expect(err).toMatch(/pids=4242/);
expect(err).toMatch(/zero flake budget/);
});
it('returns success when no suite ports are listening', function () {
const out = bashSnippet(`
e2e_port_listening() { return 1; }
e2e_abort_if_emulator_suite_ports_busy 13000 13001 13002 13003 13004 13005 13006
echo FREE
`);
expect(out.trim()).toBe('FREE');
});
});
describe('e2e_collect_targets --slot=N', function () {
it('--slot=1 without env collects slot-1 formula hub 13005 not leftover slot-2 metro', function () {
const env = cleanEnv();
env.E2E_SLOT_OVERRIDE = '1';
const out = execFileSync(
'bash',
[
'-c',
`set -euo pipefail
source ${JSON.stringify(lib)}
e2e_collect_targets
e2e_print_collected_ports
`,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
expect(out).toMatch(/^13005 emulator-hub:android-slot1$/m);
expect(out).not.toMatch(/metro:ios-slot2/);
expect(out).not.toMatch(/^4400 emulator-hub:global$/m);
});
it('canonical check and release reject invalid selectors before output or actions', function () {
const fs = require('fs');
const os = require('os');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-slot-selector-'));
const actionLog = path.join(dir, 'actions.log');
for (const command of ['xcrun', 'lsof', 'adb', 'pgrep', 'pkill', 'killall', 'sleep']) {
fs.writeFileSync(
path.join(dir, command),
`#!/bin/bash
printf '%s %s\\n' ${JSON.stringify(command)} "$*" >> "$RNFB_ACTION_LOG"
exit 0
`,
);
fs.chmodSync(path.join(dir, command), 0o755);
}
const env = cleanEnv();
env.E2E_SLOTTED_MAX = '99';
env.PATH = `${dir}:${env.PATH || '/usr/bin:/bin'}`;
env.RNFB_ACTION_LOG = actionLog;
const scripts = ['check-e2e-resources.sh', 'release-e2e-resources.sh'];
try {
for (const script of scripts) {
for (const { max, slot, expectedMax } of [
{ max: '99', slot: '8', expectedMax: '7' },
{ max: '99', slot: '99', expectedMax: '7' },
{ max: '99', slot: '-1', expectedMax: '7' },
{ max: '99', slot: '1.5', expectedMax: '7' },
{ max: '99', slot: 'foo', expectedMax: '7' },
{ max: '99', slot: '07', expectedMax: '7' },
{ max: '99', slot: '', expectedMax: '7' },
{ max: '2', slot: '3', expectedMax: '2' },
]) {
env.E2E_SLOTTED_MAX = max;
let result;
try {
result = {
status: 0,
stdout: execFileSync(
'/bin/bash',
[path.join(repoRoot, 'scripts/e2e', script), `--slot=${slot}`],
{
encoding: 'utf8',
cwd: repoRoot,
env,
stdio: ['pipe', 'pipe', 'pipe'],
},
),
stderr: '',
};
} catch (error) {
result = {
status: error.status,
stdout: error.stdout || '',
stderr: error.stderr || '',
};
}
expect(result.status).toBe(1);
expect(result.stdout).toBe('');
expect(result.stderr).toContain(`slot must be an integer 0..${expectedMax}`);
}
}
expect(fs.existsSync(actionLog)).toBe(false);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
});
describe('adb stray emulator serials (UNI-adb-leftover)', function () {
it('treats emulator-16222 as stray and keeps 5554/5556 allocated', function () {
const out = bashSnippet(`
e2e_adb_emulator_serials() { printf '%s\\n' emulator-16222 emulator-5554 emulator-5556; }
e2e_android_console_port_allocated 5554 && echo ALLOC_5554
e2e_android_console_port_allocated 5556 && echo ALLOC_5556
e2e_android_console_port_allocated 16222 && echo ALLOC_16222 || echo STRAY_PORT_16222
e2e_adb_stray_emulator_serials
`);
expect(out).toMatch(/ALLOC_5554/);
expect(out).toMatch(/ALLOC_5556/);
expect(out).toMatch(/STRAY_PORT_16222/);
expect(out).toMatch(/^emulator-16222$/m);
expect(out).not.toMatch(/^emulator-5554$/m);
expect(out).not.toMatch(/^emulator-5556$/m);
});
it('check: a FreePortFinder stray is BUSY only when this checkout recorded it; another owner’s is INFO', async function () {
const fs = require('fs');
const os = require('os');
const { spawn } = require('child_process');
const { processStartTime } = require(path.join(repoRoot, 'tests/e2e/runRecord'));
const checkPath = path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-fake-adb-'));
fs.writeFileSync(
path.join(dir, 'adb'),
`#!/bin/bash
if [[ "$1" == "devices" ]]; then
printf '%s\\n' 'List of devices attached' 'emulator-16222\tdevice' 'emulator-5554\tdevice'
exit 0
fi
exit 1
`,
);
fs.writeFileSync(path.join(dir, 'lsof'), '#!/bin/bash\nexit 1\n');
fs.chmodSync(path.join(dir, 'adb'), 0o755);
fs.chmodSync(path.join(dir, 'lsof'), 0o755);
// Stands in for the stray's qemu instance.
const qemu = spawn(
process.execPath,
['-e', 'setTimeout(() => {}, 60000)', 'qemu-system-fake', '@T66StrayAvd', '-port', '16222'],
{ stdio: 'ignore', cwd: os.tmpdir() },
);
const qemuExited = new Promise(resolve => qemu.once('exit', resolve));
const env = cleanEnv();
env.PATH = `${dir}:${env.PATH || '/usr/bin:/bin'}`;
env.RNFB_E2E_RUN_RECORD_DIR = path.join(dir, 'rec');
const check = () => {
try {
return {
status: 0,
out: execFileSync('bash', [checkPath, '--platform=android'], {
encoding: 'utf8',
cwd: repoRoot,
env,
stdio: ['pipe', 'pipe', 'pipe'],
}),
};
} catch (e) {
return { status: e.status, out: `${e.stdout || ''}${e.stderr || ''}` };
}
};
try {
const foreign = check();
expect(foreign.out).toMatch(
new RegExp(
`INFO\\s+android stray emulator serial emulator-16222 owner=unrecorded qemu pid=${qemu.pid} .*only the manual yarn tests:e2e:release --all-slots`,
),
);
expect(foreign.out).not.toMatch(/BUSY\s+android stray/);
expect(foreign.out).not.toMatch(/stray emulator serial emulator-5554/);
expect(foreign.status).toBe(0);
fs.mkdirSync(path.join(dir, 'rec', 'serial'), { recursive: true });
fs.writeFileSync(
path.join(dir, 'rec', 'serial', 'android-emulator-16222'),
`android\temulator-16222\tT66StrayAvd\t${qemu.pid}\t${processStartTime(qemu.pid)}\n`,
);
const recorded = check();
expect(recorded.status).toBe(1);
expect(recorded.out).toMatch(
/BUSY\s+android stray emulator serial emulator-16222 .*owner=this-checkout — free it with yarn tests:e2e:release --devices/,
);
} finally {
qemu.kill('SIGKILL');
await qemuExited;
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('check: identity-AVD this-checkout-unrecorded FreePortFinder is BUSY; other-identity stays INFO; recorded child is BUSY', async function () {
const fs = require('fs');
const os = require('os');
const { spawn } = require('child_process');
const { recordPid } = require(path.join(repoRoot, 'tests/e2e/runRecord'));
const checkPath = path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-fake-adb-own-'));
const realLsof = execFileSync('/bin/sh', ['-c', 'command -v lsof'], {
encoding: 'utf8',
}).trim();
fs.writeFileSync(
path.join(dir, 'adb'),
`#!/bin/bash
if [[ "$1" == "devices" ]]; then
printf '%s\\n' 'List of devices attached' 'emulator-16232\tdevice' 'emulator-16234\tdevice' 'emulator-16236\tdevice'
exit 0
fi
exit 1
`,
);
fs.writeFileSync(
path.join(dir, 'lsof'),
`#!/bin/bash\n[[ "$1" == "-a" ]] && exec ${realLsof} "$@"\nexit 1\n`,
);
fs.chmodSync(path.join(dir, 'adb'), 0o755);
fs.chmodSync(path.join(dir, 'lsof'), 0o755);
const token = (cwd, avd, port) =>
spawn(
process.execPath,
['-e', 'setTimeout(() => {}, 60000)', 'qemu-system-fake', `@${avd}`, '-port', port],
{ stdio: 'ignore', cwd },
);
// Serial identity's AVD and another identity's, both run from this checkout.
const ownAvd = token(repoRoot, 'TestingAVD', '16232');
const otherAvd = token(repoRoot, 'TestingAVD-3', '16234');
// A recorded parent (outside this checkout) whose child qemu is the third stray.
const parent = spawn(
process.execPath,
[
'-e',
`require('child_process').spawn(process.execPath, ${JSON.stringify([
'-e',
'setTimeout(() => {}, 60000)',
'qemu-system-fake',
'@TestingAVD-5',
'-port',
'16236',
])}, { stdio: 'ignore' }); setTimeout(() => {}, 60000);`,
],
{ stdio: 'ignore', cwd: os.tmpdir() },
);
const env = cleanEnv();
env.PATH = `${dir}:${env.PATH || '/usr/bin:/bin'}`;
env.RNFB_E2E_RUN_RECORD_DIR = path.join(dir, 'rec');
let childPid;
try {
recordPid(parent.pid, 'jet:8090', env);
const end = Date.now() + 5000;
while (!childPid && Date.now() < end) {
await new Promise(resolve => setTimeout(resolve, 50));
const ps = execFileSync('ps', ['-axo', 'pid=,ppid=,args='], { encoding: 'utf8' });
const line = ps
.split('\n')
.find(
l => l.includes('@TestingAVD-5') && l.trim().split(/\s+/)[1] === String(parent.pid),
);
childPid = line ? Number(line.trim().split(/\s+/)[0]) : undefined;
}
let out;
let status = 0;
try {
out = execFileSync('bash', [checkPath, '--platform=android'], {
encoding: 'utf8',
cwd: repoRoot,
env,
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
status = e.status;
out = `${e.stdout || ''}${e.stderr || ''}`;
}
expect(out).toMatch(
new RegExp(
`BUSY\\s+android emulator emulator-16232 avd=TestingAVD qemu pid=${ownAvd.pid} cwd=${repoRoot} owner=this-checkout-unrecorded — no run recorded it \\(e\\.g\\. cold-boot respawn\\); free with yarn tests:e2e:release --devices`,
),
);
expect(out).not.toMatch(/stray emulator serial emulator-16232/);
// Identity-AVD BUSY names default --devices; must not push --include-unrecorded.
expect(out).not.toMatch(/emulator-16232[^\n]*include-unrecorded/);
expect(out).not.toMatch(
/owner=this-checkout-unrecorded:.*--include-unrecorded --only [\w,-]*android-emulator/,
);
expect(out).toMatch(
/INFO\s+android stray emulator serial emulator-16234 owner=this-checkout-unrecorded .*avd=TestingAVD-3 — .*run yarn tests:e2e:release --include-unrecorded --devices --only android-emulator in the slot env of the identity whose AVD it runs/,
);
expect(out).toMatch(
/BUSY\s+android stray emulator serial emulator-16236 .*owner=this-checkout — free it with yarn tests:e2e:release --devices/,
);
expect(status).toBe(1);
} finally {
ownAvd.kill('SIGKILL');
otherAvd.kill('SIGKILL');
if (childPid) {
try {
process.kill(childPid, 'SIGKILL');
} catch {
// already gone
}
}
parent.kill('SIGKILL');
fs.rmSync(dir, { recursive: true, force: true });
}
});
});
describe('check-e2e-resources Jet port held by another user', function () {
it('check is BUSY when the Jet port accepts connections that lsof cannot attribute (another user)', async function () {
const fs = require('fs');
const os = require('os');
const net = require('net');
const checkPath = path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-fake-lsof-'));
fs.writeFileSync(path.join(dir, 'lsof'), '#!/bin/bash\nexit 0\n');
fs.chmodSync(path.join(dir, 'lsof'), 0o755);
const server = net.createServer().listen(0, '127.0.0.1');
await new Promise(resolve => server.once('listening', resolve));
const port = server.address().port;
const run = () => {
const env = cleanEnv();
env.PATH = `${dir}:${env.PATH || '/usr/bin:/bin'}`;
env.JET_REMOTE_PORT = String(port);
env.RNFB_E2E_RUN_RECORD_DIR = path.join(dir, 'rec');
try {
return execFileSync('bash', [checkPath, '--platform=android'], {
encoding: 'utf8',
cwd: repoRoot,
env,
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
return `${e.stdout || ''}${e.stderr || ''}`;
}
};
try {
try {
const out = run();
expect(out).toMatch(
new RegExp(
`BUSY\\s+port :${port} \\(jet:\\w+\\) accepts connections but has no listener lsof can attribute to this user owner=unrecorded`,
),
);
expect(out).toMatch(/owner=unrecorded: held by another owner/);
} finally {
await new Promise(resolve => server.close(resolve));
}
expect(run()).toMatch(new RegExp(`CLEAR\\s+port :${port} \\(jet:\\w+\\)`));
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
});