]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-lxc-attach
Merge pull request #4003 from brauner/2021-10-19.fixes
[mirror_lxc.git] / src / tests / lxc-test-lxc-attach
CommitLineData
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
25set -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
46allocate_pty="nopty"
47
161e4bef
CB
48ATTACH_LOG=$(mktemp --dry-run)
49
8d1ea537
CB
50FAIL() {
51 echo -n "Failed " >&2
52 echo "$*" >&2
161e4bef
CB
53 cat "${ATTACH_LOG}"
54 rm -f "${ATTACH_LOG}" || true
8d1ea537
CB
55 lxc-destroy -n busy -f
56 exit 1
57}
58
172d397e 59# Create a container, start it and wait for it to be in running state.
161e4bef
CB
60lxc-create -t busybox -n busy -l trace -o "${ATTACH_LOG}" || FAIL "creating busybox container"
61lxc-start -n busy -d -l trace -o "${ATTACH_LOG}" || FAIL "starting busybox container"
62lxc-wait -n busy -s RUNNING -l trace -o "${ATTACH_LOG}" || FAIL "waiting for busybox container to run"
8d1ea537 63
e96e5034
CB
64if [ -t 0 ] && [ -t 1 ] && [ -t 2 ]; then
65 allocate_pty="pty"
66 echo "All standard file descriptors refer to a pty."
67 echo "Tests for lxc-attach pty allocation and I/O redirection"
68 echo "will be performed correctly."
69fi
70
172d397e
CB
71# stdin --> attached to pty
72# stdout --> attached to pty
73# stderr --> attached to pty
e96e5034 74for i in `seq 1 100`; do
161e4bef 75 attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname || FAIL "to allocate or setup pty")
e96e5034
CB
76 if [ "$attach" != "busy" ]; then
77 FAIL "lxc-attach -n busy -- hostname"
78 fi
79done
8d1ea537 80
172d397e
CB
81# stdin --> /dev/null
82# stdout --> attached to pty
83# stderr --> attached to pty
161e4bef 84attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname < /dev/null || FAIL "to allocate or setup pty")
8d1ea537 85if [ "$attach" != "busy" ]; then
e96e5034 86 FAIL "lxc-attach -n busy -- hostname < /dev/null"
8d1ea537
CB
87fi
88
172d397e
CB
89# stdin --> attached to pty
90# stdout --> /dev/null
91# stderr --> attached to pty
161e4bef 92attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname > /dev/null || FAIL "to allocate or setup pty")
8d1ea537 93if [ -n "$attach" ]; then
e96e5034 94 FAIL "lxc-attach -n busy -- hostname > /dev/null"
8d1ea537
CB
95fi
96
172d397e
CB
97# stdin --> attached to pty
98# stdout --> attached to pty
99# stderr --> /dev/null
161e4bef 100attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname 2> /dev/null || FAIL "to allocate or setup pty")
172d397e 101if [ "$attach" != "busy" ]; then
e96e5034 102 FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null"
8d1ea537
CB
103fi
104
172d397e
CB
105# stdin --> /dev/null
106# stdout --> attached to pty
107# stderr --> /dev/null
161e4bef 108attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname 2> /dev/null < /dev/null || FAIL "to allocate or setup pty")
8d1ea537 109if [ "$attach" != "busy" ]; then
e96e5034 110 FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null"
8d1ea537
CB
111fi
112
172d397e
CB
113# Use a synthetic reproducer in container to produce output on stderr. stdout on
114# the host gets redirect to /dev/null. We should still be able to receive
115# containers output on stderr on the host. (The command is run in a subshell.
116# This allows us to redirect stderr to stdout for the subshell and capture the
117# output in the attach variable.)
118# stdin --> attached to pty
119# stdout --> /dev/null
120# stderr --> attached to pty
161e4bef 121attach=$( ( lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'hostname >&2' > /dev/null ) 2>&1 || FAIL "to allocate or setup pty")
8d1ea537 122if [ "$attach" != "busy" ]; then
e96e5034 123 FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' > /dev/null"
8d1ea537
CB
124fi
125
172d397e
CB
126# Use a synthetic reproducer in container to produce output on stderr. stderr on
127# the host gets redirect to /dev/null. We should not receive output on stderr on
128# the host. (The command is run in a subshell. This allows us to redirect stderr
129# to stdout for the subshell and capture the output in the attach variable.)
130# stdin --> attached to pty
131# stdout --> attach to pty
132# stderr --> /dev/null
161e4bef 133attach=$( ( lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'hostname >&2' 2> /dev/null ) 2>&1 || FAIL "to allocate or setup pty")
8d1ea537 134if [ -n "$attach" ]; then
e96e5034 135 FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null"
8d1ea537
CB
136fi
137
172d397e
CB
138
139# stdin --> attached to pty
140# stdout --> /dev/null
141# stderr --> attached to pty
142# (As we expect the exit code of the command to be 1 we ignore it.)
161e4bef 143attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'rm 2>&1' > /dev/null || true)
172d397e 144if [ -n "$attach" ]; then
e96e5034 145 FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null"
172d397e
CB
146fi
147
148
8d1ea537 149# - stdin --> attached to pty
172d397e 150# - stdout --> attached to pty
8d1ea537 151# - stderr --> /dev/null
172d397e 152# (As we expect the exit code of the command to be 1 we ignore it.)
161e4bef 153attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'rm 2>&1' 2> /dev/null || true)
172d397e 154if [ -z "$attach" ]; then
e96e5034 155 FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null"
172d397e
CB
156fi
157
158# stdin --> $in
159# stdout --> attached to pty
160# stderr --> attached to pty
161e4bef 161attach=$(echo hostname | lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- || FAIL "to allocate or setup pty")
172d397e 162if [ "$attach" != "busy" ]; then
e96e5034 163 FAIL "echo hostname | lxc-attach -n busy --"
172d397e
CB
164fi
165
166# stdin --> attached to pty
167# stdout --> $out
168# stderr --> $err
8d1ea537
CB
169out=$(mktemp /tmp/out_XXXX)
170err=$(mktemp /tmp/err_XXXX)
172d397e 171trap "rm -f $out $err" EXIT INT QUIT PIPE
161e4bef 172lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err || FAIL "to allocate or setup pty"
8d1ea537
CB
173outcontent=$(cat $out)
174errcontent=$(cat $err)
175if [ "$outcontent" != "OUT" ] || [ "$errcontent" != "ERR" ]; then
e96e5034 176 FAIL "lxc-attach -n busy -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err"
8d1ea537
CB
177fi
178
e96e5034
CB
179rm -f $out $err
180
172d397e
CB
181# stdin --> $in
182# stdout --> $out
183# stderr --> $err
184# (As we expect the exit code of the command to be 1 we ignore it.)
185out=$(mktemp /tmp/out_XXXX)
186err=$(mktemp /tmp/err_XXXX)
187trap "rm -f $out $err" EXIT INT QUIT PIPE
161e4bef 188echo "hostname; rm" | lxc-attach -n busy -l trace -o "${ATTACH_LOG}" > $out 2> $err || true
172d397e
CB
189outcontent=$(cat $out)
190errcontent=$(cat $err)
191if [ "$outcontent" != "busy" ] || [ -z "$errcontent" ]; then
e96e5034 192 FAIL "echo 'hostname; rm' | lxc-attach -n busy > $out 2> $err"
8d1ea537
CB
193fi
194
e96e5034
CB
195rm -f $out $err
196
8d1ea537 197lxc-destroy -n busy -f
161e4bef 198rm -f "${ATTACH_LOG}" || true
8d1ea537
CB
199
200exit 0