#!/bin/bash
. /usr/share/beakerlib/beakerlib.sh || exit 1

function check_current_topology () {
    topology_file=$1
    name=$2
    role=$3
    hostname=$4

    rlAssertEquals "Current guest name"     "$name"     "$(yq '.guest.name' $topology_file)"
    rlAssertEquals "Current guest role"     "$role"     "$(yq '.guest.role' $topology_file)"
    rlAssertEquals "Current guest hostname" "$hostname" "$(yq '.guest.hostname' $topology_file)"
}

function check_shared_topology () {
    rlAssertEquals "Guest names"        "client-1 client-2 server" "$(yq '."guest-names" | sort | join(" ")' $1)"
    rlAssertEquals "Role names"         "client server"            "$(yq '."role-names" | sort | join(" ")' $1)"
    rlAssertEquals "Client role guests" "client-1 client-2"        "$(yq '.roles.client | sort | join(" ")' $1)"
    rlAssertEquals "Server role guests" "server"                   "$(yq '.roles.server | sort | join(" ")' $1)"

    rlAssertEquals "Guest client-1 name"     "client-1"          "$(yq '.guests["client-1"].name' $1)"
    rlAssertEquals "Guest client-1 role"     "client"            "$(yq '.guests["client-1"].role' $1)"
    rlAssertEquals "Guest client-1 hostname" "$client1_hostname" "$(yq '.guests["client-1"].hostname' $1)"

    rlAssertEquals "Guest client-2 name"     "client-2"          "$(yq '.guests["client-2"].name' $1)"
    rlAssertEquals "Guest client-2 role"     "client"            "$(yq '.guests["client-2"].role' $1)"
    rlAssertEquals "Guest client-2 hostname" "$client2_hostname" "$(yq '.guests["client-2"].hostname' $1)"

    rlAssertEquals "Guest server name"     "server"              "$(yq '.guests["server"].name' $1)"
    rlAssertEquals "Guest server role"     "server"              "$(yq '.guests["server"].role' $1)"
    rlAssertEquals "Guest server hostname" "$server_hostname"    "$(yq '.guests["server"].hostname' $1)"
}

rlJournalStart
    rlPhaseStartSetup
        rlRun "run=\$(mktemp -d)" 0 "Create run directory"
        rlRun "pushd data"
        rlRun "set -o pipefail"
    rlPhaseEnd

    rlPhaseStartTest "Prepare"
        rlRun -s "tmt -vvv run --scratch --id $run discover provision prepare plan -n plans"

        rlRun "grep '^            #1: essential-requires on client-1 (client), client-2 (client) and server (server)' $rlRun_LOG"
        rlRun "grep '^            #2: default-0 on client-1 (client) and client-2 (client)' $rlRun_LOG"
        rlRun "grep '^            #3: default-1 on server (server)' $rlRun_LOG"

        rlRun "grep '^        prepare task #1: essential-requires on client-1 (client), client-2 (client) and server (server)' $rlRun_LOG"
        rlRun "grep '^\\[client-1 (client)\\]         how: install' $rlRun_LOG"
        rlRun "grep '^\\[client-1 (client)\\]         summary: Install essential required packages' $rlRun_LOG"
        rlRun "grep '^\\[client-2 (client)\\]         how: install' $rlRun_LOG"
        rlRun "grep '^\\[client-2 (client)\\]         summary: Install essential required packages' $rlRun_LOG"
        rlRun "grep '^\\[server (server)\\]           how: install' $rlRun_LOG"
        rlRun "grep '^\\[server (server)\\]           summary: Install essential required packages' $rlRun_LOG"

        rlRun "grep '^        prepare task #2: default-0 on client-1 (client) and client-2 (client)' $rlRun_LOG"
        rlRun "grep '^\\[client-1 (client)\\]         how: shell' $rlRun_LOG"
        rlRun "grep '^\\[client-1 (client)\\]         overview: 3 scripts found' $rlRun_LOG"
        rlRun "grep '^\\[client-2 (client)\\]         how: shell' $rlRun_LOG"
        rlRun "grep '^\\[client-2 (client)\\]         overview: 3 scripts found' $rlRun_LOG"

        rlRun "grep '^        prepare task #3: default-1 on server (server)' $rlRun_LOG"
        rlRun "grep '^        how: shell' $rlRun_LOG"
        rlRun "grep '^        guest: server' $rlRun_LOG"
        rlRun "grep '^        overview: 3 scripts found' $rlRun_LOG"

        client1_hostname="$(yq '."client-1" | .container' $run/plans/provision/guests.yaml)"
        client2_hostname="$(yq '."client-2" | .container' $run/plans/provision/guests.yaml)"
        server_hostname="$(yq '."server" | .container' $run/plans/provision/guests.yaml)"

        rlRun "client1_topology_yaml=$(grep -Po '(?<=^\[client-1 \(client\)\]             stdout: TMT_TOPOLOGY_YAML=).*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "client2_topology_yaml=$(grep -Po '(?<=^\[client-2 \(client\)\]             stdout: TMT_TOPOLOGY_YAML=).*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "server_topology_yaml=$(grep -Po '(?<=^            stdout: TMT_TOPOLOGY_YAML=).*' $rlRun_LOG | sed -e "s/\r//g")"

        rlRun "client1_topology_sh=$(grep -Po '(?<=^\[client-1 \(client\)\]             stdout: TMT_TOPOLOGY_BASH=).*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "client2_topology_sh=$(grep -Po '(?<=^\[client-2 \(client\)\]             stdout: TMT_TOPOLOGY_BASH=).*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "server_topology_sh=$(grep -Po '(?<=^            stdout: TMT_TOPOLOGY_BASH=).*' $rlRun_LOG | sed -e "s/\r//g")"

        check_current_topology "$client1_topology_yaml" "client-1" "client" "$client1_hostname"
        check_current_topology "$client2_topology_yaml" "client-2" "client" "$client2_hostname"
        check_current_topology "$server_topology_yaml"  "server"   "server" "$server_hostname"
        check_shared_topology "$client1_topology_yaml"
        check_shared_topology "$client2_topology_yaml"
        check_shared_topology "$server_topology_yaml"
    rlPhaseEnd

    rlPhaseStartTest "Execute"
        rlRun -s "tmt -vvv run --scratch --id $run discover provision execute finish cleanup plan -n plans"

        rlRun "grep 'summary: 7 tests executed' $rlRun_LOG"

        rlRun "grep '^            #1: server-setup on server (server)' $rlRun_LOG"
        rlRun "grep '^            #2: tests on client-1 (client), client-2 (client) and server (server)' $rlRun_LOG"
        rlRun "grep '^            #3: teardown on client-1 (client), client-2 (client) and server (server)' $rlRun_LOG"

        rlRun "grep '^        execute task #1: server-setup on server (server)' $rlRun_LOG"
        rlRun "grep '^                ..:..:.. pass /server-setup/tests/A (on server (server)) \\[1/1\\]' $rlRun_LOG"

        rlRun "grep  '^        execute task #2: tests on client-1 (client), client-2 (client) and server (server)' $rlRun_LOG"
        rlRun "grep  '^\\[client-1 (client)\\]                 ..:..:.. pass /tests/tests/B (on client-1 (client)) \\[1/1\\]' $rlRun_LOG"
        rlRun "grep  '^\\[client-2 (client)\\]                 ..:..:.. pass /tests/tests/B (on client-2 (client)) \\[1/1\\]' $rlRun_LOG"
        rlRun "grep  '^\\[server (server)\\]                   ..:..:.. pass /tests/tests/B (on server (server)) \\[1/1\\]' $rlRun_LOG"

        rlRun "grep '^        execute task #3: teardown on client-1 (client), client-2 (client) and server (server)' $rlRun_LOG"
        rlRun "grep '^\\[server (server)\\]                   ..:..:.. pass /teardown/tests/C (on server (server)) \\[1/1\\]' $rlRun_LOG"
        rlRun "grep '^\\[client-1 (client)\\]                 ..:..:.. pass /teardown/tests/C (on client-1 (client)) \\[1/1\\]' $rlRun_LOG"
        rlRun "grep '^\\[client-2 (client)\\]                 ..:..:.. pass /teardown/tests/C (on client-2 (client)) \\[1/1\\]' $rlRun_LOG"

        client1_hostname="$(yq '."client-1" | .container' $run/plans/provision/guests.yaml)"
        client2_hostname="$(yq '."client-2" | .container' $run/plans/provision/guests.yaml)"
        server_hostname="$(yq '."server" | .container' $run/plans/provision/guests.yaml)"

        rlRun "client1_topology_yaml=$(grep -Po '(?<=^\[client-1 \(client\)\]                 stdout: TMT_TOPOLOGY_YAML=).*B-2.*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "client2_topology_yaml=$(grep -Po '(?<=^\[client-2 \(client\)\]                 stdout: TMT_TOPOLOGY_YAML=).*B-2.*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "server_topology_yaml=$(grep -Po '(?<=^\[server \(server\)\]                   stdout: TMT_TOPOLOGY_YAML=).*B-2.*' $rlRun_LOG | sed -e "s/\r//g")"

        rlRun "client1_topology_sh=$(grep -Po '(?<=^\[client-1 \(client\)\]                 stdout: TMT_TOPOLOGY_BASH=).*B-2.*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "client2_topology_sh=$(grep -Po '(?<=^\[client-2 \(client\)\]                 stdout: TMT_TOPOLOGY_BASH=).*B-2.*' $rlRun_LOG | sed -e "s/\r//g")"
        rlRun "server_topology_sh=$(grep -Po '(?<=^\[server \(server\)\]                   stdout: TMT_TOPOLOGY_BASH=).*B-2.*' $rlRun_LOG | sed -e "s/\r//g")"

        check_current_topology "$client1_topology_yaml" "client-1" "client" "$client1_hostname"
        check_current_topology "$client2_topology_yaml" "client-2" "client" "$client2_hostname"
        check_current_topology "$server_topology_yaml"  "server"   "server" "$server_hostname"
        check_shared_topology "$client1_topology_yaml"
        check_shared_topology "$client2_topology_yaml"
        check_shared_topology "$server_topology_yaml"
    rlPhaseEnd

    rlPhaseStartTest "Require Test Skipped on Client-2 with error return code"
        rlRun -s "tmt -vvv run --scratch --id $run plan -n req_test_fail" 2
        rlAssertGrep "Required test '/tests/tests/D' on guest 'client-2' was skipped." $rlRun_LOG
    rlPhaseEnd

    rlPhaseStartCleanup
        rlRun "popd"
        rlRun "rm -r $run" 0 "Remove run directory"
    rlPhaseEnd
rlJournalEnd
