Home | History | Annotate | Download | only in tests
      1 #!/bin/bash
      2 
      3 # Copyright 2013 Divya Kothari <divya.s.kothari (at] gmail.com>
      4 # Copyright 2013 Robin Mittal <robinmittal.it (at] gmail.com>
      5 
      6 #cleaning 'yes' processes
      7 killall yes >/dev/null 2>&1
      8 
      9 [ -f testing.sh ] && . testing.sh
     10 
     11 #testing "name" "command" "result" "infile" "stdin"
     12 
     13 # Starting processes to test pgrep command
     14 yes >/dev/null &
     15 proc=$!
     16 echo "# Process created with id: $proc"
     17 sleep 1
     18 session_id=0
     19 proc_parent=`cat /proc/${proc}/stat | awk '{ print $4 }'`
     20 echo "# Parent Process id of $proc is $proc_parent"
     21 
     22 # Testcases for pgrep command
     23 testing "pgrep pattern" "pgrep yes" "$proc\n" "" ""
     24 testing "pgrep wildCardPattern" "pgrep ^y.*s$" "$proc\n" "" ""
     25 testing "pgrep -l pattern" "pgrep -l yes" "$proc yes\n" "" ""
     26 testing "pgrep -f pattern" "pgrep -f yes" "$proc\n" "" ""
     27 testing "pgrep -n pattern" "pgrep -n yes" "$proc\n" "" ""
     28 testing "pgrep -o pattern" "pgrep -o yes" "$proc\n" "" ""
     29 testing "pgrep -s" "pgrep -s $session_id yes" "$proc\n" "" ""
     30 testing "pgrep -P" "pgrep -P $proc_parent yes" "$proc\n" "" ""
     31 
     32 #Clean-up
     33 killall yes >/dev/null 2>&1
     34 
     35 # Testcases for pkill command
     36 
     37 yes >/dev/null &
     38 sleep 1
     39 testing "pkill pattern" "pkill yes && sleep 1 && (pgrep yes || echo 'yes')" \
     40   "yes\n" "" ""
     41 killall yes >/dev/null 2>&1
     42 
     43 yes >/dev/null &
     44 yes print1 >/dev/null &
     45 yes print2 >/dev/null &
     46 sleep 1
     47 testing "pkill pattern (multiple)" "pkill yes && sleep 1 &&
     48    (pgrep yes || echo 'yes')" "yes\n" "" ""
     49 killall yes >/dev/null 2>&1
     50 
     51 yes >/dev/null &
     52 sleep 1
     53 testing "pkill -f pattern (one)" "pkill -f yes && sleep 1 &&
     54    (pgrep yes || echo 'yes')" "yes\n" "" ""
     55 killall yes >/dev/null 2>&1
     56 
     57 yes print1 >/dev/null &
     58 sleep 1
     59 testing "pkill -f pattern args" "pkill -f \"yes print1\" && sleep 1 &&
     60    (pgrep yes || echo 'yes')" "yes\n" "" ""
     61 killall yes >/dev/null 2>&1
     62 
     63 yes >/dev/null &
     64 yes print1 >/dev/null &
     65 yes print2 >/dev/null &
     66 sleep 1
     67 testing "pkill -f pattern (multiple)" "pkill -f yes && sleep 1 &&
     68    (pgrep yes || echo 'yes')" "yes\n" "" ""
     69 killall yes >/dev/null 2>&1
     70 
     71 yes >/dev/null &
     72 sleep 1
     73 testing "pkill -s 0 -f pattern (regexp)" "pkill -s 0 -f ye* && sleep 1 &&
     74    (pgrep yes || echo 'yes')" "yes\n" "" ""
     75 killall yes >/dev/null 2>&1
     76 
     77 yes >/dev/null &
     78 proc1=$!
     79 yes >/dev/null &
     80 proc2=$!
     81 sleep 1
     82 testing "pkill -n pattern" "pkill -n yes && sleep 1 && pgrep yes" \
     83   "$proc1\n" "" ""
     84 killall yes >/dev/null 2>&1
     85 
     86 yes >/dev/null &
     87 proc1=$!
     88 yes >/dev/null &
     89 proc2=$!
     90 sleep 1
     91 testing "pkill -o pattern" "pkill -o yes && sleep 1 && pgrep yes" \
     92   "$proc2\n" "" ""
     93 killall yes >/dev/null 2>&1
     94 
     95 yes >/dev/null &
     96 sleep 1
     97 testing "pkill -s (blank) pattern" "pkill -s '' yes && sleep 1 &&
     98    (pgrep yes || echo 'yes')" "yes\n" "" ""
     99 killall yes >/dev/null 2>&1
    100 
    101 yes >/dev/null &
    102 sleep 1
    103 testing "pkill -s 0 pattern" "pkill -s 0 yes && sleep 1 &&
    104    (pgrep yes || echo 'yes')" "yes\n" "" ""
    105 killall yes >/dev/null 2>&1
    106 
    107 yes >/dev/null &
    108 proc=$!
    109 proc_p=`cat /proc/${proc}/stat | awk '{ print $4 }'`
    110 sleep 1
    111 testing "pkill -P parent_prodId pattern" "pkill -P $proc_p yes && sleep 1 &&
    112    (pgrep yes || echo 'yes')" "yes\n" "" ""
    113 killall yes >/dev/null 2>&1
    114 
    115 yes >/dev/null &
    116 proc=$!
    117 proc_parent=`cat /proc/${proc}/stat | awk '{ print $4 }'`
    118 sleep 1
    119 testing "pkill -9 pattern" "pkill -9 yes && sleep 1 &&
    120    (pgrep yes || echo 'yes')" "yes\n" "" ""
    121 killall yes >/dev/null 2>&1
    122 
    123