#!/bin/bash

# Run erroneous dsh commands
#
# This script will run a large number of erroneous commands and print the output to the console.
#
# To run this test the following is expected:
# * User is logged in via the single-sign-on authentication method at platform $DSH_CLI_PLATFORM
# * Tenant $DSH_CLI_TENANT exists at platform $DSH_CLI_PLATFORM
# * User is authenticated for tenant $DSH_CLI_TENANT at platform $DSH_CLI_PLATFORM

export DSH_CLI_PLATFORM=nplz
export DSH_CLI_TENANT=greenbox-dev

export DSH_CLI_VERBOSITY="high"
export DSH_CLI_SHOW_EXECUTION_TIME=""

ERRONEOUS_COMMANDS=(
  ""
  "api --wrong"
  "api delete --wrong"
  "api delete unknown"
  "api delete unknown --wrong"
  "api get --wrong"
  "api get unknown"
  "api get unknown --wrong"
  "api head --wrong"
  "api head unknown"
  "api head unknown --wrong"
  "api patch --wrong"
  "api patch unknown"
  "api patch unknown --wrong"
  "api post --wrong"
  "api post unknown"
  "api post unknown --wrong"
  "api put --wrong"
  "api put unknown"
  "api put unknown --wrong"
  "api unknown"
  "api unknown --wrong"
  "app --wrong"
  "app list extra"
  "app list extra --wrong"
  "app show"
  "app show --wrong"
  "app unknown"
  "app unknown --wrong"
  "bucket --wrong"
  "bucket list extra"
  "bucket list extra --wrong"
  "bucket show"
  "bucket show --wrong"
  "bucket unknown"
  "bucket unknown --wrong"
  "certificate --wrong"
  "certificate list extra"
  "certificate list extra --wrong"
  "certificate show"
  "certificate show --wrong"
  "certificate unknown"
  "certificate unknown --wrong"
  "env --wrong"
  "env find --wrong"
  "env find pattern --regex --ignore-case"
  "env find pattern --regex --substring --ignore-case"
  "env find pattern --regex --substring"
  "env find"
  "env unknown --wrong"
  "env unknown"
  "image --wrong"
  "image find"
  "image find --wrong"
  "image list extra"
  "image list extra --wrong"
  "image unknown"
  "image unknown --wrong"
  "metric --wrong"
  "metric list extra"
  "metric list extra --wrong"
  "metric unknown"
  "metric unknown --wrong"
  "platform --wrong"
  "platform default extra"
  "platform default extra --wrong"
  "platform list extra"
  "platform list extra --wrong"
  "platform open --wrong"
  "platform open app"
  "platform open app --wrong"
  "platform open console extra"
  "platform open console extra --wrong"
  "platform open monitoring extra"
  "platform open monitoring extra --wrong"
  "platform open service --wrong"
  "platform open swagger extra"
  "platform open swagger extra --wrong"
  "platform open tenant extra"
  "platform open tenant extra --wrong"
  "platform open tracing extra"
  "platform open tracing extra --wrong"
  "platform open unknown"
  "platform open unknown --wrong"
  "platform show unknown"
  "platform show unknown --wrong"
  "proxy --wrong"
  "proxy delete"
  "proxy delete --wrong"
  "proxy list extra"
  "proxy list extra --wrong"
  "proxy show"
  "proxy show --wrong"
  "proxy unknown"
  "proxy unknown --wrong"
  "proxy update"
  "proxy update --wrong"
  "secret --wrong"
  "secret delete"
  "secret delete --wrong"
  "secret list extra"
  "secret list extra --wrong"
  "secret new"
  "secret new --wrong"
  "secret show"
  "secret show --wrong"
  "secret unknown"
  "secret unknown --wrong"
  "secret update"
  "secret update --wrong"
  "service --wrong"
  "service list extra"
  "service list extra --wrong"
  "service show"
  "service show --wrong"
  "service unknown"
  "service unknown --wrong"
  "setting --wrong"
  "setting list extra"
  "setting list extra --wrong"
  "setting set --wrong"
  "setting unknown"
  "setting unknown --wrong"
  "setting unset --wrong"
  "target --wrong"
  "target delete"
  "target delete --wrong"
  "target list extra"
  "target list extra --wrong"
  "target new"
  "target new --wrong"
  "target unknown"
  "target unknown --wrong"
  "topic --wrong"
  "topic delete"
  "topic delete --wrong"
  "topic list extra"
  "topic list extra --wrong"
  "topic show"
  "topic show --wrong"
  "topic unknown"
  "topic unknown --wrong"
  "unknown"
  "unknown --wrong"
  "vhost --wrong"
  "vhost list extra"
  "vhost list extra --wrong"
  "volume --wrong"
  "volume delete"
  "volume delete --wrong"
  "volume list extra"
  "volume list extra --wrong"
  "volume new"
  "volume new --wrong"
  "volume show"
  "volume show --wrong"
  "volume unknown"
  "volume unknown --wrong"
)

set -f
for COMMAND in "${ERRONEOUS_COMMANDS[@]}"
do
  CMD=$(echo "dsh $COMMAND" | envsubst)
  echo "-------------------------------"
  echo "$CMD"
  echo "-------------------------------"
  if eval "$CMD"
  then
    echo "command did not fail: $CMD"
    exit 1
  fi
  echo "-------------------------------"
  echo
done
