8d1ea537851718553358a4a9767274f893b40420Christian Brauner# lxc: linux Container library
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# Christian Brauner <christian.brauner@mailbox.org>
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# This is a test script for the lxc-attach program. It tests whether I/O
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# redirection and pty allocation works correctly.
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# This library is free software; you can redistribute it and/or
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# modify it under the terms of the GNU Lesser General Public
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# License as published by the Free Software Foundation; either
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# version 2.1 of the License, or (at your option) any later version.
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# This library is distributed in the hope that it will be useful,
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# but WITHOUT ANY WARRANTY; without even the implied warranty of
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# Lesser General Public License for more details.
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# You should have received a copy of the GNU Lesser General Public
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# License along with this library; if not, write to the Free Software
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# lxc-attach allocates a pty on the host and attaches any standard file
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# descriptors that refer to a pty to it. Standard file descriptors which do not
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# refer to a pty are not attached. In order to determine whether lxc-attach
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# works correctly we test various methods of redirection on the host. E.g.:
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# lxc-attach -n busy -- hostname < /dev/null
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# This is done to check whether the file descriptor that gets redirected to
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# /dev/null is (a) left alone and (b) that lxc-attach does not fail. When
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# lxc-attach fails we know that it's behavior has been altered, e.g. by trying
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# to attach a standard file descriptor that does not refer to a pty.
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# The small table preceeding each test case show which standard file descriptors
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# we expect to be attached to a pty and which we expect to be redirected. E.g.
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# Create a container, start it and wait for it to be in running state.
8d1ea537851718553358a4a9767274f893b40420Christian Braunerlxc-create -t busybox -n busy || FAIL "creating busybox container"
8d1ea537851718553358a4a9767274f893b40420Christian Braunerlxc-start -n busy -d || FAIL "starting busybox container"
8d1ea537851718553358a4a9767274f893b40420Christian Braunerlxc-wait -n busy -s RUNNING || FAIL "waiting for busybox container to run"
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner echo "All standard file descriptors refer to a pty."
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner echo "Tests for lxc-attach pty allocation and I/O redirection"
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner echo "will be performed correctly."
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> attached to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner attach=$(lxc-attach -n busy -- hostname || FAIL "to allocate or setup pty")
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> attached to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerattach=$(lxc-attach -n busy -- hostname < /dev/null || FAIL "to allocate or setup pty")
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- hostname < /dev/null"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> attached to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerattach=$(lxc-attach -n busy -- hostname > /dev/null || FAIL "to allocate or setup pty")
8d1ea537851718553358a4a9767274f893b40420Christian Braunerif [ -n "$attach" ]; then
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- hostname > /dev/null"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> attached to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerattach=$(lxc-attach -n busy -- hostname 2> /dev/null || FAIL "to allocate or setup pty")
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> attached to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerattach=$(lxc-attach -n busy -- hostname 2> /dev/null < /dev/null || FAIL "to allocate or setup pty")
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# Use a synthetic reproducer in container to produce output on stderr. stdout on
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# the host gets redirect to /dev/null. We should still be able to receive
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# containers output on stderr on the host. (The command is run in a subshell.
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# This allows us to redirect stderr to stdout for the subshell and capture the
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# output in the attach variable.)
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> attached to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerattach=$( ( lxc-attach -n busy -- sh -c 'hostname >&2' > /dev/null ) 2>&1 || FAIL "to allocate or setup pty")
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' > /dev/null"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# Use a synthetic reproducer in container to produce output on stderr. stderr on
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# the host gets redirect to /dev/null. We should not receive output on stderr on
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# the host. (The command is run in a subshell. This allows us to redirect stderr
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# to stdout for the subshell and capture the output in the attach variable.)
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> attach to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerattach=$( ( lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null ) 2>&1 || FAIL "to allocate or setup pty")
8d1ea537851718553358a4a9767274f893b40420Christian Braunerif [ -n "$attach" ]; then
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# (As we expect the exit code of the command to be 1 we ignore it.)
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Braunerattach=$(lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null || true)
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Braunerif [ -n "$attach" ]; then
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null"
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# - stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# - stdout --> attached to pty
8d1ea537851718553358a4a9767274f893b40420Christian Brauner# - stderr --> /dev/null
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# (As we expect the exit code of the command to be 1 we ignore it.)
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Braunerattach=$(lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null || true)
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Braunerif [ -z "$attach" ]; then
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> $in
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> attached to pty
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerattach=$(echo hostname | lxc-attach -n busy -- || FAIL "to allocate or setup pty")
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "echo hostname | lxc-attach -n busy --"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> attached to pty
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> $out
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> $err
74d4266f734b778dd2abdd03345a825c590ce38bChristian Braunerlxc-attach -n busy -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err || FAIL "to allocate or setup pty"
8d1ea537851718553358a4a9767274f893b40420Christian Braunerif [ "$outcontent" != "OUT" ] || [ "$errcontent" != "ERR" ]; then
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "lxc-attach -n busy -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err"
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdin --> $in
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stdout --> $out
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# stderr --> $err
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Brauner# (As we expect the exit code of the command to be 1 we ignore it.)
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Braunerecho "hostname; rm" | lxc-attach -n busy > $out 2> $err || true
172d397e6f3451b1bf973ab705a2ca40e3bfd23fChristian Braunerif [ "$outcontent" != "busy" ] || [ -z "$errcontent" ]; then
74d4266f734b778dd2abdd03345a825c590ce38bChristian Brauner FAIL "echo 'hostname; rm' | lxc-attach -n busy > $out 2> $err"
e6487e89824c184f99bf3478122aa3bc2bed86d8Christian Brauner # Test whether logging pty output to a file works.
e6487e89824c184f99bf3478122aa3bc2bed86d8Christian Brauner trap "rm -f /tmp/ptylog" EXIT INT QUIT PIPE
e6487e89824c184f99bf3478122aa3bc2bed86d8Christian Brauner lxc-attach -n busy -L /tmp/ptylog -- hostname || FAIL "to allocate or setup pty"
e6487e89824c184f99bf3478122aa3bc2bed86d8Christian Brauner FAIL "lxc-attach -n busy -L /tmp/ptylog -- hostname"