Daemon: add testSysModules using the standard ModuleOverloader

Allow the testing of the module override in the sysInjector
by adding the testSysModule provided by the test to the standard
override behaviour.

Make possible the writing of integration test scenarios for all
libModules, including the ones that rely on the overriding of
existing modules.

Enable the replacement of the AuditModule in tests, by adding
the extra createAuditModule() method which is intended to be
overridden by tests. Then, the test-case is based on a default
audit module annotated with @ModuleImpl which is subsequently
overridden by a libModule, annotated with @ModuleImpl as well and
a matching name.

Bug: Issue 13393
Change-Id: I68306905c0db5cecb92a7a504fa8d4adc8305103
This commit is contained in:
Luca Milanesio
2020-09-10 21:31:33 +01:00
parent 1e20974c92
commit dceb7efcf5
5 changed files with 85 additions and 11 deletions

View File

@@ -429,13 +429,16 @@ public abstract class AbstractDaemonTest {
baseConfig.setInt("receive", null, "changeUpdateThreads", 4);
Module module = createModule();
Module auditModule = createAuditModule();
if (classDesc.equals(methodDesc) && !classDesc.sandboxed() && !methodDesc.sandboxed()) {
if (commonServer == null) {
commonServer = GerritServer.initAndStart(temporaryFolder, classDesc, baseConfig, module);
commonServer =
GerritServer.initAndStart(temporaryFolder, classDesc, baseConfig, module, auditModule);
}
server = commonServer;
} else {
server = GerritServer.initAndStart(temporaryFolder, methodDesc, baseConfig, module);
server =
GerritServer.initAndStart(temporaryFolder, methodDesc, baseConfig, module, auditModule);
}
server.getTestInjector().injectMembers(this);
@@ -528,6 +531,11 @@ public abstract class AbstractDaemonTest {
return null;
}
/** Override to bind an alternative audit Guice module */
public Module createAuditModule() {
return null;
}
protected void initSsh() throws Exception {
if (testRequiresSsh
&& SshMode.useSsh()

View File

@@ -329,14 +329,15 @@ public class GerritServer implements AutoCloseable {
TemporaryFolder temporaryFolder,
Description desc,
Config baseConfig,
@Nullable Module testSysModule)
@Nullable Module testSysModule,
@Nullable Module testAuditModule)
throws Exception {
Path site = temporaryFolder.newFolder().toPath();
try {
if (!desc.memory()) {
init(desc, baseConfig, site);
}
return start(desc, baseConfig, site, testSysModule, null);
return start(desc, baseConfig, site, testSysModule, testAuditModule, null);
} catch (Exception e) {
throw e;
}
@@ -364,6 +365,7 @@ public class GerritServer implements AutoCloseable {
Config baseConfig,
Path site,
@Nullable Module testSysModule,
@Nullable Module testAuditModule,
@Nullable InMemoryRepositoryManager inMemoryRepoManager,
String... additionalArgs)
throws Exception {
@@ -382,7 +384,8 @@ public class GerritServer implements AutoCloseable {
},
site);
daemon.setEmailModuleForTesting(new FakeEmailSender.Module());
daemon.setAuditEventModuleForTesting(new FakeGroupAuditService.Module());
daemon.setAuditEventModuleForTesting(
MoreObjects.firstNonNull(testAuditModule, new FakeGroupAuditService.Module()));
if (testSysModule != null) {
daemon.addAdditionalSysModuleForTesting(testSysModule);
}
@@ -609,7 +612,7 @@ public class GerritServer implements AutoCloseable {
server.close();
server.daemon.stop();
return start(server.desc, cfg, site, null, inMemoryRepoManager);
return start(server.desc, cfg, site, null, null, inMemoryRepoManager);
}
private static boolean hasBinding(Injector injector, Class<?> clazz) {

View File

@@ -187,7 +187,7 @@ public abstract class StandaloneSiteTest {
private GerritServer startImpl(@Nullable Module testSysModule, String... additionalArgs)
throws Exception {
return GerritServer.start(
serverDesc, baseConfig, sitePaths.site_path, testSysModule, null, additionalArgs);
serverDesc, baseConfig, sitePaths.site_path, testSysModule, null, null, additionalArgs);
}
protected static void runGerrit(String... args) throws Exception {

View File

@@ -491,12 +491,13 @@ public class Daemon extends SiteProgram {
modules.add(new AccountDeactivator.Module());
modules.add(new ChangeCleanupRunner.Module());
}
modules.addAll(testSysModules);
modules.add(new LocalMergeSuperSetComputation.Module());
modules.add(new DefaultProjectNameLockManager.Module());
return cfgInjector.createChildInjector(
ModuleOverloader.override(
modules, LibModuleLoader.loadModules(cfgInjector, LibModuleType.SYS_MODULE)));
List<Module> libModules = LibModuleLoader.loadModules(cfgInjector, LibModuleType.SYS_MODULE);
libModules.addAll(testSysModules);
return cfgInjector.createChildInjector(ModuleOverloader.override(modules, libModules));
}
private Module createIndexModule() {

View File

@@ -0,0 +1,62 @@
// Copyright (C) 2020 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.acceptance;
import static com.google.common.truth.Truth.assertThat;
import com.google.gerrit.server.ModuleImpl;
import com.google.gerrit.server.audit.AuditModule;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import org.junit.Test;
public class DaemonOverridesTestLibModulesIT extends AbstractDaemonTest {
private static final String TEST_MODULE = "test-module";
@Inject
@Named(value = TEST_MODULE)
private String testModuleClassName;
public abstract static class TestModule extends AuditModule {
@Override
protected void configure() {
super.configure();
bind(String.class).annotatedWith(Names.named(TEST_MODULE)).toInstance(getClass().getName());
}
}
@ModuleImpl(name = TEST_MODULE)
public static class DefaultModule extends TestModule {}
@ModuleImpl(name = TEST_MODULE)
public static class OverriddenModule extends TestModule {}
@Override
public Module createAuditModule() {
return new DefaultModule();
}
@Override
public Module createModule() {
return new OverriddenModule();
}
@Test
public void testSysModuleShouldOverrideTheDefaultOneWithSameModuleAnnotation() {
assertThat(testModuleClassName).isEqualTo(OverriddenModule.class.getName());
}
}