github.com/canonical/ubuntu-image@v0.0.0-20240430122802-2202fe98b290/tests/lib/external/snapd-testing-tools/tools/tests.pkgs (about)

     1  #!/bin/bash -e
     2  
     3  show_help() {
     4      echo "usage: tests.pkgs install [--no-install-recommends] [PACKAGE...]"
     5      echo "       tests.pkgs remove [PACKAGE...]"
     6      echo "       tests.pkgs is-installed [PACKAGE]"
     7      echo "       tests.pkgs query [PACKAGE]"
     8      echo "       tests.pkgs list-installed"
     9      echo
    10      echo "Package names are standardized based on Debian package names"
    11      echo "internally, package names are re-mapped to fit the convention"
    12      echo "of the used system."
    13  }
    14  
    15  unsupported() {
    16      echo "tests.pkgs: cannot manage packages on this system" >&2
    17      exit 1
    18  }
    19  
    20  cmd_install() {
    21      # This is re-defined by the backend file.
    22      unsupported
    23  }
    24  
    25  cmd_install_local() {
    26      # This is re-defined by the backend file.
    27      unsupported
    28  }
    29  
    30  cmd_is_installed() {
    31      # This is re-defined by the backend file.
    32      unsupported
    33  }
    34  
    35  cmd_query() {
    36      # This is re-defined by the backend file.
    37      unsupported
    38  }
    39  
    40  cmd_list_installed() {
    41      # This is re-defined by the backend file.
    42      unsupported
    43  }
    44  
    45  cmd_remove() {
    46      # This is re-defined by the backend file.
    47      unsupported
    48  }
    49  
    50  remap_one() {
    51      # This may be re-defined by the backend file.
    52      echo "$1"
    53  }
    54  
    55  remap_many() {
    56      local many
    57      many=""
    58      for pkg in "$@"; do
    59          if [ -z "$many" ]; then
    60              many="$(remap_one "$pkg")"
    61          else
    62              many="$many $(remap_one "$pkg")"
    63          fi
    64      done
    65      echo "$many"
    66  }
    67  
    68  import_backend() {
    69      local TOOLS_DIR
    70      TOOLS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
    71      if [ -n "$TESTSTOOLS" ]; then
    72          TOOLS_DIR="$TESTSTOOLS"
    73      fi
    74      
    75      if os.query is-core; then
    76          echo "tests.pkgs: Ubuntu Core is not supported" >&2
    77          return 1
    78      elif os.query is-ubuntu || os.query is-debian; then
    79          # Disabled because when the project is imported as a submodule the
    80          # source is not found failing with SC1090 and SC1091
    81          #shellcheck disable=SC1090,SC1091
    82          . "$TOOLS_DIR/tests.pkgs.apt.sh"
    83      elif os.query is-fedora || os.query is-centos || os.query is-amazon-linux; then
    84          # Disabled because when the project is imported as a submodule the
    85          # source is not found failing with SC1090 and SC1091
    86          #shellcheck disable=SC1090,SC1091
    87          . "$TOOLS_DIR/tests.pkgs.dnf-yum.sh"
    88      elif os.query is-opensuse; then
    89          # Disabled because when the project is imported as a submodule the
    90          # source is not found failing with SC1090 and SC1091
    91          #shellcheck disable=SC1090,SC1091
    92          . "$TOOLS_DIR/tests.pkgs.zypper.sh"
    93      elif os.query is-arch-linux; then
    94          # Disabled because when the project is imported as a submodule the
    95          # source is not found failing with SC1090 and SC1091
    96          #shellcheck disable=SC1090,SC1091
    97          . "$TOOLS_DIR/tests.pkgs.pacman.sh"
    98      else
    99          echo "tests.pkgs: cannot import packaging backend" >&2
   100          return 1
   101      fi
   102  }
   103  
   104  main() {
   105      if [ $# -eq 0 ]; then
   106          show_help
   107          exit 1
   108      fi
   109  
   110      import_backend
   111  
   112      action=
   113      while [ $# -gt 0 ]; do
   114          case "$1" in
   115              -h|--help)
   116                  show_help
   117                  exit 0
   118                  ;;
   119              --)
   120                  shift
   121                  break
   122                  ;;
   123              install|remove|query|is-installed|list-installed)
   124                  action="$1"
   125                  shift
   126                  break  # consume remaining arguments
   127                  ;;
   128              -*)
   129                  echo "tests.pkgs: unknown option $1" >&2
   130                  exit 1
   131                  ;;
   132              *)
   133                  echo "tests.pkgs: unknown command $1" >&2
   134                  exit 1
   135                  ;;
   136          esac
   137      done
   138  
   139      case "$action" in
   140          install)
   141              # shellcheck disable=SC2046
   142              cmd_install $(remap_many "$@")
   143              ;;
   144          is-installed)
   145              cmd_is_installed "$(remap_one "$@")"
   146              ;;
   147          query)
   148              cmd_query "$(remap_one "$@")"
   149              ;;
   150          list-installed)
   151              cmd_list_installed
   152              ;;
   153          remove)
   154              cmd_remove "$(remap_many "$@")"
   155              ;;
   156          *)
   157              echo "tests.pkgs: unknown action $action" >&2
   158              exit 1
   159              ;;
   160      esac
   161  }
   162  
   163  main "$@"