| #!/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. |
| # |
| # Source platform.sh so that we can point to jq and include it as a runtime dependency. We need to |
| # do it in a subfunction since platform.sh defines readonly variables, and it gets re-sourced to |
| # avoid conflicts. |
| function read_host_platform() { |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)/../../lib/platform.sh" || exit $? |
| echo "$HOST_PLATFORM" |
| } |
| |
| BT_FILE_DEPS=( |
| "prebuilt/third_party/jq/$(read_host_platform)/bin/jq" |
| "scripts/fx" |
| "tools/devshell/jq.fx" |
| "tools/devshell/lib/fx-cmd-locator.sh" |
| "tools/devshell/lib/fx-optional-features.sh" |
| "tools/devshell/lib/generate-ssh-config.sh" |
| "tools/devshell/lib/platform.sh" |
| "tools/devshell/lib/vars.sh" |
| "tools/devshell/ota" |
| ) |
| |
| BT_MOCKED_TOOLS=( |
| "tools/devshell/build" |
| "tools/devshell/wait" |
| "tools/devshell/shell" |
| "tools/devshell/lib/updates.sh" |
| ) |
| |
| declare fx build check_package_server trigger_update commit_update |
| |
| BT_SET_UP() { |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" |
| fx="$(btf::setup_fx)" |
| source "${BT_TEMP_DIR}/tools/devshell/lib/vars.sh" |
| fx-config-read |
| build="${BT_TEMP_DIR}/tools/devshell/build" |
| check_package_server="${BT_TEMP_DIR}/check_package_server" |
| trigger_update="${BT_TEMP_DIR}/trigger_update" |
| commit_update="${BT_TEMP_DIR}/commit_update" |
| btf::make_mock "$check_package_server" |
| btf::make_mock "$trigger_update" |
| btf::make_mock "$commit_update" |
| local updateslib="${BT_TEMP_DIR}/tools/devshell/lib/updates.sh" |
| |
| cat > "${updateslib}.mock_side_effects" <<EOF |
| function check-for-package-server { $check_package_server ; } |
| EOF |
| cat > "${BT_TEMP_DIR}/tools/devshell/shell.mock_side_effects" <<EOF |
| if [[ "\$1 \$2" == "update check-now" ]]; then |
| $trigger_update "\$@" |
| return |
| elif [[ "\$1 \$2" == "update wait-for-commit" ]]; then |
| $commit_update "\$@" |
| return |
| fi |
| EOF |
| } |
| |
| setup_versions_after_update() { |
| local from_device="$1" |
| local from_build="$2" |
| cat >> "${BT_TEMP_DIR}/tools/devshell/shell.mock_side_effects" <<EOF |
| if [[ "\$1" == "read ver"* ]]; then |
| echo "$from_device" |
| return 0 |
| fi |
| EOF |
| local system_image_merkle="${FUCHSIA_BUILD_DIR}"/obj/build/images/fuchsia/fuchsia/base/base.merkle |
| mkdir -p "${BT_TEMP_DIR}/out/default/amber-files/repository" |
| cat >> "${BT_TEMP_DIR}/out/default/amber-files/repository/targets.json" <<EOF |
| { |
| "signed": { |
| "targets": { |
| "system_image/0": { |
| "custom": { |
| "merkle": "${from_build}" |
| } |
| } |
| } |
| } |
| } |
| EOF |
| } |
| |
| TEST_ota_incr_noargs() { |
| echo "0" > "${check_package_server}.mock_status" # package server is running |
| setup_versions_after_update "1" "1" |
| |
| local out="${BT_TEMP_DIR}/_fx_ota_output" |
| BT_EXPECT ${fx} --enable=incremental ota > "$out" 2>&1 |
| # build was called with the minimal targets required for incremental ota |
| btf::expect-mock-args "${build}" "build/images/updates" |
| # update was triggered |
| BT_ASSERT_FILE_EXISTS "${trigger_update}.mock_state" |
| # update was committed |
| BT_ASSERT_FILE_EXISTS "${commit_update}.mock_state" |
| } |
| |
| TEST_ota_noincr_noargs() { |
| echo "0" > "${check_package_server}.mock_status" # package server is running |
| setup_versions_after_update "1" "1" |
| |
| local out="${BT_TEMP_DIR}/_fx_ota_output" |
| BT_EXPECT ${fx} --disable=incremental ota > "$out" 2>&1 |
| # build was not called - this is the legacy behavior before incremental |
| BT_ASSERT_FILE_DOES_NOT_EXIST "${build}.mock_state" |
| # update was triggered |
| BT_ASSERT_FILE_EXISTS "${trigger_update}.mock_state" |
| # update was committed |
| BT_ASSERT_FILE_EXISTS "${commit_update}.mock_state" |
| } |
| |
| TEST_ota_nobuild() { |
| echo "0" > "${check_package_server}.mock_status" # package server is running |
| setup_versions_after_update "1" "1" |
| |
| local out="${BT_TEMP_DIR}/_fx_ota_output" |
| BT_EXPECT ${fx} ota --no-build > "$out" 2>&1 |
| # build was not called |
| BT_ASSERT_FILE_DOES_NOT_EXIST "${build}.mock_state" |
| # update was triggered |
| BT_ASSERT_FILE_EXISTS "${trigger_update}.mock_state" |
| # update was committed |
| BT_ASSERT_FILE_EXISTS "${commit_update}.mock_state" |
| } |
| |
| TEST_ota_update_failed() { |
| echo "0" > "${check_package_server}.mock_status" # package server is running |
| setup_versions_after_update "1" "2" |
| |
| local ffx="$(btf::make_installed_hosttools_mock ffx)" |
| cat > "${ffx}.mock_side_effects" << "EOF" |
| if [[ "$@" == "ffx component select capability fuchsia.update.Manager" ]]; then |
| echo "/core/system-update/omaha-client-service" |
| else |
| echo "unknown command $@" 1>&2 |
| exit 1 |
| fi |
| EOF |
| |
| local out="${BT_TEMP_DIR}/_fx_ota_output" |
| BT_EXPECT_FAIL ${fx} ota --no-build > "$out" 2>&1 |
| # update was not triggered |
| BT_ASSERT_FILE_EXISTS "${trigger_update}.mock_state" |
| # update was not committed |
| BT_ASSERT_FILE_EXISTS "${commit_update}.mock_state" |
| } |
| |
| TEST_ota_nopackageserver() { |
| echo "1" > "${check_package_server}.mock_status" # package server is not running |
| |
| local out="${BT_TEMP_DIR}/_fx_ota_output" |
| # ota fails because package server is not running |
| BT_EXPECT_FAIL ${fx} ota > "$out" 2>&1 |
| |
| # build was not called |
| BT_ASSERT_FILE_DOES_NOT_EXIST "${build}.mock_state" |
| } |
| |
| BT_RUN_TESTS "$@" |
| |