#!/bin/bash
cvmfs_test_name="Non-Zero Return Code on Failing Repo-Management Commands"
cvmfs_test_autofs_on_startup=false
cvmfs_test_suites="quick"

cvmfs_run_test() {
  logfile=$1

  echo "create a fresh repository named $CVMFS_TEST_REPO with user $CVMFS_TEST_USER"
  create_repo $CVMFS_TEST_REPO $CVMFS_TEST_USER || return $?

  echo "starting transaction to edit repository"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "starting another transaction (should fail)"
  start_transaction $CVMFS_TEST_REPO && return 1

  echo "aborting transaction"
  abort_transaction $CVMFS_TEST_REPO || return $?

  echo "aborting transaction again (should fail)"
  abort_transaction $CVMFS_TEST_REPO && return 2

  echo "starting a transaction"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "publish repository"
  publish_repo $CVMFS_TEST_REPO || return $?

  echo "publish again (should fail)"
  publish_repo $CVMFS_TEST_REPO && return 3

  echo "open transaction for rollback"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "rollback to trunk-previous (should fail - in a transaction)"
  rollback_repo $CVMFS_TEST_REPO "trunk-previous" && return 4

  echo "abort transaction"
  abort_transaction $CVMFS_TEST_REPO || return $?

  echo "rollback to trunc-previous (should fail - unknown tag)"
  rollback_repo $CVMFS_TEST_REPO "trunc-previous" && return 5

  echo "rollback to trunk-previous"
  rollback_repo $CVMFS_TEST_REPO "trunk-previous" || return $?

  echo "inspect unknown tag (should fail)"
  cvmfs_server tag -i "foobar" $CVMFS_TEST_REPO && return 6

  echo "list unknown repo (should fail)"
  cvmfs_server tag -l ${CVMFS_TEST_REPO}.unobtainium && return 7

  echo "delete unknown tag (should fail)"
  cvmfs_server tag -r trunc-previous -f $CVMFS_TEST_REPO && return 8

  echo "open transaction"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "try to add a tag (should fail - due to transaction)"
  cvmfs_server tag -a "foobar" -m "this fails!" $CVMFS_TEST_REPO && return 9

  echo "abort transaction"
  abort_transaction $CVMFS_TEST_REPO || return $?

  echo "destroy and create repo to get new keys"
  local repo_key="/etc/cvmfs/keys/${CVMFS_TEST_REPO}.key"
  local repo_cert="/etc/cvmfs/keys/${CVMFS_TEST_REPO}.crt"
  cp $repo_key $repo_cert .
  destroy_repo $CVMFS_TEST_REPO || return $?
  create_repo $CVMFS_TEST_REPO $CVMFS_TEST_USER || return $?

  echo "restore old repo keys"
  sudo mv `basename $repo_key` $repo_key
  sudo mv `basename $repo_cert` $repo_cert

  # If we now don't resign whitelist before attempting to publish, the
  #  publish will actually succeed once.  In any case, at this point
  #  both resign for .cvmfswhitelist and resign -p for .cvmfspublished
  #  are needed before the repo will fully recover.
  echo "resign whitelist"
  sudo cvmfs_server resign $CVMFS_TEST_REPO || return $?

  echo "attempt publish with old repo keys (should fail)"
  start_transaction $CVMFS_TEST_REPO && return 10

  echo "forcing the repo to look unhealthy"
  # resign -p should work even if the repository isn't completely healthy
  #  so force it to be unhealthy by unmounting /cvmfs/$CVMFS_TEST_REPO
  sudo umount /cvmfs/$CVMFS_TEST_REPO || return $?

  echo "resign published"
  sudo cvmfs_server resign -p $CVMFS_TEST_REPO || return 12

  echo "and resign whitelist"
  sudo cvmfs_server resign $CVMFS_TEST_REPO || return 13

  echo "and restart the transaction"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "make sure that a publish now works"
  publish_repo $CVMFS_TEST_REPO || return 14

  echo "resign whitelist with broken master key (should fail)"
  local master_key="/etc/cvmfs/keys/${CVMFS_TEST_REPO}.masterkey"
  sudo cp $master_key .
  echo "this is not valid!" | sudo tee $master_key || return 15
  sudo cvmfs_server resign $CVMFS_TEST_REPO && return 16

  echo "save whitelist and remove repository"
  curl $(get_repo_url)/.cvmfswhitelist > .cvmfswhitelist
  destroy_repo $CVMFS_TEST_REPO || return $?

  echo "remove again (should fail)"
  destroy_repo $CVMFS_TEST_REPO && return 17

  echo "restore masterkey and set a trap for cleaning it"
  trap "sudo rm -rf $master_key" EXIT HUP INT TERM
  sudo mv `basename $master_key` $master_key

  echo "resign whitelist without requiring repo config"
  sudo cvmfs_server resign -w .cvmfswhitelist $CVMFS_TEST_REPO || return $?

  echo "try again with reduced expiration time"
  sudo cvmfs_server resign -d 7 -w .cvmfswhitelist $CVMFS_TEST_REPO || return $?

  echo "attempt to resign published without full repo config (should fail)"
  sudo cvmfs_server resign -p $CVMFS_TEST_REPO && return 18

  echo "remove masterkey and try to resign again (should fail)"
  sudo rm -rf $master_key
  trap - EXIT HUP INT TERM

  sudo cvmfs_server resign -w .cvmfswhitelist $CVMFS_TEST_REPO && return 19

  return 0
}

