Skip to content

Commit 99da8ce

Browse files
zijiehe-google-comCommit Queue
authored and
Commit Queue
committed
[fuchsia] reduce the standalone commands in fuchsia integration
multiple processes are now aggregated by one test_env_setup call. Cq-Include-Trybots: luci.dart.try:vm-fuchsia-release-arm64-try,vm-fuchsia-release-x64-try Bug: http://crbug.com/356463343 Change-Id: Ib41bd2b36eb5d06ceeb6b834f1024365650e0ca0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/379560 Reviewed-by: Siva Annamalai <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Zijie He <[email protected]>
1 parent 213150c commit 99da8ce

File tree

5 files changed

+90
-133
lines changed

5 files changed

+90
-133
lines changed

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ Var("dart_root") + "/third_party/pkg/tar":
701701
"packages": [
702702
{
703703
"package": "chromium/fuchsia/test-scripts",
704-
"version": "EAdD2YcYwVrhF2q_zR6xUvPkcKlPb1tJyM_6_oOc84kC",
704+
"version": "8WCo7tLjg_GoImlj5eax_w-wxsvRPnmaChqN4ok-5rYC",
705705
}
706706
],
707707
"condition": 'download_fuchsia_deps',

build/fuchsia/test_env.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env vpython3
2+
# Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
3+
# for details. All rights reserved. Use of this source code is governed by a
4+
# BSD-style license that can be found in the LICENSE file.
5+
6+
import os
7+
import sys
8+
9+
sys.path.insert(
10+
0,
11+
os.path.abspath(
12+
os.path.join(os.path.dirname(__file__),
13+
'../../third_party/fuchsia/test_scripts/test/')))
14+
15+
# pylint: disable=wrong-import-position
16+
from test_env_setup import setup_env
17+
from compatible_utils import force_running_unattended
18+
19+
if __name__ == '__main__':
20+
force_running_unattended()
21+
# Note, the dart process can only get the pid of with_envs.py, i.e. ppid of
22+
# this script.
23+
sys.exit(setup_env(os.getppid()))

build/fuchsia/with_envs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
os.path.join(os.path.dirname(__file__),
1515
'../../third_party/fuchsia/test_scripts/test/')))
1616

17-
from common import catch_sigterm, wait_for_sigterm
17+
from common import catch_sigterm
1818

1919

2020
def Main():

pkg/test_runner/lib/src/fuchsia.dart

Lines changed: 56 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -3,156 +3,90 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:convert';
76
import 'dart:io';
87

98
import 'command.dart';
109

1110
// Runs tests on a fuchsia emulator with chromium maintained test-scripts and
1211
// CFv2 targets.
1312
class FuchsiaEmulator {
14-
static const String testScriptRoot =
15-
"./third_party/fuchsia/test_scripts/test/";
1613
static const String withEnv = "./build/fuchsia/with_envs.py";
17-
static const String tmpRoot = "/tmp/dart_ffi_test/";
18-
// TODO(#38752): Once we have vm/cc and AOT tests running, this will sometimes
19-
// need to be the component for run_vm_tests or dart_precompiled_runtime.
20-
static const String cmName = "dart_test_component.cm";
2114

2215
final Map<String, String> envs = <String, String>{};
23-
Process? daemonProc;
24-
Process? emuProc;
25-
String? emuName;
26-
Process? repoProc;
16+
Directory? daemonIsolateDir;
17+
Process? publisher;
2718

2819
// Publishes the packages to the Fuchsia environment.
2920
Future<void> publishPackage(String buildDir, String mode, String arch) async {
30-
try {
31-
await Directory(tmpRoot).delete(recursive: true);
32-
} catch (_) {}
33-
// The /tmp/ should always be present, recursive creation is not expected.
34-
await Directory(tmpRoot).create();
35-
assert(daemonProc == null);
36-
daemonProc = await _run("isolate_daemon.py", []);
37-
var isolateDir = await _captureStdout(daemonProc!);
38-
print("+ ffx daemon running on $isolateDir should be ready now.");
39-
envs["FFX_ISOLATE_DIR"] = isolateDir;
40-
assert(emuProc == null);
41-
emuProc = await _run("start_emulator.py", [
42-
"--disable-graphics",
43-
"--target-id-only",
44-
"--product",
45-
arch == "arm64" ? "terminal.qemu-arm64" : "terminal.x64",
46-
"--device-spec",
47-
"virtual_device_large"
48-
]);
49-
emuName = await _captureStdout(emuProc!);
50-
print("+ Targeting emu name $emuName");
51-
await _assertRun("test_connection.py", [emuName!]);
52-
await _assertRun("publish_package.py", [
53-
"--packages",
54-
_testPackagePath(buildDir, mode),
55-
"--purge-repo",
56-
"--repo",
57-
_tempDirectoryOf("repo")
58-
]);
59-
repoProc = await _run("serve_repo.py", [
60-
"run",
61-
"--serve-repo",
62-
_tempDirectoryOf("repo"),
63-
"--repo-name",
64-
"dart-ffi-test-repo",
65-
"--target-id",
66-
emuName!
67-
]);
68-
print("+ Fuchsia repo ${await _captureStdout(repoProc!)} is running "
69-
"at ${_tempDirectoryOf('repo')}");
70-
await _assertRun("pkg_resolve.py", [emuName!, _testPackageName(mode)]);
71-
}
72-
73-
// Tears down the Fuchsia environment.
74-
Future<void> stop() async {
75-
assert(repoProc != null);
76-
repoProc!.kill();
77-
await repoProc!.exitCode;
78-
assert(emuProc != null);
79-
emuProc!.kill();
80-
await emuProc!.exitCode;
81-
assert(daemonProc != null);
82-
daemonProc!.kill();
83-
await daemonProc!.exitCode;
21+
assert(daemonIsolateDir == null);
22+
daemonIsolateDir = Directory.systemTemp.createTempSync();
23+
envs["FFX_ISOLATE_DIR"] = daemonIsolateDir!.path;
24+
assert(publisher == null);
25+
var args = <String>[
26+
"./build/fuchsia/test_env.py",
27+
"--out-dir=${_outDir(buildDir, mode)}",
28+
"--device-spec=virtual_device_large",
29+
"--packages=dart_test_$mode.far",
30+
"--logs-dir=${daemonIsolateDir!.path}"
31+
];
32+
if (arch == "arm64") {
33+
args.add("--product=terminal.qemu-arm64");
34+
}
35+
publisher = await Process.start(withEnv, args,
36+
environment: envs, mode: ProcessStartMode.inheritStdio);
37+
while (!await File(
38+
"${daemonIsolateDir!.path}/test_env_setup.${publisher!.pid}.pid")
39+
.exists() &&
40+
await _isProcessRunning(publisher!, 1000)) {}
41+
// TODO(38752): Should return a value to indicate the failure of the
42+
// enviornment setup.
43+
if (await _isProcessRunning(publisher!, 1)) {
44+
print("+ ffx daemon running on $daemonIsolateDir should be ready now.");
45+
} else {
46+
print("+ environment setup failure.");
47+
}
8448
}
8549

8650
// Returns a command to execute a set of tests against the running Fuchsia
8751
// environment.
88-
VMCommand getTestCommand(
89-
String buildDir, String mode, String arch, List<String> arguments) {
52+
VMCommand getTestCommand(String buildDir, String mode, String arch,
53+
List<String> arguments, Map<String, String> environmentOverrides) {
54+
environmentOverrides.addAll(envs);
9055
return VMCommand(
9156
withEnv,
92-
_runArgs("run_executable_test.py", [
93-
"--target-id",
94-
emuName!,
95-
"--out-dir",
96-
_tempDirectoryOf("out"),
97-
"--test-name",
98-
"fuchsia-pkg://fuchsia.com/${_testPackageName(mode)}#meta/$cmName",
99-
"--test-realm",
57+
[
58+
"./third_party/fuchsia/test_scripts/test/run_executable_test.py",
59+
"--test-name=fuchsia-pkg://fuchsia.com/dart_test_$mode#meta/dart_test_component.cm",
10060
// VmexResource not available in default hermetic realm
10161
// TODO(38752): Setup a Dart test realm.
102-
"/core/testing:system-tests",
103-
"--logs-dir",
104-
_tempDirectoryOf("logs"),
105-
"--package-deps",
106-
_testPackagePath(buildDir, mode),
62+
"--test-realm=/core/testing:system-tests",
63+
"--out-dir=${_outDir(buildDir, mode)}",
64+
"--package-deps=dart_test_$mode.far",
10765
...arguments
108-
]),
109-
envs);
110-
}
111-
112-
static String _testPackageName(String mode) {
113-
return "dart_test_$mode";
114-
}
115-
116-
static String _testPackagePath(String buildDir, String mode) {
117-
var farName = _testPackageName(mode);
118-
return "$buildDir/gen/$farName/$farName.far";
66+
],
67+
environmentOverrides);
11968
}
12069

121-
static String _tempDirectoryOf(String name) {
122-
return tmpRoot + name;
123-
}
124-
125-
List<String> _runArgs(String script, List<String> args) {
126-
return [testScriptRoot + script, ...args];
127-
}
128-
129-
/// Executes a test script inside of third_party/fuchsia/test_scripts/test/
130-
/// with the required environment setup and the arguments.
131-
Future<Process> _run(String script, List<String> args) async {
132-
var newArgs = _runArgs(script, args);
133-
print("+ Start $withEnv with $newArgs with environment $envs.");
134-
return Process.start(withEnv, newArgs, environment: envs);
135-
}
136-
137-
/// Executes a test script and asserts its return code is 0; see _run and
138-
/// _assert.
139-
Future<void> _assertRun(String script, List<String> args) async {
140-
_assert((await (await _run(script, args)).exitCode) == 0);
70+
// Tears down the Fuchsia environment.
71+
Future<void> stop() async {
72+
publisher!.kill();
73+
await publisher!.exitCode;
74+
publisher = null;
75+
daemonIsolateDir!.deleteSync(recursive: true);
76+
daemonIsolateDir = null;
14177
}
14278

143-
/// Captures the first line of output in utf8.
144-
Future<String> _captureStdout(Process proc) async {
145-
// The stderr needs to be fully consumed as well.
146-
proc.stderr.transform(utf8.decoder).forEach((x) => stderr.write(x));
147-
return (await proc.stdout.transform(utf8.decoder).first).trim();
79+
Future<bool> _isProcessRunning(Process proc, int waitMs) async {
80+
try {
81+
await proc.exitCode.timeout(Duration(milliseconds: waitMs));
82+
return false;
83+
} on TimeoutException {
84+
return true;
85+
}
14886
}
14987

150-
/// Unlike assert keyword, always evaluates the input function and throws
151-
/// exception when the evaluated result is false.
152-
void _assert(bool condition) {
153-
if (!condition) {
154-
throw AssertionError();
155-
}
88+
String _outDir(String buildDir, String mode) {
89+
return "$buildDir/gen/dart_test_$mode";
15690
}
15791

15892
static final FuchsiaEmulator _instance = _create();

pkg/test_runner/lib/src/runtime_configuration.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,15 @@ class DartkFuchsiaEmulatorRuntimeConfiguration
533533
argument.replaceAll(Directory.current.path, "pkg/data"))
534534
.toList();
535535

536-
var command = FuchsiaEmulator.instance().getTestCommand(
537-
_configuration.buildDirectory,
538-
_configuration.mode.name,
539-
_configuration.architecture.name,
540-
arguments);
541-
command.arguments
542-
.insert(command.arguments.length - 1, '--disable-dart-dev');
543-
command.environmentOverrides.addAll(environmentOverrides);
544-
return [command];
536+
arguments.insert(arguments.length - 1, '--disable-dart-dev');
537+
return [
538+
FuchsiaEmulator.instance().getTestCommand(
539+
_configuration.buildDirectory,
540+
_configuration.mode.name,
541+
_configuration.architecture.name,
542+
arguments,
543+
environmentOverrides)
544+
];
545545
}
546546
}
547547

0 commit comments

Comments
 (0)