tests: use -z / -n to test for string empty / non-empty
[babeltrace.git] / tests / cli / test_trimmer
CommitLineData
e5a54f3f
JD
1#!/bin/bash
2#
0235b0db 3# SPDX-License-Identifier: GPL-2.0-only
e5a54f3f 4#
0235b0db 5# Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
e5a54f3f 6#
e5a54f3f 7
644e0364
MJ
8SH_TAP=1
9
75e396f6 10if [ -n "${BT_TESTS_SRCDIR:-}" ]; then
644e0364
MJ
11 UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
12else
13 UTILSSH="$(dirname "$0")/../utils/utils.sh"
14fi
15
16# shellcheck source=../utils/utils.sh
17source "$UTILSSH"
e5a54f3f 18
bbff0ab4 19TRACE_PATH="${BT_CTF_TRACES_PATH}/succeed/wk-heartbeat-u/"
e5a54f3f 20
d4d7ffc9 21NUM_TESTS=118
e5a54f3f
JD
22
23plan_tests $NUM_TESTS
24
25tmp_out=$(mktemp)
d4d7ffc9
SM
26tmp_err=$(mktemp)
27
e5a54f3f 28
93fb46c6
SM
29# Run Babeltrace with some command line arguments, verify exit status and
30# number of output events (i.e. number of output lines)
31#
32# Arguments:
33#
34# $1: expected number of events
35# $2: test description
36# remaining arguments: command-line arguments to pass to Babeltrace
37
38function expect_success()
39{
40 local expected_num_events="$1"
d4d7ffc9
SM
41 local msg="$2"
42 shift 2
93fb46c6 43
f0a9b634 44 bt_cli "${tmp_out}" /dev/null "${TRACE_PATH}" "$@"
93fb46c6 45 ok $? "trimmer: ${msg}: exit status"
d4d7ffc9 46
93fb46c6 47 num_events=$(wc -l < "${tmp_out}")
891c35e4
MJ
48 # Use bash parameter expansion to strip spaces added by BSD 'wc' on macOs and Solaris
49 is "${num_events// /}" "${expected_num_events}" "trimmer: ${msg}: number of events (${expected_num_events})"
93fb46c6
SM
50}
51
d4d7ffc9
SM
52# Run Babeltrace with some command line arguments, verify that the exit status
53# is not 0 and that the error message contains a given string.
54#
55# Arguments:
56#
57# $1: a string expected to be found in the error message
58# $2: test description
59# remaining arguments: command-line arguments to pass to Babeltrace
60
61function expect_failure()
62{
63 local expected_err_string="$1"
64 local msg="$2"
65 shift 2
66
67 # We check the error message logged by the trimmer plugin, set the env
68 # var necessary for it to log errors.
f0a9b634 69 BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL=E bt_cli "${tmp_out}" "${tmp_err}" "${TRACE_PATH}" "$@"
d4d7ffc9
SM
70 isnt $? 0 "trimmer: ${msg}: exit status"
71
72 num_events=$(wc -l < "${tmp_out}")
bfb5625d
JR
73 # Use bash parameter expansion to strip spaces added by BSD 'wc' on macOs and Solaris
74 is "${num_events// /}" 0 "trimmer: ${msg}: number of events (0)"
d4d7ffc9
SM
75
76 stderr="$(cat "${tmp_err}")"
77 # "like" doesn't like when the passed text is empty.
78 if [ -n "${stderr}" ]; then
79 like "${stderr}" "${expected_err_string}" "trimmer: ${msg}: error message"
80 else
81 fail "trimmer: ${msg}: error message"
82 diag "Nothing was output on stderr".
83 fi
84
85}
86
93fb46c6
SM
87expect_success 18 "--begin, GMT relative timestamps" \
88 --clock-gmt --begin 17:48:17.587029529
89expect_success 9 "--end, GMT relative timestamps" \
90 --clock-gmt --end 17:48:17.588680018
91expect_success 7 "--begin and --end, GMT relative timestamps" \
92 --clock-gmt --begin 17:48:17.587029529 --end 17:48:17.588680018
93expect_success 0 "--begin, out of range, GMT relative timestamps" \
94 --clock-gmt --begin 18:48:17.587029529
95expect_success 0 "--end, out of range, GMT relative timestamps" \
96 --clock-gmt --end 16:48:17.588680018
97
98expect_success 18 "--begin, GMT absolute timestamps" \
99 --clock-gmt --begin "2012-10-29 17:48:17.587029529"
100expect_success 9 "--end, GMT absolute timestamps" \
101 --clock-gmt --end "2012-10-29 17:48:17.588680018"
102expect_success 7 "--begin and --end, GMT absolute timestamps" \
103 --clock-gmt --begin "2012-10-29 17:48:17.587029529" --end "2012-10-29 17:48:17.588680018"
104expect_success 0 "--begin, out of range, GMT absolute timestamps" \
105 --clock-gmt --begin "2012-10-29 18:48:17.587029529"
106expect_success 0 "--begin, out of range, GMT absolute timestamps" \
107 --clock-gmt --end "2012-10-29 16:48:17.588680018"
95abf2a0 108
2f137223
JR
109# Note here that the POSIX notation is a bit weird.
110# The libc documentation shed some light on this:
111# The offset specifies the time value you must add to the local time to get a
112# Coordinated Universal Time value. It has syntax like [+|-]hh[:mm[:ss]]. This
113# is positive if the local time zone is west of the Prime Meridian and negative
114# if it is east. The hour must be between 0 and 24, and the minute and seconds
115# between 0 and 59. [1]
116#
117# This is why we use EST5 to simulate an effective UTC-5:00 time.
118#
119# [1] https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
120export TZ=EST5
95abf2a0 121
93fb46c6
SM
122expect_success 18 "--begin, EST relative timestamps" \
123 --begin "12:48:17.587029529"
124expect_success 9 "--end, EST relative timestamps" \
125 --end "12:48:17.588680018"
126expect_success 7 "--begin and --end, EST relative timestamps" \
127 --begin "12:48:17.587029529" --end "12:48:17.588680018"
128expect_success 0 "--begin, out of range, EST relative timestamps" \
129 --begin "13:48:17.587029529"
130expect_success 0 "--end, out of range, EST relative timestamps" \
131 --end "11:48:17.588680018"
132
133expect_success 18 "--begin, EST absolute timestamps" \
134 --begin "2012-10-29 12:48:17.587029529"
135expect_success 9 "--end, EST absolute timestamps" \
136 --end "12:48:17.588680018"
137expect_success 7 "--begin and --end, EST absolute timestamps" \
138 --begin "2012-10-29 12:48:17.587029529" --end "2012-10-29 12:48:17.588680018"
139expect_success 0 "--begin, out of range, EST absolute timestamps" \
140 --begin "2012-10-29 13:48:17.587029529"
141expect_success 0 "--end, out of range, EST absolute timestamps" \
142 --end "2012-10-29 11:48:17.588680018"
e5a54f3f 143
d4d7ffc9
SM
144# Check various formats.
145#
146# We sometimes apply a clock offset to make the events of the trace span two
147# different seconds or minutes.
148
149expect_success 13 "date time format: partial nanosecond precision" \
150 --begin="2012-10-29 12:48:17.588"
151expect_success 11 "date time format: second precision" \
152 --clock-offset-ns=411282268 --begin="2012-10-29 12:48:18"
153expect_success 11 "date time format: minute precision" \
154 --clock-offset=42 --clock-offset-ns=411282268 --begin="2012-10-29 12:49"
155
156expect_success 11 "seconds from origin format: nanosecond precision" \
157 --begin="1351532897.588717732"
158expect_success 11 "seconds from origin format: partial nanosecond precision" \
159 --begin="1351532897.58871773"
160expect_success 11 "seconds from origin format: second precision" \
161 --clock-offset-ns=411282268 --begin="1351532898"
162
163expect_failure "Invalid date/time format" "date time format: too many nanosecond digits" \
164 --begin="2012-10-29 12:48:17.1231231231"
165expect_failure "Invalid date/time format" "date time format: missing nanoseconds" \
166 --begin="2012-10-29 12:48:17."
167expect_failure "Invalid date/time format" "date time format: seconds with too many digit" \
168 --begin="2012-10-29 12:48:123"
169expect_failure "Invalid date/time format" "date time format: seconds with missing digit" \
170 --begin="2012-10-29 12:48:1"
171expect_failure "Invalid date/time format" "date time format: minutes with too many digit" \
172 --begin="2012-10-29 12:489:17"
173expect_failure "Invalid date/time format" "date time format: minutes with missing digit" \
174 --begin="2012-10-29 12:4:17"
175expect_failure "Invalid date/time format" "date time format: hours with too many digit" \
176 --begin="2012-10-29 123:48:17"
177expect_failure "Invalid date/time format" "date time format: hours with missing digit" \
178 --begin="2012-10-29 2:48:17"
179expect_failure "Invalid date/time format" "date time format: missing seconds" \
180 --begin="2012-10-29 12:48:"
181expect_failure "Invalid date/time format" "date time format: missing minutes 1" \
182 --begin="2012-10-29 12:"
183expect_failure "Invalid date/time format" "date time format: missing minutes 2" \
184 --begin="2012-10-29 12"
185expect_failure "Invalid date/time format" "date time format: missing time" \
186 --begin="2012-10-29 "
187expect_failure "Invalid date/time format" "date time format: day with too many digit" \
188 --begin="2012-10-291"
189expect_failure "Invalid date/time format" "date time format: day with missing digit" \
190 --begin="2012-10-2"
191expect_failure "Invalid date/time format" "date time format: month with too many digit" \
192 --begin="2012-101-29"
193expect_failure "Invalid date/time format" "date time format: month with missing digit" \
194 --begin="2012-1-29"
195expect_failure "Invalid date/time format" "date time format: year with too many digits" \
196 --begin="20121-10-29"
197expect_failure "Invalid date/time format" "date time format: year with missing digits" \
198 --begin="12-10-29"
199expect_failure "Invalid date/time format" "date time format: missing day 1" \
200 --begin="2012-10-"
201expect_failure "Invalid date/time format" "date time format: missing day 2" \
202 --begin="2012-10"
203
204expect_failure "Invalid date/time format" "seconds from origin format: too many nanosecond digits" \
205 --begin="1351532898.1231231231"
206expect_failure "Invalid date/time format" "seconds from origin format: missing nanseconds" \
207 --begin="1351532898."
208
209rm "${tmp_out}" "${tmp_err}"
This page took 0.066447 seconds and 4 git commands to generate.