/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

import org.elasticsearch.gradle.testclusters.StandaloneRestIntegTestTask

apply plugin: 'elasticsearch.internal-testclusters'
apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.bwc-test'

dependencies {
  testImplementation project(':x-pack:qa')
}

buildParams.bwcVersions.withWireCompatible { bwcVersion, baseName ->
  def baseCluster = testClusters.register(baseName) {
    testDistribution = "DEFAULT"
    versions = [bwcVersion.toString(), project.version]
    numberOfNodes = 3

    setting 'repositories.url.allowed_urls', 'http://snapshot.test*'
    setting 'xpack.security.enabled', 'false'
    setting 'xpack.ml.enabled', 'false'
    setting 'xpack.watcher.enabled', 'false'
    setting 'xpack.license.self_generated.type', 'basic'
  }

  tasks.register("${baseName}#oldClusterTest", StandaloneRestIntegTestTask) {
    useCluster baseCluster
    mustRunAfter("precommit")
    systemProperty 'tests.rest.suite', 'old_cluster'
    systemProperty 'tests.upgrade_from_version', version.toString().replace('-SNAPSHOT', '')
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo(baseName).map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.clustername', baseName)
  }

  String oldVersion = bwcVersion.toString().replace('-SNAPSHOT', '')
  tasks.register("${baseName}#oneThirdUpgradedTest", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#oldClusterTest"
    useCluster baseCluster
    doFirst {
      getRegistry().get().nextNodeToNextVersion(baseCluster)
    }
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo(baseName).map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.clustername', baseName)
    systemProperty 'tests.rest.suite', 'mixed_cluster'
    systemProperty 'tests.first_round', 'true'
    systemProperty 'tests.upgrade_from_version', oldVersion
  }

  tasks.register("${baseName}#twoThirdsUpgradedTest", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#oneThirdUpgradedTest"
    useCluster baseCluster
    doFirst {
      getRegistry().get().nextNodeToNextVersion(baseCluster)
    }
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo(baseName).map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.clustername', baseName)
    systemProperty 'tests.rest.suite', 'mixed_cluster'
    systemProperty 'tests.first_round', 'false'
    systemProperty 'tests.upgrade_from_version', oldVersion
  }

  tasks.register("${baseName}#upgradedClusterTest", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#twoThirdsUpgradedTest"
    useCluster baseCluster
    doFirst {
      getRegistry().get().nextNodeToNextVersion(baseCluster)
    }
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo(baseName).map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.clustername', baseName)
    systemProperty 'tests.rest.suite', 'upgraded_cluster'
    systemProperty 'tests.upgrade_from_version', oldVersion
  }

  tasks.register(bwcTaskName(bwcVersion)) {
    dependsOn "${baseName}#upgradedClusterTest"
  }
}


// Security is explicitly disabled, do not run tests in FIPS mode
tasks.withType(Test).configureEach {
  enabled = buildParams.inFipsJvm == false
}
