#!/usr/bin/env bash

setup_file(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
  _add_server
  _new_app
}

teardown_file(){
  _rm_app
  _rm_server
  _reset_recipe
}

setup(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
}

teardown(){
  _reset_app
  _undeploy_app
  _reset_recipe
}

@test "validate app argument" {
  run $ABRA app rollback
  assert_failure

  run $ABRA app rollback DOESNTEXIST
  assert_failure
}

@test "retrieve recipe if missing" {
  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE"
  assert_success
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE"

  run $ABRA app rollback "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_failure
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE"
}

@test "ensure recipe up to date if no --offline" {
  wantHash=$(_get_n_hash 3)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3
  assert_success

  assert_equal $(_get_current_hash) "$wantHash"

  run $ABRA app rollback "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_failure

  assert_equal $(_get_current_hash) $(_get_head_hash)
}

@test "ensure recipe not up to date if --offline" {
  _ensure_env_version "0.1.0+1.20.0"
  latestRelease=$(_latest_release)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
  assert_success

  run $ABRA app rollback "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --offline
  assert_failure

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l
  refute_output --partial "$latestRelease"
}

@test "error if not already deployed" {
  run $ABRA app rollback "$TEST_APP_DOMAIN" --no-input
  assert_failure
  assert_output --partial 'not deployed'
}

# bats test_tags=slow
@test "no rollback if on oldest version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app rollback "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success
  assert_output --partial "no available downgrades"
}

# bats test_tags=slow
@test "error if specific version is not downgrade" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app rollback "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_failure
  assert_output --partial 'is not a downgrade'
}

# bats test_tags=slow
@test "rollback to previous version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.1+1.20.2" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.1+1.20.2'

  run $ABRA app rollback "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success
  assert_output --partial "0.1.0+1.20.0"
}

# bats test_tags=slow
@test "force rollback to previous version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.1+1.20.2" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.1+1.20.2'

  run $ABRA app rollback "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial "0.1.0+1.20.0"

  run $ABRA app rollback "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_failure
  assert_output --partial "not a downgrade"

  run $ABRA app rollback "$TEST_APP_DOMAIN" "0.1.0+1.20.0" \
    --no-input --no-converge-checks --force
  assert_success
  assert_output --partial "0.1.0+1.20.0"
}

@test "force rollback with no available downgrades" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" \
    --no-input --no-converge-checks
  assert_success

  run $ABRA app rollback "$TEST_APP_DOMAIN" \
    --force --no-input --no-converge-checks
  assert_success
  assert_output --partial "0.1.0+1.20.0"
}

# bats test_tags=slow
@test "rollback to a version 2 tags behind" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.2.0+1.21.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial "0.2.0+1.21.0"

  run $ABRA app rollback "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial "0.1.0+1.20.0"
}

# bats test_tags=slow
@test "rollback chaos deployment" {
  tagHash=$(_get_tag_hash "0.2.0+1.21.0")
  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout "$tagHash"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks --chaos
  assert_success
  assert_output --partial "${tagHash:0:8}"

  run $ABRA app rollback "$TEST_APP_DOMAIN" "0.1.1+1.20.2" --no-input --no-converge-checks
  assert_success
  assert_output --partial "0.1.1+1.20.2"
  assert_output --partial "${tagHash:0:8}"

  run $ABRA app rollback "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial "0.1.0+1.20.0"

  tagHash=$(_get_tag_hash "0.1.1+1.20.2")
  refute_output --partial "${tagHash:0:8}"
  assert_output --partial "false"
}

# bats test_tags=slow
@test "chaos commit rollback not possible" {
  _deploy_app

  tagHash=$(_get_tag_hash "0.2.0+1.21.0")

  run $ABRA app rollback "$TEST_APP_DOMAIN" "$tagHash" --no-input --no-converge-checks
  assert_failure
  assert_output --partial "not a known version"
}

# bats test_tags=slow
@test "no chaos version label if no chaos" {
  _deploy_app

  run $ABRA app rollback "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks
  assert_success

  run $ABRA app labels "$TEST_APP_DOMAIN" --no-input
  assert_success
  refute_output --regexp "coop-cloud.abra-test-recipe.$TEST_SERVER.chaos-version"
}
