#!/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 "ls validate app argument" {
  run $ABRA app volume ls
  assert_failure
  assert_output --partial 'no app provided'

  run $ABRA app volume ls DOESNTEXIST
  assert_failure
  assert_output --partial 'cannot find app'
}

@test "list no volumes" {
  run $ABRA app volume ls "$TEST_APP_DOMAIN"
  assert_success
  assert_output --partial 'no volumes created'
}

# bats test_tags=slow
@test "list volumes" {
  _deploy_app

  run $ABRA app volume ls "$TEST_APP_DOMAIN"
  assert_success
  assert_output --partial 'test-volume'
}

@test "rm validate app argument" {
  run $ABRA app volume rm
  assert_failure
  assert_output --partial 'no app provided'

  run $ABRA app volume rm DOESNTEXIST
  assert_failure
  assert_output --partial 'cannot find app'
}

# bats test_tags=slow
@test "rm error if deployed" {
  _deploy_app

  run $ABRA app volume rm "$TEST_APP_DOMAIN" --force
  assert_failure
  assert_output --partial 'is still deployed'
}

# bats test_tags=slow
@test "remove volumes" {
  sleep 3 # NOTE(d1): hack to avoid "network not found"

  _deploy_app
  _undeploy_app

  run $ABRA app volume rm "$TEST_APP_DOMAIN" --force
  assert_success
  assert_output --partial 'volumes removed successfully'
}

# bats test_tags=slow
@test "remove no volumes" {
  sleep 3 # NOTE(d1): hack to avoid "network not found"

  _deploy_app
  _undeploy_app

  run $ABRA app volume rm "$TEST_APP_DOMAIN" --force
  assert_success
  assert_output --partial 'volumes removed successfully'

  run $ABRA app volume rm "$TEST_APP_DOMAIN" --force
  assert_success
  assert_output --partial 'no volumes removed'
}
