#!/usr/bin/env bash

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

teardown_file(){
  _undeploy_app
  _rm_app
  _rm_server
}

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

teardown(){
  _rm "$BATS_TMPDIR/*.txt"
  _rm_remote "/etc/*.txt"

  _rm "$BATS_TMPDIR/mydir"
}

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

  run $ABRA app cp DOESNTEXIST
  assert_failure
}

@test "error if missing src/dest arguments" {
  run $ABRA app cp "$TEST_APP_DOMAIN"
  assert_failure

  run $ABRA app cp "$TEST_APP_DOMAIN" myfile.txt
  assert_failure
}

@test "either src/dest has correct syntax" {
  run $ABRA app cp "$TEST_APP_DOMAIN" myfile.txt app
  assert_failure
  assert_output --partial 'arguments must take $SERVICE:$PATH form'

  run $ABRA app cp "$TEST_APP_DOMAIN" app .
  assert_failure
  assert_output --partial 'arguments must take $SERVICE:$PATH form'
}

# bats test_tags=slow
@test "error if local file missing" {
  run $ABRA app cp "$TEST_APP_DOMAIN" thisfileshouldnotexist.txt app:/somewhere
  assert_failure
  assert_output --partial 'local stat thisfileshouldnotexist.txt: no such file or directory'
}

# bats test_tags=slow
@test "error if service doesn't exist" {
  _mkfile "$BATS_TMPDIR/myfile.txt" "foo"

  run $ABRA app cp "$TEST_APP_DOMAIN" "$BATS_TMPDIR/myfile.txt" doesnt_exist:/ --debug
  assert_failure
  assert_output --partial 'no containers matching'
}

# bats test_tags=slow
@test "copy local file to container directory" {
  _mkfile "$BATS_TMPDIR/myfile.txt" "foo"

  run $ABRA app cp "$TEST_APP_DOMAIN" "$BATS_TMPDIR/myfile.txt" app:/etc
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app cat /etc/myfile.txt
  assert_success
  assert_output --partial "foo"
}

# bats test_tags=slow
@test "copy local file to container file (and override on remote)" {
  _mkfile "$BATS_TMPDIR/myfile.txt" "foo"

  # create
  run $ABRA app cp "$TEST_APP_DOMAIN" "$BATS_TMPDIR/myfile.txt" app:/etc/myfile.txt
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app cat /etc/myfile.txt
  assert_success
  assert_output --partial "foo"

  _mkfile "$BATS_TMPDIR/myfile.txt" "bar"

  # override
  run $ABRA app cp "$TEST_APP_DOMAIN" "$BATS_TMPDIR/myfile.txt" app:/etc/myfile.txt
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app cat /etc/myfile.txt
  assert_success
  assert_output --partial "bar"
}

# bats test_tags=slow
@test "copy local file to container file (and rename)" {
  _mkfile "$BATS_TMPDIR/myfile.txt" "foo"

  # rename
  run $ABRA app cp "$TEST_APP_DOMAIN" "$BATS_TMPDIR/myfile.txt" app:/etc/myfile2.txt
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app cat /etc/myfile2.txt
  assert_success
  assert_output --partial "foo"

  _rm "$BATS_TMPDIR/myfile.txt"
  _rm_remote "/etc/myfile2.txt"
}

# bats test_tags=slow
@test "copy local directory to container directory (and creates missing directory)" {
  _mkdir "$BATS_TMPDIR/mydir"
  _mkfile "$BATS_TMPDIR/mydir/myfile.txt" "foo"

  run $ABRA app cp "$TEST_APP_DOMAIN" "$BATS_TMPDIR/mydir" app:/etc
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app ls /etc/mydir
  assert_success
  assert_output --partial "myfile.txt"

  _rm "$BATS_TMPDIR/mydir"
  _rm_remote "/etc/mydir"
}

# bats test_tags=slow
@test "copy local files to container directory" {
  _mkdir "$BATS_TMPDIR/mydir"
  _mkfile "$BATS_TMPDIR/mydir/myfile.txt" "foo"
  _mkfile "$BATS_TMPDIR/mydir/myfile2.txt" "foo"

  run $ABRA app cp "$TEST_APP_DOMAIN" "$BATS_TMPDIR/mydir/" app:/etc
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app ls /etc/myfile.txt
  assert_success
  assert_output --partial "myfile.txt"

  run $ABRA app run "$TEST_APP_DOMAIN" app ls /etc/myfile2.txt
  assert_success
  assert_output --partial "myfile2.txt"
}

# bats test_tags=slow
@test "copy container file to local directory" {
  run $ABRA app run "$TEST_APP_DOMAIN" app -- bash -c "echo foo > /etc/myfile.txt"
  assert_success

  run $ABRA app cp "$TEST_APP_DOMAIN" app:/etc/myfile.txt "$BATS_TMPDIR"
  assert_success
  assert_exists "$BATS_TMPDIR/myfile.txt"
  assert bash -c "cat $BATS_TMPDIR/myfile.txt | grep -q foo"
}

# bats test_tags=slow
@test "copy container file to local file" {
  run $ABRA app run "$TEST_APP_DOMAIN" app -- bash -c "echo foo > /etc/myfile.txt"
  assert_success

  run $ABRA app cp "$TEST_APP_DOMAIN" app:/etc/myfile.txt "$BATS_TMPDIR/myfile.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/myfile.txt"
  assert bash -c "cat $BATS_TMPDIR/myfile.txt | grep -q foo"
}

# bats test_tags=slow
@test "copy container file to local file and rename" {
  run $ABRA app run "$TEST_APP_DOMAIN" app -- bash -c "echo foo > /etc/myfile.txt"
  assert_success

  run $ABRA app cp "$TEST_APP_DOMAIN" app:/etc/myfile.txt "$BATS_TMPDIR/myfile2.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/myfile2.txt"
  assert bash -c "cat $BATS_TMPDIR/myfile2.txt | grep -q foo"
}

# bats test_tags=slow
@test "copy container directory to local directory" {
  run $ABRA app run "$TEST_APP_DOMAIN" app -- bash -c "echo foo > /etc/myfile.txt"
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app -- bash -c "echo bar > /etc/myfile2.txt"
  assert_success

  mkdir "$BATS_TMPDIR/mydir"

  run $ABRA app cp "$TEST_APP_DOMAIN" app:/etc "$BATS_TMPDIR/mydir"
  assert_success
  assert_exists "$BATS_TMPDIR/mydir/etc/myfile.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/mydir/etc/myfile2.txt"
}

# bats test_tags=slow
@test "copy container files to local directory" {
  run $ABRA app run "$TEST_APP_DOMAIN" app -- bash -c "echo foo > /etc/myfile.txt"
  assert_success

  run $ABRA app run "$TEST_APP_DOMAIN" app -- bash -c "echo bar > /etc/myfile2.txt"
  assert_success

  mkdir "$BATS_TMPDIR/mydir"

  run $ABRA app cp "$TEST_APP_DOMAIN" app:/etc/ "$BATS_TMPDIR/mydir"
  assert_success
  assert_exists "$BATS_TMPDIR/mydir/myfile.txt"
  assert_success
  assert_exists "$BATS_TMPDIR/mydir/myfile2.txt"
}
