github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/system/helpers.t (about)

     1  #!/bin/bash
     2  #
     3  # regression tests for helpers.bash
     4  #
     5  # Some of those helper functions are fragile, and we don't want to break
     6  # anything if we have to mess with them.
     7  #
     8  
     9  source $(dirname $0)/helpers.bash
    10  
    11  die() {
    12      echo "$(basename $0): $*" >&2
    13      exit 1
    14  }
    15  
    16  # Iterator and return code; updated in check_result()
    17  testnum=0
    18  rc=0
    19  
    20  ###############################################################################
    21  # BEGIN test the parse_table helper
    22  
    23  function check_result {
    24      testnum=$(expr $testnum + 1)
    25      if [ "$1" = "$2" ]; then
    26          echo "ok $testnum $3 = $1"
    27      else
    28          echo "not ok $testnum $3"
    29          echo "#  expected: $2"
    30          echo "#    actual: $1"
    31          rc=1
    32      fi
    33  }
    34  
    35  # IMPORTANT NOTE: you have to do
    36  #      this: while ... done < <(parse_table)
    37  #   and not: parse_table | while read ...
    38  #
    39  # ...because piping to 'while' makes it a subshell, hence testnum and rc
    40  # will not be updated.
    41  #
    42  while read x y z; do
    43      check_result "$x" "a" "parse_table simple: column 1"
    44      check_result "$y" "b" "parse_table simple: column 2"
    45      check_result "$z" "c" "parse_table simple: column 3"
    46  done < <(parse_table "a | b | c")
    47  
    48  # More complicated example, with spaces
    49  while read x y z; do
    50      check_result "$x" "a b"   "parse_table with spaces: column 1"
    51      check_result "$y" "c d"   "parse_table with spaces: column 2"
    52      check_result "$z" "e f g" "parse_table with spaces: column 3"
    53  done < <(parse_table "a b | c d | e f g")
    54  
    55  # Multi-row, with spaces and with blank lines
    56  table="
    57  a     | b   | c d e
    58  d e f | g h | i j
    59  "
    60  declare -A expect=(
    61      [0,0]="a"
    62      [0,1]="b"
    63      [0,2]="c d e"
    64      [1,0]="d e f"
    65      [1,1]="g h"
    66      [1,2]="i j"
    67  )
    68  row=0
    69  while read x y z;do
    70      check_result "$x" "${expect[$row,0]}" "parse_table multi_row[$row,0]"
    71      check_result "$y" "${expect[$row,1]}" "parse_table multi_row[$row,1]"
    72      check_result "$z" "${expect[$row,2]}" "parse_table multi_row[$row,2]"
    73      row=$(expr $row + 1)
    74  done < <(parse_table "$table")
    75  
    76  # Backslash handling. The first element should have none, the second some
    77  while read x y;do
    78      check_result "$x" '[0-9]{2}'    "backslash test - no backslashes"
    79      check_result "$y" '[0-9]\{3\}'  "backslash test - one backslash each"
    80  done < <(parse_table "[0-9]{2}  | [0-9]\\\{3\\\}")
    81  
    82  # Empty strings. I wish we could convert those to real empty strings.
    83  while read x y z; do
    84      check_result "$x" "''" "empty string - left-hand"
    85      check_result "$y" "''" "empty string - middle"
    86      check_result "$z" "''" "empty string - right"
    87  done < <(parse_table " | |")
    88  
    89  # Quotes
    90  while read x y z;do
    91      check_result "$x" "a 'b c'"     "single quotes"
    92      check_result "$y" "d \"e f\" g" "double quotes"
    93      check_result "$z" "h"           "no quotes"
    94  
    95      # FIXME FIXME FIXME: this is the only way I can find to get bash-like
    96      # splitting of tokens. It really should be done inside parse_table
    97      # but I can't find any way of doing so. If you can find a way, please
    98      # update this test and any BATS tests that rely on quoting.
    99      eval set "$x"
   100      check_result "$1" "a"     "single quotes - token split - 1"
   101      check_result "$2" "b c"   "single quotes - token split - 2"
   102      check_result "$3" ""      "single quotes - token split - 3"
   103  
   104      eval set "$y"
   105      check_result "$1" "d"     "double quotes - token split - 1"
   106      check_result "$2" "e f"   "double quotes - token split - 2"
   107      check_result "$3" "g"     "double quotes - token split - 3"
   108  done < <(parse_table "a 'b c' | d \"e f\" g | h")
   109  
   110  # END   test the parse_table helper
   111  ###############################################################################
   112  # BEGIN dprint
   113  
   114  function dprint_test_1() {
   115      dprint "$*"
   116  }
   117  
   118  # parse_table works, might as well use it
   119  #
   120  #  <value of PODMAN_TEST_DEBUG> | <blank for no msg, - for msg> | <desc>
   121  #
   122  table="
   123                             |   | debug unset
   124  dprint_test                | - | substring match
   125  dprint_test_1              | - | exact match
   126  dprint_test_10             |   | caller name mismatch
   127  xxx yyy zzz                |   | multiple callers, no match
   128  dprint_test_1 xxx yyy zzz  | - | multiple callers, match at start
   129  xxx dprint_test_1 yyy zzz  | - | multiple callers, match in middle
   130  xxx yyy zzz dprint_test_1  | - | multiple callers, match at end
   131  "
   132  while read var expect name; do
   133      random_string=$(random_string 20)
   134      PODMAN_TEST_DEBUG="$var" result=$(dprint_test_1 "$random_string" 3>&1)
   135      expect_full=""
   136      if [ -n "$expect" -a "$expect" != "''" ]; then
   137          expect_full="# dprint_test_1() : $random_string"
   138      fi
   139      check_result "$result" "$expect_full" "DEBUG='$var' - $name"
   140  done < <(parse_table "$table")
   141  
   142  # END   dprint
   143  ###############################################################################
   144  
   145  exit $rc