Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Elasticsearch tests to NoSQLUnit Elasticsearch HTTP adapter #4125

Merged
merged 14 commits into from Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion graylog-project-parent/pom.xml
Expand Up @@ -545,7 +545,7 @@
<dependency>
<groupId>com.github.joschi.nosqlunit</groupId>
<artifactId>nosqlunit-elasticsearch-http</artifactId>
<version>1.0.0-3+jackson</version>
<version>1.0.0-4+jackson</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions graylog2-server/pom.xml
Expand Up @@ -63,8 +63,8 @@
<!-- to work around filtering bug, which makes maven.build.timestamp inaccessible -->
<build.timestamp>${maven.build.timestamp}</build.timestamp>

<it.es.httpPort>9200</it.es.httpPort>
<it.es.transportPort>9300</it.es.transportPort>
<it.es.httpPort>19200</it.es.httpPort>
<it.es.transportPort>19300</it.es.transportPort>
<it.es.version>5.5.2</it.es.version>
<it.es.instanceCount>1</it.es.instanceCount>
<it.es.clusterName>test</it.es.clusterName>
Expand Down
Expand Up @@ -38,6 +38,9 @@
import io.searchbox.indices.template.DeleteTemplate;
import io.searchbox.indices.template.GetTemplate;
import io.searchbox.indices.template.PutTemplate;
import org.graylog2.indexer.IndexMapping;
import org.graylog2.indexer.IndexMapping2;
import org.graylog2.indexer.IndexMapping5;
import org.junit.Rule;

import javax.inject.Inject;
Expand All @@ -57,6 +60,8 @@

public abstract class ElasticsearchBase {
private static final String PROPERTIES_RESOURCE_NAME = "elasticsearch.properties";
private static final String DEFAULT_PORT = "9200";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be 19200 now?

Copy link
Contributor Author

@joschi joschi Sep 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because if you run the tests from the IDE, you usually want to use the default Elasticsearch HTTP port.

When the tests run in context of the integration-test phase of Maven, the property with the port of the Elasticsearch HTTP API of the "test" instance will be there and override the default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


public static final Duration ES_TIMEOUT = Duration.seconds(5L);

@Rule
Expand Down Expand Up @@ -86,7 +91,7 @@ private Properties loadProperties(String resourceName) {
}

protected String getHttpPort() {
return properties.getProperty("httpPort", "9200");
return properties.getProperty("httpPort", DEFAULT_PORT);
}

protected String getServer() {
Expand All @@ -113,6 +118,17 @@ protected void assertSucceeded(JestResult jestResult) {
.isTrue();
}

protected IndexMapping indexMapping() {
switch (elasticsearchVersion.getMajorVersion()) {
case 2:
return new IndexMapping2();
case 5:
return new IndexMapping5();
default:
throw new IllegalStateException("Only Elasticsearch 2.x and 5.x are supported");
}
}

public void createIndex(String index) throws IOException {
createIndex(index, 1, 0);
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.joschi.nosqlunit.elasticsearch.http.ElasticsearchConfiguration;
import com.google.common.collect.ImmutableMap;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
Expand All @@ -42,7 +43,6 @@
import org.graylog2.indexer.indices.events.IndicesDeletedEvent;
import org.graylog2.indexer.indices.events.IndicesReopenedEvent;
import org.graylog2.indexer.messages.Messages;
import org.graylog2.indexer.nosqlunit.IndexCreatingLoadStrategyFactory;
import org.graylog2.indexer.retention.strategies.DeletionRetentionStrategy;
import org.graylog2.indexer.retention.strategies.DeletionRetentionStrategyConfig;
import org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategy;
Expand Down Expand Up @@ -75,7 +75,7 @@ public class IndicesIT extends ElasticsearchBase {
@Rule
public final MockitoRule mockitoRule = MockitoJUnit.rule();

private final IndexSetConfig indexSetConfig = IndexSetConfig.builder()
private static final IndexSetConfig indexSetConfig = IndexSetConfig.builder()
.id("index-set-1")
.title("Index set 1")
.description("For testing")
Expand All @@ -92,14 +92,19 @@ public class IndicesIT extends ElasticsearchBase {
.indexOptimizationMaxNumSegments(1)
.indexOptimizationDisabled(false)
.build();
private final IndexSet indexSet = new TestIndexSet(indexSetConfig);
private static final IndexSet indexSet = new TestIndexSet(indexSetConfig);

private EventBus eventBus;
private Indices indices;
private IndexMappingFactory indexMappingFactory;

public IndicesIT() {
elasticsearchRule.setLoadStrategyFactory(new IndexCreatingLoadStrategyFactory(indexSet, Collections.singleton(INDEX_NAME)));
@Override
protected ElasticsearchConfiguration.Builder elasticsearchConfiguration() {
final Map<String, Map<String, Object>> messageTemplates = Collections.singletonMap("graylog-test-internal", indexMapping().messageTemplate("*", "standard"));
return super.elasticsearchConfiguration()
.indexTemplates(messageTemplates)
.createIndices(false)
.deleteAllIndices(true);
}

@Before
Expand All @@ -108,7 +113,7 @@ public void setUp() throws Exception {
final Node node = new Node(client());
indexMappingFactory = new IndexMappingFactory(node);
indices = new Indices(client(),
new ObjectMapper(),
new ObjectMapperProvider().get(),
indexMappingFactory,
new Messages(new MetricRegistry(), client()),
mock(NodeId.class),
Expand All @@ -117,7 +122,6 @@ public void setUp() throws Exception {
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testDelete() throws Exception {
final String index = createRandomIndex("indices_it_");
indices.delete(index);
Expand Down Expand Up @@ -148,18 +152,19 @@ public void testClose() throws Exception {
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testAliasExists() throws Exception {
final String index = createRandomIndex("indices_it_");
final String alias = "graylog_alias_exists";
assertThat(indices.aliasExists(alias)).isFalse();

try {
addAliasMapping(INDEX_NAME, alias);
addAliasMapping(index, alias);

assertThat(indices.aliasExists(alias)).isTrue();
assertThat(indices.exists(alias)).isFalse();
} finally {
removeAliasMapping(INDEX_NAME, alias);
removeAliasMapping(index, alias);
deleteIndex(index);
}
}

Expand All @@ -185,24 +190,24 @@ public void testIndexIfIndexExists() throws Exception {
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testExistsIfIndexDoesNotExist() throws Exception {
final String indexNotAlias = "graylog_index_does_not_exist";
assertThat(indices.exists(indexNotAlias)).isFalse();
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testAliasTarget() throws Exception {
final String index = createRandomIndex("indices_it_");
final String alias = "graylog_alias_target";
assertThat(indices.aliasTarget(alias)).isEmpty();

try {
addAliasMapping(INDEX_NAME, alias);
addAliasMapping(index, alias);

assertThat(indices.aliasTarget(alias)).contains(INDEX_NAME);
assertThat(indices.aliasTarget(alias)).contains(index);
} finally {
removeAliasMapping(INDEX_NAME, alias);
removeAliasMapping(index, alias);
deleteIndex(index);
}
}

Expand Down Expand Up @@ -311,22 +316,19 @@ public void testCreateOverwritesIndexTemplate() throws Exception {
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void indexCreationDateReturnsIndexCreationDateOfExistingIndexAsDateTime() {
final String indexName = "index_creation_date_test";
public void indexCreationDateReturnsIndexCreationDateOfExistingIndexAsDateTime() throws IOException {
final DateTime now = DateTime.now(DateTimeZone.UTC);
final String indexName = createRandomIndex("indices_it_");
try {
indices.create(indexName, indexSet);
indices.indexCreationDate(indexName).ifPresent(
indexCreationDate -> org.assertj.jodatime.api.Assertions.assertThat(indexCreationDate).isAfterOrEqualTo(now)
);
} finally {
indices.delete(indexName);
deleteIndex(indexName);
}
}

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void indexCreationDateReturnsEmptyOptionalForNonExistingIndex() {
assertThat(indices.indexCreationDate("index_missing")).isEmpty();
}
Expand Down

This file was deleted.