1#!/bin/bash
2#
3# Copyright (C) 2009 Red Hat, Inc.
4# Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation.
9#
10# This program is distributed in the hope that it would be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17#
18#
19# standard filters
20#
21
22# Checks that given_value is in range of correct_value +/- tolerance.
23# Tolerance can be an absolute value or a percentage of the correct value
24# (see examples with tolerances below).
25# Outputs suitable message to stdout if it's not in range.
26#
27# A verbose option, -v, may be used as the LAST argument
28#
29# e.g.
30# foo: 0.0298 = 0.03 +/- 5%
31# _within_tolerance "foo" 0.0298 0.03 5%
32#
33# foo: 0.0298 = 0.03 +/- 0.01
34# _within_tolerance "foo" 0.0298 0.03 0.01
35#
36# foo: 0.0298 = 0.03 -0.01 +0.002
37# _within_tolerance "foo" 0.0298 0.03 0.01 0.002
38#
39# foo: verbose output of 0.0298 = 0.03 +/- 5%
40# _within_tolerance "foo" 0.0298 0.03 5% -v
41_within_tolerance()
42{
43  _name=$1
44  _given_val=$2
45  _correct_val=$3
46  _mintol=$4
47  _maxtol=$_mintol
48  _verbose=0
49  _debug=false
50
51  # maxtol arg is optional
52  # verbose arg is optional
53  if [ $# -ge 5 ]
54  then
55     if [ "$5" = "-v" ]
56     then
57        _verbose=1
58     else
59        _maxtol=$5
60     fi
61  fi
62  if [ $# -ge 6 ]
63  then
64     [ "$6" = "-v" ] && _verbose=1
65  fi
66
67  # find min with or without %
68  _mintolerance=`echo $_mintol | sed -e 's/%//'`
69  if [ $_mintol = $_mintolerance ]
70  then
71      _min=`echo "scale=5; $_correct_val-$_mintolerance" | bc`
72  else
73      _min=`echo "scale=5; $_correct_val-$_mintolerance*0.01*$_correct_val" | bc`
74  fi
75
76  # find max with or without %
77  _maxtolerance=`echo $_maxtol | sed -e 's/%//'`
78  if [ $_maxtol = $_maxtolerance ]
79  then
80      _max=`echo "scale=5; $_correct_val+$_maxtolerance" | bc`
81  else
82      _max=`echo "scale=5; $_correct_val+$_maxtolerance*0.01*$_correct_val" | bc`
83  fi
84
85  $_debug && echo "min = $_min"
86  $_debug && echo "max = $_max"
87
88  cat <<EOF >$tmp.bc.1
89scale=5;
90if ($_min <= $_given_val) 1;
91if ($_min > $_given_val) 0;
92EOF
93
94  cat <<EOF >$tmp.bc.2
95scale=5;
96if ($_given_val <= $_max) 1;
97if ($_given_val > $_max) 0;
98EOF
99
100  _above_min=`bc <$tmp.bc.1`
101  _below_max=`bc <$tmp.bc.2`
102
103  rm -f $tmp.bc.[12]
104
105  _in_range=`expr $_above_min \& $_below_max`
106
107  # fix up min, max precision for output
108  # can vary for 5.3, 6.2
109  _min=`echo $_min | sed -e 's/0*$//'` # get rid of trailling zeroes
110  _max=`echo $_max | sed -e 's/0*$//'` # get rid of trailling zeroes
111
112  if [ $_in_range -eq 1 ]
113  then
114        [ $_verbose -eq 1 ] && echo $_name is in range
115        return 0
116  else
117        [ $_verbose -eq 1 ] && echo $_name has value of $_given_val
118        [ $_verbose -eq 1 ] && echo $_name is NOT in range $_min .. $_max
119        return 1
120  fi
121}
122
123# ctime(3) dates
124#
125_filter_date()
126{
127    sed \
128        -e 's/[A-Z][a-z][a-z] [A-z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$/DATE/'
129}
130
131_filter_generated_node_ids()
132{
133     sed -re 's/\#block[0-9]{3,}/NODE_NAME/'
134}
135
136# replace occurrences of the actual TEST_DIR value with TEST_DIR
137_filter_testdir()
138{
139    sed -e "s#$TEST_DIR#TEST_DIR#g"
140}
141
142# replace occurrences of the actual IMGFMT value with IMGFMT
143_filter_imgfmt()
144{
145    sed -e "s#$IMGFMT#IMGFMT#g"
146}
147
148# Removes \r from messages
149_filter_win32()
150{
151    sed -e 's/\r//g'
152}
153
154# sanitize qemu-io output
155_filter_qemu_io()
156{
157    _filter_win32 | sed -e "s/[0-9]* ops\; [0-9/:. sec]* ([0-9/.inf]* [EPTGMKiBbytes]*\/sec and [0-9/.inf]* ops\/sec)/X ops\; XX:XX:XX.X (XXX YYY\/sec and XXX ops\/sec)/" \
158        -e "s/: line [0-9][0-9]*:  *[0-9][0-9]*\( Aborted\| Killed\)/:\1/" \
159        -e "s/qemu-io> //g"
160}
161
162# replace occurrences of QEMU_PROG with "qemu"
163_filter_qemu()
164{
165    sed -e "s#\\(^\\|(qemu) \\)$(basename $QEMU_PROG):#\1QEMU_PROG:#" \
166        -e 's#^QEMU [0-9]\+\.[0-9]\+\.[0-9]\+ monitor#QEMU X.Y.Z monitor#' \
167        -e '/main-loop: WARNING: I\/O thread spun for [0-9]\+ iterations/d' \
168        -e $'s#\r##' # QEMU monitor uses \r\n line endings
169}
170
171# replace problematic QMP output like timestamps
172_filter_qmp()
173{
174    _filter_win32 | \
175    sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
176        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
177        -e '/^    "QMP": {\s*$/, /^    }\s*$/ c\' \
178        -e '    QMP_VERSION'
179}
180
181# replace driver-specific options in the "Formatting..." line
182_filter_img_create()
183{
184    sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
185        -e "s#$TEST_DIR#TEST_DIR#g" \
186        -e "s#$IMGFMT#IMGFMT#g" \
187        -e "s# encryption=off##g" \
188        -e "s# cluster_size=[0-9]\\+##g" \
189        -e "s# table_size=[0-9]\\+##g" \
190        -e "s# compat=[^ ]*##g" \
191        -e "s# compat6=\\(on\\|off\\)##g" \
192        -e "s# static=\\(on\\|off\\)##g" \
193        -e "s# zeroed_grain=\\(on\\|off\\)##g" \
194        -e "s# subformat='[^']*'##g" \
195        -e "s# adapter_type='[^']*'##g" \
196        -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
197        -e "s# block_size=[0-9]\\+##g" \
198        -e "s# block_state_zero=\\(on\\|off\\)##g" \
199        -e "s# log_size=[0-9]\\+##g" \
200        -e "s/archipelago:a/TEST_DIR\//g" \
201        -e "s# refcount_bits=[0-9]\\+##g"
202}
203
204_filter_img_info()
205{
206    sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
207        -e "s#$TEST_DIR#TEST_DIR#g" \
208        -e "s#$IMGFMT#IMGFMT#g" \
209        -e 's#nbd://127.0.0.1:10810$#TEST_DIR/t.IMGFMT#g' \
210        -e "/encrypted: yes/d" \
211        -e "/cluster_size: [0-9]\\+/d" \
212        -e "/table_size: [0-9]\\+/d" \
213        -e "/compat: '[^']*'/d" \
214        -e "/compat6: \\(on\\|off\\)/d" \
215        -e "/static: \\(on\\|off\\)/d" \
216        -e "/zeroed_grain: \\(on\\|off\\)/d" \
217        -e "/subformat: '[^']*'/d" \
218        -e "/adapter_type: '[^']*'/d" \
219        -e "/lazy_refcounts: \\(on\\|off\\)/d" \
220        -e "/block_size: [0-9]\\+/d" \
221        -e "/block_state_zero: \\(on\\|off\\)/d" \
222        -e "/log_size: [0-9]\\+/d" \
223        -e "s/archipelago:a/TEST_DIR\//g"
224}
225
226# filter out offsets and file names from qemu-img map
227_filter_qemu_img_map()
228{
229    sed -e 's/\([0-9a-fx]* *[0-9a-fx]* *\)[0-9a-fx]* */\1/g' \
230        -e 's/Mapped to *//' | _filter_testdir | _filter_imgfmt
231}
232
233_filter_nbd()
234{
235    # nbd.c error messages contain function names and line numbers that are
236    # prone to change.  Message ordering depends on timing between send and
237    # receive callbacks sometimes, making them unreliable.
238    #
239    # Filter out the TCP port number since this changes between runs.
240    sed -e '/nbd\/.*\.c:/d' \
241        -e 's#nbd:\(//\)\?127\.0\.0\.1:[0-9]*#nbd:\1127.0.0.1:PORT#g' \
242        -e "s#?socket=$TEST_DIR#?socket=TEST_DIR#g" \
243        -e 's#\(exportname=foo\|PORT\): Failed to .*$#\1#'
244}
245
246# make sure this script returns success
247true
248