#!/usr/bin/env python3

import sys

import run   # run.py in this directory

print('Scylla is: ' + run.find_scylla() + '.')

ssl = '--ssl' in sys.argv
if ssl:
    cmd = run.run_scylla_ssl_cql_cmd
    check_cql = run.check_ssl_cql
else:
    cmd = run.run_scylla_cmd
    check_cql = run.check_cql

# If the first option (TODO: improve the command line processing this up)
# is "--release", download that release (see fetch_scylla.py for supported
# release numbers), and use that.
# The downloaded Scylla will be cached in the directory build/<release>,
# where <release> is the specific release downloaded (e.g., if the user
# asks "--release 2022.1" and the downloaded release is 2022.1.9, it
# will be stored in build/2022.1.9.
if len(sys.argv) > 1 and sys.argv[1] == '--release':
    release = sys.argv[2]
    exe = run.download_precompiled_scylla(release)
    cmd = lambda pid, dir: run.run_precompiled_scylla_cmd(exe, pid, dir)
    check_cql = run.check_cql
    sys.argv = sys.argv[0:1] + sys.argv[3:]

# If the "--vnodes" option is given, drop the "tablets" experimental
# feature (turned on in run.py) so that all tests will be run with the
# old vnode-based replication instead of tablets. This option only has
# temporary usefulness, and should eventually be removed.
if '--vnodes' in sys.argv:
    sys.argv.remove('--vnodes')
    def run_without_tablets(pid, dir):
        (c, e) = run_without_tablets.orig_cmd(pid, dir)
        c.remove('--enable-tablets=true')
        # Tablets are now enabled by default on some releases, it is not enough to remove the enable above.
        c.append('--enable-tablets=false')
        return (c, e)
    run_without_tablets.orig_cmd = cmd
    cmd = run_without_tablets

if "-h" in sys.argv or "--help" in sys.argv:
    run.run_pytest(sys.path[0], sys.argv)
    exit(0)

pid = run.run_with_temporary_dir(cmd)
ip = run.pid_to_ip(pid)

run.wait_for_services(pid, [
    lambda: run.check_rest_api(ip),
    lambda: check_cql(ip)
])
success = run.run_pytest(sys.path[0], ['--host=' + ip] + sys.argv[1:])

run.summary = 'Scylla tests pass' if success else 'Scylla tests failure'

exit(0 if success else 1)

# Note that the run.cleanup_all() function runs now, just like on any exit
# for any reason in this script. It will delete the temporary files and
# announce the failure or success of the test (printing run.summary).
