Print this page
3484 enhance and document tail follow support
Reviewed by: Joshua M. Clulow <jmc@joyent.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/tail/tests/tailtests.sh
          +++ new/usr/src/cmd/tail/tests/tailtests.sh
↓ open down ↓ 5 lines elided ↑ open up ↑
   6    6  # You may only use this file in accordance with the terms of version
   7    7  # 1.0 of the CDDL.
   8    8  #
   9    9  # A full copy of the text of the CDDL should have accompanied this
  10   10  # source.  A copy is of the CDDL is also available via the Internet
  11   11  # at http://www.illumos.org/license/CDDL.
  12   12  #
  13   13  
  14   14  #
  15   15  # Copyright 2010 Chris Love.  All rights reserved.
       16 +# Copyright (c) 2013, Joyent, Inc. All rights reserved.
  16   17  #
  17   18  
       19 +checktest()
       20 +{
       21 +        local actual=$1
       22 +        local output=$2
       23 +        local test=$3
  18   24  
       25 +        if [[ "$actual" != "$output" ]]; then
       26 +                echo "$CMD: test $test: FAIL"
       27 +                echo -e "$CMD: test $test: expected output:\n$o"
       28 +                echo -e "$CMD: test $test: actual output:\n$a"
       29 +        else
       30 +                echo "$CMD: test $test: pass"
       31 +        fi
       32 +}
       33 +
  19   34  # 
  20   35  # Test cases for 'tail', some based on CoreUtils test cases (validated
  21      -# with legacy Solaris 'tail' and/or xpg4 'tail')
       36 +# with legacy Solaris 'tail' and/or xpg4 'tail').  Note that this is designed
       37 +# to be able to run on BSD systems as well to check our behavior against
       38 +# theirs (some behavior that is known to be idiosyncratic to illumos is
       39 +# skipped on non-illumos systems).
  22   40  #
  23   41  PROG=/usr/bin/tail
       42 +CMD=`basename $0`
       43 +DIR=""
  24   44  
  25      -case $1 in 
  26      -    -x)
  27      -        PROG=/usr/xpg4/bin/tail
  28      -        ;;
  29      -    -o)
  30      -        PROG=$2
  31      -        ;;
  32      -    -?)
  33      -        echo "Usage: tailtests.sh [-x][-o <override tail executable>]"
  34      -        exit 1
  35      -        ;;
  36      -esac
       45 +while [[ $# -gt 0 ]]; do
       46 +        case $1 in
       47 +            -x)
       48 +                PROG=/usr/xpg4/bin/tail
       49 +                shift
       50 +                ;;
       51 +            -o)
       52 +                PROG=$2
       53 +                shift 2
       54 +                ;;
       55 +            -d)
       56 +                DIR=$2
       57 +                shift 2
       58 +                ;;
       59 +            *)
       60 +                echo "Usage: tailtests.sh" \
       61 +                    "[-x][-o <override tail executable>]" \
       62 +                    "[-d <override output directory>]"
       63 +                exit 1
       64 +                ;;
       65 +        esac
       66 +done
  37   67  
  38      -echo "Using $PROG"
       68 +#
       69 +# Shut bash up upon receiving a term so we can drop it on our children
       70 +# without disrupting the output.
       71 +#
       72 +trap "exit 0" TERM
  39   73  
       74 +echo "$CMD: program is $PROG"
       75 +
       76 +if [[ $DIR != "" ]]; then
       77 +        echo "$CMD: directory is $DIR"
       78 +fi
       79 +
  40   80  o=`echo -e "bcd"`
  41   81  a=`echo -e "abcd" | $PROG +2c`
  42      -[[ "$a" != "$o" ]] && echo "Fail test 1 - $a"
       82 +checktest "$a" "$o" 1
  43   83  
  44   84  o=`echo -e ""`
  45   85  a=`echo "abcd" | $PROG +8c`
  46      -[[ "$a" != "$o" ]] && echo "Fail test 2 - $a"
       86 +checktest "$a" "$o" 2
  47   87  
  48   88  o=`echo -e "abcd"`
  49   89  a=`echo "abcd" | $PROG -9c`
  50      -[[ "$a" != "$o" ]] && echo "Fail test 3 - $a"
       90 +checktest "$a" "$o" 3
  51   91  
  52   92  o=`echo -e "x"`
  53   93  a=`echo -e "x" | $PROG -1l`
  54      -[[ "$a" != "x" ]] && echo "Fail test 4 - $a"
       94 +checktest "$a" "$o" 4
  55   95  
  56   96  o=`echo -e "\n"`
  57   97  a=`echo -e "x\ny\n" | $PROG -1l`
  58      -[[ "$a" != "$o" ]] && echo "Fail test 5 - $a"
       98 +checktest "$a" "$o" 5
  59   99  
  60  100  o=`echo -e "y\n"`
  61  101  a=`echo -e "x\ny\n" | $PROG -2l`
  62      -[[ "$a" != "$o" ]] && echo "Fail test 6 - $a"
      102 +checktest "$a" "$o" 6
  63  103  
  64  104  o=`echo -e "y"`
  65  105  a=`echo -e "x\ny" | $PROG -1l`
  66      -[[ "$a" != "$o" ]] && echo "Fail test 7 - $a"
      106 +checktest "$a" "$o" 7
  67  107  
  68  108  o=`echo -e "x\ny\n"`
  69  109  a=`echo -e "x\ny\n" | $PROG +1l`
  70      -[[ "$a" != "$o" ]] && echo "Fail test 8 - $a"
      110 +checktest "$a" "$o" 8
  71  111  
  72  112  o=`echo -e "y\n"`
  73  113  a=`echo -e "x\ny\n" | $PROG +2l`
  74      -[[ "$a" != "$o" ]] && echo "Fail test 9 - $a"
      114 +checktest "$a" "$o" 9
  75  115  
  76  116  o=`echo -e "x"`
  77  117  a=`echo -e "x" | $PROG -1`
  78      -[[ "$a" != "$o" ]] && echo "Fail test 10 - $a"
      118 +checktest "$a" "$o" 10
  79  119  
  80  120  o=`echo -e "\n"`
  81  121  a=`echo -e "x\ny\n" | $PROG -1`
  82      -[[ "$a" != "$o" ]] && echo "Fail test 11 - $a"
      122 +checktest "$a" "$o" 11
  83  123  
  84  124  o=`echo -e "y\n"`
  85  125  a=`echo -e "x\ny\n" | $PROG -2`
  86      -[[ "$a" != "$o" ]] && echo "Fail test 12 - $a"
      126 +checktest "$a" "$o" 12
  87  127  
  88  128  o=`echo -e "y"`
  89  129  a=`echo -e "x\ny" | $PROG -1`
  90      -[[ "$a" != "$o" ]] && echo "Fail test 13 - $a"
      130 +checktest "$a" "$o" 13
  91  131  
  92  132  o=`echo -e "x\ny\n"`
  93  133  a=`echo -e "x\ny\n" | $PROG +1`
  94      -[[ "$a" != "$o" ]] && echo "Fail test 14 - $a"
      134 +checktest "$a" "$o" 14
  95  135  
  96  136  o=`echo -e "y\n"`
  97  137  a=`echo -e "x\ny\n" | $PROG +2`
  98      -[[ "$a" != "$o" ]] && echo "Fail test 15 - $a"
      138 +checktest "$a" "$o" 15
  99  139  
 100      -# For compatibility with Legacy Solaris tail this should also work as '+c'
 101  140  o=`echo -e "yyz"`
 102  141  a=`echo -e "xyyyyyyyyyyz" | $PROG +10c`
 103      -[[ "$a" != "$o" ]] && echo "Fail test 16 - $a"
      142 +checktest "$a" "$o" 16
 104  143  
 105      -o=`echo -e "yyz"`
 106      -a=`echo -e "xyyyyyyyyyyz" | $PROG +c`
 107      -[[ "$a" != "$o" ]] && echo "Fail test 16a - $a"
 108      -
 109      -
 110      -# For compatibility with Legacy Solaris tail this should also work as '+l'
 111  144  o=`echo -e "y\ny\nz"`
 112  145  a=`echo -e "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz" | $PROG +10l`
 113      -[[ "$a" != "$o" ]] && echo "Fail test 17 - $a"
      146 +checktest "$a" "$o" 17
 114  147  
 115      -o=`echo -e "y\ny\nz"`
 116      -a=`echo -e "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz" | $PROG +l`
 117      -[[ "$a" != "$o" ]] && echo "Fail test 17a - $a"
 118      -
 119      -
 120      -# For compatibility with Legacy Solaris tail this should also work as '-l'
 121  148  o=`echo -e "y\ny\ny\ny\ny\ny\ny\ny\ny\nz"`
 122  149  a=`echo -e "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz" | $PROG -10l`
 123      -[[ "$a" != "$o" ]] && echo "Fail test 18 - $a"
      150 +checktest "$a" "$o" 18
 124  151  
 125      -o=`echo -e "y\ny\ny\ny\ny\ny\ny\ny\ny\nz"`
 126      -a=`echo -e "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz" | $PROG -l`
 127      -[[ "$a" != "$o" ]] && echo "Fail test 18a - $a"
      152 +#
      153 +# For reasons that are presumably as accidental as they are ancient, legacy
      154 +# (and closed) Solaris tail(1) allows +c, +l and -l to be aliases for +10c,
      155 +# +10l and -10l, respectively.  If we are on SunOS, verify that this silly
      156 +# behavior is functional.
      157 +#
      158 +if [[ `uname -s` == "SunOS" ]]; then
      159 +        o=`echo -e "yyz"`
      160 +        a=`echo -e "xyyyyyyyyyyz" | $PROG +c`
      161 +        checktest "$a" "$o" 16a
 128  162  
      163 +        o=`echo -e "y\ny\nz"`
      164 +        a=`echo -e "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz" | $PROG +l`
      165 +        checktest "$a" "$o" 17a
      166 +
      167 +        o=`echo -e "y\ny\ny\ny\ny\ny\ny\ny\ny\nz"`
      168 +        a=`echo -e "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz" | $PROG -l`
      169 +        checktest "$a" "$o" 18a
      170 +fi
      171 +
 129  172  o=`echo -e "c\nb\na"`
 130  173  a=`echo -e "a\nb\nc" | $PROG -r`
 131      -[[ "$a" != "$o" ]] && echo "Fail test 19 - $a"
      174 +checktest "$a" "$o" 19
 132  175  
      176 +#
      177 +# Now we want to do a series of follow tests.
      178 +#
      179 +if [[ $DIR == "" ]]; then
      180 +        tdir=$(mktemp -d -t tailtest.XXXXXXXX || exit 1)
      181 +else
      182 +        tdir=$(mktemp -d $DIR/tailtest.XXXXXXXX || exit 1)
      183 +fi
 133  184  
 134      -echo "Completed"
      185 +follow=$tdir/follow
      186 +moved=$tdir/follow.moved
      187 +out=$tdir/out
 135  188  
 136      -exit 0
      189 +#
      190 +# First, verify that following works in its most basic sense.
      191 +#
      192 +echo -e "a\nb\nc" > $follow
      193 +$PROG -f $follow > $out 2> /dev/null &
      194 +child=$!
      195 +sleep 2
      196 +echo -e "d\ne\nf" >> $follow
      197 +sleep 1
      198 +kill $child
      199 +sleep 1
 137  200  
 138      -# Template for additional test cases
 139      -#o=`echo -e ""`
 140      -#a=`echo -e "" | $PROG `
 141      -#[[ "$a" != "$o" ]] && echo "Fail test  - $a"
      201 +o=`echo -e "a\nb\nc\nd\ne\nf\n"`
      202 +a=`cat $out`
      203 +checktest "$a" "$o" 20
      204 +rm $follow
      205 +
      206 +#
      207 +# Now verify that following correctly follows the file being moved.
      208 +#
      209 +echo -e "a\nb\nc" > $follow
      210 +$PROG -f $follow > $out 2> /dev/null &
      211 +child=$!
      212 +sleep 2
      213 +mv $follow $moved
      214 +
      215 +echo -e "d\ne\nf" >> $moved
      216 +sleep 1
      217 +kill $child
      218 +sleep 1
      219 +
      220 +o=`echo -e "a\nb\nc\nd\ne\nf\n"`
      221 +a=`cat $out`
      222 +checktest "$a" "$o" 21
      223 +rm $moved
      224 +
      225 +#
      226 +# And now truncation with the new offset being less than the old offset.
      227 +#
      228 +echo -e "a\nb\nc" > $follow
      229 +$PROG -f $follow > $out 2> /dev/null &
      230 +child=$!
      231 +sleep 2
      232 +echo -e "d\ne\nf" >> $follow
      233 +sleep 1
      234 +echo -e "g\nh\ni" > $follow
      235 +sleep 1
      236 +kill $child
      237 +sleep 1
      238 +
      239 +o=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\n"`
      240 +a=`cat $out`
      241 +checktest "$a" "$o" 22
      242 +rm $follow
      243 +
      244 +#
      245 +# And truncation with the new offset being greater than the old offset.
      246 +#
      247 +echo -e "a\nb\nc" > $follow
      248 +sleep 1
      249 +$PROG -f $follow > $out 2> /dev/null &
      250 +child=$!
      251 +sleep 2
      252 +echo -e "d\ne\nf" >> $follow
      253 +sleep 1
      254 +echo -e "g\nh\ni\nj\nk\nl\nm\no\np\nq" > $follow
      255 +sleep 1
      256 +kill $child
      257 +sleep 1
      258 +
      259 +o=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\no\np\nq"`
      260 +a=`cat $out`
      261 +checktest "$a" "$o" 23
      262 +rm $follow
      263 +
      264 +#
      265 +# Verify that we can follow the moved file and correctly see a truncation.
      266 +#
      267 +echo -e "a\nb\nc" > $follow
      268 +$PROG -f $follow > $out 2> /dev/null &
      269 +child=$!
      270 +sleep 2
      271 +mv $follow $moved
      272 +
      273 +echo -e "d\ne\nf" >> $moved
      274 +sleep 1
      275 +echo -e "g\nh\ni\nj\nk\nl\nm\no\np\nq" > $moved
      276 +sleep 1
      277 +kill $child
      278 +sleep 1
      279 +
      280 +o=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\no\np\nq"`
      281 +a=`cat $out`
      282 +checktest "$a" "$o" 24
      283 +rm $moved
      284 +
      285 +#
      286 +# Verify that capital-F follow properly deals with truncation
      287 +#
      288 +echo -e "a\nb\nc" > $follow
      289 +$PROG -F $follow > $out 2> /dev/null &
      290 +child=$!
      291 +sleep 2
      292 +echo -e "d\ne\nf" >> $follow
      293 +sleep 1
      294 +echo -e "g\nh\ni" > $follow
      295 +sleep 1
      296 +kill $child
      297 +sleep 1
      298 +
      299 +o=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\n"`
      300 +a=`cat $out`
      301 +checktest "$a" "$o" 25
      302 +rm $follow
      303 +
      304 +#
      305 +# Verify that capital-F follow _won't_ follow the moved file and will
      306 +# correctly deal with recreation of the original file.
      307 +#
      308 +echo -e "a\nb\nc" > $follow
      309 +$PROG -F $follow > $out 2> /dev/null &
      310 +child=$!
      311 +sleep 2
      312 +mv $follow $moved
      313 +
      314 +echo -e "x\ny\nz" >> $moved
      315 +echo -e "d\ne\nf" > $follow
      316 +sleep 1
      317 +kill $child
      318 +sleep 1
      319 +
      320 +o=`echo -e "a\nb\nc\nd\ne\nf\n"`
      321 +a=`cat $out`
      322 +checktest "$a" "$o" 26
      323 +rm $moved
      324 +
      325 +#
      326 +# Verify that following two files works.
      327 +#
      328 +echo -e "one" > $follow
      329 +echo -e "two" > $moved
      330 +$PROG -f $follow $moved > $out 2> /dev/null &
      331 +child=$!
      332 +sleep 2
      333 +echo -e "three" >> $follow
      334 +sleep 1
      335 +echo -e "four" >> $moved
      336 +sleep 1
      337 +echo -e "five" >> $follow
      338 +sleep 1
      339 +kill $child
      340 +sleep 1
      341 +
      342 +# There is a bug where the content comes before the header lines,
      343 +# where rlines/mapprint happens before the header.  A pain to fix.
      344 +# In this test, just make sure we see both files change.
      345 +o="one
      346 +
      347 +==> $follow <==
      348 +two
      349 +
      350 +==> $moved <==
      351 +
      352 +==> $follow <==
      353 +three
      354 +
      355 +==> $moved <==
      356 +four
      357 +
      358 +==> $follow <==
      359 +five"
      360 +a=`cat $out`
      361 +checktest "$a" "$o" 27
      362 +rm $follow $moved
      363 +
      364 +echo "$CMD: completed"
      365 +
      366 +exit $errs
      367 +
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX