Add Wildfly integration test

An important use case for our users is deploying our clients inside of
applications containers like Wildly. Sometimes, we make changes that
unintentionally break this use case. We need to know before we ship a
release that we have broken such use cases. As Wildfly is one of the
bigger application containers, this commit starts by adding an
integration test that deploys an application using the transport client
to Wildfly and ensures that all is well. Future work can add similar
integration tests for the low-level and high-level REST clients.

Relates #24147
This commit is contained in:
Jason Tedor 2017-04-21 12:51:14 -04:00 committed by GitHub
parent ba48674695
commit 2dd924bc15
11 changed files with 674 additions and 0 deletions

198
qa/wildfly/build.gradle Normal file
View File

@ -0,0 +1,198 @@
import org.elasticsearch.gradle.LoggedExec
import org.elasticsearch.gradle.VersionProperties
import org.apache.tools.ant.taskdefs.condition.Os
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util.stream.Stream
/*
* 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.
*/
apply plugin: 'war'
apply plugin: 'elasticsearch.build'
apply plugin: 'elasticsearch.rest-test'
final String wildflyVersion = '10.0.0.Final'
final String wildflyDir = "${buildDir}/wildfly"
final String wildflyInstall = "${buildDir}/wildfly/wildfly-${wildflyVersion}"
// TODO: use ephemeral ports
final int portOffset = 30000
final int managementPort = 9990 + portOffset
// we skip these tests on Windows so we do no need to worry about compatibility here
final String stopWildflyCommand = "${wildflyInstall}/bin/jboss-cli.sh --controller=localhost:${managementPort} --connect command=:shutdown"
repositories {
mavenCentral()
// the Wildfly distribution is not available via a repository, so we fake an Ivy repository on top of the download site
ivy {
url "http://download.jboss.org"
layout 'pattern', {
artifact 'wildfly/[revision]/[module]-[revision].[ext]'
}
}
}
configurations {
wildfly
}
dependencies {
providedCompile 'javax.enterprise:cdi-api:1.2'
providedCompile 'org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec:1.0.0.Final'
providedCompile 'org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.0_spec:1.0.0.Final'
compile ('org.jboss.resteasy:resteasy-jackson2-provider:3.0.19.Final') {
exclude module: 'jackson-annotations'
exclude module: 'jackson-core'
exclude module: 'jackson-databind'
exclude module: 'jackson-jaxrs-json-provider'
}
compile "com.fasterxml.jackson.core:jackson-annotations:${versions.jackson}"
compile "com.fasterxml.jackson.core:jackson-core:${versions.jackson}"
compile "com.fasterxml.jackson.core:jackson-databind:${versions.jackson}"
compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:${versions.jackson}"
compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:${versions.jackson}"
compile "com.fasterxml.jackson.module:jackson-module-jaxb-annotations:${versions.jackson}"
compile "org.apache.logging.log4j:log4j-api:${versions.log4j}"
compile "org.apache.logging.log4j:log4j-core:${versions.log4j}"
compile project(path: ':client:transport', configuration: 'runtime')
wildfly "org.jboss:wildfly:${wildflyVersion}@zip"
testCompile "org.elasticsearch.test:framework:${VersionProperties.elasticsearch}"
}
task unzipWildfly(type: Sync) {
into wildflyDir
from { zipTree(configurations.wildfly.singleFile) }
}
task deploy(type: Copy) {
dependsOn unzipWildfly, war
from war
into "${wildflyInstall}/standalone/deployments"
}
task writeElasticsearchProperties {
onlyIf { !Os.isFamily(Os.FAMILY_WINDOWS) }
dependsOn 'integTestCluster#wait', deploy
doLast {
final File elasticsearchProperties =
file("${wildflyInstall}/standalone/configuration/elasticsearch.properties")
elasticsearchProperties.write(
[
"transport.uri=${-> integTest.getNodes().get(0).transportUri()}",
"cluster.name=${-> integTest.getNodes().get(0).clusterName}"
].join("\n"))
}
}
// the default configuration ships with IPv6 disabled but our cluster could be bound to IPv6 if the host supports it
task enableIPv6 {
dependsOn unzipWildfly
doLast {
final File standaloneConf = file("${wildflyInstall}/bin/standalone.conf")
final List<String> lines =
Files.readAllLines(standaloneConf.toPath())
.collect { line -> line.replace("-Djava.net.preferIPv4Stack=true", "-Djava.net.preferIPv4Stack=false") }
standaloneConf.write(lines.join("\n"))
}
}
task startWildfly {
dependsOn enableIPv6, writeElasticsearchProperties
doFirst {
// we skip these tests on Windows so we do no need to worry about compatibility here
final File script = new File(project.buildDir, "wildfly/wildfly.killer.sh")
script.setText(
["function shutdown {",
" ${stopWildflyCommand}",
"}",
"trap shutdown EXIT",
// will wait indefinitely for input, but we never pass input, and the pipe is only closed when the build dies
"read line"].join('\n'), 'UTF-8')
final ProcessBuilder pb = new ProcessBuilder("bash", script.absolutePath)
pb.start()
}
doLast {
// we skip these tests on Windows so we do no need to worry about compatibility here
final ProcessBuilder pb =
new ProcessBuilder("${wildflyInstall}/bin/standalone.sh", "-Djboss.socket.binding.port-offset=${portOffset}")
final Process process = pb.start()
new BufferedReader(new InputStreamReader(process.getInputStream())).withReader { br ->
String line
while ((line = br.readLine()) != null) {
if (line.matches(".*WildFly Full \\d+\\.\\d+\\.\\d+\\.Final \\(WildFly Core \\d+\\.\\d+\\.\\d+\\.Final\\) started.*")) {
break
}
}
}
}
}
task configureTransportClient(type: LoggedExec) {
dependsOn startWildfly
// we skip these tests on Windows so we do no need to worry about compatibility here
commandLine "${wildflyInstall}/bin/jboss-cli.sh",
"--controller=localhost:${managementPort}",
"--connect",
"--command=/system-property=elasticsearch.properties:add(value=\${jboss.server.config.dir}/elasticsearch.properties)"
}
task stopWildfly(type: LoggedExec) {
commandLine stopWildflyCommand.split(' ')
}
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
integTestRunner.dependsOn(configureTransportClient)
final TaskExecutionAdapter logDumpListener = new TaskExecutionAdapter() {
@Override
void afterExecute(final Task task, final TaskState state) {
if (state.failure != null) {
final File logFile = new File(wildflyInstall, "standalone/log/server.log")
println("\nWildfly server log (from ${logFile}):")
println('-----------------------------------------')
final Stream<String> stream = Files.lines(logFile.toPath(), StandardCharsets.UTF_8)
try {
for (String line : stream) {
println(line)
}
} finally {
stream.close()
}
println('=========================================')
}
}
}
integTestRunner.doFirst {
project.gradle.addListener(logDumpListener)
}
integTestRunner.doLast {
project.gradle.removeListener(logDumpListener)
}
integTestRunner.finalizedBy(stopWildfly)
} else {
integTest.enabled = false
}
check.dependsOn(integTest)
test.enabled = false
dependencyLicenses.enabled = false
thirdPartyAudit.enabled = false

View File

@ -0,0 +1,87 @@
/*
* 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.
*/
package org.elasticsearch.wildfly.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Consumes(MediaType.APPLICATION_JSON)
public class Employee {
@JsonProperty(value = "first_name")
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@JsonProperty(value = "last_name")
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@JsonProperty(value = "age")
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@JsonProperty(value = "about")
private String about;
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
@JsonProperty(value = "interests")
private List<String> interests;
public List<String> getInterests() {
return interests;
}
public void setInterests(List<String> interests) {
this.interests = interests;
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.
*/
package org.elasticsearch.wildfly.transport;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.Collections;
import java.util.Set;
@ApplicationPath("/transport")
public class TransportClientActivator extends Application {
@Override
public Set<Class<?>> getClasses() {
return Collections.singleton(TransportClientEmployeeResource.class);
}
}

View File

@ -0,0 +1,106 @@
/*
* 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.
*/
package org.elasticsearch.wildfly.transport;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.wildfly.model.Employee;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@Path("/employees")
public class TransportClientEmployeeResource {
@Inject
private TransportClient client;
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getEmployeeById(final @PathParam("id") Long id) {
Objects.requireNonNull(id);
final GetResponse response = client.prepareGet("megacorp", "employee", Long.toString(id)).get();
if (response.isExists()) {
final Map<String, Object> source = response.getSource();
final Employee employee = new Employee();
employee.setFirstName((String) source.get("first_name"));
employee.setLastName((String) source.get("last_name"));
employee.setAge((Integer) source.get("age"));
employee.setAbout((String) source.get("about"));
@SuppressWarnings("unchecked") final List<String> interests = (List<String>) source.get("interests");
employee.setInterests(interests);
return Response.ok(employee).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
@PUT
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response putEmployeeById(final @PathParam("id") Long id, final Employee employee) throws URISyntaxException, IOException {
Objects.requireNonNull(id);
Objects.requireNonNull(employee);
try (XContentBuilder builder = jsonBuilder()) {
builder.startObject();
{
builder.field("first_name", employee.getFirstName());
builder.field("last_name", employee.getLastName());
builder.field("age", employee.getAge());
builder.field("about", employee.getAbout());
if (employee.getInterests() != null) {
builder.startArray("interests");
{
for (final String interest : employee.getInterests()) {
builder.value(interest);
}
}
builder.endArray();
}
}
builder.endObject();
final IndexResponse response = client.prepareIndex("megacorp", "employee", Long.toString(id)).setSource(builder).get();
if (response.status().getStatus() == 201) {
return Response.created(new URI("/employees/" + id)).build();
} else {
return Response.ok().build();
}
}
}
}

View File

@ -0,0 +1,72 @@
/*
* 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.
*/
package org.elasticsearch.wildfly.transport;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import javax.enterprise.inject.Produces;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Properties;
@SuppressWarnings("unused")
public final class TransportClientProducer {
@Produces
public TransportClient createTransportClient() throws IOException {
final String elasticsearchProperties = System.getProperty("elasticsearch.properties");
final Properties properties = new Properties();
final String transportUri;
final String clusterName;
try (InputStream is = Files.newInputStream(getPath(elasticsearchProperties))) {
properties.load(is);
transportUri = properties.getProperty("transport.uri");
clusterName = properties.getProperty("cluster.name");
}
final int lastColon = transportUri.lastIndexOf(':');
final String host = transportUri.substring(0, lastColon);
final int port = Integer.parseInt(transportUri.substring(lastColon + 1));
final Settings settings = Settings.builder().put("cluster.name", clusterName).build();
final TransportClient transportClient = new PreBuiltTransportClient(settings, Collections.emptyList());
transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
return transportClient;
}
@SuppressForbidden(reason = "get path not configured in environment")
private Path getPath(final String elasticsearchProperties) {
return PathUtils.get(elasticsearchProperties);
}
}

View File

@ -0,0 +1,28 @@
/*
* 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.
*/
package org.elasticsearch.wildfly.transport;
import org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider;
import javax.ws.rs.ext.Provider;
@Provider
public class TransportJacksonJsonProvider extends ResteasyJackson2Provider {
}

View File

@ -0,0 +1,9 @@
status = error
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%m%n
rootLogger.level = info
rootLogger.appenderRef.console.ref = console

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<!-- Marker file indicating CDI should be enabled -->
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

View File

@ -0,0 +1,10 @@
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="com.fasterxml.jackson.core.jackson-core" />
<module name="com.fasterxml.jackson.core.jackson-databind" />
<module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" />
<module name="org.jboss.resteasy.resteasy-jackson2-provider" />
</exclusions>
</deployment>
</jboss-deployment-structure>

View File

@ -0,0 +1,101 @@
package org.elasticsearch.wildfly;/*
* 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.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.Build;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
public class WildflyIT extends LuceneTestCase {
public void testTransportClient() throws URISyntaxException, IOException {
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
final String str = String.format(
Locale.ROOT,
"http://localhost:38080/wildfly-%s%s/transport/employees/1",
Version.CURRENT,
Build.CURRENT.isSnapshot() ? "-SNAPSHOT" : "");
final HttpPut put = new HttpPut(new URI(str));
final String body;
try (XContentBuilder builder = jsonBuilder()) {
builder.startObject();
{
builder.field("first_name", "John");
builder.field("last_name", "Smith");
builder.field("age", 25);
builder.field("about", "I love to go rock climbing");
builder.startArray("interests");
{
builder.value("sports");
builder.value("music");
}
builder.endArray();
}
builder.endObject();
body = builder.string();
}
put.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = client.execute(put)) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(201));
}
final HttpGet get = new HttpGet(new URI(str));
try (
CloseableHttpResponse response = client.execute(get);
XContentParser parser =
JsonXContent.jsonXContent.createParser(
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
response.getEntity().getContent())) {
final Map<String, Object> map = parser.map();
assertThat(map.get("first_name"), equalTo("John"));
assertThat(map.get("last_name"), equalTo("Smith"));
assertThat(map.get("age"), equalTo(25));
assertThat(map.get("about"), equalTo("I love to go rock climbing"));
final Object interests = map.get("interests");
assertThat(interests, instanceOf(List.class));
@SuppressWarnings("unchecked") final List<String> interestsAsList = (List<String>) interests;
assertThat(interestsAsList, containsInAnyOrder("sports", "music"));
}
}
}
}

View File

@ -71,6 +71,7 @@ List projects = [
'qa:smoke-test-reindex-with-painless',
'qa:smoke-test-tribe-node',
'qa:vagrant',
'qa:wildfly'
]
boolean isEclipse = System.getProperty("eclipse.launcher") != null || gradle.startParameter.taskNames.contains('eclipse') || gradle.startParameter.taskNames.contains('cleanEclipse')