]>
Commit | Line | Data |
---|---|---|
8d1ea537 CB |
1 | #!/bin/sh |
2 | ||
3 | # lxc: linux Container library | |
4 | ||
5 | # Authors: | |
6 | # Christian Brauner <christian.brauner@mailbox.org> | |
7 | # | |
8 | # This is a test script for the lxc-attach program. It tests whether I/O | |
172d397e | 9 | # redirection and pty allocation works correctly. |
8d1ea537 CB |
10 | |
11 | # This library is free software; you can redistribute it and/or | |
12 | # modify it under the terms of the GNU Lesser General Public | |
13 | # License as published by the Free Software Foundation; either | |
14 | # version 2.1 of the License, or (at your option) any later version. | |
15 | ||
16 | # This library is distributed in the hope that it will be useful, | |
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | # Lesser General Public License for more details. | |
20 | ||
21 | # You should have received a copy of the GNU Lesser General Public | |
22 | # License along with this library; if not, write to the Free Software | |
23 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | ||
25 | set -e | |
26 | ||
172d397e CB |
27 | # NOTE: |
28 | # lxc-attach allocates a pty on the host and attaches any standard file | |
29 | # descriptors that refer to a pty to it. Standard file descriptors which do not | |
30 | # refer to a pty are not attached. In order to determine whether lxc-attach | |
31 | # works correctly we test various methods of redirection on the host. E.g.: | |
32 | # | |
33 | # lxc-attach -n busy -- hostname < /dev/null | |
34 | # | |
35 | # This is done to check whether the file descriptor that gets redirected to | |
36 | # /dev/null is (a) left alone and (b) that lxc-attach does not fail. When | |
37 | # lxc-attach fails we know that it's behavior has been altered, e.g. by trying | |
38 | # to attach a standard file descriptor that does not refer to a pty. | |
39 | # The small table preceeding each test case show which standard file descriptors | |
40 | # we expect to be attached to a pty and which we expect to be redirected. E.g. | |
41 | # | |
42 | # stdin --> attached to pty | |
43 | # stdout --> attached to pty | |
44 | # stderr --> attached to pty | |
45 | ||
e96e5034 CB |
46 | allocate_pty="nopty" |
47 | ||
8d1ea537 CB |
48 | FAIL() { |
49 | echo -n "Failed " >&2 | |
50 | echo "$*" >&2 | |
51 | lxc-destroy -n busy -f | |
52 | exit 1 | |
53 | } | |
54 | ||
172d397e | 55 | # Create a container, start it and wait for it to be in running state. |
8d1ea537 CB |
56 | lxc-create -t busybox -n busy || FAIL "creating busybox container" |
57 | lxc-start -n busy -d || FAIL "starting busybox container" | |
58 | lxc-wait -n busy -s RUNNING || FAIL "waiting for busybox container to run" | |
59 | ||
e96e5034 CB |
60 | if [ -t 0 ] && [ -t 1 ] && [ -t 2 ]; then |
61 | allocate_pty="pty" | |
62 | echo "All standard file descriptors refer to a pty." | |
63 | echo "Tests for lxc-attach pty allocation and I/O redirection" | |
64 | echo "will be performed correctly." | |
65 | fi | |
66 | ||
172d397e CB |
67 | # stdin --> attached to pty |
68 | # stdout --> attached to pty | |
69 | # stderr --> attached to pty | |
e96e5034 CB |
70 | for i in `seq 1 100`; do |
71 | attach=$(lxc-attach -n busy -- hostname || FAIL "to allocate or setup pty") | |
72 | if [ "$attach" != "busy" ]; then | |
73 | FAIL "lxc-attach -n busy -- hostname" | |
74 | fi | |
75 | done | |
8d1ea537 | 76 | |
172d397e CB |
77 | # stdin --> /dev/null |
78 | # stdout --> attached to pty | |
79 | # stderr --> attached to pty | |
e96e5034 | 80 | attach=$(lxc-attach -n busy -- hostname < /dev/null || FAIL "to allocate or setup pty") |
8d1ea537 | 81 | if [ "$attach" != "busy" ]; then |
e96e5034 | 82 | FAIL "lxc-attach -n busy -- hostname < /dev/null" |
8d1ea537 CB |
83 | fi |
84 | ||
172d397e CB |
85 | # stdin --> attached to pty |
86 | # stdout --> /dev/null | |
87 | # stderr --> attached to pty | |
e96e5034 | 88 | attach=$(lxc-attach -n busy -- hostname > /dev/null || FAIL "to allocate or setup pty") |
8d1ea537 | 89 | if [ -n "$attach" ]; then |
e96e5034 | 90 | FAIL "lxc-attach -n busy -- hostname > /dev/null" |
8d1ea537 CB |
91 | fi |
92 | ||
172d397e CB |
93 | # stdin --> attached to pty |
94 | # stdout --> attached to pty | |
95 | # stderr --> /dev/null | |
e96e5034 | 96 | attach=$(lxc-attach -n busy -- hostname 2> /dev/null || FAIL "to allocate or setup pty") |
172d397e | 97 | if [ "$attach" != "busy" ]; then |
e96e5034 | 98 | FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null" |
8d1ea537 CB |
99 | fi |
100 | ||
172d397e CB |
101 | # stdin --> /dev/null |
102 | # stdout --> attached to pty | |
103 | # stderr --> /dev/null | |
e96e5034 | 104 | attach=$(lxc-attach -n busy -- hostname 2> /dev/null < /dev/null || FAIL "to allocate or setup pty") |
8d1ea537 | 105 | if [ "$attach" != "busy" ]; then |
e96e5034 | 106 | FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null" |
8d1ea537 CB |
107 | fi |
108 | ||
172d397e CB |
109 | # Use a synthetic reproducer in container to produce output on stderr. stdout on |
110 | # the host gets redirect to /dev/null. We should still be able to receive | |
111 | # containers output on stderr on the host. (The command is run in a subshell. | |
112 | # This allows us to redirect stderr to stdout for the subshell and capture the | |
113 | # output in the attach variable.) | |
114 | # stdin --> attached to pty | |
115 | # stdout --> /dev/null | |
116 | # stderr --> attached to pty | |
e96e5034 | 117 | attach=$( ( lxc-attach -n busy -- sh -c 'hostname >&2' > /dev/null ) 2>&1 || FAIL "to allocate or setup pty") |
8d1ea537 | 118 | if [ "$attach" != "busy" ]; then |
e96e5034 | 119 | FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' > /dev/null" |
8d1ea537 CB |
120 | fi |
121 | ||
172d397e CB |
122 | # Use a synthetic reproducer in container to produce output on stderr. stderr on |
123 | # the host gets redirect to /dev/null. We should not receive output on stderr on | |
124 | # the host. (The command is run in a subshell. This allows us to redirect stderr | |
125 | # to stdout for the subshell and capture the output in the attach variable.) | |
126 | # stdin --> attached to pty | |
127 | # stdout --> attach to pty | |
128 | # stderr --> /dev/null | |
e96e5034 | 129 | attach=$( ( lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null ) 2>&1 || FAIL "to allocate or setup pty") |
8d1ea537 | 130 | if [ -n "$attach" ]; then |
e96e5034 | 131 | FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null" |
8d1ea537 CB |
132 | fi |
133 | ||
172d397e CB |
134 | |
135 | # stdin --> attached to pty | |
136 | # stdout --> /dev/null | |
137 | # stderr --> attached to pty | |
138 | # (As we expect the exit code of the command to be 1 we ignore it.) | |
139 | attach=$(lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null || true) | |
140 | if [ -n "$attach" ]; then | |
e96e5034 | 141 | FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null" |
172d397e CB |
142 | fi |
143 | ||
144 | ||
8d1ea537 | 145 | # - stdin --> attached to pty |
172d397e | 146 | # - stdout --> attached to pty |
8d1ea537 | 147 | # - stderr --> /dev/null |
172d397e CB |
148 | # (As we expect the exit code of the command to be 1 we ignore it.) |
149 | attach=$(lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null || true) | |
150 | if [ -z "$attach" ]; then | |
e96e5034 | 151 | FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null" |
172d397e CB |
152 | fi |
153 | ||
154 | # stdin --> $in | |
155 | # stdout --> attached to pty | |
156 | # stderr --> attached to pty | |
e96e5034 | 157 | attach=$(echo hostname | lxc-attach -n busy -- || FAIL "to allocate or setup pty") |
172d397e | 158 | if [ "$attach" != "busy" ]; then |
e96e5034 | 159 | FAIL "echo hostname | lxc-attach -n busy --" |
172d397e CB |
160 | fi |
161 | ||
162 | # stdin --> attached to pty | |
163 | # stdout --> $out | |
164 | # stderr --> $err | |
8d1ea537 CB |
165 | out=$(mktemp /tmp/out_XXXX) |
166 | err=$(mktemp /tmp/err_XXXX) | |
172d397e | 167 | trap "rm -f $out $err" EXIT INT QUIT PIPE |
e96e5034 | 168 | lxc-attach -n busy -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err || FAIL "to allocate or setup pty" |
8d1ea537 CB |
169 | outcontent=$(cat $out) |
170 | errcontent=$(cat $err) | |
171 | if [ "$outcontent" != "OUT" ] || [ "$errcontent" != "ERR" ]; then | |
e96e5034 | 172 | FAIL "lxc-attach -n busy -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err" |
8d1ea537 CB |
173 | fi |
174 | ||
e96e5034 CB |
175 | rm -f $out $err |
176 | ||
172d397e CB |
177 | # stdin --> $in |
178 | # stdout --> $out | |
179 | # stderr --> $err | |
180 | # (As we expect the exit code of the command to be 1 we ignore it.) | |
181 | out=$(mktemp /tmp/out_XXXX) | |
182 | err=$(mktemp /tmp/err_XXXX) | |
183 | trap "rm -f $out $err" EXIT INT QUIT PIPE | |
184 | echo "hostname; rm" | lxc-attach -n busy > $out 2> $err || true | |
185 | outcontent=$(cat $out) | |
186 | errcontent=$(cat $err) | |
187 | if [ "$outcontent" != "busy" ] || [ -z "$errcontent" ]; then | |
e96e5034 | 188 | FAIL "echo 'hostname; rm' | lxc-attach -n busy > $out 2> $err" |
8d1ea537 CB |
189 | fi |
190 | ||
e96e5034 CB |
191 | rm -f $out $err |
192 | ||
8d1ea537 CB |
193 | lxc-destroy -n busy -f |
194 | ||
195 | exit 0 |