Use different NotesMigration implementation for testing

This uses volatile booleans for each bit that can be changed on the
fly. This is not especially important for most AbstractDaemonTests,
since we don't use this functionality and they previously used
ConfigSuite to set the fields. But it will be necessary for testing
notedb rebuilding.

Change-Id: I55f01c873d6d1f4fb3d459799bb3933afbd0c59f
This commit is contained in:
Dave Borowitz
2016-02-11 16:57:45 -05:00
parent 8f5db32405
commit ed92a5daa9
10 changed files with 73 additions and 30 deletions

View File

@@ -58,14 +58,13 @@ import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.index.ChangeIndexer;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.notedb.ConfigNotesMigration;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.Util;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.InternalChangeQuery;
import com.google.gerrit.testutil.ConfigSuite;
import com.google.gerrit.testutil.TempFileUtil;
import com.google.gerrit.testutil.TestNotesMigration;
import com.google.gson.Gson;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
@@ -187,7 +186,7 @@ public abstract class AbstractDaemonTest {
protected Project.NameKey project;
@Inject
protected NotesMigration notesMigration;
protected TestNotesMigration notesMigration;
@Inject
protected ChangeNotes.Factory notesFactory;
@@ -254,9 +253,6 @@ public abstract class AbstractDaemonTest {
GerritServer.Description methodDesc =
GerritServer.Description.forTestMethod(description, configName);
if (isNoteDbTestEnabled()) {
ConfigNotesMigration.setAllEnabledConfig(baseConfig);
}
baseConfig.setString("gerrit", null, "tempSiteDir",
tempSiteDir.getRoot().getPath());
if (classDesc.equals(methodDesc)) {
@@ -269,6 +265,7 @@ public abstract class AbstractDaemonTest {
}
server.getTestInjector().injectMembers(this);
notesMigration.setAllEnabled(isNoteDbTestEnabled());
Transport.register(inProcessProtocol);
toClose = Collections.synchronizedList(new ArrayList<Repository>());
admin = accounts.admin();

View File

@@ -27,7 +27,7 @@ import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.config.TrackingFooters;
import com.google.gerrit.server.config.TrackingFootersProvider;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.notedb.ConfigNotesMigration;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.server.schema.DataSourceType;
import com.google.gerrit.server.schema.NotesMigrationSchemaFactory;
import com.google.gerrit.server.schema.ReviewDbFactory;
@@ -36,6 +36,7 @@ import com.google.gerrit.server.schema.SchemaVersion;
import com.google.gerrit.testutil.InMemoryDatabase;
import com.google.gerrit.testutil.InMemoryH2Type;
import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.TestNotesMigration;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.OrmRuntimeException;
import com.google.gwtorm.server.SchemaFactory;
@@ -76,7 +77,7 @@ class InMemoryTestingDatabaseModule extends LifecycleModule {
bind(MetricMaker.class).to(DisabledMetricMaker.class);
bind(DataSourceType.class).to(InMemoryH2Type.class);
install(new ConfigNotesMigration.Module());
bind(NotesMigration.class).to(TestNotesMigration.class);
TypeLiteral<SchemaFactory<ReviewDb>> schemaFactory =
new TypeLiteral<SchemaFactory<ReviewDb>>() {};
bind(schemaFactory).to(NotesMigrationSchemaFactory.class);

View File

@@ -46,6 +46,7 @@ import com.google.gerrit.server.util.RequestContext;
import com.google.gerrit.server.util.ThreadLocalRequestContext;
import com.google.gerrit.testutil.InMemoryDatabase;
import com.google.gerrit.testutil.InMemoryModule;
import com.google.gerrit.testutil.TestNotesMigration;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
@@ -107,7 +108,8 @@ public class GerritPublicKeyCheckerTest {
cfg.setStringList("receive", null, "trustedKey", ImmutableList.of(
Fingerprint.toString(keyB().getPublicKey().getFingerprint()),
Fingerprint.toString(keyD().getPublicKey().getFingerprint())));
Injector injector = Guice.createInjector(new InMemoryModule(cfg));
Injector injector = Guice.createInjector(
new InMemoryModule(cfg, new TestNotesMigration()));
lifecycle = new LifecycleManager();
lifecycle.add(injector);

View File

@@ -77,21 +77,13 @@ public class ConfigNotesMigration extends NotesMigration {
}
}
public static ConfigNotesMigration allEnabled() {
return new ConfigNotesMigration(allEnabledConfig());
}
public static Config allEnabledConfig() {
Config cfg = new Config();
setAllEnabledConfig(cfg);
return cfg;
}
public static void setAllEnabledConfig(Config cfg) {
for (Table t : Table.values()) {
cfg.setBoolean(NOTEDB, t.key(), WRITE, true);
cfg.setBoolean(NOTEDB, t.key(), READ, true);
}
return cfg;
}
private final boolean writeChanges;

View File

@@ -52,6 +52,7 @@ import com.google.gerrit.testutil.FakeAccountCache;
import com.google.gerrit.testutil.GerritBaseTests;
import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.TestChanges;
import com.google.gerrit.testutil.TestNotesMigration;
import com.google.gerrit.testutil.TestTimeUtil;
import com.google.gwtorm.client.KeyUtil;
import com.google.gwtorm.server.OrmException;
@@ -77,7 +78,8 @@ public class AbstractChangeNotesTest extends GerritBaseTests {
private static final TimeZone TZ =
TimeZone.getTimeZone("America/Los_Angeles");
private static final NotesMigration MIGRATION = ConfigNotesMigration.allEnabled();
private static final NotesMigration MIGRATION =
new TestNotesMigration().setAllEnabled(true);
protected Account.Id otherUserId;
protected FakeAccountCache accountCache;

View File

@@ -61,7 +61,6 @@ import com.google.gerrit.server.git.validators.CommitValidators;
import com.google.gerrit.server.index.ChangeField;
import com.google.gerrit.server.index.IndexCollection;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.project.RefControl;
import com.google.gerrit.server.schema.SchemaCreator;
@@ -111,7 +110,6 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
@Inject protected InMemoryDatabase schemaFactory;
@Inject protected InMemoryRepositoryManager repoManager;
@Inject protected InternalChangeQuery internalChangeQuery;
@Inject protected NotesMigration notesMigration;
@Inject protected ChangeNotes.Factory notesFactory;
@Inject protected PatchSetInserter.Factory patchSetFactory;
@Inject protected ProjectControl.GenericFactory projectControlFactory;

View File

@@ -30,7 +30,8 @@ public class LuceneQueryChangesTest extends AbstractQueryChangesTest {
protected Injector createInjector() {
Config luceneConfig = new Config(config);
InMemoryModule.setDefaults(luceneConfig);
return Guice.createInjector(new InMemoryModule(luceneConfig));
return Guice.createInjector(
new InMemoryModule(luceneConfig, notesMigration));
}
@Test

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.testutil;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.server.notedb.ConfigNotesMigration;
import org.eclipse.jgit.lib.Config;
import org.junit.Rule;
@@ -34,6 +33,8 @@ public class GerritServerTests extends GerritBaseTests {
@ConfigSuite.Name
private String configName;
protected TestNotesMigration notesMigration;
public static boolean isNoteDbTestEnabled() {
List<String> runValues = ImmutableList.of("yes", "y", "true", "1");
String value = System.getenv("GERRIT_ENABLE_NOTEDB");
@@ -59,9 +60,8 @@ public class GerritServerTests extends GerritBaseTests {
};
public void beforeTest() throws Exception {
if (isNoteDbTestEnabled()) {
ConfigNotesMigration.setAllEnabledConfig(config);
}
notesMigration = new TestNotesMigration()
.setAllEnabled(isNoteDbTestEnabled());
}
public void afterTest() {

View File

@@ -50,7 +50,6 @@ import com.google.gerrit.server.git.SendEmailExecutor;
import com.google.gerrit.server.index.ChangeSchemas;
import com.google.gerrit.server.index.IndexModule.IndexType;
import com.google.gerrit.server.mail.SignedTokenEmailTokenVerifier;
import com.google.gerrit.server.notedb.ConfigNotesMigration;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.server.patch.DiffExecutor;
import com.google.gerrit.server.schema.DataSourceType;
@@ -105,13 +104,15 @@ public class InMemoryModule extends FactoryModule {
}
private final Config cfg;
private final TestNotesMigration notesMigration;
public InMemoryModule() {
this(newDefaultConfig());
this(newDefaultConfig(), new TestNotesMigration());
}
public InMemoryModule(Config cfg) {
public InMemoryModule(Config cfg, TestNotesMigration notesMigration) {
this.cfg = cfg;
this.notesMigration = notesMigration;
}
public void inject(Object instance) {
@@ -161,7 +162,7 @@ public class InMemoryModule extends FactoryModule {
bind(InMemoryRepositoryManager.class).in(SINGLETON);
bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class)
.in(SINGLETON);
bind(NotesMigration.class).to(ConfigNotesMigration.class);
bind(NotesMigration.class).toInstance(notesMigration);
bind(DataSourceType.class)
.to(InMemoryH2Type.class);

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2016 The Android Open Source Project
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 com.google.gerrit.testutil;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.inject.Singleton;
/** {@link NotesMigration} with bits that can be flipped live for testing. */
@Singleton
public class TestNotesMigration extends NotesMigration {
private volatile boolean readChanges;
private volatile boolean writeChanges;
@Override
public boolean readChanges() {
return readChanges;
}
@Override
public boolean writeChanges() {
return writeChanges;
}
public TestNotesMigration setReadChanges(boolean readChanges) {
this.readChanges = readChanges;
return this;
}
public TestNotesMigration setWriteChanges(boolean writeChanges) {
this.writeChanges = writeChanges;
return this;
}
public TestNotesMigration setAllEnabled(boolean enabled) {
return setReadChanges(enabled).setWriteChanges(enabled);
}
}