Acceptance tests: Remove ES bootstrap boilerplate

Instead of initializing the indices and mapping using Transport client,
just use the production code for that: AbstractElasticIndex.deleteAll()
does the job. Refactor the code to access the injector and call this
method iterating over Collection<IndexDefinition<?, ?, ?>>. That way
we also don't need clean up anymore, because deleteAll does two things
delete all indices and create the mapping.

With this change embedded ES mode is still used for acceptance tests,
but only Node API to start a single node of cluster.

Change-Id: I57e5d0718424b8ff1593f5152b5b1113bd0b6a8e
This commit is contained in:
David Ostrovsky
2018-05-21 18:57:52 +02:00
committed by David Pursehouse
parent a29b921dc1
commit 764af7d0b5
9 changed files with 43 additions and 122 deletions

View File

@@ -42,8 +42,6 @@ public class ReindexIT extends StandaloneSiteTest {
elasticNodeInfo = ElasticTestUtils.startElasticsearchNode();
}
String indicesPrefix = UUID.randomUUID().toString();
ElasticTestUtils.createAllIndexes(elasticNodeInfo, indicesPrefix);
Config cfg = new Config();
ElasticTestUtils.configure(cfg, elasticNodeInfo.port, indicesPrefix);
return cfg;
@@ -56,6 +54,9 @@ public class ReindexIT extends StandaloneSiteTest {
Project.NameKey project = new Project.NameKey("project");
String changeId;
try (ServerContext ctx = startServer()) {
if (elasticNodeInfo != null) {
ElasticTestUtils.createAllIndexes(ctx.getInjector());
}
GerritApi gApi = ctx.getInjector().getInstance(GerritApi.class);
gApi.projects().create("project");

View File

@@ -39,6 +39,7 @@ java_library(
"//lib:gson",
"//lib:truth",
"//lib/elasticsearch",
"//lib/guice",
"//lib/jgit/org.eclipse.jgit:jgit",
],
)
@@ -54,6 +55,7 @@ junit_tests(
":elasticsearch",
":elasticsearch_test_utils",
"//gerrit-server:query_tests_code",
"//gerrit-server:server",
"//gerrit-server:testutil",
"//lib/guice",
"//lib/jgit/org.eclipse.jgit:jgit",

View File

@@ -15,32 +15,21 @@
package com.google.gerrit.elasticsearch.testing;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.elasticsearch.ElasticAccountIndex.ACCOUNTS;
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CHANGES;
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CLOSED_CHANGES;
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.OPEN_CHANGES;
import static com.google.gerrit.elasticsearch.ElasticGroupIndex.GROUPS;
import com.google.common.base.Strings;
import com.google.common.io.Files;
import com.google.gerrit.elasticsearch.ElasticAccountIndex;
import com.google.gerrit.elasticsearch.ElasticAccountIndex.AccountMapping;
import com.google.gerrit.elasticsearch.ElasticChangeIndex.ChangeMapping;
import com.google.gerrit.elasticsearch.ElasticGroupIndex;
import com.google.gerrit.elasticsearch.ElasticGroupIndex.GroupMapping;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.index.IndexDefinition;
import com.google.gerrit.server.index.IndexModule.IndexType;
import com.google.gerrit.server.index.Schema;
import com.google.gerrit.server.index.account.AccountSchemaDefinitions;
import com.google.gerrit.server.index.change.ChangeSchemaDefinitions;
import com.google.gerrit.server.index.group.GroupSchemaDefinitions;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;
@@ -51,11 +40,6 @@ import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
public final class ElasticTestUtils {
static final Gson gson =
new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
public static class ElasticNodeInfo {
public final Node node;
public final String port;
@@ -109,38 +93,6 @@ public final class ElasticTestUtils {
return new ElasticNodeInfo(node, elasticDir, getHttpPort(node));
}
public static void deleteAllIndexes(ElasticNodeInfo nodeInfo, String prefix) {
Schema<ChangeData> changeSchema = ChangeSchemaDefinitions.INSTANCE.getLatest();
nodeInfo
.node
.client()
.admin()
.indices()
.prepareDelete(String.format("%s%s_%04d", prefix, CHANGES, changeSchema.getVersion()))
.execute()
.actionGet();
Schema<AccountState> accountSchema = AccountSchemaDefinitions.INSTANCE.getLatest();
nodeInfo
.node
.client()
.admin()
.indices()
.prepareDelete(String.format("%s%s_%04d", prefix, ACCOUNTS, accountSchema.getVersion()))
.execute()
.actionGet();
Schema<AccountGroup> groupSchema = GroupSchemaDefinitions.INSTANCE.getLatest();
nodeInfo
.node
.client()
.admin()
.indices()
.prepareDelete(String.format("%s%s_%04d", prefix, GROUPS, groupSchema.getVersion()))
.execute()
.actionGet();
}
public static class NodeInfo {
String httpAddress;
}
@@ -149,46 +101,12 @@ public final class ElasticTestUtils {
Map<String, NodeInfo> nodes;
}
public static void createAllIndexes(ElasticNodeInfo nodeInfo, String prefix) {
Schema<ChangeData> changeSchema = ChangeSchemaDefinitions.INSTANCE.getLatest();
ChangeMapping openChangesMapping = new ChangeMapping(changeSchema);
ChangeMapping closedChangesMapping = new ChangeMapping(changeSchema);
openChangesMapping.closedChanges = null;
closedChangesMapping.openChanges = null;
nodeInfo
.node
.client()
.admin()
.indices()
.prepareCreate(String.format("%s%s_%04d", prefix, CHANGES, changeSchema.getVersion()))
.addMapping(OPEN_CHANGES, gson.toJson(openChangesMapping))
.addMapping(CLOSED_CHANGES, gson.toJson(closedChangesMapping))
.execute()
.actionGet();
Schema<AccountState> accountSchema = AccountSchemaDefinitions.INSTANCE.getLatest();
AccountMapping accountMapping = new AccountMapping(accountSchema);
nodeInfo
.node
.client()
.admin()
.indices()
.prepareCreate(String.format("%s%s_%04d", prefix, ACCOUNTS, accountSchema.getVersion()))
.addMapping(ElasticAccountIndex.ACCOUNTS, gson.toJson(accountMapping))
.execute()
.actionGet();
Schema<AccountGroup> groupSchema = GroupSchemaDefinitions.INSTANCE.getLatest();
GroupMapping groupMapping = new GroupMapping(groupSchema);
nodeInfo
.node
.client()
.admin()
.indices()
.prepareCreate(String.format("%s%s_%04d", prefix, GROUPS, groupSchema.getVersion()))
.addMapping(ElasticGroupIndex.GROUPS, gson.toJson(groupMapping))
.execute()
.actionGet();
public static void createAllIndexes(Injector injector) throws IOException {
Collection<IndexDefinition<?, ?, ?>> indexDefs =
injector.getInstance(Key.get(new TypeLiteral<Collection<IndexDefinition<?, ?, ?>>>() {}));
for (IndexDefinition<?, ?, ?> indexDef : indexDefs) {
indexDef.getIndexCollection().getSearchIndex().deleteAll();
}
}
private static String getHttpPort(Node node) throws InterruptedException, ExecutionException {

View File

@@ -22,7 +22,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.lib.Config;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -51,11 +50,10 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
return testName.getMethodName().toLowerCase() + "_";
}
@After
public void cleanupIndex() {
if (nodeInfo != null) {
ElasticTestUtils.deleteAllIndexes(nodeInfo, testName());
}
@Override
protected void initAfterLifecycleStart() throws Exception {
super.initAfterLifecycleStart();
ElasticTestUtils.createAllIndexes(injector);
}
@Override
@@ -64,7 +62,6 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
InMemoryModule.setDefaults(elasticsearchConfig);
String indicesPrefix = testName();
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix);
ElasticTestUtils.createAllIndexes(nodeInfo, indicesPrefix);
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
}
}

View File

@@ -24,7 +24,6 @@ import com.google.inject.Injector;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Config;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
@@ -58,11 +57,10 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
return testName.getMethodName().toLowerCase() + "_";
}
@After
public void cleanupIndex() {
if (nodeInfo != null) {
ElasticTestUtils.deleteAllIndexes(nodeInfo, testName());
}
@Override
protected void initAfterLifecycleStart() throws Exception {
super.initAfterLifecycleStart();
ElasticTestUtils.createAllIndexes(injector);
}
@Override
@@ -71,7 +69,6 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
InMemoryModule.setDefaults(elasticsearchConfig);
String indicesPrefix = testName();
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix);
ElasticTestUtils.createAllIndexes(nodeInfo, indicesPrefix);
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
}

View File

@@ -22,7 +22,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.lib.Config;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -51,11 +50,10 @@ public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
return testName.getMethodName().toLowerCase() + "_";
}
@After
public void cleanupIndex() {
if (nodeInfo != null) {
ElasticTestUtils.deleteAllIndexes(nodeInfo, testName());
}
@Override
protected void initAfterLifecycleStart() throws Exception {
super.initAfterLifecycleStart();
ElasticTestUtils.createAllIndexes(injector);
}
@Override
@@ -64,7 +62,6 @@ public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
InMemoryModule.setDefaults(elasticsearchConfig);
String indicesPrefix = testName();
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix);
ElasticTestUtils.createAllIndexes(nodeInfo, indicesPrefix);
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
}
}

View File

@@ -109,6 +109,7 @@ public abstract class AbstractQueryAccountsTest extends GerritServerTests {
@Inject protected AllProjectsName allProjects;
protected Injector injector;
protected LifecycleManager lifecycle;
protected ReviewDb db;
protected AccountInfo currentUserInfo;
@@ -119,11 +120,11 @@ public abstract class AbstractQueryAccountsTest extends GerritServerTests {
@Before
public void setUpInjector() throws Exception {
lifecycle = new LifecycleManager();
Injector injector = createInjector();
injector = createInjector();
lifecycle.add(injector);
injector.injectMembers(this);
lifecycle.start();
initAfterLifecycleStart();
db = schemaFactory.open();
schemaCreator.create(db);
@@ -139,6 +140,8 @@ public abstract class AbstractQueryAccountsTest extends GerritServerTests {
db.close();
}
protected void initAfterLifecycleStart() throws Exception {}
protected RequestContext newRequestContext(Account.Id requestUserId) {
final CurrentUser requestUser = userFactory.create(requestUserId);
return new RequestContext() {

View File

@@ -181,7 +181,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
private String systemTimeZone;
protected abstract Injector createInjector();
protected abstract Injector createInjector() throws Exception;
@Before
public void setUpInjector() throws Exception {
@@ -190,6 +190,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
lifecycle.add(injector);
injector.injectMembers(this);
lifecycle.start();
initAfterLifecycleStart();
setUpDatabase();
}
@@ -199,6 +200,8 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
db.close();
}
protected void initAfterLifecycleStart() throws Exception {}
protected void setUpDatabase() throws Exception {
db = schemaFactory.open();
schemaCreator.create(db);

View File

@@ -97,6 +97,7 @@ public abstract class AbstractQueryGroupsTest extends GerritServerTests {
@Inject protected GroupCache groupCache;
protected Injector injector;
protected LifecycleManager lifecycle;
protected ReviewDb db;
protected AccountInfo currentUserInfo;
@@ -107,11 +108,11 @@ public abstract class AbstractQueryGroupsTest extends GerritServerTests {
@Before
public void setUpInjector() throws Exception {
lifecycle = new LifecycleManager();
Injector injector = createInjector();
injector = createInjector();
lifecycle.add(injector);
injector.injectMembers(this);
lifecycle.start();
initAfterLifecycleStart();
db = schemaFactory.open();
schemaCreator.create(db);
@@ -127,6 +128,8 @@ public abstract class AbstractQueryGroupsTest extends GerritServerTests {
db.close();
}
protected void initAfterLifecycleStart() throws Exception {}
protected RequestContext newRequestContext(Account.Id requestUserId) {
final CurrentUser requestUser = userFactory.create(requestUserId);
return new RequestContext() {