elasticsearch: Always set up both change and account indexes
Change query tests subtly depend on the account index for the account setup step: omitting the account index caused AccountManager#authenticate to throw an exception if an account external ID was repeated, instead of returning the account with that ID. Always set up all indexes, using a helper in ElasticTestUtils. The delete code can be simplified as well using the special string "_all" to delete all indexes. Change-Id: I7ab7be37a8cf953b6873745014e781e6b5e910f8
This commit is contained in:
@@ -34,6 +34,7 @@ java_library(
|
|||||||
testonly = 1,
|
testonly = 1,
|
||||||
srcs = glob(["src/test/java/**/ElasticTestUtils.java"]),
|
srcs = glob(["src/test/java/**/ElasticTestUtils.java"]),
|
||||||
deps = [
|
deps = [
|
||||||
|
":elasticsearch",
|
||||||
"//gerrit-extension-api:api",
|
"//gerrit-extension-api:api",
|
||||||
"//gerrit-server:server",
|
"//gerrit-server:server",
|
||||||
"//lib:gson",
|
"//lib:gson",
|
||||||
|
@@ -14,11 +14,7 @@
|
|||||||
|
|
||||||
package com.google.gerrit.elasticsearch;
|
package com.google.gerrit.elasticsearch;
|
||||||
|
|
||||||
import static com.google.gerrit.elasticsearch.ElasticAccountIndex.ACCOUNTS_PREFIX;
|
|
||||||
|
|
||||||
import com.google.gerrit.elasticsearch.ElasticAccountIndex.AccountMapping;
|
|
||||||
import com.google.gerrit.elasticsearch.ElasticTestUtils.ElasticNodeInfo;
|
import com.google.gerrit.elasticsearch.ElasticTestUtils.ElasticNodeInfo;
|
||||||
import com.google.gerrit.server.index.account.AccountSchemaDefinitions;
|
|
||||||
import com.google.gerrit.server.query.account.AbstractQueryAccountsTest;
|
import com.google.gerrit.server.query.account.AbstractQueryAccountsTest;
|
||||||
import com.google.gerrit.testutil.InMemoryModule;
|
import com.google.gerrit.testutil.InMemoryModule;
|
||||||
import com.google.inject.Guice;
|
import com.google.inject.Guice;
|
||||||
@@ -32,9 +28,6 @@ import org.junit.BeforeClass;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
|
public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
|
||||||
private static final String INDEX_NAME =
|
|
||||||
String.format("%s%04d", ACCOUNTS_PREFIX,
|
|
||||||
AccountSchemaDefinitions.INSTANCE.getLatest().getVersion());
|
|
||||||
private static ElasticNodeInfo nodeInfo;
|
private static ElasticNodeInfo nodeInfo;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
@@ -45,21 +38,7 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
||||||
createIndexes();
|
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||||
}
|
|
||||||
|
|
||||||
private static void createIndexes() {
|
|
||||||
AccountMapping accountMapping =
|
|
||||||
new AccountMapping(AccountSchemaDefinitions.INSTANCE.getLatest());
|
|
||||||
nodeInfo.node
|
|
||||||
.client()
|
|
||||||
.admin()
|
|
||||||
.indices()
|
|
||||||
.prepareCreate(INDEX_NAME)
|
|
||||||
.addMapping(ElasticAccountIndex.ACCOUNTS,
|
|
||||||
ElasticTestUtils.gson.toJson(accountMapping))
|
|
||||||
.execute()
|
|
||||||
.actionGet();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
@@ -74,8 +53,8 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
|
|||||||
@After
|
@After
|
||||||
public void cleanupIndex() {
|
public void cleanupIndex() {
|
||||||
if (nodeInfo != null) {
|
if (nodeInfo != null) {
|
||||||
ElasticTestUtils.deleteIndexes(nodeInfo.node, INDEX_NAME);
|
ElasticTestUtils.deleteAllIndexes(nodeInfo);
|
||||||
createIndexes();
|
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,13 +14,7 @@
|
|||||||
|
|
||||||
package com.google.gerrit.elasticsearch;
|
package com.google.gerrit.elasticsearch;
|
||||||
|
|
||||||
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CHANGES_PREFIX;
|
|
||||||
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CLOSED_CHANGES;
|
|
||||||
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.OPEN_CHANGES;
|
|
||||||
|
|
||||||
import com.google.gerrit.elasticsearch.ElasticChangeIndex.ChangeMapping;
|
|
||||||
import com.google.gerrit.elasticsearch.ElasticTestUtils.ElasticNodeInfo;
|
import com.google.gerrit.elasticsearch.ElasticTestUtils.ElasticNodeInfo;
|
||||||
import com.google.gerrit.server.index.change.ChangeSchemaDefinitions;
|
|
||||||
import com.google.gerrit.server.query.change.AbstractQueryChangesTest;
|
import com.google.gerrit.server.query.change.AbstractQueryChangesTest;
|
||||||
import com.google.gerrit.testutil.InMemoryModule;
|
import com.google.gerrit.testutil.InMemoryModule;
|
||||||
import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo;
|
import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo;
|
||||||
@@ -37,9 +31,6 @@ import org.junit.Test;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
|
public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
|
||||||
private static final String INDEX_NAME =
|
|
||||||
String.format("%s%04d", CHANGES_PREFIX,
|
|
||||||
ChangeSchemaDefinitions.INSTANCE.getLatest().getVersion());
|
|
||||||
private static ElasticNodeInfo nodeInfo;
|
private static ElasticNodeInfo nodeInfo;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
@@ -51,14 +42,14 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
|
|||||||
}
|
}
|
||||||
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
||||||
|
|
||||||
createIndexes();
|
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanupIndex() {
|
public void cleanupIndex() {
|
||||||
if (nodeInfo != null) {
|
if (nodeInfo != null) {
|
||||||
ElasticTestUtils.deleteIndexes(nodeInfo.node, INDEX_NAME);
|
ElasticTestUtils.deleteAllIndexes(nodeInfo);
|
||||||
createIndexes();
|
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,26 +71,6 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
|
|||||||
new InMemoryModule(elasticsearchConfig, notesMigration));
|
new InMemoryModule(elasticsearchConfig, notesMigration));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createIndexes() {
|
|
||||||
ChangeMapping openChangesMapping =
|
|
||||||
new ChangeMapping(ChangeSchemaDefinitions.INSTANCE.getLatest());
|
|
||||||
ChangeMapping closedChangesMapping =
|
|
||||||
new ChangeMapping(ChangeSchemaDefinitions.INSTANCE.getLatest());
|
|
||||||
openChangesMapping.closedChanges = null;
|
|
||||||
closedChangesMapping.openChanges = null;
|
|
||||||
nodeInfo.node
|
|
||||||
.client()
|
|
||||||
.admin()
|
|
||||||
.indices()
|
|
||||||
.prepareCreate(INDEX_NAME)
|
|
||||||
.addMapping(OPEN_CHANGES,
|
|
||||||
ElasticTestUtils.gson.toJson(openChangesMapping))
|
|
||||||
.addMapping(CLOSED_CHANGES,
|
|
||||||
ElasticTestUtils.gson.toJson(closedChangesMapping))
|
|
||||||
.execute()
|
|
||||||
.actionGet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void byOwnerInvalidQuery() throws Exception {
|
public void byOwnerInvalidQuery() throws Exception {
|
||||||
TestRepository<Repo> repo = createProject("repo");
|
TestRepository<Repo> repo = createProject("repo");
|
||||||
|
@@ -15,10 +15,21 @@
|
|||||||
package com.google.gerrit.elasticsearch;
|
package com.google.gerrit.elasticsearch;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static com.google.gerrit.elasticsearch.ElasticAccountIndex.ACCOUNTS_PREFIX;
|
||||||
|
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CHANGES_PREFIX;
|
||||||
|
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CLOSED_CHANGES;
|
||||||
|
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.OPEN_CHANGES;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
|
import com.google.gerrit.elasticsearch.ElasticAccountIndex.AccountMapping;
|
||||||
|
import com.google.gerrit.elasticsearch.ElasticChangeIndex.ChangeMapping;
|
||||||
|
import com.google.gerrit.server.account.AccountState;
|
||||||
import com.google.gerrit.server.index.IndexModule.IndexType;
|
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.query.change.ChangeData;
|
||||||
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.FieldNamingPolicy;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
@@ -100,8 +111,8 @@ final class ElasticTestUtils {
|
|||||||
return new ElasticNodeInfo(node, elasticDir, getHttpPort(node));
|
return new ElasticNodeInfo(node, elasticDir, getHttpPort(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deleteIndexes(Node node, String index) {
|
static void deleteAllIndexes(ElasticNodeInfo nodeInfo) {
|
||||||
node.client().admin().indices().prepareDelete(index).execute();
|
nodeInfo.node.client().admin().indices().prepareDelete("_all").execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
static class NodeInfo {
|
static class NodeInfo {
|
||||||
@@ -112,6 +123,39 @@ final class ElasticTestUtils {
|
|||||||
Map<String, NodeInfo> nodes;
|
Map<String, NodeInfo> nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void createAllIndexes(ElasticNodeInfo nodeInfo) {
|
||||||
|
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%04d", CHANGES_PREFIX, 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%04d", ACCOUNTS_PREFIX, accountSchema.getVersion()))
|
||||||
|
.addMapping(ElasticAccountIndex.ACCOUNTS, gson.toJson(accountMapping))
|
||||||
|
.execute()
|
||||||
|
.actionGet();
|
||||||
|
}
|
||||||
|
|
||||||
private static String getHttpPort(Node node)
|
private static String getHttpPort(Node node)
|
||||||
throws InterruptedException, ExecutionException {
|
throws InterruptedException, ExecutionException {
|
||||||
String nodes = node.client().admin().cluster()
|
String nodes = node.client().admin().cluster()
|
||||||
|
Reference in New Issue
Block a user