Skip to content

Commit c854144

Browse files
committed
Fix REST tests when ran against a cloud instance
1 parent e25de1e commit c854144

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ mvn clean install -DskipUnitTests
571571

572572
## Integration Tests
573573

574-
Integration tests are launching a Docker instance. So you need to have Docker installed.
574+
Integration tests are launching a Docker instance using [TestContainers](https://java.testcontainers.org/modules/elasticsearch/).
575+
So you need to have Docker installed.
575576

576577
If you want to disable running integration tests, use `skipIntegTests` option:
577578

@@ -582,27 +583,27 @@ mvn clean install -DskipIntegTests
582583
If you wish to run integration tests against a cluster which is already running externally, you can configure the
583584
following settings to locate your cluster:
584585

585-
| setting | default |
586-
|:-----------------------------:|:-----------------------:|
587-
| `tests.cluster` | `http://127.0.0.1:9400` |
586+
| setting | default |
587+
|:--------------------:|:------------------------:|
588+
| `tests.cluster` | `https://127.0.0.1:9200` |
589+
| `tests.cluster.user` | `elastic` |
590+
| `tests.cluster.pass` | `changeme` |
588591

589592
For example:
590593

591594
```sh
592-
mvn clean install -Dtests.cluster=http://127.0.0.1:9200
595+
mvn clean install -Dtests.cluster=https://127.0.0.1:9200
593596
```
594597

595598
If you want to run your tests against an [Elastic Cloud](https://cloud.elastic.co/) instance, you can use something like:
596599

597600
```sh
598601
mvn clean install \
599-
-Dtests.cluster=https://CLUSTERID.eu-west-1.aws.found.io:9243 \
602+
-Dtests.cluster=https://CLUSTERID.us-central1.gcp.cloud.es.io:443 \
600603
-Dtests.cluster.user=elastic \
601604
-Dtests.cluster.pass=GENERATEDPASSWORD
602605
```
603606

604-
When user and password are set only Rest Tests are ran.
605-
606607
Why this name?
607608
==============
608609

src/main/documentation/README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ mvn clean install -DskipUnitTests
571571

572572
## Integration Tests
573573

574-
Integration tests are launching a Docker instance. So you need to have Docker installed.
574+
Integration tests are launching a Docker instance using [TestContainers](https://java.testcontainers.org/modules/elasticsearch/).
575+
So you need to have Docker installed.
575576

576577
If you want to disable running integration tests, use `skipIntegTests` option:
577578

@@ -582,27 +583,27 @@ mvn clean install -DskipIntegTests
582583
If you wish to run integration tests against a cluster which is already running externally, you can configure the
583584
following settings to locate your cluster:
584585

585-
| setting | default |
586-
|:-----------------------------:|:-----------------------:|
587-
| `tests.cluster` | `http://127.0.0.1:9400` |
586+
| setting | default |
587+
|:--------------------:|:------------------------:|
588+
| `tests.cluster` | `https://127.0.0.1:9200` |
589+
| `tests.cluster.user` | `elastic` |
590+
| `tests.cluster.pass` | `changeme` |
588591

589592
For example:
590593

591594
```sh
592-
mvn clean install -Dtests.cluster=http://127.0.0.1:9200
595+
mvn clean install -Dtests.cluster=https://127.0.0.1:9200
593596
```
594597

595598
If you want to run your tests against an [Elastic Cloud](https://cloud.elastic.co/) instance, you can use something like:
596599

597600
```sh
598601
mvn clean install \
599-
-Dtests.cluster=https://CLUSTERID.eu-west-1.aws.found.io:9243 \
602+
-Dtests.cluster=https://CLUSTERID.us-central1.gcp.cloud.es.io:443 \
600603
-Dtests.cluster.user=elastic \
601604
-Dtests.cluster.pass=GENERATEDPASSWORD
602605
```
603606

604-
When user and password are set only Rest Tests are ran.
605-
606607
Why this name?
607608
==============
608609

src/test/java/fr/pilato/elasticsearch/tools/BeyonderRestIT.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import static org.hamcrest.MatcherAssert.assertThat;
5555
import static org.hamcrest.Matchers.*;
5656
import static org.junit.Assume.assumeNoException;
57-
import static org.junit.Assume.assumeThat;
5857

5958
public class BeyonderRestIT extends AbstractBeyonderTest {
6059

@@ -81,39 +80,44 @@ private static void startRestClient() throws IOException {
8180
if (client == null) {
8281
client = buildClient(testCluster, testClusterUser, null);
8382
try {
84-
if (!testClusterRunning()) {
85-
Properties props = new Properties();
86-
props.load(TestContainerHelper.class.getResourceAsStream("/beyonder-tests.properties"));
87-
String version = props.getProperty("elasticsearch.version");
88-
89-
TestContainerHelper containerHelper = new TestContainerHelper();
90-
String url = containerHelper.startElasticsearch(version, testClusterPass);
91-
Path clusterCaCrtPath = null;
92-
if (containerHelper.getCertAsBytes() != null) {
93-
clusterCaCrtPath = rootTmpDir.resolve("cluster-ca.crt");
94-
Files.write(clusterCaCrtPath, containerHelper.getCertAsBytes());
83+
ConnectException connectException = testClusterRunning();
84+
if (connectException != null) {
85+
if (testCluster.equals(DEFAULT_TEST_CLUSTER)) {
86+
Properties props = new Properties();
87+
props.load(TestContainerHelper.class.getResourceAsStream("/beyonder-tests.properties"));
88+
String version = props.getProperty("elasticsearch.version");
89+
90+
TestContainerHelper containerHelper = new TestContainerHelper();
91+
String url = containerHelper.startElasticsearch(version, testClusterPass);
92+
Path clusterCaCrtPath = null;
93+
if (containerHelper.getCertAsBytes() != null) {
94+
clusterCaCrtPath = rootTmpDir.resolve("cluster-ca.crt");
95+
Files.write(clusterCaCrtPath, containerHelper.getCertAsBytes());
96+
}
97+
client = buildClient(url, DEFAULT_TEST_USER, clusterCaCrtPath);
98+
connectException = testClusterRunning();
99+
if (connectException != null) {
100+
throw new IOException(connectException);
101+
}
102+
} else {
103+
throw new IOException(connectException);
95104
}
96-
client = buildClient(url, DEFAULT_TEST_USER, clusterCaCrtPath);
97105
}
98-
} catch (ConnectException e) {
99-
// If we have an exception here, let's ignore the test
100-
logger.warn("Integration tests are skipped: [{}]", e.getMessage());
101-
assumeThat("Integration tests are skipped", e.getMessage(), not(containsString("Connection refused")));
102106
} catch (IOException e) {
103-
logger.error("Full error is", e);
107+
logger.error("Can not connect to [{}]: {}", testCluster, e.getMessage());
104108
throw e;
105109
}
106110
}
107111
}
108112

109-
private static boolean testClusterRunning() throws IOException {
113+
private static ConnectException testClusterRunning() throws IOException {
110114
try {
111115
Response response = client.performRequest(new Request("GET", "/"));
112116
Map<String, Object> asMap = (Map<String, Object>) JsonUtil.asMap(response).get("version");
113117
logger.info("Starting integration tests against an external cluster running elasticsearch [{}]", asMap.get("number"));
114-
return true;
118+
return null;
115119
} catch (ConnectException e) {
116-
return false;
120+
return e;
117121
}
118122
}
119123

@@ -598,7 +602,7 @@ public void testDateMathIndices() throws Exception {
598602
@Test
599603
public void testDateMathIndicesWithExistingDailyIndex() throws Exception {
600604
// Manually create an index named <my-index-{now/d}>
601-
client.performRequest(new Request("PUT", "%3Cmy-index-%7Bnow%2Fd%7D%3E"));
605+
client.performRequest(new Request("PUT", "/%3Cmy-index-%7Bnow%2Fd%7D%3E"));
602606

603607
testBeyonder("models/date-math-indices",
604608
singletonList("my-index-*"),
@@ -616,7 +620,7 @@ public void testDateMathIndicesWithExistingDailyIndex() throws Exception {
616620
@Test
617621
public void testDateMathIndicesWithExistingOlderIndex() throws Exception {
618622
// Manually create an index named <my-index-{now/d-1d}>
619-
client.performRequest(new Request("PUT", "%3Cmy-index-%7Bnow%2Fd-1d%7D%3E"));
623+
client.performRequest(new Request("PUT", "/%3Cmy-index-%7Bnow%2Fd-1d%7D%3E"));
620624

621625
testBeyonder("models/date-math-indices",
622626
singletonList("my-index-*"),
@@ -747,7 +751,7 @@ public void testRolloverWithDateMaths() throws Exception {
747751
}
748752

749753
private String getMapping(String indexName) throws IOException {
750-
HttpEntity response = client.performRequest(new Request("GET", indexName + "/_mapping")).getEntity();
754+
HttpEntity response = client.performRequest(new Request("GET", "/" + indexName + "/_mapping")).getEntity();
751755
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.toIntExact(response.getContentLength() > 0 ? response.getContentLength() : 4000L));
752756
IOUtils.copy(response.getContent(), out);
753757
return out.toString();

0 commit comments

Comments
 (0)