summary: Check that the tmp.size settings work

details: |
  Check that snapd allows to modify the size of /tmp through the 'tmp.size'
  config. Verify also the unset of the config gets things back to defaults  

environment:
  MOUNTCFG_FILE: /etc/systemd/system/tmp.mount.d/override.conf

prepare: |
  if [ -f "$MOUNTCFG_FILE" ]; then
      echo "tmpfs configuration file already present, testbed not clean"
      exit 1
  fi

restore: |
    rm -f "$MOUNTCFG_FILE"

execute: |
  echo "Ensure tmp.size is not set initially"
  test ! -f "$MOUNTCFG_FILE"
  if snap get system tmp.size; then
      echo "Error: tmp.size is unexpectedly set"
      exit 1
  fi
  def_size=$(df --output=size /tmp | tail -1)
  
  echo "Ensure setting tmp.size works"
  for size in 100 200; do
      snap set system tmp.size="$size"M
      snap get system tmp.size | MATCH "$size"M
      df -h --output=size /tmp | MATCH "$size"M
      grep '^tmpfs /tmp' /proc/mounts | MATCH nosuid,nodev
      MATCH "Options=mode=1777,strictatime,nosuid,nodev,size=${size}M" "$MOUNTCFG_FILE"
      # Check that systemd is happy with the generated override.conf
      systemctl daemon-reload
  done

  echo "Unsetting gets things back to defaults"
  snap unset system tmp.size
  if snap get system tmp.size; then
      echo "Error: tmp.size is unexpectedly set"
      exit 1
  fi
  test ! -f "$MOUNTCFG_FILE"
  cur_size=$(df --output=size /tmp | tail -1)
  # For some odd reason, resizing to the default can have a difference with the
  # old one of one page (4k), at least in GCE, so we take that into account.
  test "$cur_size" -le $((def_size + 4)) && test "$cur_size" -ge $((def_size - 4))
  systemctl daemon-reload
