github.com/containers/podman/v5@v5.1.0-rc1/hack/bats (about)

     1  #!/bin/bash
     2  #
     3  # bats wrapper - invokes bats, root & rootless, on podman system tests
     4  #
     5  
     6  ###############################################################################
     7  # BEGIN usage message
     8  
     9  usage="Usage: $0 [--root] [--rootless] [FILENAME-PATTERN[:TEST-PATTERN]]
    10  
    11  $0 is a wrapper for invoking podman system tests.
    12  
    13     --root         Run only as root
    14     --rootless     Run only as user (i.e. you)
    15     --remote       Run with podman-remote (see below)
    16  
    17     FILENAME-PATTERN Run only test files that match 'test/system/*name*',
    18                      e.g. '500' or 'net' will match 500-networking.bats.
    19  
    20     TEST-PATTERN     When appended to a filename-pattern, and you have a
    21                      modern-enough version of bats installed (i.e. Fedora
    22                      but not RHEL), runs with '--filter TEST-PATTERN' which
    23                      runs only subtests within FILENAME-PATTERH whose names
    24                      match that string.
    25  
    26     --tag=TAG      Passed on to bats as '--filter-tags TAG'
    27                    As of 2023-07-26 the only tag used is 'distro-integration'
    28  
    29     -T             Passed on to bats, which will then show timing data
    30  
    31     --help         display usage message
    32  
    33  By default, tests ./bin/podman. To test a different podman, do:
    34  
    35      \$ env PODMAN=/abs/path/to/podman $0 ....
    36  
    37  To test podman-remote, start your own servers (root and rootless) via:
    38  
    39      \$ bin/podman system service --timeout=0 &
    40      \$ sudo !!
    41  
    42  ...then invoke this script with --remote. (This script can't start the
    43  servers, because we can sudo *starting* the service but can't sudo
    44  stopping it: by the time the bats tests finish, the sudo timeout will
    45  have expired. We apologize for the inconvenience.)
    46  
    47  $0 also passes through \$OCI_RUNTIME, should you need to test that.
    48  
    49  Examples:
    50  
    51      \$ $0 220:\"restart cleans up\"
    52         ... only the \"restart cleans up\" test in 220-healthcheck.bats
    53  
    54      \$ $0 --root 160:\"ps -f\"
    55         ... runs all tests in 160-volumes.bats that match \"ps -f\" (root only)
    56  "
    57  
    58  # END   usage message
    59  ###############################################################################
    60  # BEGIN initialization and command-line arg checking
    61  
    62  # By default, test the podman in our working directory.
    63  # Some tests cd out of our workdir, so abs path is important
    64  export PODMAN=${PODMAN:-$(pwd)/bin/podman}
    65  export QUADLET=${QUADLET:-$(pwd)/bin/quadlet}
    66  
    67  # Directory in which
    68  TESTS=test/system
    69  
    70  REMOTE=
    71  TEST_ROOT=1
    72  TEST_ROOTLESS=1
    73  
    74  declare -a bats_opts=()
    75  
    76  declare -a bats_filter=()
    77  
    78  for i;do
    79      value=`expr "$i" : '[^=]*=\(.*\)'`
    80      case "$i" in
    81          -h|--help)  echo "$usage"; exit 0;;
    82          --root)     TEST_ROOTLESS= ;;
    83          --rootless) TEST_ROOT= ;;
    84          --remote)   REMOTE=remote ;;
    85          --ts|-T)    bats_opts+=("-T") ;;
    86          --tag=*)    bats_filter=("--filter-tags" "$value") ;;
    87          */*.bats)   TESTS=$i ;;
    88          *)
    89              if [[ $i =~ : ]]; then
    90                  tname=${i%:*}          # network:localhost -> network
    91                  filt=${i#*:}           # network:localhost ->   localhost
    92                  TESTS=$(echo $TESTS/*$tname*.bats)
    93                  bats_filter=("--filter" "$filt")
    94              else
    95                  TESTS=$(echo $TESTS/*$i*.bats)
    96              fi
    97              ;;
    98      esac
    99  done
   100  
   101  # With --remote, use correct binary and make sure daem--I mean server--is live
   102  if [[ "$REMOTE" ]]; then
   103      if ! [[ $PODMAN =~ -remote ]]; then
   104          PODMAN=${PODMAN}-remote
   105      fi
   106  
   107      if [[ -n "$TEST_ROOT" ]]; then
   108          sudo $PODMAN info >/dev/null || exit 1
   109      fi
   110      if [[ -n "$TEST_ROOTLESS" ]]; then
   111          $PODMAN info >/dev/null || exit 1
   112      fi
   113  fi
   114  
   115  # END   initialization and command-line arg checking
   116  ###############################################################################
   117  
   118  rc=0
   119  
   120  # As of 2021-11 podman has a bunch of external helper binaries
   121  if [[ -z "$CONTAINERS_HELPER_BINARY_DIR" ]]; then
   122      export CONTAINERS_HELPER_BINARY_DIR=$(pwd)/bin
   123  fi
   124  
   125  # Used in 120-load test to identify rootless destination for podman image scp
   126  export PODMAN_ROOTLESS_USER=$(id -un)
   127  
   128  # Root
   129  if [[ "$TEST_ROOT" ]]; then
   130      echo "# bats ${bats_filter[*]} $TESTS"
   131      sudo    --preserve-env=PODMAN \
   132              --preserve-env=QUADLET \
   133              --preserve-env=PODMAN_TEST_DEBUG \
   134              --preserve-env=OCI_RUNTIME \
   135              --preserve-env=CONTAINERS_HELPER_BINARY_DIR \
   136              --preserve-env=PODMAN_ROOTLESS_USER \
   137              bats "${bats_opts[@]}" "${bats_filter[@]}" $TESTS
   138      rc=$?
   139  fi
   140  
   141  # Rootless. (Only if we're not already root)
   142  if [[ "$TEST_ROOTLESS" && "$(id -u)" != 0 ]]; then
   143      echo "--------------------------------------------------"
   144      echo "\$ bats ${bats_filter[*]} $TESTS"
   145      bats "${bats_opts[@]}" "${bats_filter[@]}" $TESTS
   146      rc=$((rc | $?))
   147  fi
   148  
   149  exit $rc