blob: 8f44b497a923ffeb847a029f4392b8bb03a1fe67 [file] [log] [blame] [edit]
#!/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 expected behavior of metrics collection
# Source vars.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/vars.sh || exit $?
BT_FILE_DEPS=(
"prebuilt/third_party/jq/${HOST_PLATFORM}/bin/jq"
"tools/devshell/jq.fx"
"scripts/fx"
"tools/devshell/metrics"
"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/lib/metrics.sh"
"tools/devshell/lib/style.sh"
)
BT_MOCKED_TOOLS=(
# commands defined in tools/devshell/lib/metrics.sh constants:
"tools/devshell/emu"
"tools/devshell/set"
"tools/devshell/shell"
"tools/devshell/fidlcat"
"tools/devshell/run-host-tests"
"tools/devshell/build"
"tools/devshell/test"
# this is a fake command that is a substring of a valid command
"tools/devshell/fidl"
)
declare fx mycurl metrics_log
BT_SET_UP() {
local mock_py_path="prebuilt/third_party/python3/${HOST_PLATFORM}/bin/python3"
source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh"
fx="$(btf::setup_fx_with_metrics)"
mycurl="$(btf::make_mock_binary curl)"
btf::add_binary_to_path "${mycurl}"
btf::make_mock ${BT_TEMP_DIR}/${mock_py_path}
echo "1234567890000000" > "${BT_TEMP_DIR}/${mock_py_path}.mock_stdout"
metrics_log="${BT_TEMP_DIR}/metrics_log"
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/.fx-metrics-config"
source "${BT_TEMP_DIR}/tools/devshell/lib/vars.sh"
source "${BT_TEMP_DIR}/tools/devshell/lib/metrics.sh"
metrics-write-config 1 TESTUUID "${metrics_log}"
metrics-read-config
}
verify_execution_event() {
local cmd="$1"
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"name\":\"invoke\""
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"subcommand\":\"${cmd}\""
}
TEST_metrics_no_arguments_tracking() {
local cmd="build"
local arg="something"
track-command-execution "${cmd}" "$arg"
track-command-finished "200" "0" "${cmd}" "$arg"
verify_execution_event "${cmd}"
# the "build" command does not track arguments:
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"args\":\"\""
}
TEST_metrics_track_specific_arguments() {
local cmd="shell"
local subcmd="uname"
local arg="$subcmd blah"
track-command-execution "${cmd}" "$arg"
track-command-finished "200" "0" "${cmd}" "$arg"
verify_execution_event ${cmd}
# The "shell" command tracks one subcommand, let's verify:
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"args\":\"${subcmd}\""
}
TEST_metrics_track_unknown_arguments() {
local cmd="shell"
local subcmd="\$mandragora"
local replacement_text="\$unknown_subcommand"
local arg="$subcmd blah"
track-command-execution "${cmd}" "$arg"
track-command-finished "200" "0" "${cmd}" "$arg"
verify_execution_event ${cmd}
# The "shell" command tracks one subcommand, let's verify:
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"args\":\"${replacement_text}\""
}
TEST_metrics_custom_track() {
local cmd="test"
local action="myaction"
local label="mylabel"
track-command-execution "${cmd}" "$arg"
track-subcommand-custom-event "${cmd}" "${action}" "${label}"
track-command-finished "200" "0" "${cmd}" "$arg"
verify_execution_event ${cmd}
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"name\":\"custom\""
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"action\":\"${action}\""
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${metrics_log}" "\"label\":\"${label}\""
}
TEST_metrics_no_custom_track() {
local cmd="build"
local action="myaction"
local label="mylabel"
track-command-execution "${cmd}" "$arg"
track-subcommand-custom-event "${cmd}" "${action}" "${label}"
track-command-finished "200" "0" "${cmd}" "$arg"
verify_execution_event ${cmd}
BT_EXPECT_FILE_DOES_NOT_CONTAIN_SUBSTRING "${metrics_log}" "\"name\":\"custom\""
}
BT_RUN_TESTS "$@"