/*
 * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
 * Public License v 1"; you may not use this file except in compliance with, at
 * your election, the "Elastic License 2.0", the "GNU Affero General Public
 * License v3.0 only", or the "Server Side Public License, v 1".
 */



import org.elasticsearch.gradle.Version
import org.elasticsearch.gradle.testclusters.StandaloneRestIntegTestTask

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

buildParams.bwcVersions.withWireCompatible { bwcVersion, baseName ->
  /*
   * NOTE: This module is for the tests that were problematic when converting :qa:rolling-upgrade to the junit-based bwc test definition
   * Over time, these should be migrated into the :qa:rolling-upgrade module and fixed properly
   *
   * The goal here is to:
   * <ul>
   *  <li>start three nodes on the old version
   *  <li>run tests with systemProperty 'tests.rest.suite', 'old_cluster'
   *  <li>upgrade one node
   *  <li>run tests with systemProperty 'tests.rest.suite', 'mixed_cluster'
   *  <li>upgrade one more node
   *  <li>run tests with systemProperty 'tests.rest.suite', 'mixed_cluster' again
   *  <li>updgrade the last node
   *  <li>run tests with systemProperty 'tests.rest.suite', 'upgraded_cluster'
   * </ul>
   */

  def baseCluster = testClusters.register(baseName) {
    versions = [bwcVersion.toString(), project.version]
    numberOfNodes = 3

    setting 'repositories.url.allowed_urls', 'http://snapshot.test*'
    setting 'path.repo', "${layout.buildDirectory.get().asFile}/cluster/shared/repo/${baseName}"
    setting 'xpack.security.enabled', 'false'
    requiresFeature 'es.index_mode_feature_flag_registered', Version.fromString("8.0.0")
  }

  String oldVersion = bwcVersion.toString()

  tasks.register("${baseName}#oldClusterTest", StandaloneRestIntegTestTask) {
    dependsOn "processTestResources"
    useCluster baseCluster
    mustRunAfter("precommit")
    doFirst {
      delete("${layout.buildDirectory.get().asFile}/cluster/shared/repo/${baseName}")
    }
    def excludeList = []
    systemProperty 'tests.rest.suite', 'old_cluster'
    systemProperty 'tests.upgrade_from_version', oldVersion
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo(baseName).map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.clustername', baseName)
    if (excludeList.isEmpty() == false) {
      systemProperty 'tests.rest.blacklist', excludeList.join(',')
    }
  }

  tasks.register("${baseName}#oneThirdUpgradedTest", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#oldClusterTest"
    useCluster baseCluster
    doFirst {
      getRegistry().get().nextNodeToNextVersion(baseCluster)
    }
    systemProperty 'tests.rest.suite', 'mixed_cluster'
    systemProperty 'tests.upgrade_from_version', oldVersion
    systemProperty 'tests.first_round', 'true'
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo(baseName).map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.clustername', baseName)
    def excludeList = []
    if (excludeList.isEmpty() == false) {
      systemProperty 'tests.rest.blacklist', excludeList.join(',')
    }
  }

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

  tasks.register("${baseName}#upgradedClusterTest", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#twoThirdsUpgradedTest"
    doFirst {
      getRegistry().get().nextNodeToNextVersion(baseCluster)
    }
    useCluster testClusters.named(baseName)
    systemProperty 'tests.rest.suite', 'upgraded_cluster'
    systemProperty 'tests.upgrade_from_version', oldVersion
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo(baseName).map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.clustername', baseName)
    def excludeList = []
    if (excludeList.isEmpty() == false) {
      systemProperty 'tests.rest.blacklist', excludeList.join(',')
    }
  }

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