github.com/containers/podman/v5@v5.1.0-rc1/test/system/450-interactive.bats (about)

     1  # -*- bats -*-
     2  #
     3  # tests of podman commands that require an interactive pty
     4  #
     5  
     6  load helpers
     7  
     8  ###############################################################################
     9  # BEGIN setup/teardown
    10  
    11  # Each test runs with its own PTY, managed by socat.
    12  PODMAN_TEST_PTY=$(mktemp -u --tmpdir=${BATS_TMPDIR:-/tmp} podman_pty.XXXXXX)
    13  PODMAN_DUMMY=$(mktemp -u --tmpdir=${BATS_TMPDIR:-/tmp} podman_dummy.XXXXXX)
    14  PODMAN_SOCAT_PID=
    15  
    16  function setup() {
    17      basic_setup
    18  
    19      # Create a pty. Run under 'timeout' because BATS reaps child processes
    20      # and if we exit before killing socat, bats will hang forever.
    21      timeout 10 socat \
    22              PTY,link=$PODMAN_TEST_PTY,raw,echo=0 \
    23              PTY,link=$PODMAN_DUMMY,raw,echo=0 &
    24      PODMAN_SOCAT_PID=$!
    25  
    26      # Wait for pty
    27      retries=5
    28      while [[ ! -e $PODMAN_TEST_PTY ]]; do
    29          retries=$(( retries - 1 ))
    30          assert $retries -gt 0 "Timed out waiting for $PODMAN_TEST_PTY"
    31          sleep 0.5
    32      done
    33  }
    34  
    35  function teardown() {
    36      if [[ -n $PODMAN_SOCAT_PID ]]; then
    37          kill $PODMAN_SOCAT_PID
    38          PODMAN_SOCAT_PID=
    39      fi
    40      rm -f $PODMAN_TEST_PTY $PODMAN_DUMMY_PTY
    41  
    42      basic_teardown
    43  }
    44  
    45  # END   setup/teardown
    46  ###############################################################################
    47  # BEGIN tests
    48  
    49  @test "podman detects correct tty size" {
    50      # Set the pty to a random size. Make rows/columns odd/even, to guarantee
    51      # that they can never be the same
    52      rows=$(( 15 + RANDOM % 60 |   1 ))
    53      cols=$(( 15 + RANDOM % 60 & 126 ))
    54      stty rows $rows cols $cols <$PODMAN_TEST_PTY
    55  
    56      CR=$'\r'
    57  
    58      # ...and make sure stty under podman reads that.
    59      # This flakes often ("stty: standard input"), so, retry.
    60      run_podman run -it --name mystty $IMAGE stty size <$PODMAN_TEST_PTY
    61      if [[ "$output" =~ stty ]]; then
    62          echo "# stty flaked, retrying: $output" >&3
    63          run_podman rm -f mystty
    64          sleep 1
    65          run_podman run -it --name mystty $IMAGE stty size <$PODMAN_TEST_PTY
    66      fi
    67      is "$output" "$rows $cols$CR" "stty under podman run reads the correct dimensions"
    68  
    69      run_podman rm -t 0 -f mystty
    70  
    71      # FIXME: the checks below are flaking a lot (see #10710).
    72  
    73      # check that the same works for podman exec
    74  #    run_podman run -d --name mystty $IMAGE top
    75  #    run_podman exec -it mystty stty size <$PODMAN_TEST_PTY
    76  #    is "$output" "$rows $cols" "stty under podman exec reads the correct dimensions"
    77  #
    78  #    run_podman rm -t 0 -f mystty
    79  }
    80  
    81  
    82  @test "podman load - will not read from tty" {
    83      run_podman 125 load <$PODMAN_TEST_PTY
    84      is "$output" \
    85         "Error: cannot read from terminal, use command-line redirection or the --input flag" \
    86         "Diagnostic from 'podman load' without redirection or -i"
    87  }
    88  
    89  
    90  @test "podman run --tty -i failure with no tty" {
    91      run_podman 0+w run --tty -i --rm $IMAGE echo hello < /dev/null
    92      require_warning "The input device is not a TTY.*" "-it _without_ a tty"
    93  
    94      CR=$'\r'
    95      run_podman run --tty -i --rm $IMAGE echo hello <$PODMAN_TEST_PTY
    96      is "$output" "hello$CR" "-it _with_ a pty"
    97  
    98      run_podman run --tty=false -i --rm $IMAGE echo hello < /dev/null
    99      is "$output" "hello" "-tty=false: no warning"
   100  
   101      run_podman run --tty -i=false --rm $IMAGE echo hello < /dev/null
   102      is "$output" "hello$CR" "-i=false: no warning"
   103  }
   104  
   105  
   106  @test "podman run -l passthrough-tty" {
   107      skip_if_remote
   108  
   109      # Requires conmon 2.1.10 or greater
   110      want=2.1.10
   111      run_podman info --format '{{.Host.Conmon.Path}}'
   112      conmon_path="$output"
   113      conmon_version=$($conmon_path --version | sed -ne 's/^.* version //p')
   114      if ! printf "%s\n%s\n" "$want" "$conmon_version" | sort --check=quiet --version-sort; then
   115          skip "need conmon >= $want; have $conmon_version"
   116      fi
   117  
   118      run tty <$PODMAN_TEST_PTY
   119      expected_tty="$output"
   120  
   121      run_podman run -v/dev:/dev --log-driver=passthrough-tty $IMAGE tty <$PODMAN_TEST_PTY
   122      is "$output" "$expected_tty" "passthrough-tty: tty matches"
   123  }
   124  
   125  # vim: filetype=sh