/*
 * 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.Architecture
import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.internal.testfixtures.TestFixturesPlugin
import static org.elasticsearch.gradle.internal.distribution.InternalElasticsearchDistributionTypes.DOCKER;

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

tasks.register("copyNodeKeyMaterial", Sync) {
  from project(':x-pack:plugin:core')
    .files(
      'src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem',
      'src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt'
    )
  into "${buildDir}/certs"
  doLast {
    file("${buildDir}/certs").setReadable(true, false)
    file("${buildDir}/certs/testnode.pem").setReadable(true, false)
    file("${buildDir}/certs/testnode.crt").setReadable(true, false)
  }
}

elasticsearch_distributions {
  docker {
    type = DOCKER
    architecture = Architecture.current()
    version = VersionProperties.getElasticsearch()
    failIfUnavailable = false // This ensures we skip this testing if Docker is unavailable
  }
}


interface Injected {
  @Inject
  FileSystemOperations getFs()
}

tasks.named("preProcessFixture").configure {
  dependsOn "copyNodeKeyMaterial", elasticsearch_distributions.docker
  def injected = project.objects.newInstance(Injected)

  doLast {
    // tests expect to have an empty repo
    injected.fs.delete {
      it.delete("${testFixturesDir}/repo")
      it.delete("${testFixturesDir}/oss-repo")
    }
    createAndSetWritable(
      "${testFixturesDir}/repo",
      "${testFixturesDir}/oss-repo",
      "${testFixturesDir}/logs/default-1",
      "${testFixturesDir}/logs/default-2",
      "${testFixturesDir}/logs/oss-1",
      "${testFixturesDir}/logs/oss-2"
    )
  }
}

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

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

tasks.named("processTestResources").configure {
  from project(':x-pack:plugin:core')
    .files(
      'src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem',
      'src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt'
    )
}

tasks.register("integTest", Test) {
  outputs.doNotCacheIf('Build cache is disabled for Docker tests') { true }
  maxParallelForks = '1'
  include '**/*IT.class'
}

tasks.named("check").configure { dependsOn "integTest" }
