| #!/bin/bash |
| # Copyright 2023 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 "get-flash-source" script. |
| |
| # Source platform.sh so that we can point to jq and include it as a runtime dependency. |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)/../../lib/platform.sh" || exit $? |
| |
| BT_FILE_DEPS=( |
| "scripts/fx" |
| "prebuilt/third_party/jq/${HOST_PLATFORM}/bin/jq" |
| "tools/devshell/get-flash-source" |
| "tools/devshell/jq.fx" |
| "tools/devshell/lib/fx-optional-features.sh" |
| "tools/devshell/lib/fx-cmd-locator.sh" |
| "tools/devshell/lib/vars.sh" |
| "tools/devshell/lib/platform.sh" |
| "tools/devshell/lib/generate-ssh-config.sh" |
| ) |
| |
| BT_MKDIR_DEPS=( |
| "out/default" |
| ) |
| |
| # A contrived "cpu" identifier that won't match $HOST_CPU. |
| export readonly CROSS_COMPILED_CPU="CROSS_COMPILED_CPU" |
| |
| declare build_mock |
| |
| BT_SET_UP() { |
| # Set up the testing framework. |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" |
| fx="$(btf::setup_fx)" |
| |
| # Set up mock build API modules. |
| cat > "${BT_TEMP_DIR}/out/default/args.json" <<EOF |
| { |
| "use_bazel_images_only": false, |
| "build_info_product": "core", |
| "build_info_board": "x64" |
| } |
| EOF |
| |
| cat > "${BT_TEMP_DIR}/out/default/images.json" <<EOF |
| [ |
| { |
| "label": "//build/images/flash:fastboot_manifest(//build/toolchain/fuchsia:x64)", |
| "name": "flash-manifest", |
| "path": "flash.json", |
| "type": "manifest" |
| } |
| ] |
| EOF |
| |
| cat > "${BT_TEMP_DIR}/out/default/product_bundles.json" <<EOF |
| [ |
| { |
| "label": "//build/images/fuchsia:product_bundle(//build/toolchain/fuchsia:x64)", |
| "name": "core.x64", |
| "path": "obj/build/images/fuchsia/product_bundle", |
| "product_version": "12.99991231.0.1", |
| "transfer_manifest_path": "obj/build/images/fuchsia/transfer.json", |
| "transfer_manifest_url": "file://obj/build/images/fuchsia/transfer.json" |
| }, |
| { |
| "json": "kernel.phys32/obj/zircon/kernel/phys/test/qemu-hello-world-test.linuxboot.product_bundle/product_bundle.json", |
| "label": "//zircon/kernel/phys/test:qemu-hello-world-test.linuxboot.product_bundle(//zircon/kernel/arch/x86/phys:kernel.phys32)", |
| "name": "qemu-hello-world-test.linuxboot", |
| "path": "kernel.phys32/obj/zircon/kernel/phys/test/qemu-hello-world-test.linuxboot.product_bundle", |
| "product_version": "16.99991231.0.1", |
| "transfer_manifest_path": "kernel.phys32/obj/zircon/kernel/phys/test/qemu-hello-world-test.linuxboot.transfer.json", |
| "transfer_manifest_url": "file://kernel.phys32/obj/zircon/kernel/phys/test/qemu-hello-world-test.linuxboot.transfer.json" |
| } |
| ] |
| EOF |
| |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/build" |
| } |
| |
| TEST_fx-get-flash-source-flash-manifest() { |
| BT_EXPECT_EQ "$(${fx} get-flash-source)" "flash-manifest:flash.json" |
| } |
| |
| TEST_fx-get-flash-source-flash-manifest-with-build() { |
| local build_args="${BT_TEMP_DIR}/build_args" |
| cat >"${BT_TEMP_DIR}/tools/devshell/build.mock_side_effects"<<INPUT |
| printf %s "\$@" > "${build_args}" |
| echo "Fake build output for testing - should be ignored" |
| INPUT |
| BT_EXPECT_EQ "$(${fx} get-flash-source --build)" "flash-manifest:flash.json" |
| BT_EXPECT_FILE_CONTAINS "${build_args}" "build/images/flash:fastboot_manifest" |
| } |
| |
| TEST_fx-get-flash-source-flash-manifest-with-build-and-toolchain() { |
| local build_args="${BT_TEMP_DIR}/build_args" |
| |
| # Change the toolchain value from the default |
| cat > "${BT_TEMP_DIR}/out/default/images.json" <<EOF |
| [ |
| { |
| "label": "//build/images/flash:fastboot_manifest(//unknown:toolchain)", |
| "name": "flash-manifest", |
| "path": "flash.json", |
| "type": "manifest" |
| } |
| ] |
| EOF |
| cat >"${BT_TEMP_DIR}/tools/devshell/build.mock_side_effects"<<INPUT |
| printf %s "\$@" > "${build_args}" |
| echo "Fake build output for testing - should be ignored" |
| INPUT |
| BT_EXPECT_EQ "$(${fx} get-flash-source --build)" "flash-manifest:flash.json" |
| BT_EXPECT_FILE_CONTAINS "${build_args}" "build/images/flash:fastboot_manifest" |
| } |
| |
| TEST_fx-get-flash-source-missing-flash-manifest() { |
| cat > "${BT_TEMP_DIR}/out/default/images.json" <<EOF |
| [ |
| ] |
| EOF |
| BT_EXPECT_FAIL "${fx}" get-flash-source 1>/dev/null 2>stderr |
| BT_EXPECT_FILE_CONTAINS_SUBSTRING stderr "Failed to find a flash manifest" |
| } |
| |
| TEST_fx-get-flash-source-product-bundle() { |
| cat > "${BT_TEMP_DIR}/out/default/args.json" <<EOF |
| { |
| "use_bazel_images_only": true, |
| "build_info_product": "core", |
| "build_info_board": "x64" |
| } |
| EOF |
| |
| BT_EXPECT_EQ "$(${fx} get-flash-source)" "product-bundle:obj/build/images/fuchsia/product_bundle" |
| } |
| |
| TEST_fx-get-flash-source-product-bundle-with-build() { |
| local build_args="${BT_TEMP_DIR}/build_args" |
| cat >"${BT_TEMP_DIR}/tools/devshell/build.mock_side_effects"<<INPUT |
| printf %s "\$@" > "${build_args}" |
| echo "Fake build output for testing - should be ignored" |
| INPUT |
| cat > "${BT_TEMP_DIR}/out/default/args.json" <<EOF |
| { |
| "use_bazel_images_only": true, |
| "build_info_product": "core", |
| "build_info_board": "x64" |
| } |
| EOF |
| BT_EXPECT_EQ "$(${fx} get-flash-source --build)" "product-bundle:obj/build/images/fuchsia/product_bundle" |
| BT_EXPECT_FILE_CONTAINS "${build_args}" "build/images/fuchsia:product_bundle" |
| } |
| |
| TEST_fx-get-flash-source-missing-product-bundle() { |
| cat > "${BT_TEMP_DIR}/out/default/args.json" <<EOF |
| { |
| "use_bazel_images_only": true |
| } |
| EOF |
| cat > "${BT_TEMP_DIR}/out/default/product_bundles.json" <<EOF |
| [ |
| ] |
| EOF |
| BT_EXPECT_FAIL "${fx}" get-flash-source 1>/dev/null 2>stderr |
| BT_EXPECT_FILE_CONTAINS_SUBSTRING stderr "Failed to find a product bundle" |
| } |
| |
| |
| BT_RUN_TESTS "$@" |