| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) - 2020 EfficiOS, inc |
| 4 | # |
| 5 | # This library is free software; you can redistribute it and/or modify it under |
| 6 | # the terms of the GNU Lesser General Public License as published by the Free |
| 7 | # Software Foundation; version 2.1 of the License. |
| 8 | # |
| 9 | # This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 12 | # details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Lesser General Public License |
| 15 | # along with this library; if not, write to the Free Software Foundation, Inc., |
| 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | |
| 18 | # Test the `lttng remove-trigger` command line interface. |
| 19 | |
| 20 | CURDIR="$(dirname "$0")" |
| 21 | TESTDIR="$CURDIR/../../.." |
| 22 | |
| 23 | # shellcheck source=../../../utils/utils.sh |
| 24 | source "$TESTDIR/utils/utils.sh" |
| 25 | |
| 26 | plan_tests 22 |
| 27 | |
| 28 | FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}" |
| 29 | |
| 30 | tmp_stdout=$(mktemp -t test_remove_triggers_cli_stdout.XXXXXX) |
| 31 | tmp_stderr=$(mktemp -t test_remove_triggers_cli_stderr.XXXXXX) |
| 32 | tmp_expected_stdout=$(mktemp -t test_remove_triggers_cli_expected_stdout.XXXXXX) |
| 33 | |
| 34 | uid=$(id --user) |
| 35 | gid=$(id --group) |
| 36 | |
| 37 | function list_triggers () |
| 38 | { |
| 39 | local test_name="$1" |
| 40 | local expected_stdout_file="$2" |
| 41 | |
| 42 | "${FULL_LTTNG_BIN}" list-triggers > "${tmp_stdout}" 2> "${tmp_stderr}" |
| 43 | ok $? "${test_name}: exit code is 0" |
| 44 | |
| 45 | diff -u "${expected_stdout_file}" "${tmp_stdout}" |
| 46 | ok $? "${test_name}: expected stdout" |
| 47 | |
| 48 | diff -u /dev/null "${tmp_stderr}" |
| 49 | ok $? "${test_name}: expected stderr" |
| 50 | } |
| 51 | |
| 52 | function remove_trigger () |
| 53 | { |
| 54 | local name="$1" |
| 55 | local test_name="remove trigger ${name}" |
| 56 | |
| 57 | "${FULL_LTTNG_BIN}" remove-trigger "${name}" > "${tmp_stdout}" 2> "${tmp_stderr}" |
| 58 | ok $? "${test_name}: exit code is 0" |
| 59 | |
| 60 | diff -u <(echo "Removed trigger \`${name}\`.") "${tmp_stdout}" |
| 61 | ok $? "${test_name}: expected stdout" |
| 62 | |
| 63 | diff -u /dev/null "${tmp_stderr}" |
| 64 | ok $? "${test_name}: expected stderr" |
| 65 | } |
| 66 | |
| 67 | function test_mi () |
| 68 | { |
| 69 | local tmp_stdout_raw |
| 70 | local tmp_expected_stdout |
| 71 | |
| 72 | # Concretely the code used to serialize a trigger object is the same as |
| 73 | # the one used by the list command. Here we simply validate that a |
| 74 | # simple trigger is correctly generated on removal for MI. |
| 75 | |
| 76 | tmp_stdout_raw=$(mktemp -t "tmp.${FUNCNAME[0]}_stdout.XXXXXX") |
| 77 | tmp_expected_stdout=$(mktemp -t "tmp.${FUNCNAME[0]}_expected_stdout.XXXXXX") |
| 78 | |
| 79 | diag "${FULL_LTTNG_BIN} --mi=xml remove-trigger" |
| 80 | |
| 81 | cat > "${tmp_expected_stdout}" <<- EOF |
| 82 | <?xml version="1.0" encoding="UTF-8"?> |
| 83 | <command xmlns="https://lttng.org/xml/ns/lttng-mi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://lttng.org/xml/ns/lttng-mi https://lttng.org/xml/schemas/lttng-mi/${MI_XSD_MAJOR_VERSION}/lttng-mi-${MI_XSD_MAJOR_VERSION}.${MI_XSD_MINOR_VERSION}.xsd" schemaVersion="${MI_XSD_MAJOR_VERSION}.${MI_XSD_MINOR_VERSION}"> |
| 84 | <name>remove-trigger</name> |
| 85 | <output> |
| 86 | <trigger> |
| 87 | <name>mi</name> |
| 88 | <owner_uid>${UID}</owner_uid> |
| 89 | <condition> |
| 90 | <condition_event_rule_matches> |
| 91 | <event_rule> |
| 92 | <event_rule_user_tracepoint> |
| 93 | <name_pattern>mi_aa</name_pattern> |
| 94 | <filter_expression>p == 2</filter_expression> |
| 95 | </event_rule_user_tracepoint> |
| 96 | </event_rule> |
| 97 | <capture_descriptors/> |
| 98 | </condition_event_rule_matches> |
| 99 | </condition> |
| 100 | <action> |
| 101 | <action_list> |
| 102 | <action> |
| 103 | <action_notify> |
| 104 | <rate_policy> |
| 105 | <rate_policy_every_n> |
| 106 | <interval>1</interval> |
| 107 | </rate_policy_every_n> |
| 108 | </rate_policy> |
| 109 | </action_notify> |
| 110 | </action> |
| 111 | </action_list> |
| 112 | </action> |
| 113 | </trigger> |
| 114 | </output> |
| 115 | <success>true</success> |
| 116 | </command> |
| 117 | EOF |
| 118 | |
| 119 | # Add a trigger |
| 120 | lttng_add_trigger_ok "mi" --condition event-rule-matches --name=mi_aa --type=user --filter 'p == 2' --action notify |
| 121 | |
| 122 | # Remove it |
| 123 | "${FULL_LTTNG_BIN}" --mi xml remove-trigger mi > "${tmp_stdout_raw}" 2> "${tmp_stderr}" |
| 124 | ok $? "remove-trigger mi: exit code is 0" |
| 125 | |
| 126 | # Pretty-fy xml before further test. |
| 127 | $XML_PRETTY < "${tmp_stdout_raw}" > "${tmp_stdout}" |
| 128 | |
| 129 | $MI_VALIDATE "${tmp_stdout}" |
| 130 | ok $? "remove-trigger mi is valid" |
| 131 | |
| 132 | diff -u "${tmp_expected_stdout}" "${tmp_stdout}" |
| 133 | ok $? "mi: expected stdout" |
| 134 | |
| 135 | diff -u "${tmp_stderr}" /dev/null |
| 136 | ok $? "mi: expected stderr" |
| 137 | |
| 138 | rm -f "${tmp_stdout_raw}" |
| 139 | } |
| 140 | |
| 141 | # shellcheck disable=SC2119 |
| 142 | start_lttng_sessiond_notap |
| 143 | |
| 144 | # Add a few triggers |
| 145 | lttng_add_trigger_ok "ABC" --condition event-rule-matches --name=aaa --type=user --filter 'p == 2' --action notify |
| 146 | lttng_add_trigger_ok "DEF" --condition event-rule-matches --type=user --action notify |
| 147 | |
| 148 | cat > "${tmp_expected_stdout}" <<- EOF |
| 149 | - name: ABC |
| 150 | owner uid: ${uid} |
| 151 | condition: event rule matches |
| 152 | rule: aaa (type: user tracepoint, filter: p == 2) |
| 153 | errors: none |
| 154 | actions: |
| 155 | notify |
| 156 | errors: none |
| 157 | errors: none |
| 158 | - name: DEF |
| 159 | owner uid: ${uid} |
| 160 | condition: event rule matches |
| 161 | rule: * (type: user tracepoint) |
| 162 | errors: none |
| 163 | actions: |
| 164 | notify |
| 165 | errors: none |
| 166 | errors: none |
| 167 | EOF |
| 168 | list_triggers "two triggers left" "${tmp_expected_stdout}" |
| 169 | |
| 170 | remove_trigger "ABC" |
| 171 | |
| 172 | cat > "${tmp_expected_stdout}" <<- EOF |
| 173 | - name: DEF |
| 174 | owner uid: ${uid} |
| 175 | condition: event rule matches |
| 176 | rule: * (type: user tracepoint) |
| 177 | errors: none |
| 178 | actions: |
| 179 | notify |
| 180 | errors: none |
| 181 | errors: none |
| 182 | EOF |
| 183 | list_triggers "one trigger left" "${tmp_expected_stdout}" |
| 184 | |
| 185 | remove_trigger "DEF" |
| 186 | |
| 187 | list_triggers "no triggers left" "/dev/null" |
| 188 | |
| 189 | test_mi |
| 190 | |
| 191 | # Cleanup |
| 192 | stop_lttng_sessiond_notap |
| 193 | rm -f "${tmp_stdout}" |
| 194 | rm -f "${tmp_stderr}" |
| 195 | rm -f "${tmp_expected_stdout}" |