1#!/bin/bash
2
3# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
4# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
5
6#cleaning 'yes' processes
7killall yes >/dev/null 2>&1
8
9[ -f testing.sh ] && . testing.sh
10
11#testing "name" "command" "result" "infile" "stdin"
12
13# Testcases for pkill command
14
15yes >/dev/null &
16sleep 1
17testing "pattern" "pkill yes && sleep 1 && (pgrep yes || echo 'yes')" \
18  "yes\n" "" ""
19killall yes >/dev/null 2>&1
20
21yes >/dev/null &
22yes print1 >/dev/null &
23yes print2 >/dev/null &
24sleep 1
25testing "pattern (multiple)" "pkill yes && sleep 1 &&
26   (pgrep yes || echo 'yes')" "yes\n" "" ""
27killall yes >/dev/null 2>&1
28
29yes >/dev/null &
30sleep 1
31testing "-f pattern (one)" "pkill -f yes && sleep 1 &&
32   (pgrep yes || echo 'yes')" "yes\n" "" ""
33killall yes >/dev/null 2>&1
34
35yes print1 >/dev/null &
36sleep 1
37testing "-f pattern args" "pkill -f \"yes print1\" && sleep 1 &&
38   (pgrep yes || echo 'yes')" "yes\n" "" ""
39killall yes >/dev/null 2>&1
40
41yes >/dev/null &
42yes print1 >/dev/null &
43yes print2 >/dev/null &
44sleep 1
45testing "-f pattern (multiple)" "pkill -f yes && sleep 1 &&
46   (pgrep yes || echo 'yes')" "yes\n" "" ""
47killall yes >/dev/null 2>&1
48
49yes this-is-unlikely-to-appear-otherwise >/dev/null &
50sleep 1
51testing "-s 0 -f pattern (regexp)" "pkill -s 0 -f .*-unlikely-.*-otherwise &&
52   sleep 1 && (pgrep yes || echo 'yes')" "yes\n" "" ""
53killall yes >/dev/null 2>&1
54
55yes >/dev/null &
56proc1=$!
57yes >/dev/null &
58proc2=$!
59sleep 1
60testing "-n pattern" "pkill -n yes && sleep 1 && pgrep yes" \
61  "$proc1\n" "" ""
62killall yes >/dev/null 2>&1
63
64yes >/dev/null &
65proc1=$!
66sleep 1
67yes >/dev/null &
68proc2=$!
69sleep 1
70testing "-o pattern" "pkill -o yes && sleep 1 && pgrep yes" \
71  "$proc2\n" "" ""
72killall yes >/dev/null 2>&1
73
74yes >/dev/null &
75sleep 1
76testing "-s (blank) pattern" "pkill -s '' yes && sleep 1 &&
77   (pgrep yes || echo 'yes')" "yes\n" "" ""
78killall yes >/dev/null 2>&1
79
80yes >/dev/null &
81sleep 1
82testing "-s 0 pattern" "pkill -s 0 yes && sleep 1 &&
83   (pgrep yes || echo 'yes')" "yes\n" "" ""
84killall yes >/dev/null 2>&1
85
86yes >/dev/null &
87proc=$!
88proc_p=`cat /proc/${proc}/stat | cut -d' ' -f4`
89sleep 1
90testing "-P parent_prodId pattern" "pkill -P $proc_p yes && sleep 1 &&
91   (pgrep yes || echo 'yes')" "yes\n" "" ""
92killall yes >/dev/null 2>&1
93
94yes >/dev/null &
95sleep 1
96testing "-9 pattern" "pkill -9 yes && sleep 1 &&
97   (pgrep yes || echo 'yes')" "yes\n" "" ""
98killall yes >/dev/null 2>&1
99
100yes >/dev/null &
101sleep 1
102testing "return success" "pkill yes && echo success" "success\n" "" ""
103killall yes >/dev/null 2>&1
104
105testing "return failure" "pkill almost-certainly-not || echo failure" \
106    "failure\n" "" ""
107
108