| #!/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/generate-ssh-config.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" |
| metrics_done="${BT_TEMP_DIR}/metrics_done" |
| metrics_fifo="${BT_TEMP_DIR}/metrics_fifo" |
| 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 |
| mkfifo ${metrics_done} |
| mkfifo ${metrics_fifo} |
| } |
| |
| # Special version of metrics_init that allows waiting for the analytics/metrics |
| # Note: this can not be called from BT_SET_UP because somehow the TEST_xxx |
| # function is executed in a subshell of BT_SET_UP function, and closing a file |
| # descriptor in a subshell does not affect its parent. If metrics_init calls |
| # were moved from each TEST_xxx function into BT_SET_UP, metrics_wait will wait |
| # forever. |
| metrics_init() { |
| (_metrics-service <"${metrics_fifo}"; : >"${metrics_done}") & |
| exec 10>&- |
| exec 10>"${metrics_fifo}" |
| } |
| |
| metrics_wait(){ |
| exec 10>&- |
| read <"${metrics_done}" |
| } |
| |
| 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" |
| metrics_init |
| # Execute track-* command in subshell since they will close stdout and stderr |
| # which will confuse our bash test framework |
| (track-command-execution "${cmd}" "$arg") |
| (track-command-finished "200" "0" "${cmd}" "$arg") |
| metrics_wait |
| 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" |
| metrics_init |
| (track-command-execution "${cmd}" "$arg") |
| (track-command-finished "200" "0" "${cmd}" "$arg") |
| metrics_wait |
| 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" |
| metrics_init |
| (track-command-execution "${cmd}" "$arg") |
| (track-command-finished "200" "0" "${cmd}" "$arg") |
| metrics_wait |
| 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" |
| |
| metrics_init |
| (track-command-execution "${cmd}" "$arg") |
| (track-subcommand-custom-event "${cmd}" "${action}" "${label}") |
| (track-command-finished "200" "0" "${cmd}" "$arg") |
| metrics_wait |
| 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" |
| |
| metrics_init |
| (track-command-execution "${cmd}" "$arg") |
| (track-subcommand-custom-event "${cmd}" "${action}" "${label}") |
| (track-command-finished "200" "0" "${cmd}" "$arg") |
| metrics_wait |
| verify_execution_event ${cmd} |
| BT_EXPECT_FILE_DOES_NOT_CONTAIN_SUBSTRING "${metrics_log}" "\"name\":\"custom\"" |
| } |
| |
| BT_RUN_TESTS "$@" |