...

Text file src/github.com/opencontainers/runc/tests/integration/tty.bats

Documentation: github.com/opencontainers/runc/tests/integration

     1#!/usr/bin/env bats
     2
     3load helpers
     4
     5function setup() {
     6	setup_busybox
     7}
     8
     9function teardown() {
    10	teardown_bundle
    11}
    12
    13@test "runc run [stdin not a tty]" {
    14	# stty size fails without a tty
    15	update_config '(.. | select(.[]? == "sh")) += ["-c", "stty size"]'
    16	# note that stdout/stderr are already redirected by bats' run
    17	runc run test_busybox </dev/null
    18	[ "$status" -eq 0 ]
    19}
    20
    21@test "runc run [tty ptsname]" {
    22	# Replace sh script with readlink.
    23	# shellcheck disable=SC2016
    24	update_config '(.. | select(.[]? == "sh")) += ["-c", "for file in /proc/self/fd/[012]; do readlink $file; done"]'
    25
    26	# run busybox
    27	runc run test_busybox
    28	[ "$status" -eq 0 ]
    29	[[ ${lines[0]} =~ /dev/pts/+ ]]
    30	[[ ${lines[1]} =~ /dev/pts/+ ]]
    31	[[ ${lines[2]} =~ /dev/pts/+ ]]
    32}
    33
    34@test "runc run [tty owner]" {
    35	# tty chmod is not doable in rootless containers without idmap.
    36	# TODO: this can be made as a change to the gid test.
    37	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
    38
    39	# Replace sh script with stat.
    40	# shellcheck disable=SC2016
    41	update_config '(.. | select(.[]? == "sh")) += ["-c", "stat -c %u:%g $(tty) | tr : \\\\n"]'
    42
    43	# run busybox
    44	runc run test_busybox
    45	[ "$status" -eq 0 ]
    46	[[ ${lines[0]} =~ 0 ]]
    47	# This is set by the default config.json (it corresponds to the standard tty group).
    48	[[ ${lines[1]} =~ 5 ]]
    49}
    50
    51@test "runc run [tty owner] ({u,g}id != 0)" {
    52	# tty chmod is not doable in rootless containers without idmap.
    53	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
    54
    55	# replace "uid": 0 with "uid": 1000
    56	# and do a similar thing for gid.
    57	# Replace sh script with stat.
    58	# shellcheck disable=SC2016
    59	update_config ' (.. | select(.uid? == 0)) .uid |= 1000
    60			| (.. | select(.gid? == 0)) .gid |= 100
    61			| (.. | select(.[]? == "sh")) += ["-c", "stat -c %u:%g $(tty) | tr : \\\\n"]'
    62
    63	# run busybox
    64	runc run test_busybox
    65	[ "$status" -eq 0 ]
    66	[[ ${lines[0]} =~ 1000 ]]
    67	# This is set by the default config.json (it corresponds to the standard tty group).
    68	[[ ${lines[1]} =~ 5 ]]
    69}
    70
    71@test "runc exec [stdin not a tty]" {
    72	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
    73	[ "$status" -eq 0 ]
    74
    75	# make sure we're running
    76	testcontainer test_busybox running
    77
    78	# note that stdout/stderr are already redirected by bats' run
    79	runc exec -t test_busybox sh -c "stty size" </dev/null
    80	[ "$status" -eq 0 ]
    81}
    82
    83@test "runc exec [tty ptsname]" {
    84	# run busybox detached
    85	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
    86	[ "$status" -eq 0 ]
    87
    88	# make sure we're running
    89	testcontainer test_busybox running
    90
    91	# run the exec
    92	# shellcheck disable=SC2016
    93	runc exec -t test_busybox sh -c 'for file in /proc/self/fd/[012]; do readlink $file; done'
    94	[ "$status" -eq 0 ]
    95	[[ ${lines[0]} =~ /dev/pts/+ ]]
    96	[[ ${lines[1]} =~ /dev/pts/+ ]]
    97	[[ ${lines[2]} =~ /dev/pts/+ ]]
    98}
    99
   100@test "runc exec [tty owner]" {
   101	# tty chmod is not doable in rootless containers without idmap.
   102	# TODO: this can be made as a change to the gid test.
   103	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
   104
   105	# run busybox detached
   106	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
   107	[ "$status" -eq 0 ]
   108
   109	# make sure we're running
   110	testcontainer test_busybox running
   111
   112	# run the exec
   113	# shellcheck disable=SC2016
   114	runc exec -t test_busybox sh -c 'stat -c %u:%g $(tty) | tr : \\n'
   115	[ "$status" -eq 0 ]
   116	[[ ${lines[0]} =~ 0 ]]
   117	[[ ${lines[1]} =~ 5 ]]
   118}
   119
   120@test "runc exec [tty owner] ({u,g}id != 0)" {
   121	# tty chmod is not doable in rootless containers without idmap.
   122	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
   123
   124	# replace "uid": 0 with "uid": 1000
   125	# and do a similar thing for gid.
   126	# shellcheck disable=SC2016
   127	update_config ' (.. | select(.uid? == 0)) .uid |= 1000
   128			| (.. | select(.gid? == 0)) .gid |= 100'
   129
   130	# run busybox detached
   131	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
   132	[ "$status" -eq 0 ]
   133
   134	# make sure we're running
   135	testcontainer test_busybox running
   136
   137	# run the exec
   138	# shellcheck disable=SC2016
   139	runc exec -t test_busybox sh -c 'stat -c %u:%g $(tty) | tr : \\n'
   140	[ "$status" -eq 0 ]
   141	[[ ${lines[0]} =~ 1000 ]]
   142	[[ ${lines[1]} =~ 5 ]]
   143}
   144
   145@test "runc exec [tty consolesize]" {
   146	# allow writing to filesystem
   147	update_config '(.. | select(.readonly? != null)) .readonly |= false'
   148
   149	# run busybox detached
   150	runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
   151	[ "$status" -eq 0 ]
   152
   153	# make sure we're running
   154	testcontainer test_busybox running
   155
   156	tty_info_with_consize_size=$(
   157		cat <<EOF
   158{
   159    "terminal": true,
   160    "consoleSize": {
   161	    "height": 10,
   162	    "width": 110
   163    },
   164    "args": [
   165	    "/bin/sh",
   166	    "-c",
   167	    "/bin/stty -a > /tmp/tty-info"
   168    ],
   169    "cwd": "/"
   170}
   171EOF
   172	)
   173
   174	# run the exec
   175	runc exec -t --pid-file pid.txt -d --console-socket "$CONSOLE_SOCKET" -p <(echo "$tty_info_with_consize_size") test_busybox
   176	[ "$status" -eq 0 ]
   177
   178	# check the pid was generated
   179	[ -e pid.txt ]
   180
   181	# wait for the process to finish
   182	timeout 5 tail --pid="$(head -n 1 pid.txt)" -f /dev/null
   183
   184	tty_info=$(
   185		cat <<EOF
   186{
   187    "args": [
   188	"/bin/cat",
   189	"/tmp/tty-info"
   190    ],
   191    "cwd": "/"
   192}
   193EOF
   194	)
   195
   196	# run the exec
   197	runc exec -t -p <(echo "$tty_info") test_busybox
   198	[ "$status" -eq 0 ]
   199
   200	# test tty width and height against original process.json
   201	[[ ${lines[0]} =~ "rows 10; columns 110" ]]
   202}
   203
   204@test "runc create [terminal=false]" {
   205	# Disable terminal creation.
   206	# Replace sh script with sleep.
   207	update_config ' (.. | select(.terminal? != null)) .terminal |= false
   208			| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
   209			| del(.. | select(.? == "sh"))'
   210
   211	# Make sure that the handling of detached IO is done properly. See #1354.
   212	__runc create test_busybox
   213
   214	# Start the command.
   215	runc start test_busybox
   216	[ "$status" -eq 0 ]
   217
   218	testcontainer test_busybox running
   219
   220	# Kill the container.
   221	runc kill test_busybox KILL
   222	[ "$status" -eq 0 ]
   223}
   224
   225@test "runc run [terminal=false]" {
   226	# Disable terminal creation.
   227	# Replace sh script with sleep.
   228
   229	update_config ' (.. | select(.terminal? != null)) .terminal |= false
   230			| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
   231			| del(.. | select(.? == "sh"))'
   232
   233	# Make sure that the handling of non-detached IO is done properly. See #1354.
   234	(
   235		__runc run test_busybox
   236	) &
   237
   238	wait_for_container 15 1 test_busybox running
   239	testcontainer test_busybox running
   240
   241	# Kill the container.
   242	runc kill test_busybox KILL
   243	[ "$status" -eq 0 ]
   244}
   245
   246@test "runc run -d [terminal=false]" {
   247	# Disable terminal creation.
   248	# Replace sh script with sleep.
   249	update_config ' (.. | select(.terminal? != null)) .terminal |= false
   250			| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
   251			| del(.. | select(.? == "sh"))'
   252
   253	# Make sure that the handling of detached IO is done properly. See #1354.
   254	__runc run -d test_busybox
   255
   256	testcontainer test_busybox running
   257
   258	# Kill the container.
   259	runc kill test_busybox KILL
   260	[ "$status" -eq 0 ]
   261}

View as plain text