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