github.com/containers/podman/v4@v4.9.4/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  # Because 'make' doesn't do this by default
    68  chcon -t container_runtime_exec_t $PODMAN
    69  
    70  # Directory in which
    71  TESTS=test/system
    72  
    73  REMOTE=
    74  TEST_ROOT=1
    75  TEST_ROOTLESS=1
    76  
    77  declare -a bats_opts=()
    78  
    79  declare -a bats_filter=()
    80  
    81  for i;do
    82      value=`expr "$i" : '[^=]*=\(.*\)'`
    83      case "$i" in
    84          -h|--help)  echo "$usage"; exit 0;;
    85          --root)     TEST_ROOTLESS= ;;
    86          --rootless) TEST_ROOT= ;;
    87          --remote)   REMOTE=remote ;;
    88          --ts|-T)    bats_opts+=("-T") ;;
    89          --tag=*)    bats_filter=("--filter-tags" "$value") ;;
    90          */*.bats)   TESTS=$i ;;
    91          *)
    92              if [[ $i =~ : ]]; then
    93                  tname=${i%:*}          # network:localhost -> network
    94                  filt=${i#*:}           # network:localhost ->   localhost
    95                  TESTS=$(echo $TESTS/*$tname*.bats)
    96                  bats_filter=("--filter" "$filt")
    97              else
    98                  TESTS=$(echo $TESTS/*$i*.bats)
    99              fi
   100              ;;
   101      esac
   102  done
   103  
   104  # With --remote, use correct binary and make sure daem--I mean server--is live
   105  if [[ "$REMOTE" ]]; then
   106      if ! [[ $PODMAN =~ -remote ]]; then
   107          PODMAN=${PODMAN}-remote
   108      fi
   109  
   110      if [[ -n "$TEST_ROOT" ]]; then
   111          sudo $PODMAN info >/dev/null || exit 1
   112      fi
   113      if [[ -n "$TEST_ROOTLESS" ]]; then
   114          $PODMAN info >/dev/null || exit 1
   115      fi
   116  fi
   117  
   118  # END   initialization and command-line arg checking
   119  ###############################################################################
   120  
   121  rc=0
   122  
   123  # As of 2021-11 podman has a bunch of external helper binaries
   124  if [[ -z "$CONTAINERS_HELPER_BINARY_DIR" ]]; then
   125      export CONTAINERS_HELPER_BINARY_DIR=$(pwd)/bin
   126  fi
   127  
   128  # Used in 120-load test to identify rootless destination for podman image scp
   129  export PODMAN_ROOTLESS_USER=$(id -un)
   130  
   131  # Root
   132  if [[ "$TEST_ROOT" ]]; then
   133      echo "# bats ${bats_filter[*]} $TESTS"
   134      sudo    --preserve-env=PODMAN \
   135              --preserve-env=QUADLET \
   136              --preserve-env=PODMAN_TEST_DEBUG \
   137              --preserve-env=OCI_RUNTIME \
   138              --preserve-env=CONTAINERS_HELPER_BINARY_DIR \
   139              --preserve-env=PODMAN_ROOTLESS_USER \
   140              bats "${bats_opts[@]}" "${bats_filter[@]}" $TESTS
   141      rc=$?
   142  fi
   143  
   144  # Rootless. (Only if we're not already root)
   145  if [[ "$TEST_ROOTLESS" && "$(id -u)" != 0 ]]; then
   146      echo "--------------------------------------------------"
   147      echo "\$ bats ${bats_filter[*]} $TESTS"
   148      bats "${bats_opts[@]}" "${bats_filter[@]}" $TESTS
   149      rc=$((rc | $?))
   150  fi
   151  
   152  exit $rc