secrets : securedKeys.entrySet()) {
168 | logger().debug("Adding secured key [{}]", secrets.getKey());
169 | builder.run("echo '" + secrets.getValue() + "' | bin/elasticsearch-keystore add --stdin " + secrets.getKey());
170 | }
171 | String s = builder.build();
172 |
173 | logger().debug("Image generated: {}", s);
174 | });
175 |
176 | if (pluginDir != null) {
177 | dockerImage.withFileFromFile("/tmp/plugins", this.pluginDir.toAbsolutePath().toFile());
178 | }
179 |
180 | setImage(dockerImage);
181 | addExposedPort(ELASTICSEARCH_DEFAULT_PORT);
182 | addExposedPort(ELASTICSEARCH_DEFAULT_TCP_PORT);
183 | }
184 |
185 | public HttpHost getHost() {
186 | return new HttpHost(getContainerIpAddress(), getMappedPort(ELASTICSEARCH_DEFAULT_PORT));
187 | }
188 | }
189 |
--------------------------------------------------------------------------------
/src/main/java/fr/pilato/elasticsearch/containers/ElasticsearchResource.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 | import org.apache.http.HttpHost;
23 | import org.jetbrains.annotations.Nullable;
24 | import org.junit.rules.ExternalResource;
25 | import org.rnorth.ducttape.Preconditions;
26 |
27 | import java.io.IOException;
28 | import java.io.InputStream;
29 | import java.nio.file.Path;
30 | import java.nio.file.Paths;
31 | import java.util.ArrayList;
32 | import java.util.Arrays;
33 | import java.util.Collections;
34 | import java.util.List;
35 | import java.util.Map;
36 | import java.util.Properties;
37 |
38 | import static fr.pilato.elasticsearch.containers.ElasticsearchContainer.ELASTICSEARCH_DEFAULT_BASE_URL;
39 | import static fr.pilato.elasticsearch.containers.ElasticsearchContainer.ELASTICSEARCH_DEFAULT_VERSION;
40 |
41 | /**
42 | * Junit Test Resource for elasticsearch.
43 | */
44 | public class ElasticsearchResource extends ExternalResource {
45 |
46 | private static final String DEFAULT_RESOURCE_NAME = "elasticsearch.properties";
47 | static final String FALLBACK_RESOURCE_NAME = "elasticsearch-default.properties";
48 | private final String baseUrl;
49 | private final String version;
50 | private final Path pluginDir;
51 | private final String password;
52 | private final List plugins;
53 | private final Map securedSettings;
54 | @Nullable private ElasticsearchContainer delegate;
55 |
56 | public ElasticsearchResource() {
57 | this(DEFAULT_RESOURCE_NAME);
58 | }
59 |
60 | /**
61 | * Generate a resource programmatically
62 | * @param baseUrl If null defaults to ELASTICSEARCH_DEFAULT_BASE_URL
63 | * @param version Elasticsearch version to start. If null defaults to ELASTICSEARCH_DEFAULT_VERSION
64 | * @param pluginDir Plugin dir which might contain plugins to install. Can be null.
65 | * @param plugins Plugins to install. Can be null.
66 | * @param securedSettings Map of secured settings (key/value). Can be null.
67 | * @param password X-Pack default password. Can be null.
68 | */
69 | public ElasticsearchResource(String baseUrl, String version, Path pluginDir, List plugins, Map securedSettings,
70 | String password) {
71 | this.baseUrl = baseUrl == null ? ELASTICSEARCH_DEFAULT_BASE_URL : baseUrl;
72 | this.version = version == null ? ELASTICSEARCH_DEFAULT_VERSION : version;
73 | this.pluginDir = pluginDir;
74 | this.plugins = plugins;
75 | this.securedSettings = securedSettings;
76 | this.password = password;
77 | }
78 |
79 | public ElasticsearchResource(String resourceName) {
80 | // Find the latest version this project was built with
81 | String propVersion;
82 | String propBaseUrl;
83 | String propPlugins;
84 | String propPluginDir;
85 | String propPassword;
86 | String defaultBaseUrl = null;
87 | String defaultVersion = null;
88 | String defaultPlugins = null;
89 | String defaultPluginDir = null;
90 | String defaultPassword = null;
91 | Properties props = new Properties();
92 | try {
93 | props.load(ElasticsearchResource.class.getResourceAsStream(FALLBACK_RESOURCE_NAME));
94 | defaultBaseUrl = props.getProperty("baseUrl");
95 | defaultVersion = props.getProperty("version");
96 | defaultPlugins = props.getProperty("plugins");
97 | defaultPluginDir = props.getProperty("pluginDir");
98 | defaultPassword = props.getProperty("password");
99 | } catch (IOException ignored) {
100 | // This can normally never happen unless someone modifies the JAR file o_O
101 | }
102 | try {
103 | InputStream stream = ElasticsearchResource.class.getResourceAsStream(resourceName);
104 | if (stream != null) {
105 | props.load(stream);
106 | propBaseUrl = props.getProperty("baseUrl", defaultBaseUrl);
107 | propVersion = props.getProperty("version", defaultVersion);
108 | propPlugins = props.getProperty("plugins", defaultPluginDir);
109 | propPluginDir = props.getProperty("pluginDir", defaultPluginDir);
110 | propPassword = props.getProperty("password", defaultPassword);
111 | } else {
112 | propBaseUrl = defaultBaseUrl;
113 | propVersion = defaultVersion;
114 | propPlugins = defaultPlugins;
115 | propPluginDir = defaultPluginDir;
116 | propPassword = defaultPassword;
117 | }
118 | } catch (IOException e) {
119 | // We might get that exception if the user provides a badly formatted property file
120 | propBaseUrl = null;
121 | propVersion = null;
122 | propPlugins = null;
123 | propPluginDir = null;
124 | propPassword = null;
125 | }
126 | baseUrl = propBaseUrl;
127 | version = propVersion;
128 | plugins = generateFromCommaSeparatedString(propPlugins);
129 | pluginDir = propPluginDir == null ? null : Paths.get(propPluginDir);
130 | password = propPassword;
131 | securedSettings = Collections.emptyMap();
132 | }
133 |
134 | private List generateFromCommaSeparatedString(String value) {
135 | List values = new ArrayList<>();
136 | if (value != null) {
137 | values.addAll(Arrays.asList(value.split(",")));
138 | }
139 |
140 | return values;
141 | }
142 |
143 | @Override
144 | protected void before() {
145 | Preconditions.check("baseUrl can't be null", baseUrl != null);
146 | Preconditions.check("version can't be null", version != null);
147 | Preconditions.check("plugins can't be null. Should be empty list instead", plugins != null);
148 | Preconditions.check("securedSettings can't be null. Should be empty map instead", securedSettings != null);
149 | delegate = new ElasticsearchContainer()
150 | .withBaseUrl(baseUrl)
151 | .withVersion(version)
152 | .withPluginDir(pluginDir);
153 |
154 | for (String plugin : plugins) {
155 | delegate.withPlugin(plugin);
156 | }
157 |
158 | for (Map.Entry securedSetting : securedSettings.entrySet()) {
159 | delegate.withSecureSetting(securedSetting.getKey(), securedSetting.getValue());
160 | }
161 |
162 | if (password != null && !password.isEmpty()) {
163 | delegate.withEnv("ELASTIC_PASSWORD", password);
164 | }
165 |
166 | delegate.start();
167 | }
168 |
169 | @Override
170 | protected void after() {
171 | Preconditions.check("delegate must have been created by before()", delegate != null);
172 | delegate.stop();
173 | }
174 |
175 | /**
176 | * Get the HttpHost instance you can use to build an elasticsearch Rest client
177 | * @return an HttpHost
178 | */
179 | public HttpHost getHost() {
180 | Preconditions.check("delegate must have been created by before()", delegate != null);
181 | return delegate.getHost();
182 | }
183 |
184 | @Nullable
185 | public ElasticsearchContainer getContainer() {
186 | return delegate;
187 | }
188 |
189 | public String getPassword() {
190 | return password;
191 | }
192 | }
193 |
--------------------------------------------------------------------------------
/src/main/resources/fr/pilato/elasticsearch/containers/elasticsearch-default.properties:
--------------------------------------------------------------------------------
1 | baseUrl=docker.elastic.co/elasticsearch/elasticsearch
2 | version=${elasticsearch.version}
3 | password=${xpack.password}
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/apache/maven/plugin/announcement/announcement.vm:
--------------------------------------------------------------------------------
1 | ## Licensed to the Apache Software Foundation (ASF) under one
2 | ## or more contributor license agreements. See the NOTICE file
3 | ## distributed with this work for additional information
4 | ## regarding copyright ownership. The ASF licenses this file
5 | ## to you under the Apache License, Version 2.0 (the
6 | ## "License"); you may not use this file except in compliance
7 | ## with the License. You may obtain a copy of the License at
8 | ##
9 | ## http://www.apache.org/licenses/LICENSE-2.0
10 | ##
11 | ## Unless required by applicable law or agreed to in writing,
12 | ## software distributed under the License is distributed on an
13 | ## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | ## KIND, either express or implied. See the License for the
15 | ## specific language governing permissions and limitations
16 | ## under the License.
17 | The ${developmentTeam} is pleased to announce the **${finalName}** release!
18 |
19 | # ${project.name}
20 |
21 | ${introduction}
22 |
23 | Usage
24 |
25 | Add this library to you Maven project:
26 |
27 | ```
28 |
29 | ${project.groupId}
30 | ${project.artifactId}
31 | ${project.version}
32 |
33 | ```
34 |
35 | Or if you are using Gradle:
36 |
37 | ```
38 | compile group: '${project.groupId}', name: '${project.artifactId}', version: '${project.version}'
39 | ```
40 |
41 | Then basically just start elasticsearch within Docker from your java code:
42 |
43 | ```java
44 | ElasticsearchContainer container = new ElasticsearchContainer().withVersion("$announceParameters.elasticsearch");
45 | container.configure();
46 | container.start();
47 | ```
48 |
49 | More details in the [documentation](${project.url}).
50 |
51 | #if ($release.getActions().size() == 0)
52 | No changes defined in this version.
53 | #else
54 |
55 | #if ($release.getActions('add').size() !=0)
56 | New features
57 |
58 | #foreach($actionItem in $release.getActions('add'))
59 | #set($action=$actionItem.getAction())
60 | #if ($actionItem.getIssue())
61 | #set($issue=$actionItem.getIssue())
62 | #else
63 | #set($issue="")
64 | #end
65 | #if ($actionItem.getDueTo())
66 | #set($dueto=$actionItem.getDueTo())
67 | #else
68 | #set($dueto="")
69 | #end
70 | * [#$issue](${project.url}/issues/$issue): ${action} #if($!issue != ""). #end#if($!dueto != "")Thanks to $dueto. #end
71 |
72 | #set($issue="")
73 | #set($dueto="")
74 | #end
75 | #end
76 |
77 | #if ($release.getActions('fix').size() !=0)
78 | Fixed Bugs
79 |
80 | #foreach($actionItem in $release.getActions('fix'))
81 | #set($action=$actionItem.getAction())
82 | #if ($actionItem.getIssue())
83 | #set($issue=$actionItem.getIssue())
84 | #else
85 | #set($issue="")
86 | #end
87 | #if ($actionItem.getDueTo())
88 | #set($dueto=$actionItem.getDueTo())
89 | #else
90 | #set($dueto="")
91 | #end
92 | * [#$issue](${project.url}/issues/$issue): ${action} #if($!issue != ""). #end#if($!dueto != "")Thanks to $dueto. #end
93 |
94 | #set($issue="")
95 | #set($dueto="")
96 | #end
97 | #end
98 |
99 | #if ($release.getActions('update').size() !=0)
100 | Changes
101 |
102 | #foreach($actionItem in $release.getActions('update'))
103 | #set($action=$actionItem.getAction())
104 | #if ($actionItem.getIssue())
105 | #set($issue=$actionItem.getIssue())
106 | #else
107 | #set($issue="")
108 | #end
109 | #if ($actionItem.getDueTo())
110 | #set($dueto=$actionItem.getDueTo())
111 | #else
112 | #set($dueto="")
113 | #end
114 | * [#$issue](${project.url}/issues/$issue): ${action} #if($!issue != ""). #end#if($!dueto != "")Thanks to $dueto. #end
115 |
116 | #set($issue="")
117 | #set($dueto="")
118 | #end
119 | #end
120 |
121 | #if ($release.getActions('remove').size() !=0)
122 | Removed
123 |
124 | #foreach($actionItem in $release.getActions('remove'))
125 | #set($action=$actionItem.getAction())
126 | #if ($actionItem.getIssue())
127 | #set($issue=$actionItem.getIssue())
128 | #else
129 | #set($issue="")
130 | #end
131 | #if ($actionItem.getDueTo())
132 | #set($dueto=$actionItem.getDueTo())
133 | #else
134 | #set($dueto="")
135 | #end
136 | * [#$issue](${project.url}/issues/$issue): ${action} #if($!issue != ""). #end#if($!dueto != "")Thanks to $dueto. #end
137 |
138 | #set($issue="")
139 | #set($dueto="")
140 | #end
141 | #end
142 | ## End of main loop
143 | #end
144 |
145 | Have fun!
146 | -${developmentTeam}
147 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchContainerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import com.github.dockerjava.api.exception.DockerClientException;
24 | import org.apache.http.auth.AuthScope;
25 | import org.apache.http.auth.UsernamePasswordCredentials;
26 | import org.apache.http.client.CredentialsProvider;
27 | import org.apache.http.impl.client.BasicCredentialsProvider;
28 | import org.apache.http.util.EntityUtils;
29 | import org.elasticsearch.client.Response;
30 | import org.elasticsearch.client.RestClient;
31 | import org.junit.After;
32 | import org.junit.Test;
33 | import org.testcontainers.containers.ContainerFetchException;
34 |
35 | import java.io.IOException;
36 | import java.nio.file.Paths;
37 | import java.util.Properties;
38 |
39 | import static fr.pilato.elasticsearch.containers.ElasticsearchContainer.ELASTICSEARCH_DEFAULT_BASE_URL;
40 | import static fr.pilato.elasticsearch.containers.ElasticsearchContainer.ELASTICSEARCH_DEFAULT_VERSION;
41 | import static org.hamcrest.CoreMatchers.containsString;
42 | import static org.hamcrest.CoreMatchers.instanceOf;
43 | import static org.hamcrest.CoreMatchers.is;
44 | import static org.hamcrest.MatcherAssert.assertThat;
45 | import static org.junit.Assume.assumeTrue;
46 |
47 | public class ElasticsearchContainerTest {
48 |
49 | private ElasticsearchContainer container = null;
50 | private RestClient client = null;
51 |
52 | @After
53 | public void stopRestClient() throws IOException {
54 | if (client != null) {
55 | client.close();
56 | }
57 | }
58 |
59 | @After
60 | public void stopContainer() {
61 | if (container != null) {
62 | container.stop();
63 | }
64 | }
65 |
66 | @Test
67 | public void elasticsearchNoVersionTest() throws IOException {
68 | container = new ElasticsearchContainer();
69 | container.withEnv("ELASTIC_PASSWORD", "changeme");
70 | container.start();
71 | Response response = getClient(container).performRequest("GET", "/");
72 | assertThat(response.getStatusLine().getStatusCode(), is(200));
73 | }
74 |
75 | @Test
76 | public void elasticsearchDefaultTest() throws IOException {
77 | container = new ElasticsearchContainer();
78 | container.withVersion(ELASTICSEARCH_DEFAULT_VERSION);
79 | container.withEnv("ELASTIC_PASSWORD", "changeme");
80 | container.start();
81 | Response response = getClient(container).performRequest("GET", "/");
82 | assertThat(response.getStatusLine().getStatusCode(), is(200));
83 | }
84 |
85 | @Test
86 | public void elasticsearchFullTest() throws IOException {
87 | container = new ElasticsearchContainer();
88 | container.withVersion(ELASTICSEARCH_DEFAULT_VERSION);
89 | container.withBaseUrl(ELASTICSEARCH_DEFAULT_BASE_URL);
90 |
91 | // We need to read where we exactly put the files
92 | Properties props = new Properties();
93 | try {
94 | props.load(ElasticsearchResource.class.getResourceAsStream("elasticsearch-plugins-dir.properties"));
95 | String pluginDir = props.getProperty("pluginDir");
96 | container.withPluginDir(Paths.get(pluginDir));
97 | } catch (IOException ignored) {
98 | // This can normally never happen unless someone modifies the test resources dir o_O
99 | }
100 |
101 | container.withEnv("ELASTIC_PASSWORD", "changeme");
102 |
103 | container.start();
104 |
105 | Response response = getClient(container).performRequest("GET", "/");
106 | assertThat(response.getStatusLine().getStatusCode(), is(200));
107 |
108 | response = getClient(container).performRequest("GET", "/_cat/plugins");
109 | assertThat(response.getStatusLine().getStatusCode(), is(200));
110 | String responseAsString = EntityUtils.toString(response.getEntity());
111 | assertThat(responseAsString, containsString("ingest-attachment"));
112 | }
113 |
114 | @Test
115 | public void elasticsearchWithPlugin() throws IOException {
116 | // This test might fail if no internet connection is available
117 | // As elasticsearch-plugin would download files from internet
118 | // In which case, we should just ignore the test
119 |
120 | container = new ElasticsearchContainer();
121 | container.withVersion(ELASTICSEARCH_DEFAULT_VERSION);
122 | container.withPlugin("discovery-gce");
123 | container.withEnv("ELASTIC_PASSWORD", "changeme");
124 |
125 | try {
126 | container.start();
127 | Response response = getClient(container).performRequest("GET", "/");
128 | assertThat(response.getStatusLine().getStatusCode(), is(200));
129 |
130 | response = getClient(container).performRequest("GET", "/_cat/plugins");
131 | assertThat(response.getStatusLine().getStatusCode(), is(200));
132 | String responseAsString = EntityUtils.toString(response.getEntity());
133 | assertThat(responseAsString, containsString("discovery-gce"));
134 | } catch (ContainerFetchException exception) {
135 | assertThat(exception.getCause(), instanceOf(DockerClientException.class));
136 | assertThat(exception.getCause().getMessage(), containsString("The command '/bin/sh -c bin/elasticsearch-plugin install discovery-gce' returned a non-zero code: 1"));
137 | assumeTrue("We can't test this if internet is not available because we can't download elasticsearch plugins.", false);
138 | }
139 | }
140 |
141 | private RestClient getClient(ElasticsearchContainer container) {
142 | if (client == null) {
143 | final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
144 | credentialsProvider.setCredentials(AuthScope.ANY,
145 | new UsernamePasswordCredentials("elastic", "changeme"));
146 |
147 | client = RestClient.builder(container.getHost())
148 | .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
149 | .build();
150 | }
151 |
152 | return client;
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceBaseTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.apache.http.auth.AuthScope;
24 | import org.apache.http.auth.UsernamePasswordCredentials;
25 | import org.apache.http.client.CredentialsProvider;
26 | import org.apache.http.impl.client.BasicCredentialsProvider;
27 | import org.elasticsearch.client.Response;
28 | import org.elasticsearch.client.RestClient;
29 | import org.junit.After;
30 | import org.junit.Before;
31 | import org.junit.Test;
32 |
33 | import java.io.IOException;
34 |
35 | import static org.hamcrest.CoreMatchers.is;
36 | import static org.hamcrest.MatcherAssert.assertThat;
37 |
38 | public abstract class ElasticsearchResourceBaseTest {
39 |
40 | abstract ElasticsearchResource getElasticsearchResource();
41 |
42 | RestClient restClient = null;
43 |
44 | @Before
45 | public void createRestClient() {
46 | if (restClient == null) {
47 | final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
48 | credentialsProvider.setCredentials(AuthScope.ANY,
49 | new UsernamePasswordCredentials("elastic", getElasticsearchResource().getPassword()));
50 |
51 | restClient = RestClient.builder(getElasticsearchResource().getHost())
52 | .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
53 | .build();
54 | }
55 | }
56 |
57 | @After
58 | public void stopRestClient() throws IOException {
59 | if (restClient != null) {
60 | restClient.close();
61 | }
62 | }
63 |
64 | @Test
65 | public void elasticsearchTest() throws IOException {
66 | Response response = restClient.performRequest("GET", "/");
67 | assertThat(response.getStatusLine().getStatusCode(), is(200));
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceContructorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.junit.Ignore;
24 |
25 | import java.nio.file.Paths;
26 | import java.util.Collections;
27 |
28 | /**
29 | * We just use this class to help for documenting the project.
30 | * We disable the test (well, it's not a test technically speaking)
31 | */
32 | @Ignore
33 | public class ElasticsearchResourceContructorTest {
34 |
35 | // We don't annotate it as a Rule as we just want to have it here for documentation
36 | public ElasticsearchResource elasticsearch = new ElasticsearchResource(
37 | "docker.elastic.co/elasticsearch/elasticsearch", // baseUrl (can be null)
38 | "6.3.0", // version (can be null)
39 | Paths.get("/path/to/zipped-plugins-dir"), // pluginsDir (can be null)
40 | Collections.singletonList("ingest-attachment"), // standard plugins (can be empty)
41 | Collections.singletonMap("foo", "bar"), // Map of secured settings (can be empty)
42 | "changeme"); // X-Pack security password to set (can be null)
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceDefaultsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.junit.ClassRule;
24 |
25 | public class ElasticsearchResourceDefaultsTest extends ElasticsearchResourceBaseTest {
26 | @ClassRule
27 | public static ElasticsearchResource elasticsearch = new ElasticsearchResource();
28 |
29 | @Override
30 | ElasticsearchResource getElasticsearchResource() {
31 | return elasticsearch;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceWithOssImageTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.apache.http.util.EntityUtils;
24 | import org.elasticsearch.client.Response;
25 | import org.junit.ClassRule;
26 | import org.junit.Test;
27 |
28 | import java.io.IOException;
29 |
30 | import static org.hamcrest.CoreMatchers.containsString;
31 | import static org.hamcrest.CoreMatchers.is;
32 | import static org.hamcrest.CoreMatchers.not;
33 | import static org.hamcrest.MatcherAssert.assertThat;
34 |
35 | public class ElasticsearchResourceWithOssImageTest extends ElasticsearchResourceBaseTest {
36 | @ClassRule
37 | public static ElasticsearchResource elasticsearch = new ElasticsearchResource("elasticsearch-oss-image.properties");
38 |
39 | @Override
40 | ElasticsearchResource getElasticsearchResource() {
41 | return elasticsearch;
42 | }
43 |
44 | @Test
45 | public void testXPackIsNotHere() throws IOException {
46 | Response response = restClient.performRequest("GET", "/_cat/plugins");
47 | assertThat(response.getStatusLine().getStatusCode(), is(200));
48 | String responseAsString = EntityUtils.toString(response.getEntity());
49 | assertThat(responseAsString, not(containsString("x-pack")));
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceWithPluginsAndPluginsDirTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.apache.http.util.EntityUtils;
24 | import org.elasticsearch.client.Response;
25 | import org.junit.BeforeClass;
26 | import org.junit.ClassRule;
27 | import org.junit.Test;
28 | import org.junit.rules.ExternalResource;
29 |
30 | import java.io.IOException;
31 |
32 | import static org.hamcrest.CoreMatchers.containsString;
33 | import static org.hamcrest.CoreMatchers.is;
34 | import static org.hamcrest.MatcherAssert.assertThat;
35 | import static org.junit.Assume.assumeTrue;
36 |
37 | /**
38 | * Note that this class requires internet to run as it will try to download an elasticsearch plugin
39 | */
40 | public class ElasticsearchResourceWithPluginsAndPluginsDirTest extends ElasticsearchResourceBaseTest {
41 |
42 | @ClassRule
43 | public static ElasticsearchResource elasticsearch = new ElasticsearchResource("elasticsearch-plugins-and-plugins-dir.properties");
44 |
45 | @Override
46 | ElasticsearchResource getElasticsearchResource() {
47 | return elasticsearch;
48 | }
49 |
50 | @Test
51 | public void testPluginsAreInstalled() {
52 | String list = executeCommandInDocker("bin/elasticsearch-plugin", "list");
53 | assertThat(list, containsString("ingest-attachment"));
54 | assertThat(list, containsString("discovery-gce"));
55 | }
56 |
57 | @Test
58 | public void testPluginsAreLoaded() throws IOException {
59 | Response response = restClient.performRequest("GET", "/_cat/plugins");
60 | assertThat(response.getStatusLine().getStatusCode(), is(200));
61 | String responseAsString = EntityUtils.toString(response.getEntity());
62 | assertThat(responseAsString, containsString("ingest-attachment"));
63 | assertThat(responseAsString, containsString("discovery-gce"));
64 | }
65 |
66 | private String executeCommandInDocker(String... params) {
67 | try {
68 | return elasticsearch.getContainer().execInContainer(params).getStdout();
69 | } catch (IOException | InterruptedException e) {
70 | throw new RuntimeException(e);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceWithPluginsDirAndVersionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.apache.http.util.EntityUtils;
24 | import org.elasticsearch.client.Response;
25 | import org.junit.ClassRule;
26 | import org.junit.Test;
27 |
28 | import java.io.IOException;
29 |
30 | import static org.hamcrest.CoreMatchers.containsString;
31 | import static org.hamcrest.CoreMatchers.is;
32 | import static org.hamcrest.MatcherAssert.assertThat;
33 |
34 | public class ElasticsearchResourceWithPluginsDirAndVersionTest extends ElasticsearchResourceBaseTest {
35 | @ClassRule
36 | public static ElasticsearchResource elasticsearch = new ElasticsearchResource("elasticsearch-plugins-dir-version.properties");
37 |
38 | @Override
39 | ElasticsearchResource getElasticsearchResource() {
40 | return elasticsearch;
41 | }
42 |
43 | @Test
44 | public void testPluginsAreInstalled() {
45 | String list = executeCommandInDocker("bin/elasticsearch-plugin", "list");
46 | assertThat(list, containsString("ingest-attachment"));
47 | }
48 |
49 | @Test
50 | public void testPluginsAreLoaded() throws IOException {
51 | Response response = restClient.performRequest("GET", "/_cat/plugins");
52 | assertThat(response.getStatusLine().getStatusCode(), is(200));
53 | String responseAsString = EntityUtils.toString(response.getEntity());
54 | assertThat(responseAsString, containsString("ingest-attachment"));
55 | }
56 |
57 | private String executeCommandInDocker(String... params) {
58 | try {
59 | return elasticsearch.getContainer().execInContainer(params).getStdout();
60 | } catch (IOException | InterruptedException e) {
61 | throw new RuntimeException(e);
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceWithPluginsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.apache.http.util.EntityUtils;
24 | import org.elasticsearch.client.Response;
25 | import org.junit.ClassRule;
26 | import org.junit.Test;
27 |
28 | import java.io.IOException;
29 |
30 | import static org.hamcrest.CoreMatchers.containsString;
31 | import static org.hamcrest.CoreMatchers.is;
32 | import static org.hamcrest.MatcherAssert.assertThat;
33 |
34 | /**
35 | * Note that this class requires internet to run as it will try to download an elasticsearch plugin
36 | */
37 | public class ElasticsearchResourceWithPluginsTest extends ElasticsearchResourceBaseTest {
38 | @ClassRule
39 | public static ElasticsearchResource elasticsearch = new ElasticsearchResource("elasticsearch-plugins.properties");
40 |
41 | @Override
42 | ElasticsearchResource getElasticsearchResource() {
43 | return elasticsearch;
44 | }
45 |
46 | @Test
47 | public void testPluginsAreInstalled() {
48 | String list = executeCommandInDocker("bin/elasticsearch-plugin", "list");
49 | assertThat(list, containsString("ingest-attachment"));
50 | assertThat(list, containsString("discovery-gce"));
51 | }
52 |
53 | @Test
54 | public void testPluginsAreLoaded() throws IOException {
55 | Response response = restClient.performRequest("GET", "/_cat/plugins");
56 | assertThat(response.getStatusLine().getStatusCode(), is(200));
57 | String responseAsString = EntityUtils.toString(response.getEntity());
58 | assertThat(responseAsString, containsString("ingest-attachment"));
59 | assertThat(responseAsString, containsString("discovery-gce"));
60 | }
61 |
62 | private String executeCommandInDocker(String... params) {
63 | try {
64 | return elasticsearch.getContainer().execInContainer(params).getStdout();
65 | } catch (IOException | InterruptedException e) {
66 | throw new RuntimeException(e);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/test/java/fr/pilato/elasticsearch/containers/ElasticsearchResourceWithVersionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to David Pilato (the "Author") under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. Author licenses this
6 | * file to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package fr.pilato.elasticsearch.containers;
21 |
22 |
23 | import org.junit.ClassRule;
24 |
25 | public class ElasticsearchResourceWithVersionTest extends ElasticsearchResourceBaseTest {
26 | @ClassRule
27 | public static ElasticsearchResource elasticsearch = new ElasticsearchResource("elasticsearch-version.properties");
28 |
29 | @Override
30 | ElasticsearchResource getElasticsearchResource() {
31 | return elasticsearch;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/resources/fr/pilato/elasticsearch/containers/elasticsearch-oss-image.properties:
--------------------------------------------------------------------------------
1 | baseUrl=docker.elastic.co/elasticsearch/elasticsearch-oss
2 |
--------------------------------------------------------------------------------
/src/test/resources/fr/pilato/elasticsearch/containers/elasticsearch-plugins-and-plugins-dir.properties:
--------------------------------------------------------------------------------
1 | plugins=discovery-gce
2 | pluginDir=${plugin.dir}/${elasticsearch.version}
3 |
--------------------------------------------------------------------------------
/src/test/resources/fr/pilato/elasticsearch/containers/elasticsearch-plugins-dir-version.properties:
--------------------------------------------------------------------------------
1 | version=${elasticsearch.version.old}
2 | pluginDir=${plugin.dir}/${elasticsearch.version.old}
3 |
--------------------------------------------------------------------------------
/src/test/resources/fr/pilato/elasticsearch/containers/elasticsearch-plugins-dir.properties:
--------------------------------------------------------------------------------
1 | pluginDir=${plugin.dir}/${elasticsearch.version}
2 |
--------------------------------------------------------------------------------
/src/test/resources/fr/pilato/elasticsearch/containers/elasticsearch-plugins.properties:
--------------------------------------------------------------------------------
1 | plugins=ingest-attachment,discovery-gce
2 |
--------------------------------------------------------------------------------
/src/test/resources/fr/pilato/elasticsearch/containers/elasticsearch-version.properties:
--------------------------------------------------------------------------------
1 | version=${elasticsearch.version.old}
2 |
--------------------------------------------------------------------------------
/src/test/resources/logback-test.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 | %d{HH:mm:ss.SSS} %-5level %logger - %msg%n
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | PROFILER
28 | DENY
29 |
30 |
31 |
--------------------------------------------------------------------------------