Fix: trimmer: use regexes to parse dates and times
[babeltrace.git] / tests / cli / test_trimmer
1 #!/bin/bash
2 #
3 # Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License, version 2 only, as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, write to the Free Software Foundation, Inc., 51
16 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 SH_TAP=1
19
20 if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then
21 UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
22 else
23 UTILSSH="$(dirname "$0")/../utils/utils.sh"
24 fi
25
26 # shellcheck source=../utils/utils.sh
27 source "$UTILSSH"
28
29 TRACE_PATH="${BT_CTF_TRACES_PATH}/succeed/wk-heartbeat-u/"
30
31 NUM_TESTS=118
32
33 plan_tests $NUM_TESTS
34
35 tmp_out=$(mktemp)
36 tmp_err=$(mktemp)
37
38
39 # Run Babeltrace with some command line arguments, verify exit status and
40 # number of output events (i.e. number of output lines)
41 #
42 # Arguments:
43 #
44 # $1: expected number of events
45 # $2: test description
46 # remaining arguments: command-line arguments to pass to Babeltrace
47
48 function expect_success()
49 {
50 local expected_num_events="$1"
51 local msg="$2"
52 shift 2
53
54 "${BT_TESTS_BT2_BIN}" "${TRACE_PATH}" "$@" 2>/dev/null > "${tmp_out}"
55 ok $? "trimmer: ${msg}: exit status"
56
57 num_events=$(wc -l < "${tmp_out}")
58 # Use bash parameter expansion to strip spaces added by BSD 'wc' on macOs and Solaris
59 is "${num_events// /}" "${expected_num_events}" "trimmer: ${msg}: number of events (${expected_num_events})"
60 }
61
62 # Run Babeltrace with some command line arguments, verify that the exit status
63 # is not 0 and that the error message contains a given string.
64 #
65 # Arguments:
66 #
67 # $1: a string expected to be found in the error message
68 # $2: test description
69 # remaining arguments: command-line arguments to pass to Babeltrace
70
71 function expect_failure()
72 {
73 local expected_err_string="$1"
74 local msg="$2"
75 shift 2
76
77 # We check the error message logged by the trimmer plugin, set the env
78 # var necessary for it to log errors.
79 BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL=E "${BT_TESTS_BT2_BIN}" "${TRACE_PATH}" "$@" 2> "${tmp_err}" > "${tmp_out}"
80 isnt $? 0 "trimmer: ${msg}: exit status"
81
82 num_events=$(wc -l < "${tmp_out}")
83 is "${num_events}" 0 "trimmer: ${msg}: number of events (0)"
84
85 stderr="$(cat "${tmp_err}")"
86 # "like" doesn't like when the passed text is empty.
87 if [ -n "${stderr}" ]; then
88 like "${stderr}" "${expected_err_string}" "trimmer: ${msg}: error message"
89 else
90 fail "trimmer: ${msg}: error message"
91 diag "Nothing was output on stderr".
92 fi
93
94 }
95
96 expect_success 18 "--begin, GMT relative timestamps" \
97 --clock-gmt --begin 17:48:17.587029529
98 expect_success 9 "--end, GMT relative timestamps" \
99 --clock-gmt --end 17:48:17.588680018
100 expect_success 7 "--begin and --end, GMT relative timestamps" \
101 --clock-gmt --begin 17:48:17.587029529 --end 17:48:17.588680018
102 expect_success 0 "--begin, out of range, GMT relative timestamps" \
103 --clock-gmt --begin 18:48:17.587029529
104 expect_success 0 "--end, out of range, GMT relative timestamps" \
105 --clock-gmt --end 16:48:17.588680018
106
107 expect_success 18 "--begin, GMT absolute timestamps" \
108 --clock-gmt --begin "2012-10-29 17:48:17.587029529"
109 expect_success 9 "--end, GMT absolute timestamps" \
110 --clock-gmt --end "2012-10-29 17:48:17.588680018"
111 expect_success 7 "--begin and --end, GMT absolute timestamps" \
112 --clock-gmt --begin "2012-10-29 17:48:17.587029529" --end "2012-10-29 17:48:17.588680018"
113 expect_success 0 "--begin, out of range, GMT absolute timestamps" \
114 --clock-gmt --begin "2012-10-29 18:48:17.587029529"
115 expect_success 0 "--begin, out of range, GMT absolute timestamps" \
116 --clock-gmt --end "2012-10-29 16:48:17.588680018"
117
118 export TZ=EST
119
120 expect_success 18 "--begin, EST relative timestamps" \
121 --begin "12:48:17.587029529"
122 expect_success 9 "--end, EST relative timestamps" \
123 --end "12:48:17.588680018"
124 expect_success 7 "--begin and --end, EST relative timestamps" \
125 --begin "12:48:17.587029529" --end "12:48:17.588680018"
126 expect_success 0 "--begin, out of range, EST relative timestamps" \
127 --begin "13:48:17.587029529"
128 expect_success 0 "--end, out of range, EST relative timestamps" \
129 --end "11:48:17.588680018"
130
131 expect_success 18 "--begin, EST absolute timestamps" \
132 --begin "2012-10-29 12:48:17.587029529"
133 expect_success 9 "--end, EST absolute timestamps" \
134 --end "12:48:17.588680018"
135 expect_success 7 "--begin and --end, EST absolute timestamps" \
136 --begin "2012-10-29 12:48:17.587029529" --end "2012-10-29 12:48:17.588680018"
137 expect_success 0 "--begin, out of range, EST absolute timestamps" \
138 --begin "2012-10-29 13:48:17.587029529"
139 expect_success 0 "--end, out of range, EST absolute timestamps" \
140 --end "2012-10-29 11:48:17.588680018"
141
142 # Check various formats.
143 #
144 # We sometimes apply a clock offset to make the events of the trace span two
145 # different seconds or minutes.
146
147 expect_success 13 "date time format: partial nanosecond precision" \
148 --begin="2012-10-29 12:48:17.588"
149 expect_success 11 "date time format: second precision" \
150 --clock-offset-ns=411282268 --begin="2012-10-29 12:48:18"
151 expect_success 11 "date time format: minute precision" \
152 --clock-offset=42 --clock-offset-ns=411282268 --begin="2012-10-29 12:49"
153
154 expect_success 11 "seconds from origin format: nanosecond precision" \
155 --begin="1351532897.588717732"
156 expect_success 11 "seconds from origin format: partial nanosecond precision" \
157 --begin="1351532897.58871773"
158 expect_success 11 "seconds from origin format: second precision" \
159 --clock-offset-ns=411282268 --begin="1351532898"
160
161 expect_failure "Invalid date/time format" "date time format: too many nanosecond digits" \
162 --begin="2012-10-29 12:48:17.1231231231"
163 expect_failure "Invalid date/time format" "date time format: missing nanoseconds" \
164 --begin="2012-10-29 12:48:17."
165 expect_failure "Invalid date/time format" "date time format: seconds with too many digit" \
166 --begin="2012-10-29 12:48:123"
167 expect_failure "Invalid date/time format" "date time format: seconds with missing digit" \
168 --begin="2012-10-29 12:48:1"
169 expect_failure "Invalid date/time format" "date time format: minutes with too many digit" \
170 --begin="2012-10-29 12:489:17"
171 expect_failure "Invalid date/time format" "date time format: minutes with missing digit" \
172 --begin="2012-10-29 12:4:17"
173 expect_failure "Invalid date/time format" "date time format: hours with too many digit" \
174 --begin="2012-10-29 123:48:17"
175 expect_failure "Invalid date/time format" "date time format: hours with missing digit" \
176 --begin="2012-10-29 2:48:17"
177 expect_failure "Invalid date/time format" "date time format: missing seconds" \
178 --begin="2012-10-29 12:48:"
179 expect_failure "Invalid date/time format" "date time format: missing minutes 1" \
180 --begin="2012-10-29 12:"
181 expect_failure "Invalid date/time format" "date time format: missing minutes 2" \
182 --begin="2012-10-29 12"
183 expect_failure "Invalid date/time format" "date time format: missing time" \
184 --begin="2012-10-29 "
185 expect_failure "Invalid date/time format" "date time format: day with too many digit" \
186 --begin="2012-10-291"
187 expect_failure "Invalid date/time format" "date time format: day with missing digit" \
188 --begin="2012-10-2"
189 expect_failure "Invalid date/time format" "date time format: month with too many digit" \
190 --begin="2012-101-29"
191 expect_failure "Invalid date/time format" "date time format: month with missing digit" \
192 --begin="2012-1-29"
193 expect_failure "Invalid date/time format" "date time format: year with too many digits" \
194 --begin="20121-10-29"
195 expect_failure "Invalid date/time format" "date time format: year with missing digits" \
196 --begin="12-10-29"
197 expect_failure "Invalid date/time format" "date time format: missing day 1" \
198 --begin="2012-10-"
199 expect_failure "Invalid date/time format" "date time format: missing day 2" \
200 --begin="2012-10"
201
202 expect_failure "Invalid date/time format" "seconds from origin format: too many nanosecond digits" \
203 --begin="1351532898.1231231231"
204 expect_failure "Invalid date/time format" "seconds from origin format: missing nanseconds" \
205 --begin="1351532898."
206
207 rm "${tmp_out}" "${tmp_err}"
This page took 0.035919 seconds and 5 git commands to generate.