blob: 7094df8a4d29b589ed536e70895d3a678af3cdc2 [file] [log] [blame]
#!/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.
### Test fx fdt
BT_FILE_DEPS=(
"scripts/fx"
"tools/devshell/fdt"
"tools/devshell/lib/fx-cmd-locator.sh"
"tools/devshell/lib/fx-optional-features.sh"
"tools/devshell/lib/platform.sh"
"tools/devshell/lib/vars.sh"
)
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../../lib/vars.sh || exit $?
# shellcheck disable=SC2034
BT_MOCKED_TOOLS=(
"bin/cipd"
"bin/open" # mock out MacOS' open command
"bin/unzip"
)
BT_MKDIR_DEPS=(
".jiri_root"
)
declare fx
BT_SET_UP() {
source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh"
fx="$(btf::setup_fx)"
ffx="$(btf::make_installed_hosttools_mock ffx)"
export FUCHSIA_DEVICE_NAME="foo-bar-baz-quux"
echo "::3%lo0" > "${ffx}.mock_stdout"
# Set up mocked replacements for system utils.
export PATH="${BT_TEMP_DIR}/bin:${PATH}"
cipd="${BT_TEMP_DIR}/bin/cipd"
unzip="${BT_TEMP_DIR}/bin/unzip"
}
# Test that prebuilt is run with no args works with new binary.
TEST_noargs_uses_prebuilt_new_binary() {
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/macos.zip"
BT_EXPECT ${fx} fdt
BT_EXPECT btf::does-mock-args-contain "${unzip}.mock_state" "-d ${BT_TEMP_DIR}/prebuilt/gui_tools/fuchsia_devtools/macos-extracted-temp"
else
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/fuchsia_devtools"
BT_EXPECT ${fx} fdt
fi
}
# Test that prebuilt is run with no args works with old binary.
TEST_noargs_uses_prebuilt_old_binary() {
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/macos/macos.zip"
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/linux/fuchsia_devtools"
BT_EXPECT ${fx} fdt
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
BT_EXPECT btf::does-mock-args-contain "${unzip}.mock_state" "-d ${BT_TEMP_DIR}/prebuilt/gui_tools/fuchsia_devtools/macos-extracted-temp"
fi
}
# Test that prebuilt fails if not downloaded.
TEST_prebuilt_unavailable() {
local out="${BT_TEMP_DIR}/_fx_test_output"
BT_EXPECT_FAIL ${fx} fdt > "${out}"
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${out}" "JIRI"
}
# Test that CIPD is used to fetch "latest" with the new binary.
TEST_uses_cipd_for_latest_new_binary() {
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/download/macos.zip"
BT_EXPECT ${fx} fdt latest
BT_EXPECT btf::does-mock-args-contain "${unzip}.mock_state" "-d ${BT_TEMP_DIR}/prebuilt/gui_tools/download/fuchsia_devtools/macos-extracted-temp"
else
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/download/fuchsia_devtools"
BT_EXPECT ${fx} fdt latest
fi
BT_EXPECT btf::does-mock-args-contain "${cipd}.mock_state" "ensure"
}
# Test that CIPD is used to fetch "latest" with the old binary.
TEST_uses_cipd_for_latest_old_binary() {
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/download/macos/macos.zip"
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/download/linux/fuchsia_devtools"
BT_EXPECT ${fx} fdt latest
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
BT_EXPECT btf::does-mock-args-contain "${unzip}.mock_state" "-d ${BT_TEMP_DIR}/prebuilt/gui_tools/download/fuchsia_devtools/macos-extracted-temp"
fi
BT_EXPECT btf::does-mock-args-contain "${cipd}.mock_state" "ensure"
}
# Test that fx-export-device-address fills in environment variables.
TEST_uses_fx_export_device_address() {
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
FDT="${BT_TEMP_DIR}/prebuilt/gui_tools/fuchsia_devtools/macos-extracted-temp/Fuchsia DevTools.app"
FDT_MOVED="${BT_TEMP_DIR}/prebuilt/gui_tools/fuchsia_devtools/macos-extracted/Fuchsia DevTools.app"
btf::make_mock "${BT_TEMP_DIR}/prebuilt/gui_tools/macos.zip"
btf::make_mock "${FDT}"
cat > "${BT_TEMP_DIR}/bin/open.mock_side_effects" <<EOF
exec "${FDT_MOVED}"
EOF
cat > "${FDT}.mock_side_effects" <<EOF
echo "\${FX_DEVICE_NAME}" >> "${FDT_MOVED}.fx_device_name"
echo "\${FX_DEVICE_ADDR}" >> "${FDT_MOVED}.fx_device_addr"
echo "\${FX_SSH_ADDR}" >> "${FDT_MOVED}.fx_ssh_addr"
echo "\${FX_SSH_PORT}" >> "${FDT_MOVED}.fx_ssh_port"
EOF
else
FDT="${BT_TEMP_DIR}/prebuilt/gui_tools/fuchsia_devtools"
btf::make_mock "${FDT}"
cat > "${FDT}.mock_side_effects" <<EOF
echo "\${FX_DEVICE_NAME}" >> "${FDT}.fx_device_name"
echo "\${FX_DEVICE_ADDR}" >> "${FDT}.fx_device_addr"
echo "\${FX_SSH_ADDR}" >> "${FDT}.fx_ssh_addr"
echo "\${FX_SSH_PORT}" >> "${FDT}.fx_ssh_port"
EOF
fi
BT_EXPECT ${fx} fdt
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
FDT="$FDT_MOVED"
fi
BT_EXPECT_FILE_CONTAINS "${FDT}.fx_device_name" "foo-bar-baz-quux"
BT_EXPECT_FILE_CONTAINS "${FDT}.fx_device_addr" "::3%lo0"
BT_EXPECT_FILE_CONTAINS "${FDT}.fx_ssh_addr" "[::3%lo0]"
BT_EXPECT_FILE_CONTAINS "${FDT}.fx_ssh_port" ""
}
BT_RUN_TESTS "$@"