b817637e574b6afafd86e6e5141fd2dace08a5d1
[samba.git] / ctdb / tests / cunit / run_proc_001.sh
1 #!/bin/sh
2
3 . "${TEST_SCRIPTS_DIR}/unit.sh"
4
5 # Invalid path
6 ok <<EOF
7 Process exited with error 2
8 EOF
9 unit_test run_proc_test 0 -1 /a/b/c
10
11 # Non-executable path
12 prog=$(mktemp --tmpdir="$TEST_VAR_DIR")
13 cat > "$prog" <<EOF
14 echo hello
15 EOF
16
17 ok <<EOF
18 Process exited with error 13
19 EOF
20 unit_test run_proc_test 0 -1 "$prog"
21
22 # Executable path
23 chmod +x "$prog"
24
25 ok <<EOF
26 Process exited with error 8
27 EOF
28 unit_test run_proc_test 0 -1 "$prog"
29
30 # Capture output
31 cat > "$prog" <<EOF
32 #!/bin/sh
33 echo hello
34 EOF
35
36 ok <<EOF
37 Process exited with status 0
38 Output = (hello
39 )
40 EOF
41 unit_test run_proc_test 0 -1 "$prog"
42
43 # Specify timeout
44 ok <<EOF
45 Process exited with status 0
46 Output = (hello
47 )
48 EOF
49 unit_test run_proc_test 5 -1 "$prog"
50
51 # Redirected output
52 output=$(mktemp --tmpdir="$TEST_VAR_DIR")
53 cat > "$prog" <<EOF
54 #!/bin/sh
55 exec >"$output" 2>&1
56 echo hello
57 EOF
58
59 ok <<EOF
60 Process exited with status 0
61 EOF
62 unit_test run_proc_test 0 -1 "$prog"
63
64 ok <<EOF
65 hello
66 EOF
67 unit_test cat "$output"
68
69 # Exit with error
70 cat > "$prog" <<EOF
71 #!/bin/sh
72 exit 1
73 EOF
74
75 ok <<EOF
76 Process exited with status 1
77 EOF
78 unit_test run_proc_test 0 -1 "$prog"
79
80 # Exit with signal
81 cat > "$prog" <<EOF
82 #!/bin/sh
83 kill \$$
84 EOF
85
86 ok <<EOF
87 Process exited with signal 15
88 EOF
89 unit_test run_proc_test 0 -1 "$prog"
90
91 # Exit with timeout
92 cat > "$prog" <<EOF
93 #!/bin/sh
94 echo "Sleeping for 5 seconds"
95 sleep 5
96 EOF
97
98 result_filter ()
99 {
100         _pid="[0-9][0-9]*"
101         sed -e "s|= ${_pid}|= PID|"
102 }
103
104 ok <<EOF
105 Process exited with error 62
106 Child = PID
107 Output = (Sleeping for 5 seconds
108 )
109 EOF
110 unit_test run_proc_test 1 -1 "$prog"
111
112 # No zombie processes
113 pidfile=$(mktemp --tmpdir="$TEST_VAR_DIR")
114
115 cat > "$prog" <<EOF
116 #!/bin/sh
117 echo \$$ > "$pidfile"
118 sleep 10
119 EOF
120
121 ok <<EOF
122 Process exited with error 62
123 Child = PID
124 EOF
125 unit_test run_proc_test 1 -1 "$prog"
126
127 result_filter ()
128 {
129         _header="  *PID  *TTY  *TIME  *CMD"
130         sed -e "s|^${_header}|HEADER|"
131 }
132
133 pid=$(cat "$pidfile")
134 required_result 1 <<EOF
135 HEADER
136 EOF
137 unit_test ps -p "$pid"
138
139 # Redirect stdin
140 cat > "$prog" <<EOF
141 #!/bin/sh
142 cat -
143 EOF
144
145 cat > "$output" <<EOF
146 this is sample input
147 EOF
148
149 ok <<EOF
150 Process exited with status 0
151 Output = (this is sample input
152 )
153 EOF
154 (unit_test run_proc_test 0 4 "$prog") 4<"$output"
155
156 rm -f "$pidfile"
157 rm -f "$output"
158 rm -f "$prog"