/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.tools.ant.taskdefs.condition.Os
import org.elasticsearch.gradle.info.BuildParams

import static org.elasticsearch.gradle.BuildPlugin.getJavaHome

apply plugin: 'elasticsearch.test-with-dependencies'

esplugin {
  description 'The Reindex module adds APIs to reindex from one index to another or update documents in place.'
  classname 'org.elasticsearch.index.reindex.ReindexPlugin'
  hasClientJar = true
}

testClusters.integTest {
  // Modules who's integration is explicitly tested in integration tests
  module file(project(':modules:parent-join').tasks.bundlePlugin.archiveFile)
  module file(project(':modules:lang-painless').tasks.bundlePlugin.archiveFile)
  // Whitelist reindexing from the local node so we can test reindex-from-remote.
  setting 'reindex.remote.whitelist', '127.0.0.1:*'
}

test {
  /*
   * We have to disable setting the number of available processors as tests in the
   * same JVM randomize processors and will step on each other if we allow them to
   * set the number of available processors as it's set-once in Netty.
   */
  systemProperty 'es.set.netty.runtime.available.processors', 'false'
}

dependencies {
  compile project(":client:rest")
  compile project(":libs:elasticsearch-ssl-config")
  // for http - testing reindex from remote
  testCompile project(path: ':modules:transport-netty4', configuration: 'runtime')
  // for parent/child testing
  testCompile project(path: ':modules:parent-join', configuration: 'runtime')
}

thirdPartyAudit.ignoreMissingClasses(
  // Commons logging
  'javax.servlet.ServletContextEvent',
  'javax.servlet.ServletContextListener',
  'org.apache.avalon.framework.logger.Logger',
  'org.apache.log.Hierarchy',
  'org.apache.log.Logger',
)

forbiddenPatterns {
  // PKCS#12 file are not UTF-8
  exclude '**/*.p12'
}

// Support for testing reindex-from-remote against old Elasticsearch versions
configurations {
  oldesFixture
  es2
  es1
  es090
}

dependencies {
  oldesFixture project(':test:fixtures:old-elasticsearch')
  /* Right now we just test against the latest version of each major we expect
   * reindex-from-remote to work against. We could randomize the versions but
   * that doesn't seem worth it at this point. */
  es2 'org.elasticsearch.distribution.zip:elasticsearch:2.4.5@zip'
  es1 'org.elasticsearch:elasticsearch:1.7.6@zip'
  es090 'org.elasticsearch:elasticsearch:0.90.13@zip'
}

if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  logger.warn("Disabling reindex-from-old tests because we can't get the pid file on windows")
  integTest.runner {
    systemProperty "tests.fromOld", "false"
  }
} else if (rootProject.rootDir.toString().contains(" ")) {
  logger.warn("Disabling reindex-from-old tests because Elasticsearch 1.7 won't start with spaces in the path")
  integTest.runner {
    systemProperty "tests.fromOld", "false"
  }
} else {
  /* Set up tasks to unzip and run the old versions of ES before running the
   * integration tests. */
  def versions = ['2', '1', '090']
  if (Os.isFamily(Os.FAMILY_MAC)) {
    // 0.90 fails sometimes on mac, given that it is so old, let us disable it
    // see: https://github.com/elastic/elasticsearch/issues/51202
    versions = ['2', '1']
  }
  for (String version : versions) {
    Task unzip = task("unzipEs${version}", type: Sync) {
      Configuration oldEsDependency = configurations['es' + version]
      dependsOn oldEsDependency
      /* Use a closure here to delay resolution of the dependency until we need
       * it */
      from {
        oldEsDependency.collect { zipTree(it) }
      }
      into temporaryDir
    }

    Task fixture = task("oldEs${version}Fixture", type: org.elasticsearch.gradle.test.AntFixture) {
      dependsOn project.configurations.oldesFixture
      dependsOn unzip
      executable = "${BuildParams.runtimeJavaHome}/bin/java"
      env 'CLASSPATH', "${-> project.configurations.oldesFixture.asPath}"
      env 'JAVA_HOME', getJavaHome(it, 8)
      args 'oldes.OldElasticsearch',
        baseDir,
        unzip.temporaryDir,
        version == '090'
      waitCondition = { fixture, ant ->
        // the fixture writes the ports file when Elasticsearch's HTTP service
        // is ready, so we can just wait for the file to exist
        return fixture.portsFile.exists()
      }
    }

    integTest {
      dependsOn fixture
      runner {
        systemProperty "tests.fromOld", "true"
        /* Use a closure on the string to delay evaluation until right before we
         * run the integration tests so that we can be sure that the file is
         * ready. */
        nonInputProperties.systemProperty "es${version}.port", "${-> fixture.addressAndPort}"
      }
    }
  }
}