| #!/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 the helper functions defined in //tools/devshell/lib/fvm.sh | |
| BT_FILE_DEPS=( | |
| "tools/devshell/lib/fvm.sh" | |
| "tools/devshell/lib/fx-cmd-locator.sh" | |
| "tools/devshell/lib/fx-optional-features.sh" | |
| "tools/devshell/lib/platform.sh" | |
| "tools/devshell/lib/vars.sh" | |
| ) | |
| BT_SET_UP() { | |
| source "${BT_TEMP_DIR}/tools/devshell/lib/fvm.sh" | |
| source "${BT_TEMP_DIR}/tools/devshell/lib/vars.sh" | |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" | |
| FUCHSIA_BUILD_DIR="${BT_TEMP_DIR}/out/default" | |
| mkdir -p "${FUCHSIA_BUILD_DIR}" | |
| fvm_raw_rel="fvm.blk" | |
| fvm_sparse_rel="fvm.sparse.blk" | |
| fvm_fastboot_rel="fvm.fastboot.blk" | |
| touch "${FUCHSIA_BUILD_DIR}/${fvm_raw_rel}" | |
| touch "${FUCHSIA_BUILD_DIR}/${fvm_sparse_rel}" | |
| touch "${FUCHSIA_BUILD_DIR}/${fvm_fastboot_rel}" | |
| } | |
| TEST_fx-fvm-find-raw-source() { | |
| # If all FVM files are present, we should use raw by default. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | |
| if [[ "$@" == "--name storage-full --allow-empty images" ]] ; then | |
| echo "fvm.blk" | |
| elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | |
| echo "fvm.sparse.blk" | |
| elif [[ "$@" == "--name fvm.fastboot --allow-empty images" ]] ; then | |
| echo "fvm.fastboot.blk" | |
| fi | |
| EOF | |
| BT_ASSERT_EQ "$(fx-fvm-find-raw-source)" "${fvm_raw_rel}" | |
| } | |
| TEST_fx-fvm-find-raw-source_no_raw_path() { | |
| # If the raw FVM path is missing, fall back to sparse FVM. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | |
| if [[ "$@" == "--name storage-full --allow-empty images" ]] ; then | |
| echo "" | |
| elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | |
| echo "fvm.sparse.blk" | |
| elif [[ "$@" == "--name fvm.fastboot --allow-empty images" ]] ; then | |
| echo "fvm.fastboot.blk" | |
| fi | |
| EOF | |
| BT_ASSERT_EQ "$(fx-fvm-find-raw-source)" "${fvm_sparse_rel}" | |
| } | |
| TEST_fx-fvm-find-raw-source_no_raw_or_sparse_path() { | |
| # If the raw and sparse FVM paths are missing, fall back to fastboot FVM. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | |
| if [[ "$@" == "--name storage-full --allow-empty images" ]] ; then | |
| echo "" | |
| elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | |
| echo "" | |
| elif [[ "$@" == "--name fvm.fastboot --allow-empty images" ]] ; then | |
| echo "fvm.fastboot.blk" | |
| fi | |
| EOF | |
| BT_ASSERT_EQ "$(fx-fvm-find-raw-source)" "${fvm_fastboot_rel}" | |
| } | |
| TEST_fx-fvm-find-raw-source_no_paths() { | |
| # If all FVM paths are missing, output should be empty. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | |
| if [[ "$@" == "--name storage-full --allow-empty images" ]] ; then | |
| echo "" | |
| elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | |
| echo "" | |
| elif [[ "$@" == "--name fvm.fastboot --allow-empty images" ]] ; then | |
| echo "" | |
| fi | |
| EOF | |
| BT_ASSERT_EQ "$(fx-fvm-find-raw-source)" "" | |
| } | |
| TEST_fx-fvm-find-raw-source_no_raw_file() { | |
| # If the raw FVM file is missing, fall back to sparse FVM. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | |
| if [[ "$@" == "--name storage-full --allow-empty images" ]] ; then | |
| echo "fvm.blk" | |
| elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | |
| echo "fvm.sparse.blk" | |
| elif [[ "$@" == "--name fvm.fastboot --allow-empty images" ]] ; then | |
| echo "fvm.fastboot.blk" | |
| fi | |
| EOF | |
| rm -f "${FUCHSIA_BUILD_DIR}/${fvm_raw_rel}" | |
| BT_ASSERT_EQ "$(fx-fvm-find-raw-source)" "${fvm_sparse_rel}" | |
| } | |
| TEST_fx-fvm-find-raw-source_no_raw_or_sparse_files() { | |
| # If the raw and sparse FVM files are missing, fall back to fastboot FVM. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | |
| if [[ "$@" == "--name storage-full --allow-empty images" ]] ; then | |
| echo "fvm.blk" | |
| elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | |
| echo "fvm.sparse.blk" | |
| elif [[ "$@" == "--name fvm.fastboot --allow-empty images" ]] ; then | |
| echo "fvm.fastboot.blk" | |
| fi | |
| EOF | |
| rm -f "${FUCHSIA_BUILD_DIR}/${fvm_raw_rel}" | |
| rm -f "${FUCHSIA_BUILD_DIR}/${fvm_sparse_rel}" | |
| BT_ASSERT_EQ "$(fx-fvm-find-raw-source)" "${fvm_fastboot_rel}" | |
| } | |
| TEST_fx-fvm-find-raw-source_no_files() { | |
| # If all FVM files are missing, output should be empty. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/list-build-artifacts.mock_side_effects" << "EOF" | |
| if [[ "$@" == "--name storage-full --allow-empty images" ]] ; then | |
| echo "fvm.blk" | |
| elif [[ "$@" == "--name storage-sparse --allow-empty images" ]] ; then | |
| echo "fvm.sparse.blk" | |
| elif [[ "$@" == "--name fvm.fastboot --allow-empty images" ]] ; then | |
| echo "fvm.fastboot.blk" | |
| fi | |
| EOF | |
| rm -f "${FUCHSIA_BUILD_DIR}/${fvm_raw_rel}" | |
| rm -f "${FUCHSIA_BUILD_DIR}/${fvm_sparse_rel}" | |
| rm -f "${FUCHSIA_BUILD_DIR}/${fvm_fastboot_rel}" | |
| BT_ASSERT_EQ "$(fx-fvm-find-raw-source)" "" | |
| } | |
| BT_RUN_TESTS "$@" |