Repository navigation
Expand file tree
/
Copy pathcreateE2eDevices.test.js
More file actions
521 lines (497 loc) · 16.7 KB
/
Copy pathcreateE2eDevices.test.js
File metadata and controls
521 lines (497 loc) · 16.7 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
const { execFileSync, spawn } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const repoRoot = path.resolve(__dirname, '../../..');
const { processStartTime } = require(path.join(repoRoot, 'tests/e2e/runRecord'));
function checkIos(bootedJson, args) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-ios-sim-check-serial-'));
fs.writeFileSync(
path.join(dir, 'xcrun'),
`#!/bin/bash
if [[ "$*" == "simctl list devices booted -j" ]]; then
printf '%s\\n' '${bootedJson}'
exit 0
fi
exit 92
`,
);
fs.writeFileSync(path.join(dir, 'lsof'), '#!/bin/bash\nexit 1\n');
fs.chmodSync(path.join(dir, 'xcrun'), 0o755);
fs.chmodSync(path.join(dir, 'lsof'), 0o755);
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (
key.startsWith('RNFB_') ||
key.startsWith('E2E_') ||
key.startsWith('JET_') ||
key === 'RCT_METRO_PORT'
) {
delete env[key];
}
}
env.PATH = `${dir}:${env.PATH}`;
env.RNFB_E2E_RUN_RECORD_DIR = path.join(dir, 'rec');
try {
return {
status: 0,
output: execFileSync(
'/bin/bash',
[path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh'), ...args],
{ encoding: 'utf8', cwd: repoRoot, env, stdio: ['pipe', 'pipe', 'pipe'] },
),
};
} catch (error) {
return { status: error.status, output: `${error.stdout || ''}${error.stderr || ''}` };
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
describe('e2e device setup default count', function () {
it('create-android-avds.sh defaults count=1 and loops i < COUNT', function () {
const src = fs.readFileSync(path.join(repoRoot, 'scripts/e2e/create-android-avds.sh'), 'utf8');
expect(src).toMatch(/COUNT="\$\{1:-1\}"/);
expect(src).toMatch(/for \(\(i = 0; i < COUNT; i\+\+\)\)/);
expect(src).not.toMatch(/seq 0 "\$COUNT"/);
});
it('create-ios-simulators.sh defaults count=1 and loops i < COUNT', function () {
const src = fs.readFileSync(
path.join(repoRoot, 'scripts/e2e/create-ios-simulators.sh'),
'utf8',
);
expect(src).toMatch(/COUNT="\$\{1:-1\}"/);
expect(src).toMatch(/for \(\(i = 0; i < COUNT; i\+\+\)\)/);
});
it('rejects unsupported setup counts before invoking device runners', function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-device-count-'));
const actionLog = path.join(dir, 'actions.log');
fs.writeFileSync(
path.join(dir, 'xcrun'),
`#!/bin/bash
printf 'xcrun %s\\n' "$*" >> "$RNFB_ACTION_LOG"
exit 90
`,
);
fs.chmodSync(path.join(dir, 'xcrun'), 0o755);
const androidHome = path.join(dir, 'sdk');
fs.mkdirSync(path.join(androidHome, 'emulator'), { recursive: true });
fs.writeFileSync(
path.join(androidHome, 'emulator', 'emulator'),
`#!/bin/bash
printf 'emulator %s\\n' "$*" >> "$RNFB_ACTION_LOG"
exit 90
`,
);
fs.chmodSync(path.join(androidHome, 'emulator', 'emulator'), 0o755);
const env = {
...process.env,
PATH: `${dir}:${process.env.PATH}`,
ANDROID_HOME: androidHome,
RNFB_ACTION_LOG: actionLog,
};
const scripts = ['create-ios-simulators.sh', 'create-android-avds.sh'];
try {
for (const script of scripts) {
for (const count of ['9', '99', '0', '-1', '1.5', 'foo', '08']) {
let result;
try {
result = {
status: 0,
stdout: execFileSync(
'/bin/bash',
[path.join(repoRoot, 'scripts/e2e', script), count],
{
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(2);
expect(result.stdout).toBe('');
expect(result.stderr).toContain(`count must be an integer 1..8 (got ${count})`);
}
}
expect(fs.existsSync(actionLog)).toBe(false);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('reuses exact neutral bases and only creates missing neutral bases', function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-ios-sim-setup-'));
const xcrun = path.join(dir, 'xcrun');
const log = path.join(dir, 'xcrun.log');
fs.writeFileSync(
xcrun,
`#!/usr/bin/env bash
printf '%s\\n' "$*" >> "$RNFB_XCRUN_LOG"
case "$*" in
"simctl list runtimes available -j")
printf '%s\\n' '{"runtimes":[{"isAvailable":true,"platform":"iOS","version":"27.0","name":"iOS 27.0","identifier":"runtime-ios-27"}]}'
;;
"simctl list devices available")
# Text listing used by rnfb_resolve_ios_sim_udid_for_name (pinned runtime section).
printf '%s\\n' '-- iOS 27.0 --'
printf '%s\\n' ' RN E2E iOS slot-0 (AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA) (Shutdown)'
printf '%s\\n' ' RNFB E2E iOS slot-0 (BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB) (Shutdown)'
;;
"simctl create RN E2E iOS slot-1 iPhone 17 runtime-ios-27")
printf '%s\\n' created-slot-1
;;
*)
exit 91
;;
esac
`,
);
fs.chmodSync(xcrun, 0o755);
try {
const output = execFileSync(
'bash',
[path.join(repoRoot, 'scripts/e2e/create-ios-simulators.sh'), '2'],
{
encoding: 'utf8',
cwd: repoRoot,
env: {
...process.env,
PATH: `${dir}:${process.env.PATH}`,
RNFB_XCRUN_LOG: log,
},
},
);
const calls = fs.readFileSync(log, 'utf8');
expect(output).toMatch(/RN E2E iOS slot-0 exists udid=AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA/);
expect(calls).toMatch(/^simctl create RN E2E iOS slot-1 iPhone 17 runtime-ios-27$/m);
expect(calls).not.toMatch(/simctl create RN E2E iOS slot-0/);
expect(calls).not.toMatch(/-Detox/);
expect(calls).not.toMatch(/\b(delete|erase|rename|shutdown)\b/);
expect(calls).not.toMatch(/simctl create RNFB E2E iOS/);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('release shuts down only the simulator the selected slot recorded', async function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-ios-sim-release-'));
const log = path.join(dir, 'xcrun.log');
fs.writeFileSync(
path.join(dir, 'xcrun'),
`#!/bin/bash
if [[ "$*" == "simctl list devices booted -j" ]]; then
booted='{"devices":{}}'
printf '%s\\n' "\${RNFB_FAKE_SIMCTL_JSON:-$booted}"
exit 0
fi
printf '%s\\n' "$*" >> "$RNFB_XCRUN_LOG"
`,
);
fs.writeFileSync(
path.join(dir, 'bash'),
`#!/bin/bash
if [[ "$1" == *"/check-e2e-resources.sh" ]]; then
exit 0
fi
exec /bin/bash "$@"
`,
);
fs.writeFileSync(path.join(dir, 'sleep'), '#!/bin/bash\nexit 0\n');
const realLsof = execFileSync('/bin/sh', ['-c', 'command -v lsof'], {
encoding: 'utf8',
}).trim();
// No listeners, but the real cwd lookup (`lsof -a -p <pid> -d cwd`).
fs.writeFileSync(
path.join(dir, 'lsof'),
`#!/bin/bash\n[[ "$1" == "-a" ]] && exec ${realLsof} "$@"\nexit 1\n`,
);
for (const command of ['xcrun', 'bash', 'sleep', 'lsof']) {
fs.chmodSync(path.join(dir, command), 0o755);
}
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (
key.startsWith('RNFB_') ||
key.startsWith('E2E_') ||
key.startsWith('JET_') ||
key === 'RCT_METRO_PORT'
) {
delete env[key];
}
}
env.PATH = `${dir}:${env.PATH}`;
env.TMPDIR = dir;
env.RNFB_XCRUN_LOG = log;
env.RNFB_E2E_RUN_RECORD_DIR = path.join(dir, 'rec');
const release = (...extra) =>
execFileSync(
'/bin/bash',
[
path.join(repoRoot, 'scripts/e2e/release-e2e-resources.sh'),
'--platform=ios',
'--slot=1',
'--only=ios-sims',
...extra,
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
// Stands in for the recorded launchd_sim instance of UDID-1.
const token = spawn(
process.execPath,
[
'-e',
'setTimeout(() => {}, 60000)',
'launchd_sim',
'/sims/CoreSimulator/Devices/UDID-1/data/var/run/launchd_bootstrap.plist',
],
{ stdio: 'ignore' },
);
const tokenExited = new Promise(resolve => token.once('exit', resolve));
let foreign;
let own;
try {
release();
expect(fs.existsSync(log)).toBe(false);
const slotRecord = path.join(dir, 'rec', 'slot-1');
const entry = path.join(slotRecord, 'ios-sim-UDID-1');
fs.mkdirSync(slotRecord, { recursive: true });
fs.writeFileSync(
entry,
`ios-sim\tUDID-1\tRN E2E iOS slot-1\t${token.pid}\t${processStartTime(token.pid)}\n`,
);
release();
expect(fs.readFileSync(log, 'utf8').trim().split('\n')).toEqual(['simctl shutdown UDID-1']);
// Kept while the recorded instance is still alive (a later round can retry).
expect(fs.existsSync(entry)).toBe(true);
token.kill('SIGKILL');
await tokenExited;
fs.rmSync(log);
release();
// That instance is gone (e.g. rebooted by another owner): no shutdown, entry pruned.
expect(fs.existsSync(log)).toBe(false);
expect(fs.existsSync(entry)).toBe(false);
// --include-unrecorded: nothing booted, nothing shut down.
release('--include-unrecorded');
expect(fs.existsSync(log)).toBe(false);
// The identity's simulator booted by another owner (launchd_sim outside this
// checkout, as every real launchd_sim is): left running.
env.RNFB_FAKE_SIMCTL_JSON = JSON.stringify({
devices: { runtime: [{ name: 'RN E2E iOS slot-1', udid: 'UDID-2', state: 'Booted' }] },
});
foreign = spawn(
process.execPath,
[
'-e',
'setTimeout(() => {}, 60000)',
'launchd_sim',
'/sims/CoreSimulator/Devices/UDID-2/data/var/run/launchd_bootstrap.plist',
],
{ stdio: 'ignore', cwd: os.tmpdir() },
);
release('--include-unrecorded');
expect(fs.existsSync(log)).toBe(false);
// Traceable to this checkout (launchd_sim cwd inside it): shut down by UDID.
env.RNFB_FAKE_SIMCTL_JSON = JSON.stringify({
devices: { runtime: [{ name: 'RN E2E iOS slot-1', udid: 'UDID-3', state: 'Booted' }] },
});
own = spawn(
process.execPath,
[
'-e',
'setTimeout(() => {}, 60000)',
'launchd_sim',
'/sims/CoreSimulator/Devices/UDID-3/data/var/run/launchd_bootstrap.plist',
],
{ stdio: 'ignore', cwd: repoRoot },
);
release('--include-unrecorded');
expect(fs.readFileSync(log, 'utf8').trim().split('\n')).toEqual(['simctl shutdown UDID-3']);
} finally {
token.kill('SIGKILL');
foreign?.kill('SIGKILL');
own?.kill('SIGKILL');
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('caps all-slot release device actions at slot 7 when configured max is 99', function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-all-slot-release-'));
const log = path.join(dir, 'actions.log');
fs.writeFileSync(
path.join(dir, 'adb'),
`#!/bin/bash
printf 'adb %s\\n' "$*" >> "$RNFB_ACTION_LOG"
if [[ "$1" == "devices" ]]; then
printf '%s\\n' 'List of devices attached'
fi
exit 0
`,
);
fs.writeFileSync(
path.join(dir, 'pkill'),
`#!/bin/bash
printf 'pkill %s\\n' "$*" >> "$RNFB_ACTION_LOG"
exit 0
`,
);
fs.writeFileSync(
path.join(dir, 'bash'),
`#!/bin/bash
if [[ "$1" == *"/check-e2e-resources.sh" ]]; then
exit 0
fi
exec /bin/bash "$@"
`,
);
fs.writeFileSync(path.join(dir, 'sleep'), '#!/bin/bash\nexit 0\n');
fs.writeFileSync(path.join(dir, 'lsof'), '#!/bin/bash\nexit 1\n');
for (const command of ['adb', 'pkill', 'bash', 'sleep', 'lsof']) {
fs.chmodSync(path.join(dir, command), 0o755);
}
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (
key.startsWith('RNFB_') ||
key.startsWith('E2E_') ||
key.startsWith('JET_') ||
key === 'RCT_METRO_PORT'
) {
delete env[key];
}
}
env.E2E_SLOTTED_MAX = '99';
env.PATH = `${dir}:${env.PATH}`;
env.TMPDIR = dir;
env.RNFB_ACTION_LOG = log;
env.RNFB_E2E_RUN_RECORD_DIR = path.join(dir, 'rec');
try {
execFileSync(
'/bin/bash',
[
path.join(repoRoot, 'scripts/e2e/release-e2e-resources.sh'),
'--platform=android',
'--all-slots',
'--devices',
'--only=android-emulator',
],
{ encoding: 'utf8', cwd: repoRoot, env },
);
const calls = fs.readFileSync(log, 'utf8');
expect(calls).toMatch(/@TestingAVD-7/);
expect(calls).not.toMatch(/@TestingAVD-8/);
expect(calls).toMatch(/adb -s emulator-5570 emu kill/);
expect(calls).not.toMatch(/adb -s emulator-5572 emu kill/);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('check reports a selected slot busy when its exact neutral base is booted', function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-ios-sim-check-'));
fs.writeFileSync(
path.join(dir, 'xcrun'),
`#!/bin/bash
if [[ "$*" == "simctl list devices booted -j" ]]; then
printf '%s\\n' '{"devices":{"runtime":[{"name":"RN E2E iOS slot-1","state":"Booted"}]}}'
exit 0
fi
exit 92
`,
);
fs.writeFileSync(path.join(dir, 'lsof'), '#!/bin/bash\nexit 1\n');
fs.chmodSync(path.join(dir, 'xcrun'), 0o755);
fs.chmodSync(path.join(dir, 'lsof'), 0o755);
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (
key.startsWith('RNFB_') ||
key.startsWith('E2E_') ||
key.startsWith('JET_') ||
key === 'RCT_METRO_PORT'
) {
delete env[key];
}
}
env.PATH = `${dir}:${env.PATH}`;
try {
let status = 0;
let output = '';
try {
output = execFileSync(
'/bin/bash',
[path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh'), '--platform=ios', '--slot=1'],
{ encoding: 'utf8', cwd: repoRoot, env, stdio: ['pipe', 'pipe', 'pipe'] },
);
} catch (error) {
status = error.status;
output = `${error.stdout || ''}${error.stderr || ''}`;
}
expect(status).toBe(1);
expect(output).toMatch(/BUSY\s+ios simulator booted \(RN E2E iOS slot-1/);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('check CLEAR names the probed slot base, not serial iPhone 17', function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-ios-sim-check-clear-'));
fs.writeFileSync(
path.join(dir, 'xcrun'),
`#!/bin/bash
if [[ "$*" == "simctl list devices booted -j" ]]; then
printf '%s\\n' '{"devices":{}}'
exit 0
fi
exit 92
`,
);
fs.writeFileSync(path.join(dir, 'lsof'), '#!/bin/bash\nexit 1\n');
fs.chmodSync(path.join(dir, 'xcrun'), 0o755);
fs.chmodSync(path.join(dir, 'lsof'), 0o755);
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (
key.startsWith('RNFB_') ||
key.startsWith('E2E_') ||
key.startsWith('JET_') ||
key === 'RCT_METRO_PORT'
) {
delete env[key];
}
}
env.PATH = `${dir}:${env.PATH}`;
try {
const output = execFileSync(
'/bin/bash',
[path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh'), '--platform=ios', '--slot=1'],
{ encoding: 'utf8', cwd: repoRoot, env, stdio: ['pipe', 'pipe', 'pipe'] },
);
expect(output).toMatch(/CLEAR\s+ios simulator \(RN E2E iOS slot-1\)/);
expect(output).not.toMatch(/CLEAR\s+ios simulator \(iPhone 17\)/);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('serial iOS check fails only on its own iPhone 17 and reports other booted simulators as INFO', function () {
const other = checkIos(
'{"devices":{"rt":[{"udid":"OTHER-1","name":"iPhone 18 Pro","state":"Booted"}]}}',
['--platform=ios'],
);
expect(other.status).toBe(0);
expect(other.output).toMatch(
/INFO\s+ios simulator iPhone 18 Pro \(OTHER-1\) booted — not this identity's device/,
);
expect(other.output).not.toMatch(/BUSY\s+ios simulator/);
const own = checkIos(
'{"devices":{"rt":[{"udid":"SERIAL-1","name":"iPhone 17","state":"Booted"}]}}',
['--platform=ios'],
);
expect(own.status).toBe(1);
expect(own.output).toMatch(
/BUSY\s+ios simulator booted \(iPhone 17 SERIAL-1\) owner=unrecorded/,
);
});
});