Use builtin FFX isolation for Fuchsia test runner

FFX has new builtin support for isolating the daemon's environment. This
switches the manual isolation originally written to that new builtin
feature.
This commit is contained in:
David Koloski 2023-05-03 14:56:04 -04:00
parent 901fdb3b04
commit e97383657e

View File

@ -168,85 +168,17 @@ class TestEnvironment:
def ffx_isolate_dir(self): def ffx_isolate_dir(self):
return os.path.join(self.tmp_dir(), "ffx_isolate") return os.path.join(self.tmp_dir(), "ffx_isolate")
def ffx_home_dir(self): def home_dir(self):
return os.path.join(self.ffx_isolate_dir(), "user-home") return os.path.join(self.tmp_dir(), "user-home")
def ffx_tmp_dir(self):
return os.path.join(self.ffx_isolate_dir(), "tmp")
def ffx_log_dir(self):
return os.path.join(self.ffx_isolate_dir(), "log")
def ffx_user_config_dir(self):
return os.path.join(self.ffx_xdg_config_home(), "Fuchsia", "ffx", "config")
def ffx_user_config_path(self):
return os.path.join(self.ffx_user_config_dir(), "config.json")
def ffx_xdg_config_home(self):
if platform.system() == "Darwin":
return os.path.join(self.ffx_home_dir(), "Library", "Preferences")
return os.path.join(self.ffx_home_dir(), ".local", "share")
def ffx_ascendd_path(self):
return os.path.join(self.ffx_tmp_dir(), "ascendd")
def start_ffx_isolation(self): def start_ffx_isolation(self):
# Most of this is translated directly from ffx's isolate library # Most of this is translated directly from ffx's isolate library
os.mkdir(self.ffx_isolate_dir()) os.mkdir(self.ffx_isolate_dir())
os.mkdir(self.ffx_home_dir()) os.mkdir(self.home_dir())
os.mkdir(self.ffx_tmp_dir())
os.mkdir(self.ffx_log_dir())
fuchsia_dir = os.path.join(self.ffx_home_dir(), ".fuchsia") ffx_path = self.tool_path("ffx")
os.mkdir(fuchsia_dir) ffx_env = self.ffx_cmd_env()
fuchsia_debug_dir = os.path.join(fuchsia_dir, "debug")
os.mkdir(fuchsia_debug_dir)
metrics_dir = os.path.join(fuchsia_dir, "metrics")
os.mkdir(metrics_dir)
analytics_path = os.path.join(metrics_dir, "analytics-status")
with open(analytics_path, "w", encoding="utf-8") as analytics_file:
print("0", file=analytics_file)
ffx_path = os.path.join(metrics_dir, "ffx")
with open(ffx_path, "w", encoding="utf-8") as ffx_file:
print("1", file=ffx_file)
os.makedirs(self.ffx_user_config_dir())
with open(
self.ffx_user_config_path(), "w", encoding="utf-8"
) as config_json_file:
user_config_for_test = {
"log": {
"enabled": True,
"dir": self.ffx_log_dir(),
},
"overnet": {
"socket": self.ffx_ascendd_path(),
},
"ssh": {
"pub": self.ssh_authfile_path(),
"priv": self.ssh_keyfile_path(),
},
"test": {
"is_isolated": True,
"experimental_structured_output": True,
},
}
print(json.dumps(user_config_for_test), file=config_json_file)
ffx_env_path = os.path.join(self.ffx_user_config_dir(), ".ffx_env")
with open(ffx_env_path, "w", encoding="utf-8") as ffx_env_file:
ffx_env_config_for_test = {
"user": self.ffx_user_config_path(),
"build": None,
"global": None,
}
print(json.dumps(ffx_env_config_for_test), file=ffx_env_file)
# Start ffx daemon # Start ffx daemon
# We want this to be a long-running process that persists after the script finishes # We want this to be a long-running process that persists after the script finishes
@ -256,23 +188,54 @@ class TestEnvironment:
) as ffx_daemon_log_file: ) as ffx_daemon_log_file:
subprocess.Popen( subprocess.Popen(
[ [
self.tool_path("ffx"), ffx_path,
"--config",
self.ffx_user_config_path(),
"daemon", "daemon",
"start", "start",
], ],
env=self.ffx_cmd_env(), env=ffx_env,
stdout=ffx_daemon_log_file, stdout=ffx_daemon_log_file,
stderr=ffx_daemon_log_file, stderr=ffx_daemon_log_file,
) )
# Disable analytics
subprocess.check_call(
[
ffx_path,
"config",
"analytics",
"disable",
],
env=ffx_env,
stdout=self.subprocess_output(),
stderr=self.subprocess_output(),
)
# Set configs
configs = {
"log.enabled": "true",
"ssh.pub": self.ssh_authfile_path(),
"ssh.priv": self.ssh_keyfile_path(),
"test.is_isolated": "true",
"test.experimental_structured_output": "true",
}
for key, value in configs.items():
subprocess.check_call(
[
self.tool_path("ffx"),
"config",
"set",
key,
value,
],
env=self.ffx_cmd_env(),
stdout=self.subprocess_output(),
stderr=self.subprocess_output(),
)
def ffx_cmd_env(self): def ffx_cmd_env(self):
result = { return {
"HOME": self.ffx_home_dir(), "HOME": self.home_dir(),
"XDG_CONFIG_HOME": self.ffx_xdg_config_home(), "FFX_ISOLATE_DIR": self.ffx_isolate_dir(),
"ASCENDD": self.ffx_ascendd_path(),
"FUCHSIA_SSH_KEY": self.ssh_keyfile_path(),
# We want to use our own specified temp directory # We want to use our own specified temp directory
"TMP": self.tmp_dir(), "TMP": self.tmp_dir(),
"TEMP": self.tmp_dir(), "TEMP": self.tmp_dir(),
@ -280,14 +243,10 @@ class TestEnvironment:
"TEMPDIR": self.tmp_dir(), "TEMPDIR": self.tmp_dir(),
} }
return result
def stop_ffx_isolation(self): def stop_ffx_isolation(self):
subprocess.check_call( subprocess.check_call(
[ [
self.tool_path("ffx"), self.tool_path("ffx"),
"--config",
self.ffx_user_config_path(),
"daemon", "daemon",
"stop", "stop",
], ],
@ -709,8 +668,6 @@ class TestEnvironment:
subprocess.run( subprocess.run(
[ [
self.tool_path("ffx"), self.tool_path("ffx"),
"--config",
self.ffx_user_config_path(),
"test", "test",
"run", "run",
f"fuchsia-pkg://{self.TEST_REPO_NAME}/{package_name}#meta/{package_name}.cm", f"fuchsia-pkg://{self.TEST_REPO_NAME}/{package_name}#meta/{package_name}.cm",
@ -849,8 +806,6 @@ class TestEnvironment:
def debug(self, args): def debug(self, args):
command = [ command = [
self.tool_path("ffx"), self.tool_path("ffx"),
"--config",
self.ffx_user_config_path(),
"debug", "debug",
"connect", "connect",
"--", "--",
@ -948,8 +903,6 @@ class TestEnvironment:
subprocess.run( subprocess.run(
[ [
self.tool_path("ffx"), self.tool_path("ffx"),
"--config",
self.ffx_user_config_path(),
"log", "log",
"--since", "--since",
"now", "now",