blob: 52b8511654a06a97bbe8eabfd54e6b98dc275257 [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.
BT_FILE_DEPS=(
"scripts/fx"
"tools/devshell/lib/fx-cmd-locator.sh"
"tools/devshell/lib/fx-optional-features.sh"
"tools/devshell/lib/vars.sh"
"tools/devshell/lib/platform.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-amber-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/system_image.meta/meta.far.merkle
mkdir -p "$(dirname "$system_image_merkle")"
echo "$from_build" > "$system_image_merkle"
}
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:packages" "build/images:update.meta"
# 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 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 "$@"