Skip to content

Commit

Permalink
Ground work to start up the docker image in the build (#37754)
Browse files Browse the repository at this point in the history
This change adds a docker compose configuration that's used with
the `elasticsearch.test.fixtures` plugin to start up the image
and check that the TCP ports are up.

We can build on this to add other checks for culster health,
run REST tests, etc.

We can add multiple containers and configurations to the compose
file (e.x. test different env vars) and form clusters.
  • Loading branch information
alpar-t committed Jan 24, 2019
1 parent 8e13a10 commit 8a3a92f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ public void useFixture(String path) {
fixtures.add(fixtureProject);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.avast.gradle.dockercompose.ComposeExtension;
import com.avast.gradle.dockercompose.DockerComposePlugin;
import com.avast.gradle.dockercompose.tasks.ComposeUp;
import org.elasticsearch.gradle.precommit.JarHellTask;
import org.elasticsearch.gradle.precommit.TestingConventionsTasks;
import org.elasticsearch.gradle.precommit.ThirdPartyAuditTask;
Expand Down Expand Up @@ -54,15 +55,13 @@ public void apply(Project project) {
// convenience boilerplate with build plugin
// Can't reference tasks that are implemented in Groovy, use reflection instead
disableTaskByType(tasks, getTaskClass("org.elasticsearch.gradle.precommit.LicenseHeadersTask"));
disableTaskByType(tasks, getTaskClass("com.carrotsearch.gradle.junit4.RandomizedTestingTask"));
disableTaskByType(tasks, ThirdPartyAuditTask.class);
disableTaskByType(tasks, JarHellTask.class);

Task buildFixture = project.getTasks().create("buildFixture");
Task preProcessFixture = project.getTasks().create("preProcessFixture");
buildFixture.dependsOn(preProcessFixture);
Task postProcessFixture = project.getTasks().create("postProcessFixture");
buildFixture.dependsOn(postProcessFixture);

if (dockerComposeSupported(project) == false) {
preProcessFixture.setEnabled(false);
Expand All @@ -83,46 +82,44 @@ public void apply(Project project) {

buildFixture.dependsOn(tasks.getByName("composeUp"));
tasks.getByName("composeUp").mustRunAfter(preProcessFixture);
postProcessFixture.dependsOn("composeUp");
postProcessFixture.dependsOn(buildFixture);

configureServiceInfoForTask(
postProcessFixture,
project,
(name, port) -> postProcessFixture.getExtensions()
.getByType(ExtraPropertiesExtension.class).set(name, port)
);
} else {
extension.fixtures.all(fixtureProject -> project.evaluationDependsOn(fixtureProject.getPath()));
if (dockerComposeSupported(project) == false) {
project.getLogger().warn(
"Tests for {} require docker-compose at /usr/local/bin/docker-compose or /usr/bin/docker-compose " +
"but none could be found so these will be skipped", project.getPath()
}

extension.fixtures.all(fixtureProject -> project.evaluationDependsOn(fixtureProject.getPath()));
if (dockerComposeSupported(project) == false) {
project.getLogger().warn(
"Tests for {} require docker-compose at /usr/local/bin/docker-compose or /usr/bin/docker-compose " +
"but none could be found so these will be skipped", project.getPath()
);
disableTaskByType(tasks, getTaskClass("com.carrotsearch.gradle.junit4.RandomizedTestingTask"));
// conventions are not honored when the tasks are disabled
disableTaskByType(tasks, TestingConventionsTasks.class);
disableTaskByType(tasks, ComposeUp.class);
return;
}
tasks.withType(getTaskClass("com.carrotsearch.gradle.junit4.RandomizedTestingTask"), task ->
extension.fixtures.all(fixtureProject -> {
fixtureProject.getTasks().matching(it -> it.getName().equals("buildFixture")).all(buildFixture ->
task.dependsOn(buildFixture)
);
tasks.withType(getTaskClass("com.carrotsearch.gradle.junit4.RandomizedTestingTask"), task ->
task.setEnabled(false)
fixtureProject.getTasks().matching(it -> it.getName().equals("composeDown")).all(composeDown ->
task.finalizedBy(composeDown)
);
// conventions are not honored when the tasks are disabled
tasks.withType(TestingConventionsTasks.class, task ->
task.setEnabled(false)
configureServiceInfoForTask(
task,
fixtureProject,
(name, port) -> setSystemProperty(task, name, port)
);
return;
}
tasks.withType(getTaskClass("com.carrotsearch.gradle.junit4.RandomizedTestingTask"), task ->
extension.fixtures.all(fixtureProject -> {
fixtureProject.getTasks().matching(it->it.getName().equals("buildFixture")).all(buildFixture ->
task.dependsOn(buildFixture)
);
fixtureProject.getTasks().matching(it->it.getName().equals("composeDown")).all(composeDown ->
task.finalizedBy(composeDown)
);
configureServiceInfoForTask(
task,
fixtureProject,
(name, port) -> setSystemProperty(task, name, port)
);
})
);
}
})
);

}

private void configureServiceInfoForTask(Task task, Project fixtureProject, BiConsumer<String, Integer> consumer) {
Expand All @@ -131,23 +128,24 @@ private void configureServiceInfoForTask(Task task, Project fixtureProject, BiCo
task.doFirst(theTask ->
fixtureProject.getExtensions().getByType(ComposeExtension.class).getServicesInfos()
.forEach((service, infos) -> {
theTask.getLogger().info(
"Port maps for {}\nTCP:{}\nUDP:{}\nexposed to {}",
fixtureProject.getPath(),
infos.getTcpPorts(),
infos.getUdpPorts(),
theTask.getPath()
);
infos.getTcpPorts()
.forEach((container, host) -> consumer.accept(
"test.fixtures." + service + ".tcp." + container,
host
));
.forEach((container, host) -> {
String name = "test.fixtures." + service + ".tcp." + container;
theTask.getLogger().info("port mapping property: {}={}", name, host);
consumer.accept(
name,
host
);
});
infos.getUdpPorts()
.forEach((container, host) -> consumer.accept(
"test.fixtures." + service + ".udp." + container,
host
));
.forEach((container, host) -> {
String name = "test.fixtures." + service + ".udp." + container;
theTask.getLogger().info("port mapping property: {}={}", name, host);
consumer.accept(
name,
host
);
});
})
);
}
Expand Down
17 changes: 17 additions & 0 deletions distribution/docker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.elasticsearch.gradle.MavenFilteringHack
import org.elasticsearch.gradle.VersionProperties

apply plugin: 'base'
apply plugin: 'elasticsearch.test.fixtures'

configurations {
dockerPlugins
Expand Down Expand Up @@ -68,6 +69,22 @@ void addCopyDockerfileTask(final boolean oss) {
}
}


preProcessFixture {
dependsOn taskName("copy", true, "DockerContext")
dependsOn taskName("copy", true, "Dockerfile")
dependsOn taskName("copy", false, "DockerContext")
dependsOn taskName("copy", false, "Dockerfile")
}

postProcessFixture.doLast {
println "docker default distro is on port: ${ext."test.fixtures.elasticsearch-default.tcp.9200"}, " +
"oss is on: ${ext."test.fixtures.elasticsearch-oss.tcp.9200"}"
}

// TODO: Add some actual tests, this will just check that the TPC port in the container is up
check.dependsOn postProcessFixture

void addBuildDockerImage(final boolean oss) {
final Task buildDockerImageTask = task(taskName("build", oss, "DockerImage"), type: LoggedExec) {
dependsOn taskName("copy", oss, "DockerContext")
Expand Down
21 changes: 21 additions & 0 deletions distribution/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Only used for testing the docker images
version: '3'
services:
elasticsearch-default:
build:
context: ./build/docker
dockerfile: Dockerfile
environment:
- cluster.name=elasticsearch-default
- discovery.type=single-node
ports:
- "9200"
elasticsearch-oss:
build:
context: ./build/oss-docker
dockerfile: Dockerfile
environment:
- cluster.name=elasticsearch-oss
- discovery.type=single-node
ports:
- "9200"
2 changes: 2 additions & 0 deletions x-pack/test/smb-fixture/build.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
apply plugin: 'elasticsearch.build'
apply plugin: 'elasticsearch.test.fixtures'

unitTest.enabled = false

0 comments on commit 8a3a92f

Please sign in to comment.