# Disposable WANGuard Console for running this extension's integration tests.
#
# WANGuard is commercial software, but Andrisoft's package repository is public
# and the REST API turns out to work without a licence key, so this builds a
# fully usable API target unattended. See docs/topics/testing.md.
#
#   vagrant up
#   eval "$(vagrant ssh -c 'sudo cat /vagrant/.wanguard-test-env')"
#   python -m pytest tests/integration --run-destructive
#
# The REST API works without a licence key, so this needs no manual steps. Only
# the licence_manager endpoint is unavailable, and that test skips itself.
#
# Never point the integration tests at a production Console.

WANGUARD_VERSION = ENV.fetch("WANGUARD_VERSION", "9.0")

# Andrisoft supports both AlmaLinux 9 and 10 for WANGuard 9.x and publishes an
# install guide for each. This defaults to 9 anyway, because AlmaLinux 10.2
# ships PHP 8.4 while the SourceGuardian loader bundled with wanrestapi 9.0-3
# (and the current build on sourceguardian.com) supports at most PHP 8.3. On
# 10.2 the Console and API return HTTP 500 until Andrisoft ships a newer loader.
#
# AlmaLinux 9 offers PHP 8.1/8.2/8.3 module streams, all within range.
#
# To try 10 anyway:  WANGUARD_BOX=almalinux/10 WANGUARD_REPO_DIST=rhel10 vagrant up
BOX = ENV.fetch("WANGUARD_BOX", "almalinux/9")

# The repo path segment has to match the box's major version.
REPO_DIST = ENV.fetch("WANGUARD_REPO_DIST", BOX.end_with?("/10") ? "rhel10" : "rhel9")

Vagrant.configure("2") do |config|
  config.vm.box = BOX
  config.vm.hostname = "wanguard-lab"

  # The Console runs MariaDB, Apache/PHP, InfluxDB and the supervisor. It is not
  # happy in less than this.
  memory = Integer(ENV.fetch("WANGUARD_MEMORY", "4096"))
  cpus = Integer(ENV.fetch("WANGUARD_CPUS", "2"))

  config.vm.provider :libvirt do |lv|
    lv.memory = memory
    lv.cpus = cpus
  end

  config.vm.provider :virtualbox do |vb|
    vb.memory = memory
    vb.cpus = cpus
  end

  config.vm.network "private_network", ip: ENV.fetch("WANGUARD_IP", "192.168.56.30")

  config.vm.provision "shell",
    path: "tests/vagrant/provision.sh",
    env: {
      "WANGUARD_VERSION" => WANGUARD_VERSION,
      "REPO_DIST" => REPO_DIST,
      "WANGUARD_IP" => ENV.fetch("WANGUARD_IP", "192.168.56.30"),
    }
end
