/usr/bin/lxc-test-lxc-attach is in lxc-tests 2.0.7-0ubuntu1~16.04.2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | #!/bin/sh
# 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.
# This library is free software; you can redistribute it and/or
# 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.:
#
# lxc-attach -n busy -- hostname < /dev/null
#
# This is done to check whether the file descriptor that gets redirected to
# /dev/null is (a) left alone and (b) that lxc-attach does not fail. When
# 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"
FAIL() {
echo -n "Failed " >&2
echo "$*" >&2
lxc-destroy -n busy -f
exit 1
}
# Create a container, start it and wait for it to be in running state.
lxc-create -t busybox -n busy || FAIL "creating busybox container"
lxc-start -n busy -d || FAIL "starting busybox container"
lxc-wait -n busy -s RUNNING || FAIL "waiting for busybox container to run"
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
for i in `seq 1 100`; do
attach=$(lxc-attach -n busy -- hostname || FAIL "to allocate or setup pty")
if [ "$attach" != "busy" ]; then
FAIL "lxc-attach -n busy -- hostname"
fi
done
# stdin --> /dev/null
# stdout --> attached to pty
# stderr --> attached to pty
attach=$(lxc-attach -n busy -- hostname < /dev/null || FAIL "to allocate or setup pty")
if [ "$attach" != "busy" ]; then
FAIL "lxc-attach -n busy -- hostname < /dev/null"
fi
# stdin --> attached to pty
# stdout --> /dev/null
# stderr --> attached to pty
attach=$(lxc-attach -n busy -- hostname > /dev/null || FAIL "to allocate or setup pty")
if [ -n "$attach" ]; then
FAIL "lxc-attach -n busy -- hostname > /dev/null"
fi
# stdin --> attached to pty
# stdout --> attached to pty
# stderr --> /dev/null
attach=$(lxc-attach -n busy -- hostname 2> /dev/null || FAIL "to allocate or setup pty")
if [ "$attach" != "busy" ]; then
FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null"
fi
# stdin --> /dev/null
# stdout --> attached to pty
# stderr --> /dev/null
attach=$(lxc-attach -n busy -- hostname 2> /dev/null < /dev/null || FAIL "to allocate or setup pty")
if [ "$attach" != "busy" ]; then
FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null"
fi
# Use a synthetic reproducer in container to produce output on stderr. stdout on
# the host gets redirect to /dev/null. We should still be able to receive
# 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
# stdout --> /dev/null
# 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
FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' > /dev/null"
fi
# Use a synthetic reproducer in container to produce output on stderr. stderr on
# the host gets redirect to /dev/null. We should not receive 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
# stdout --> attach to pty
# stderr --> /dev/null
attach=$( ( lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null ) 2>&1 || FAIL "to allocate or setup pty")
if [ -n "$attach" ]; then
FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null"
fi
# stdin --> attached to pty
# stdout --> /dev/null
# stderr --> attached to pty
# (As we expect the exit code of the command to be 1 we ignore it.)
attach=$(lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null || true)
if [ -n "$attach" ]; then
FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null"
fi
# - stdin --> attached to pty
# - stdout --> attached to pty
# - stderr --> /dev/null
# (As we expect the exit code of the command to be 1 we ignore it.)
attach=$(lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null || true)
if [ -z "$attach" ]; then
FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null"
fi
# stdin --> $in
# stdout --> attached to pty
# stderr --> attached to pty
attach=$(echo hostname | lxc-attach -n busy -- || FAIL "to allocate or setup pty")
if [ "$attach" != "busy" ]; then
FAIL "echo hostname | lxc-attach -n busy --"
fi
# stdin --> attached to pty
# stdout --> $out
# stderr --> $err
out=$(mktemp /tmp/out_XXXX)
err=$(mktemp /tmp/err_XXXX)
trap "rm -f $out $err" EXIT INT QUIT PIPE
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)
if [ "$outcontent" != "OUT" ] || [ "$errcontent" != "ERR" ]; then
FAIL "lxc-attach -n busy -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err"
fi
rm -f $out $err
# stdin --> $in
# stdout --> $out
# stderr --> $err
# (As we expect the exit code of the command to be 1 we ignore it.)
out=$(mktemp /tmp/out_XXXX)
err=$(mktemp /tmp/err_XXXX)
trap "rm -f $out $err" EXIT INT QUIT PIPE
echo "hostname; rm" | lxc-attach -n busy > $out 2> $err || true
outcontent=$(cat $out)
errcontent=$(cat $err)
if [ "$outcontent" != "busy" ] || [ -z "$errcontent" ]; then
FAIL "echo 'hostname; rm' | lxc-attach -n busy > $out 2> $err"
fi
rm -f $out $err
if [ $allocate_pty = "pty" ]; then
# Test whether logging pty output to a file works.
trap "rm -f /tmp/ptylog" EXIT INT QUIT PIPE
lxc-attach -n busy -L /tmp/ptylog -- hostname || FAIL "to allocate or setup pty"
if [ ! -s /tmp/ptylog ]; then
FAIL "lxc-attach -n busy -L /tmp/ptylog -- hostname"
fi
rm -f /tmp/ptylog
fi
lxc-destroy -n busy -f
exit 0
|