| #!/bin/bash | |
| # Copyright 2020 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| # | |
| # Common helpers specific for Fuchsia in-tree testing. | |
| btf::setup_fx() { | |
| if [[ -z "${BT_TEMP_DIR}" ]]; then | |
| echo "Cannot proceed, BT_TEMP_DIR is unset" 1>&2 | |
| exit 1 | |
| fi | |
| local with_metrics=0 | |
| if [[ $# -gt 0 && "$1" == "1" ]]; then | |
| with_metrics=1 | |
| fi | |
| local _FUCHSIA_DIR="${BT_TEMP_DIR}" | |
| local _FX="${_FUCHSIA_DIR}/scripts/fx" | |
| BT_ASSERT_FILE_EXISTS "${_FX}" | |
| mkdir -p "${_FUCHSIA_DIR}/.ssh" "${_FUCHSIA_DIR}/.jiri_root/bin" | |
| ln -s "${_FX}" "${_FUCHSIA_DIR}/.jiri_root/bin/fx" | |
| touch "${_FUCHSIA_DIR}/.fx-root" | |
| # Fake fuchsia dir configuration. | |
| BT_ASSERT_FILE_EXISTS "${_FUCHSIA_DIR}/tools/devshell/lib/vars.sh" | |
| BT_ASSERT_FILE_EXISTS "${_FUCHSIA_DIR}/tools/devshell/lib/platform.sh" | |
| touch "${_FUCHSIA_DIR}/.ssh/fuchsia_ed25519" "${_FUCHSIA_DIR}/.ssh/fuchsia_authorized_keys" | |
| echo -e "${_FUCHSIA_DIR}/.ssh/fuchsia_ed25519\n${_FUCHSIA_DIR}/.ssh/fuchsia_authorized_keys" > "${_FUCHSIA_DIR}/.fx-ssh-path" | |
| # Fake a build directory with the minimal content so that fx works | |
| btf::make_out_dir "out/default" | |
| echo "out/default" > "${_FUCHSIA_DIR}/.fx-build-dir" | |
| # Create centralized mock RBE settings for main_build.py to parse successfully | |
| cat > "${_FUCHSIA_DIR}/out/default/rbe_settings.json" <<EOF | |
| { | |
| "final": { | |
| "bazel_enable": false, | |
| "bazel_exec_strategy": "local", | |
| "bazel_download_outputs": "all", | |
| "cxx_download_objects": false, | |
| "cxx_enable": false, | |
| "cxx_exec_strategy": "local", | |
| "cxx_minimalist_wrapper": false, | |
| "link_download_unstripped_outputs": false, | |
| "link_enable": false, | |
| "link_exec_strategy": "local", | |
| "rust_download_rlibs": false, | |
| "rust_download_unstripped_binaries": false, | |
| "rust_enable": false, | |
| "rust_exec_strategy": "local", | |
| "needs_reproxy": false, | |
| "needs_auth": false | |
| } | |
| } | |
| EOF | |
| # main_build.py imports build/rbe/build_summary.py, which isn't part of the mock checkout. | |
| mkdir -p "${_FUCHSIA_DIR}/build/rbe" | |
| cat > "${_FUCHSIA_DIR}/build/rbe/build_summary.py" <<EOF | |
| def summarize_rbe_metrics_from_logdir(reproxy_logdir): | |
| return None | |
| EOF | |
| # Mock credentials.sh to avoid executing real credentials isolation in unit tests | |
| btf::make_mock "${_FUCHSIA_DIR}/tools/devshell/lib/credentials.sh" | |
| cat > "${_FUCHSIA_DIR}/tools/devshell/lib/credentials.sh.mock_side_effects" <<EOF | |
| function fx-setup-isolated-adc { | |
| : | |
| } | |
| EOF | |
| # Create centralized mock profiles for RbeProfile and RsProfile loading | |
| mkdir -p "${_FUCHSIA_DIR}/.fx/config" | |
| cat > "${_FUCHSIA_DIR}/.fx/config/rbe_profile.json" <<EOF | |
| { | |
| "rbe_instance": "projects/fake-project/instances/fake-instance", | |
| "remote_exec_root": "/b/f/w", | |
| "worker_pools": { | |
| "default": "Pool=fake-default-pool" | |
| }, | |
| "auth_type": "google" | |
| } | |
| EOF | |
| cat > "${_FUCHSIA_DIR}/.fx/config/rs_profile.json" <<EOF | |
| { | |
| "rs_instance": "projects/fake-project/instances/fake-instance", | |
| "cas_instance": "projects/fake-project/instances/fake-cas", | |
| "results_url": "http://fake-results-url", | |
| "auth_type": "google" | |
| } | |
| EOF | |
| if (( with_metrics )); then | |
| BT_ASSERT_FILE_EXISTS "${_FUCHSIA_DIR}/tools/devshell/lib/metrics.sh" | |
| else | |
| btf::make_mock "${_FUCHSIA_DIR}/tools/devshell/lib/metrics.sh" | |
| cat > "${_FUCHSIA_DIR}/tools/devshell/lib/metrics.sh.mock_side_effects" <<EOF | |
| function metrics-init { | |
| : | |
| } | |
| function track-feature-status { | |
| : | |
| } | |
| function track-command-execution { | |
| : | |
| } | |
| function track-command-finished { | |
| : | |
| } | |
| function track-build-event { | |
| : | |
| } | |
| EOF | |
| fi | |
| # If the host-tool command is not mocked, some code in vars.sh will | |
| # not work well. If a custom mock of 'fx host-tool' is needed, mock it | |
| # before calling 'setup_fx' | |
| btf::_make_fx_host_tool_mock | |
| echo "${_FX}" | |
| } | |
| btf::make_out_dir() { | |
| if [[ ! $# -eq 1 ]]; then | |
| echo "Expected a single argument" | |
| fi | |
| if [[ -z "${BT_TEMP_DIR}" ]]; then | |
| echo "Cannot proceed, BT_TEMP_DIR is unset" 1>&2 | |
| exit 1 | |
| fi | |
| local _REL_BUILD_DIR="$1" | |
| local _HOST_OUT_DIR="host_x64" | |
| local _FUCHSIA_DIR="${BT_TEMP_DIR}" | |
| local _FUCHSIA_BUILD_DIR="${_FUCHSIA_DIR}/${_REL_BUILD_DIR}" | |
| # Fake a build directory with the minimal content so that fx works | |
| mkdir -p "${_FUCHSIA_BUILD_DIR}/${_HOST_OUT_DIR}" | |
| touch "${_FUCHSIA_BUILD_DIR}/args.gn" | |
| cat > "${_FUCHSIA_BUILD_DIR}/fx.config" << EOF | |
| # Generated by fuchsia-mock.sh. | |
| FUCHSIA_ARCH='x64' | |
| FUCHSIA_BOARD_NAME=pc | |
| FUCHSIA_ZBI_COMPRESSION=zstd | |
| HOST_OUT_DIR='${_HOST_OUT_DIR}' | |
| EOF | |
| } | |
| btf::setup_fx_with_metrics() { | |
| btf::setup_fx 1 | |
| } | |
| btf::_make_buildtool_mock() { | |
| local rel_path="$1" | |
| local path="${BT_TEMP_DIR}/${rel_path}" | |
| btf::make_mock "${path}" | |
| echo "${path}" >> "${BT_TEMP_DIR}/.mocked_tools" | |
| echo "${path}" | |
| } | |
| btf::make_installed_hosttools_mock() { | |
| local hosttool_name="$1" | |
| btf::_make_buildtool_mock "${REL_BUILD_DIR:-out/default}/host-tools/${hosttool_name}" | |
| } | |
| btf::make_hosttools_mock() { | |
| local hosttool_name="$1" | |
| btf::_make_buildtool_mock "${REL_BUILD_DIR:-out/default}/host_x64/${hosttool_name}" | |
| } | |
| btf::make_mock_binary() { | |
| local -r _BIN_PATH="$(mktemp -d "${BT_TEMP_DIR}/binpath.XXXXXX")" | |
| local tool_name="$1" | |
| local tool_path="${_BIN_PATH}/${tool_name}" | |
| btf::make_mock "${tool_path}" | |
| echo "${tool_path}" | |
| } | |
| btf::add_binary_to_path() { | |
| local tool_path="$(dirname "$1")" | |
| export PATH="${tool_path}:$PATH" | |
| } | |
| btf::_make_fx_host_tool_mock() { | |
| local tool="${BT_TEMP_DIR}/tools/devshell/host-tool" | |
| if [[ -f "${tool}" ]]; then | |
| return 0 | |
| fi | |
| btf::make_mock "${tool}" | |
| cat > "${tool}.mock_side_effects" <<EOF | |
| # remove host-tool args (like --no-build) | |
| print=false | |
| while [[ \$# -gt 0 && \$1 =~ ^- ]]; do | |
| if [[ \$1 == "--print" ]]; then | |
| print=true | |
| fi | |
| shift | |
| done | |
| if [[ \$# == 0 ]]; then | |
| echo >&2 "Invalid state, cannot find host tool name" | |
| return 1 | |
| fi | |
| tool="\$1" | |
| p="\$(grep "/\${tool}$" "${BT_TEMP_DIR}/.mocked_tools")" | |
| if [[ -z "\$p" ]]; then | |
| echo >&2 "Invalid state, cannot find path for \$tool." \ | |
| "Use a btf::make_*_mock() method from fuchsia-mock.sh to mock it." | |
| return 1 | |
| fi | |
| if [[ ! -x "\$p" ]]; then | |
| echo >&2 "Invalid state, \$p is not executable." | |
| return 1 | |
| fi | |
| if \$print; then | |
| echo "\$p" | |
| else | |
| "\$p" "\$@" | |
| fi | |
| EOF | |
| } | |
| # Verifies that the arguments in BT_MOCK_ARGS match the arguments to this function. | |
| # The number and order of arguments must match, or non-zero is returned. | |
| # If a value of an argument is un-important it can be marked with the string | |
| # _ANY_. This allows for matching arguments that may have unique values, such as temp | |
| # filenames. | |
| # Args: toolname|mock_state_file arg [arg] [arg] ... | |
| # Returns: 0 if found; 1 if not found. | |
| btf::expect-mock-args() { | |
| local tool="$1" | |
| if [[ ! "${tool}" =~ \.mock_state(\.[0-9]+)?$ ]]; then | |
| tool="${tool}.mock_state" | |
| fi | |
| BT_ASSERT_FILE_EXISTS "${tool}" | |
| shift | |
| source "${tool}" | |
| local expected=("$@") | |
| local actual=("${BT_MOCK_ARGS[@]:1}") # ignore the first arg, the mocked command | |
| if [[ ${#actual[@]} -ne ${#expected[@]} ]]; then | |
| btf::_fail 1 "Unexpected number of arguments to ${tool}.\n Expected ${#expected[@]}: ${expected[*]}\n Actual ${#actual[@]}: ${actual[*]}" | |
| return | |
| fi | |
| for (( i=0; i<"${#expected[@]}"; i++ )); do | |
| if [[ "${expected[$i]}" != "_ANY_" ]]; then | |
| if [[ "${actual[$i]}" != "${expected[$i]}" ]]; then | |
| btf::_fail 1 "Unexpected arguments to ${tool}.\n Expected: ${expected[*]}\n Actual: ${actual[*]}" | |
| return | |
| fi | |
| fi | |
| done | |
| return 0 | |
| } | |
| btf::does-mock-args-contain() { | |
| local state_file="$1" | |
| BT_ASSERT_FILE_EXISTS "${state_file}" | |
| shift | |
| source "${state_file}" | |
| local expected=("$@") | |
| local actual=("${BT_MOCK_ARGS[@]}") | |
| local al=${#actual[@]} | |
| local el=${#expected[@]} | |
| if [[ $el -gt $al ]]; then | |
| return 1 | |
| fi | |
| for (( a=1; a<"$(( al - el + 1 ))"; a++ )); do | |
| for (( e=0; e<"${el}"; e++ )); do | |
| local shifted="$((a + e))" | |
| if [[ "${expected[$e]}" != "_ANY_" && "${actual[$shifted]}" != "${expected[$e]}" ]]; then | |
| continue 2 # continue the outer loop | |
| fi | |
| done | |
| # if we got here, we went through all the expected arguments, so success | |
| return 0 | |
| done | |
| return 1 | |
| } | |
| btf::does-mock-args-not-contain() { | |
| local state_file="$1" | |
| BT_ASSERT_FILE_EXISTS "${state_file}" | |
| shift | |
| source "${state_file}" | |
| local expected=("$@") | |
| local actual=("${BT_MOCK_ARGS[@]}") | |
| local al=${#actual[@]} | |
| local el=${#expected[@]} | |
| if [[ $el -gt $al ]]; then | |
| return 0 | |
| fi | |
| for (( a=1; a<"$(( al - el + 1 ))"; a++ )); do | |
| for (( e=0; e<"${el}"; e++ )); do | |
| local shifted="$((a + e))" | |
| if [[ "${expected[$e]}" != "_ANY_" && "${actual[$shifted]}" != "${expected[$e]}" ]]; then | |
| continue 2 # continue the outer loop | |
| fi | |
| done | |
| # if we got here, we went through all the expected arguments, so we found it | |
| return 1 | |
| done | |
| } |