#!/usr/bin/env bash-shlib
# -*- mode: shell-script -*-

include shunit

depends sed grep git mkdir readlink

export -f matches
export grep

tmp=/tmp
tprog="../bin/compose-core"
tprog=$(readlink -f $tprog)


export PATH=".:$PATH"
short_tprog=$(basename "$tprog")


##
## Convenience function
##

init_test() {
    test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
    cd "$test_tmpdir"
    export CACHEDIR="$test_tmpdir/.cache"
    export VARDIR="$test_tmpdir/.var"
    mkdir -p "$CACHEDIR"
}



tear_test() {
    rm -rf "$test_tmpdir"
}


##
## Tests
##


function test_uses {

    init_test

    export CHARM_STORE=$test_tmpdir
    mkdir -p  $test_tmpdir/www/actions
    cat <<EOF2 > $test_tmpdir/www/metadata.yml
docker-image: bar   ## required as we want relation to know the base image
uses:
  myrelation: myrelationdef
EOF2

    cat <<EOF2 > $test_tmpdir/compose.yml
web_site:
  charm: www
EOF2

    export DISABLE_SYSTEM_CONFIG_FILE=true

    assert_list <<EOF

### Testing _get_services_uses

## -- simple case with only one service with one relation

cd "$test_tmpdir"
. "$tprog" || exit 1

_setup_state_dir


out=\$(_get_services_uses web_site | tr '\0' ':')
expected="web_site:myrelation:myrelationdef:"

[[ "\$out" == "\$expected" ]] || {
    echo "doesn't end with: \$expected" >&2
    echo "\$out"
    exit 1
}

## -- service not defined in compose, but has charm

cd "$test_tmpdir"
. "$tprog" || exit 1

_setup_state_dir

out=\$(_get_services_uses www | tr '\0' ':') || exit 2
expected="www:myrelation:myrelationdef:"

[[ "\$out" == "\$expected" ]] || {
    echo "doesn't end with: \$expected" >&2
    echo "\$out"
    exit 1
}


## -- service not defined in compose, nor has charm should fail

cd "$test_tmpdir"
. "$tprog" || exit 1

_setup_state_dir

_get_services_uses xxx || exit 0

echo "Expected it to fail"
exit 1

EOF

    tear_test
}


function test_provides {

    init_test

    export CHARM_STORE=$test_tmpdir
    mkdir -p  $test_tmpdir/www/actions
    cat <<EOF2 > $test_tmpdir/www/metadata.yml
docker-image: bar   ## required as we want relation to know the base image
provides:
  myrelation: myrelationdef
EOF2

    cat <<EOF2 > $test_tmpdir/compose.yml
web_site:
  charm: www
EOF2

    export DISABLE_SYSTEM_CONFIG_FILE=true

    assert_list <<EOF

### Testing _get_services_provides

## -- simple case with only one service with one relation

cd "$test_tmpdir"
. "$tprog" || exit 1

_setup_state_dir


out=\$(_get_services_provides web_site | tr '\0' ':')
expected="web_site:myrelation:myrelationdef:"

[[ "\$out" == "\$expected" ]] || {
    echo "doesn't end with: \$expected" >&2
    echo "\$out"
    exit 1
}


## -- service not defined in compose, but has charm

cd "$test_tmpdir"
. "$tprog" || exit 1

_setup_state_dir


out=\$(_get_services_provides www | tr '\0' ':')
expected="www:myrelation:myrelationdef:"

[[ "\$out" == "\$expected" ]] || {
    echo "doesn't end with: \$expected" >&2
    echo "\$out"
    exit 1
}


## -- service not defined in compose, nor has charm should fail

cd "$test_tmpdir"
. "$tprog" || exit 1

_setup_state_dir

out=\$(set -o pipefail
  _get_provides_uses xxx | tr '\0' :) || exit 0

echo "Expected it to fail"
exit 1


EOF

    tear_test
}




continue_on_error="0" testbench $*