Skip to content

Commit

Permalink
Add integration tests for Elasticsearch meter registry (#1434)
Browse files Browse the repository at this point in the history
Tests the ElasticMeterRegistry against a running Elasticsearch instance in a docker container using testcontainers. Currently tested against Elasticsearch 6 and 7.

See gh-1837
Closes gh-1429

Co-authored-by: Adrian Cole <adriancole@users.noreply.github.com>
Co-authored-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 6, 2020
1 parent 7b5dbfe commit 0fcd8ee
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions implementations/micrometer-registry-elastic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ dependencies {
implementation 'org.slf4j:slf4j-api'

testImplementation project(':micrometer-test')
testImplementation 'org.testcontainers:elasticsearch:latest.release'
testImplementation 'com.jayway.jsonpath:json-path:latest.release'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright 2019 Pivotal Software, Inc.
* <p>
* Licensed 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 io.micrometer.elastic;

import com.jayway.jsonpath.JsonPath;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.ipc.http.HttpSender;
import io.micrometer.core.ipc.http.HttpUrlConnectionSender;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.junit.jupiter.Container;

import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Base class for integration tests on {@link ElasticMeterRegistry}.
*
* @author Johnny Lim
*/
abstract class AbstractElasticsearchMeterRegistryIntegrationTest {

private static final String USER = "elastic";
private static final String PASSWORD = "changeme";

@Container
private final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(getDockerImageName(getVersion()));

private final HttpSender httpSender = new HttpUrlConnectionSender();

private String host;
private ElasticMeterRegistry registry;

protected abstract String getVersion();

@BeforeEach
void setUp() {
host = "http://" + elasticsearch.getHttpHostAddress();

ElasticConfig config = new ElasticConfig() {
@Override
public String get(String key) {
return null;
}

@Override
public Duration step() {
return Duration.ofSeconds(10);
}

@Override
public String host() {
return host;
}

@Override
public String userName() {
return USER;
}

@Override
public String password() {
return PASSWORD;
}
};
registry = ElasticMeterRegistry.builder(config).build();
}

@Test
void indexTemplateShouldApply() throws Throwable {
String response = sendHttpGet(host);
String versionNumber = JsonPath.parse(response).read("$.version.number");
assertThat(versionNumber).isEqualTo(getVersion());

Counter counter = registry.counter("test.counter");
counter.increment();

registry.publish();

String indexName = registry.indexName();
String mapping = sendHttpGet(host + "/" + indexName + "/_mapping");
String countType = JsonPath.parse(mapping).read(getCountTypePath(indexName));
assertThat(countType).isEqualTo("double");
}

protected String getCountTypePath(String indexName) {
return "$." + indexName + ".mappings.doc.properties.count.type";
}

private String sendHttpGet(String uri) throws Throwable {
return httpSender.get(uri).withBasicAuthentication(USER, PASSWORD).send().body();
}

private static String getDockerImageName(String version) {
return "docker.elastic.co/elasticsearch/elasticsearch-oss:" + version;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2019 Pivotal Software, Inc.
* <p>
* Licensed 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 io.micrometer.elastic;

import org.junit.jupiter.api.Tag;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* Integration tests on {@link ElasticMeterRegistry} for Elasticsearch 6.
*
* @author Johnny Lim
*/
@Testcontainers
@Tag("docker")
class ElasticsearchMeterRegistryElasticsearch6IntegrationTest
extends AbstractElasticsearchMeterRegistryIntegrationTest {

@Override
protected String getVersion() {
return "6.8.6";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2019 Pivotal Software, Inc.
* <p>
* Licensed 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 io.micrometer.elastic;

import org.junit.jupiter.api.Tag;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* Integration tests on {@link ElasticMeterRegistry} for Elasticsearch 7.
*
* @author Johnny Lim
*/
@Testcontainers
@Tag("docker")
class ElasticsearchMeterRegistryElasticsearch7IntegrationTest
extends AbstractElasticsearchMeterRegistryIntegrationTest {

@Override
protected String getVersion() {
return "7.5.2";
}

@Override
protected String getCountTypePath(String indexName) {
return "$." + indexName + ".mappings.properties.count.type";
}

}
2 changes: 2 additions & 0 deletions micrometer-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ dependencies {
}
api 'com.github.tomakehurst:wiremock-jre8'
api 'org.mockito:mockito-core:latest.release'
api 'org.testcontainers:testcontainers:latest.release'
api 'org.testcontainers:junit-jupiter:latest.release'

testImplementation 'org.jsr107.ri:cache-ri-impl'

Expand Down

0 comments on commit 0fcd8ee

Please sign in to comment.