summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/UNIT/cunit/run_proc_001.sh
blob: 3f48885298d841a339b078d59c895e3572123778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/sh

. "${TEST_SCRIPTS_DIR}/unit.sh"

# Invalid path
ok <<EOF
Process exited with error $(errcode ENOENT)
EOF
unit_test run_proc_test 0 -1 /a/b/c

# Non-executable path
prog=$(TMPDIR="$CTDB_TEST_TMP_DIR" mktemp)
cat > "$prog" <<EOF
echo hello
EOF

ok <<EOF
Process exited with error $(errcode EACCES)
EOF
unit_test run_proc_test 0 -1 "$prog"

# Executable path
chmod +x "$prog"

ok <<EOF
Process exited with error $(errcode ENOEXEC)
EOF
unit_test run_proc_test 0 -1 "$prog"

# Capture output
cat > "$prog" <<EOF
#!/bin/sh
echo hello
EOF

ok <<EOF
Process exited with status 0
Output = (hello
)
EOF
unit_test run_proc_test 0 -1 "$prog"

# Specify timeout
ok <<EOF
Process exited with status 0
Output = (hello
)
EOF
unit_test run_proc_test 5 -1 "$prog"

# Redirected output
output=$(TMPDIR="$CTDB_TEST_TMP_DIR" mktemp)
cat > "$prog" <<EOF
#!/bin/sh
exec >"$output" 2>&1
echo hello
EOF

ok <<EOF
Process exited with status 0
EOF
unit_test run_proc_test 0 -1 "$prog"

ok <<EOF
hello
EOF
unit_test cat "$output"

# Exit with error
cat > "$prog" <<EOF
#!/bin/sh
exit 1
EOF

ok <<EOF
Process exited with status 1
EOF
unit_test run_proc_test 0 -1 "$prog"

# Exit with signal
cat > "$prog" <<EOF
#!/bin/sh
kill \$$
EOF

ok <<EOF
Process exited with signal 15
EOF
unit_test run_proc_test 0 -1 "$prog"

# Exit with timeout
cat > "$prog" <<EOF
#!/bin/sh
echo "Sleeping for 5 seconds"
sleep 5
EOF

result_filter ()
{
	_pid="[0-9][0-9]*"
	sed -e "s|= ${_pid}|= PID|"
}

ok <<EOF
Process exited with error $(errcode ETIMEDOUT)
Child = PID
Output = (Sleeping for 5 seconds
)
EOF
unit_test run_proc_test 1 -1 "$prog"

# No zombie processes
pidfile=$(TMPDIR="$CTDB_TEST_TMP_DIR" mktemp)

cat > "$prog" <<EOF
#!/bin/sh
echo \$$ > "$pidfile"
sleep 10
EOF

ok <<EOF
Process exited with error $(errcode ETIMEDOUT)
Child = PID
EOF
unit_test run_proc_test 1 -1 "$prog"

result_filter ()
{
	_header="  *PID  *TTY  *TIME  *CMD"
	_header2=" *PID  *TT  *STAT  *TIME  *COMMAND"
	sed -e "s|^${_header}|HEADER|" -e "s|^${_header2}|HEADER|"
}

pid=$(cat "$pidfile")
required_result 1 <<EOF
HEADER
EOF
unit_test ps -p "$pid"

# Redirect stdin
cat > "$prog" <<EOF
#!/bin/sh
cat -
EOF

cat > "$output" <<EOF
this is sample input
EOF

ok <<EOF
Process exited with status 0
Output = (this is sample input
)
EOF
(unit_test run_proc_test 0 4 "$prog") 4<"$output"

rm -f "$pidfile"
rm -f "$output"
rm -f "$prog"