/*
 * 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.elasticsearch.gradle.Architecture
import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.testfixtures.TestFixturesPlugin

apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.test.fixtures'
apply plugin: 'elasticsearch.distribution-download'

testFixtures.useFixture()

dependencies {
  testCompile project(':client:rest-high-level')
}

task copyKeystore(type: Sync) {
  from project(':x-pack:plugin:core')
    .file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks')
  into "${buildDir}/certs"
  doLast {
    file("${buildDir}/certs").setReadable(true, false)
    file("${buildDir}/certs/testnode.jks").setReadable(true, false)
  }
}

elasticsearch_distributions {
  docker {
    type = 'docker'
    architecture = Architecture.current()
    flavor = System.getProperty('tests.distribution', 'default')
    version = VersionProperties.getElasticsearch()
    failIfUnavailable = false // This ensures we skip this testing if Docker is unavailable
  }
}

preProcessFixture {
  dependsOn copyKeystore, elasticsearch_distributions.docker
  doLast {
    // tests expect to have an empty repo
    project.delete(
      "${buildDir}/repo",
      "${buildDir}/oss-repo"
    )
    createAndSetWritable(
      "${buildDir}/repo",
      "${buildDir}/oss-repo",
      "${buildDir}/logs/default-1",
      "${buildDir}/logs/default-2",
      "${buildDir}/logs/oss-1",
      "${buildDir}/logs/oss-2"
    )
  }
}

dockerCompose {
  tcpPortsToIgnoreWhenWaiting = [9600, 9601]
  if ('default'.equalsIgnoreCase(System.getProperty('tests.distribution', 'default'))) {
    useComposeFiles = ['docker-compose.yml']
  } else {
    useComposeFiles = ['docker-compose-oss.yml']
  }
}

def createAndSetWritable(Object... locations) {
  locations.each { location ->
    File file = file(location)
    file.mkdirs()
    file.setWritable(true, false)
  }
}

processTestResources {
  from project(':x-pack:plugin:core')
    .file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks')
}

task integTest(type: Test) {
  outputs.doNotCacheIf('Build cache is disabled for Docker tests') { true }
  maxParallelForks = '1'
  include '**/*IT.class'
}

check.dependsOn integTest