blob: 5bf6d9347a2d5739f17dfaf86d6d1d6bc1837694 [file] [log] [blame]
#!/bin/bash
# Copyright 2019 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.
readonly SERVE_UPDATES_PORT=8083
BT_FILE_DEPS=(
"out/default/host_x64/pm"
"tools/devshell/lib/vars.sh"
"tools/devshell/lib/fx-optional-features.sh"
"tools/devshell/tests/subcommands/data/fx_serve_update_test/testpackage.cmx"
"tools/devshell/tests/subcommands/data/fx_serve_update_test/testpackage.json"
"tools/devshell/tests/subcommands/data/fx_serve_update_test/testpackage.manifest"
"tools/devshell/serve-updates"
)
# Build a fake gn to satisfy vars.sh and some other commands to satisfy
# serve-updates.
BT_MOCKED_TOOLS=(
"tools/devshell/add-update-source"
"tools/devshell/shell"
)
BT_SET_UP() {
declare -i TEST_SERVE_UPDATES_PID=
declare TEST_WITH_MOCK=true
# Process optional test arguments:
# fx run-bash-test fx_serve_update_test -- --nomock
set -- "${BT_TEST_ARGS[@]}"
while (( $# > 0 )); do
local opt="$1"; shift
case "${opt}" in
--nomock)
TEST_WITH_MOCK=false
;;
*)
btf::stderr "Invalid test arguments after ' -- ': $@"
btf::stderr "Supported option(s):"
btf::stderr " --nomock (connect to a live server, instead of the default mock server)"
return 1
esac
done
FUCHSIA_DIR="${BT_TEMP_DIR}"
FUCHSIA_BUILD_DIR="${FUCHSIA_DIR}/out/default"
FUCHSIA_DEVSHELL_DIR="${FUCHSIA_DIR}/tools/devshell"
readonly PM="${FUCHSIA_BUILD_DIR}/host_x64/pm"
readonly REPODIR="${FUCHSIA_BUILD_DIR}/amber-files"
# Fake fuchsia dir configuration.
echo "out/default" > "${FUCHSIA_DIR}/.fx-build-dir"
touch "${FUCHSIA_BUILD_DIR}/args.gn"
touch "${FUCHSIA_BUILD_DIR}/fx.config"
if $TEST_WITH_MOCK; then
local -r serve_updates="${FUCHSIA_DIR}/tools/devshell/serve-updates"
btf::make_mock "${serve_updates}"
# When the mock serve-updates is called, serve a mock json file on the port
local nc_opts=( -l ${SERVE_UPDATES_PORT} )
if [[ "$(uname -s)" != "Darwin" ]]; then
nc_opts+=( -q 0 )
fi
cat > "${serve_updates}.mock_side_effects" <<EOF
while [[ ! -e ${REPODIR}/repository/targets.json ]]; do
sleep 1
done
nc ${nc_opts[@]} 2>&1 >/dev/null <<EOHTTP
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: netcat!
\$(cat ${REPODIR}/repository/targets.json)
EOHTTP
[[ $? -eq 0 ]] || exit $?
EOF
fi
}
BT_TEAR_DOWN() {
if [[ -n "${TEST_SERVE_UPDATES_PID}" ]]; then
if kill -0 "${TEST_SERVE_UPDATES_PID}" 2> /dev/null; then
kill -TERM "${TEST_SERVE_UPDATES_PID}" 2> /dev/null
wait "${TEST_SERVE_UPDATES_PID}" 2> /dev/null
fi
fi
}
_get_targets() {
curl -s http://127.0.0.1:${SERVE_UPDATES_PORT}/targets.json? > targets.json
return $?
}
TEST_fx_serve_updates() {
# The pm tool needs to exist.
BT_ASSERT_FILE_EXISTS "${PM}"
cd data/fx_serve_update_test
# Build the package from the manifest.
"${PM}" -o testpackage -m testpackage.manifest build
# Archive the package to a .far.
(cd testpackage; "${PM}" archive)
# Generate an empty repo and fill it with the .far.
"${PM}" newrepo -repo "${REPODIR}"
"${PM}" publish -a -f testpackage/testpackage-0.far -r "${REPODIR}"
BT_ASSERT_FILE_EXISTS "${REPODIR}/repository/targets.json"
# Fire up fx serve-updates. Note, test output is captured, so re-route background task output
# to stderr to avoid hanging.
>&2 "${FUCHSIA_DEVSHELL_DIR}/serve-updates" &
TEST_SERVE_UPDATES_PID=$!
# Request the list of targets (but wait if the server isn't alive yet).
local -i max_tries=10
local -i try_count=1
local -i status=0
_get_targets
status=$?
while (( $status != 0 && try_count < max_tries )); do
sleep 0.1
_get_targets
status=$?
: $(( try_count++ ))
done
BT_ASSERT_GOOD_STATUS "${status}" "Failed to get targets.json after ${max_tries} attempts. Last status was '${status}'."
# Confirm testpackage is one of the targets.
BT_EXPECT_FILE_CONTAINS_SUBSTRING targets.json "testpackage/0"
}
BT_RUN_TESTS "$@"