/*
 * 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.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 ->

  /**
   * We execute tests 3 times.
   * - The local cluster is unchanged and it consists of two new version nodes.
   * - Nodes in the remote cluster are upgraded one by one in three steps.
   * - Only node-0 and node-2 of the remote cluster can accept remote connections.
   */
  def localCluster = testClusters.register("${baseName}-local") {
    testDistribution = 'DEFAULT'
    numberOfNodes = 2
    versions = [project.version, project.version]
    setting 'cluster.remote.node.attr', 'gateway'
    setting 'xpack.security.enabled', 'false'
  }
  def remoteCluster = testClusters.register("${baseName}-remote") {
    testDistribution = 'DEFAULT'
    numberOfNodes = 3
    versions = [bwcVersion.toString(), project.version]
    firstNode.setting 'node.attr.gateway', 'true'
    lastNode.setting 'node.attr.gateway', 'true'
    setting 'xpack.security.enabled', 'false'
  }


  tasks.withType(StandaloneRestIntegTestTask).matching { it.name.startsWith("${baseName}#") }.configureEach {
    useCluster localCluster
    useCluster remoteCluster
    systemProperty 'tests.upgrade_from_version', bwcVersion.toString().replace('-SNAPSHOT', '')
    nonInputProperties.systemProperty('tests.rest.cluster', getClusterInfo("${baseName}-local").map { it.allHttpSocketURI.join(",") })
    nonInputProperties.systemProperty('tests.rest.remote_cluster', getClusterInfo("${baseName}-remote").map { it.allHttpSocketURI.join(",") })
  }

  tasks.register("${baseName}#oldClusterTest", StandaloneRestIntegTestTask) {
    dependsOn "processTestResources"
    mustRunAfter("precommit")
    doFirst {
      getRegistry().get().nextNodeToNextVersion(localCluster)
    }
  }

  tasks.register("${baseName}#oneThirdUpgraded", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#oldClusterTest"
    doFirst {
      getRegistry().get().nextNodeToNextVersion(remoteCluster)
    }
  }

  tasks.register("${baseName}#twoThirdUpgraded", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#oneThirdUpgraded"
    doFirst {
      getRegistry().get().nextNodeToNextVersion(remoteCluster)
    }
  }

  tasks.register("${baseName}#fullUpgraded", StandaloneRestIntegTestTask) {
    dependsOn "${baseName}#twoThirdUpgraded"
    doFirst {
      getRegistry().get().nextNodeToNextVersion(remoteCluster)
    }
  }

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