Skip to content

Commit

Permalink
Auto merge of #124695 - erickt:bump-fuchsia, r=tmandry
Browse files Browse the repository at this point in the history
Fuchsia test runner: fixup script

This commit fixes several issues in the fuchsia-test-runner.py script:

1. Migrate from `pm` to `ffx` for package management, as `pm` is now deprecated. Furthermore, the `pm` calls used in this script no longer work at Fuchsia's HEAD. This is the largest change in this commit, and impacts all steps around repository management (creation and registration of the repo, as well as package publishing).

2. Allow for `libtest` to be either statically or dynamically linked. The script assumed it was dynamically linked, but the current Rust behavior at HEAD is to statically link it.

3. Minor cleanup to use `ffx --machine json` rather than string parsing.

4. Minor cleanup to the docs around the script.
  • Loading branch information
bors committed May 8, 2024
2 parents 5ce96b1 + f44d611 commit 7aa17df
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 93 deletions.
180 changes: 93 additions & 87 deletions src/ci/docker/scripts/fuchsia-test-runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

import argparse
from dataclasses import dataclass
import fcntl
import glob
import hashlib
import io
import json
import os
import platform
Expand Down Expand Up @@ -143,6 +141,14 @@ def subprocess_output(self):
return sys.stdout
return subprocess.DEVNULL

def check_call(self, args, **kwargs):
self.log_info(f"Running: {' '.join(args)}")
return subprocess.check_call(args, **kwargs)

def check_output(self, args, **kwargs):
self.log_info(f"Running: {' '.join(args)}")
return subprocess.check_output(args, **kwargs)

def ffx_daemon_log_path(self):
return os.path.join(self.tmp_dir(), "ffx_daemon_log")

Expand Down Expand Up @@ -178,7 +184,7 @@ def start_ffx_isolation(self):
)

# Disable analytics
subprocess.check_call(
self.check_call(
[
ffx_path,
"config",
Expand All @@ -197,7 +203,7 @@ def start_ffx_isolation(self):
"test.experimental_structured_output": "true",
}
for key, value in configs.items():
subprocess.check_call(
self.check_call(
[
ffx_path,
"config",
Expand All @@ -222,7 +228,7 @@ def ffx_cmd_env(self):
}

def stop_ffx_isolation(self):
subprocess.check_call(
self.check_call(
[
self.tool_path("ffx"),
"daemon",
Expand Down Expand Up @@ -265,7 +271,7 @@ def start(self):
self.start_ffx_isolation()

# Stop any running emulators (there shouldn't be any)
subprocess.check_call(
self.check_call(
[
ffx_path,
"emu",
Expand All @@ -282,11 +288,11 @@ def start(self):
product_name = "minimal." + self.triple_to_arch(self.target)
fuchsia_version = "20.20240412.3.1"

# FIXME: We should be able to replace this with the machine parsable
# `ffx --machine json product lookup ...` once F15 is released.
out = subprocess.check_output(
out = self.check_output(
[
ffx_path,
"--machine",
"json",
"product",
"lookup",
product_name,
Expand All @@ -300,16 +306,15 @@ def start(self):

self.log_debug(out)

for line in io.BytesIO(out):
if line.startswith(b"gs://"):
transfer_manifest_url = line.rstrip()
break
else:
raise Exception("Unable to parse transfer manifest")
try:
transfer_manifest_url = json.loads(out)["transfer_manifest_url"]
except Exception as e:
print(e)
raise Exception("Unable to parse transfer manifest") from e

# Download the product bundle.
product_bundle_dir = os.path.join(self.tmp_dir(), 'product-bundle')
subprocess.check_call(
self.check_call(
[
ffx_path,
"product",
Expand All @@ -325,7 +330,7 @@ def start(self):

# Start emulator
# FIXME: condition --accel hyper on target arch matching host arch
subprocess.check_call(
self.check_call(
[
ffx_path,
"emu",
Expand All @@ -346,42 +351,52 @@ def start(self):

# Create new package repo
self.log_info("Creating package repo...")
subprocess.check_call(
self.check_call(
[
self.tool_path("pm"),
"newrepo",
"-repo",
ffx_path,
"repository",
"create",
self.repo_dir(),
],
env=ffx_env,
stdout=self.subprocess_output(),
stderr=self.subprocess_output(),
)

# Add repo
subprocess.check_call(
self.check_call(
[
ffx_path,
"repository",
"add-from-pm",
self.repo_dir(),
"--repository",
self.TEST_REPO_NAME,
self.repo_dir(),
],
env=ffx_env,
stdout=self.subprocess_output(),
stderr=self.subprocess_output(),
)

# Write to file
self.write_to_file()

# Start repository server
subprocess.check_call(
[ffx_path, "repository", "server", "start", "--address", "[::]:0"],
self.check_call(
[
ffx_path,
"repository",
"server",
"start",
"--address",
"[::]:0",
],
env=ffx_env,
stdout=self.subprocess_output(),
stderr=self.subprocess_output(),
)

# Register with newly-started emulator
subprocess.check_call(
self.check_call(
[
ffx_path,
"target",
Expand All @@ -395,12 +410,6 @@ def start(self):
stderr=self.subprocess_output(),
)

# Create lockfiles
open(self.pm_lockfile_path(), "a").close()

# Write to file
self.write_to_file()

self.log_info("Success! Your environment is ready to run tests.")

# FIXME: shardify this
Expand Down Expand Up @@ -445,7 +454,6 @@ def start(self):
meta/{package_name}.cm={package_dir}/meta/{package_name}.cm
bin/{exe_name}={bin_path}
lib/{libstd_name}={libstd_path}
lib/{libtest_name}={libtest_path}
lib/ld.so.1={sdk_dir}/arch/{target_arch}/sysroot/dist/lib/ld.so.1
lib/libfdio.so={sdk_dir}/arch/{target_arch}/dist/libfdio.so
"""
Expand Down Expand Up @@ -482,9 +490,6 @@ def run(self, args):
if not libstd_paths:
raise Exception(f"Failed to locate libstd (in {self.rustlibs_dir()})")

if not libtest_paths:
raise Exception(f"Failed to locate libtest (in {self.rustlibs_dir()})")

# Build a unique, deterministic name for the test using the name of the
# binary and the last 6 hex digits of the hash of the full path
def path_checksum(path):
Expand All @@ -500,6 +505,7 @@ def path_checksum(path):
cml_path = os.path.join(package_dir, "meta", f"{package_name}.cml")
cm_path = os.path.join(package_dir, "meta", f"{package_name}.cm")
manifest_path = os.path.join(package_dir, f"{package_name}.manifest")
manifest_json_path = os.path.join(package_dir, "package_manifest.json")
far_path = os.path.join(package_dir, f"{package_name}-0.far")

shared_libs = args.shared_libs[: args.n]
Expand All @@ -523,22 +529,6 @@ def log(msg):

log(f"Bin path: {bin_path}")

log("Setting up package...")

# Set up package
subprocess.check_call(
[
self.tool_path("pm"),
"-o",
package_dir,
"-n",
package_name,
"init",
],
stdout=log_file,
stderr=log_file,
)

log("Writing CML...")

# Write and compile CML
Expand All @@ -563,7 +553,7 @@ def log(msg):

log("Compiling CML...")

subprocess.check_call(
self.check_call(
[
self.tool_path("cmc"),
"compile",
Expand All @@ -590,64 +580,80 @@ def log(msg):
target=self.target,
sdk_dir=self.sdk_dir,
libstd_name=os.path.basename(libstd_paths[0]),
libtest_name=os.path.basename(libtest_paths[0]),
libstd_path=libstd_paths[0],
libtest_path=libtest_paths[0],
target_arch=self.triple_to_arch(self.target),
)
)
# `libtest`` was historically a shared library, but now seems to be (sometimes?)
# statically linked. If we find it as a shared library, include it in the manifest.
if libtest_paths:
manifest.write(
f"lib/{os.path.basename(libtest_paths[0])}={libtest_paths[0]}\n"
)
for shared_lib in shared_libs:
manifest.write(f"lib/{os.path.basename(shared_lib)}={shared_lib}\n")

log("Determining API level...")
out = self.check_output(
[
self.tool_path("ffx"),
"--machine",
"json",
"version",
],
env=self.ffx_cmd_env(),
stderr=log_file,
)
api_level = json.loads(out)["tool_version"]["api_level"]

log("Compiling and archiving manifest...")

subprocess.check_call(
self.check_call(
[
self.tool_path("pm"),
self.tool_path("ffx"),
"package",
"build",
manifest_path,
"-o",
package_dir,
"-m",
manifest_path,
"build",
"--api-level",
str(api_level),
],
env=self.ffx_cmd_env(),
stdout=log_file,
stderr=log_file,
)
subprocess.check_call(

self.check_call(
[
self.tool_path("pm"),
"-o",
package_dir,
"-m",
manifest_path,
self.tool_path("ffx"),
"package",
"archive",
"create",
"-o",
far_path,
manifest_json_path,
],
env=self.ffx_cmd_env(),
stdout=log_file,
stderr=log_file,
)

log("Publishing package to repo...")

# Publish package to repo
with open(self.pm_lockfile_path(), "w") as pm_lockfile:
fcntl.lockf(pm_lockfile.fileno(), fcntl.LOCK_EX)
subprocess.check_call(
[
self.tool_path("pm"),
"publish",
"-a",
"-repo",
self.repo_dir(),
"-f",
far_path,
],
stdout=log_file,
stderr=log_file,
)
# This lock should be released automatically when the pm
# lockfile is closed, but we'll be polite and unlock it now
# since the spec leaves some wiggle room.
fcntl.lockf(pm_lockfile.fileno(), fcntl.LOCK_UN)
self.check_call(
[
self.tool_path("ffx"),
"repository",
"publish",
"--package",
os.path.join(package_dir, "package_manifest.json"),
self.repo_dir(),
],
stdout=log_file,
stderr=log_file,
)

log("Running ffx test...")

Expand Down Expand Up @@ -765,7 +771,7 @@ def stop(self):

# Shut down the emulator
self.log_info("Stopping emulator...")
subprocess.check_call(
self.check_call(
[
self.tool_path("ffx"),
"emu",
Expand Down
Loading

0 comments on commit 7aa17df

Please sign in to comment.