#!/usr/bin/env bash

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

teardown_file(){
  _rm_app
  _rm_server
}

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

teardown(){
  _undeploy_app
}

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

  run $ABRA app restart DOESNTEXIST
  assert_failure
}

@test "error if service missing" {
  run $ABRA app restart "$TEST_APP_DOMAIN"
  assert_failure
}

@test "error if not deployed" {
  run $ABRA app restart "$TEST_APP_DOMAIN" app
  assert_failure
  assert_output --partial 'is not deployed'
}

# bats test_tags=slow
@test "app is restarted" {
  _deploy_app

  run $ABRA app restart "$TEST_APP_DOMAIN" app --debug
  assert_success
  assert_output --regexp 'attempting to scale .* to 0'
  assert_output --regexp 'attempting to scale .* to 1'
  assert_output --partial 'service successfully restarted'
}

@test "cannot use <service> and --all-services together" {
  run $ABRA app restart "$TEST_APP_DOMAIN" app --all-services
  assert_failure
  assert_output --regexp "cannot use .* together"
}

# bats test_tags=slow
@test "all services are restarted" {
  _deploy_app

  run $ABRA app restart "$TEST_APP_DOMAIN" --all-services --debug
  assert_success
}
