github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/tests/integration/tty.bats (about)

     1  #!/usr/bin/env bats
     2  
     3  load helpers
     4  
     5  function setup() {
     6  	setup_busybox
     7  }
     8  
     9  function 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  	[ $EUID -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  	[ $EUID -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  	[ $EUID -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  	[ $EUID -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  }
   171  EOF
   172  	)
   173  
   174  	# Run the detached 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  	[ -e pid.txt ]
   178  
   179  	# Wait for the exec to finish.
   180  	wait_pids_gone 100 0.5 "$(cat pid.txt)"
   181  
   182  	tty_info=$(
   183  		cat <<EOF
   184  {
   185      "args": [
   186  	"/bin/cat",
   187  	"/tmp/tty-info"
   188      ],
   189      "cwd": "/"
   190  }
   191  EOF
   192  	)
   193  
   194  	# run the exec
   195  	runc exec -t -p <(echo "$tty_info") test_busybox
   196  	[ "$status" -eq 0 ]
   197  
   198  	# test tty width and height against original process.json
   199  	[[ ${lines[0]} =~ "rows 10; columns 110" ]]
   200  }
   201  
   202  @test "runc create [terminal=false]" {
   203  	# Disable terminal creation.
   204  	# Replace sh script with sleep.
   205  	update_config ' (.. | select(.terminal? != null)) .terminal |= false
   206  			| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
   207  			| del(.. | select(.? == "sh"))'
   208  
   209  	# Make sure that the handling of detached IO is done properly. See #1354.
   210  	__runc create test_busybox
   211  
   212  	# Start the command.
   213  	runc start test_busybox
   214  	[ "$status" -eq 0 ]
   215  
   216  	testcontainer test_busybox running
   217  
   218  	# Kill the container.
   219  	runc kill test_busybox KILL
   220  	[ "$status" -eq 0 ]
   221  }
   222  
   223  @test "runc run [terminal=false]" {
   224  	# Disable terminal creation.
   225  	# Replace sh script with sleep.
   226  
   227  	update_config ' (.. | select(.terminal? != null)) .terminal |= false
   228  			| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
   229  			| del(.. | select(.? == "sh"))'
   230  
   231  	# Make sure that the handling of non-detached IO is done properly. See #1354.
   232  	(
   233  		__runc run test_busybox
   234  	) &
   235  
   236  	wait_for_container 15 1 test_busybox running
   237  	testcontainer test_busybox running
   238  
   239  	# Kill the container.
   240  	runc kill test_busybox KILL
   241  	[ "$status" -eq 0 ]
   242  }
   243  
   244  @test "runc run -d [terminal=false]" {
   245  	# Disable terminal creation.
   246  	# Replace sh script with sleep.
   247  	update_config ' (.. | select(.terminal? != null)) .terminal |= false
   248  			| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
   249  			| del(.. | select(.? == "sh"))'
   250  
   251  	# Make sure that the handling of detached IO is done properly. See #1354.
   252  	__runc run -d test_busybox
   253  
   254  	testcontainer test_busybox running
   255  
   256  	# Kill the container.
   257  	runc kill test_busybox KILL
   258  	[ "$status" -eq 0 ]
   259  }