| #!/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 emu | 
 |  | 
 | BT_FILE_DEPS=( | 
 |   "scripts/fx" | 
 |   "tools/devshell/emu" | 
 |   "tools/devshell/get-build-dir" | 
 |   "tools/devshell/lib/fvm.sh" | 
 |   "tools/devshell/lib/fx-cmd-locator.sh" | 
 |   "tools/devshell/lib/fx-optional-features.sh" | 
 |   "tools/devshell/lib/image_build_vars.sh" | 
 |   "tools/devshell/lib/platform.sh" | 
 |   "tools/devshell/lib/vars.sh" | 
 | ) | 
 |  | 
 | declare fx | 
 |  | 
 | BT_SET_UP() { | 
 |   source "${BT_TEMP_DIR}/tools/devshell/lib/vars.sh" | 
 |   source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" | 
 |   fx="$(btf::setup_fx)" | 
 |   btf::make_mock "${PREBUILT_AEMU_DIR}/emulator" | 
 |   btf::make_mock "${BT_TEMP_DIR}/out/default/image_paths.sh" | 
 |  | 
 |   btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | 
 |   btf::make_mock "${BT_TEMP_DIR}/out/default/host/zbi" | 
 |   fvm=${BT_TEMP_DIR}/out/default/host/fvm | 
 |   btf::make_mock "${fvm}" | 
 |  | 
 |   cat >> "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | 
 | if [[ "$@" == "--name zbi --expect-one tools" ]] ; then | 
 |   echo "host/zbi" | 
 | elif [[ "$@" == "--name fvm --expect-one tools" ]] ; then | 
 |   echo "host/fvm" | 
 | fi | 
 | EOF | 
 | } | 
 |  | 
 | # Test that -c arguments are added to the end of the kernel command line. | 
 | TEST_kernel_cmdline_append() { | 
 |   BT_EXPECT ${fx} emu --headless -a off --experiment-arm64 -c foobar | 
 |   source "${PREBUILT_AEMU_DIR}/emulator.mock_state" | 
 |  | 
 |   # The last arg will contain the kernel command line string. | 
 |   local cmdline_string="${BT_MOCK_ARGS[${#BT_MOCK_ARGS[@]}-1]}" | 
 |  | 
 |   # Convert the string to an array so we can easily examine the last element. | 
 |   local cmdline_array=($cmdline_string) | 
 |   BT_EXPECT_EQ "${cmdline_array[${#cmdline_array[@]}-1]}" "foobar" | 
 | } | 
 |  | 
 | # Test that -A argument works. | 
 | TEST_arch_arm64() { | 
 |   BT_EXPECT ${fx} emu --headless -a off --experiment-arm64 -A arm64 | 
 |   source "${PREBUILT_AEMU_DIR}/emulator.mock_state" | 
 |  | 
 |   local machine_arg="$(get_machine_arg)" | 
 |   BT_EXPECT_EQ "${machine_arg}" "virt" | 
 | } | 
 |  | 
 | # Test that -A argument works. | 
 | TEST_arch_x64() { | 
 |   BT_EXPECT ${fx} emu --headless -a off -A x64 | 
 |   source "${PREBUILT_AEMU_DIR}/emulator.mock_state" | 
 |  | 
 |   local machine_arg="$(get_machine_arg)" | 
 |   BT_EXPECT_EQ "${machine_arg}" "q35" | 
 | } | 
 |  | 
 | get_machine_arg() { | 
 |   for i in "${!BT_MOCK_ARGS[@]}"; do | 
 |     if [[ "${BT_MOCK_ARGS[$i]}" == "-machine" ]]; then | 
 |       echo ${BT_MOCK_ARGS[(($i + 1))]} | 
 |       return | 
 |     fi | 
 |   done | 
 |   echo "invalid" | 
 | } | 
 |  | 
 | # Test creating a raw FVM image on-demand. | 
 | TEST_create_fvm() { | 
 |   # Fake that we have a sparse FVM image to use as a source. | 
 |   touch "${BT_TEMP_DIR}/out/default/fvm.blk" | 
 |   touch "${BT_TEMP_DIR}/out/default/fvm.blk.decompressed" | 
 |  | 
 |   cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | 
 | if [[ "$@" == "--name zbi --expect-one tools" ]] ; then | 
 |   echo "host/zbi" | 
 | elif [[ "$@" == "--name fvm --expect-one tools" ]] ; then | 
 |   echo "host/fvm" | 
 | elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | 
 |   echo "fvm.blk" | 
 | fi | 
 | EOF | 
 |  | 
 |   # We need to mock out 2 `fvm` tool behaviors: | 
 |   #  1. "decompress" should create the requested file | 
 |   #  2. "extend" should save the file path so we can verify it's passed as | 
 |   #     an emulator arg later | 
 |   local created_fvm_path_file="${BT_TEMP_DIR}/out/default/created-fvm-path" | 
 |   cat > "${fvm}.mock_side_effects" << EOF | 
 | if [[ \$2 == "decompress" ]]; then | 
 |   touch \$1 | 
 | elif [[ \$2 == "extend" ]]; then | 
 |   echo \$1 > "${created_fvm_path_file}" | 
 | fi | 
 | EOF | 
 |  | 
 |   BT_EXPECT ${fx} emu --headless -a off | 
 |  | 
 |   local created_fvm_path=$(cat "${created_fvm_path_file}") | 
 |   BT_EXPECT btf::does-mock-args-contain \ | 
 |       "${PREBUILT_AEMU_DIR}/emulator.mock_state" \ | 
 |       "-drive" "file=${created_fvm_path},format=raw,if=none,id=vdisk" \ | 
 |       "-device" "virtio-blk-pci,drive=vdisk" | 
 | } | 
 |  | 
 | BT_RUN_TESTS "$@" |