Refactor Replication*IT tests to share a base class These classes have very similar setups and duplicate helper methods. Improve maintainability by reducing the duplication. ReplicationQueueIT is not modified because it is merged into ReplicationIT on stable-3.0. Change-Id: Ibc22ae4d0db2d09009f65c0e745f1095c67827ba
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDaemon.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDaemon.java new file mode 100644 index 0000000..5e38570 --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationDaemon.java
@@ -0,0 +1,129 @@ +// 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.googlesource.gerrit.plugins.replication; + +import static java.util.stream.Collectors.toList; + +import com.google.common.flogger.FluentLogger; +import com.google.gerrit.acceptance.LightweightPluginDaemonTest; +import com.google.gerrit.acceptance.TestPlugin; +import com.google.gerrit.acceptance.UseLocalDisk; +import com.google.gerrit.reviewdb.client.Project; +import com.google.gerrit.server.config.SitePaths; +import com.google.inject.Inject; +import java.io.IOException; +import java.nio.file.Path; +import java.time.Duration; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.storage.file.FileBasedConfig; +import org.eclipse.jgit.util.FS; + +/** + * This class can be extended by any Replication*IT class and provides common setup and helper + * methods. + */ +@UseLocalDisk +@TestPlugin( + name = "replication", + sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule") +public class ReplicationDaemon extends LightweightPluginDaemonTest { + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); + protected static final Optional<String> ALL_PROJECTS = Optional.empty(); + + protected static final int TEST_REPLICATION_DELAY_SECONDS = 1; + protected static final int TEST_REPLICATION_RETRY_MINUTES = 1; + protected static final int TEST_PUSH_TIME_SECONDS = 1; + protected static final Duration TEST_PUSH_TIMEOUT = + Duration.ofSeconds(TEST_REPLICATION_DELAY_SECONDS + TEST_PUSH_TIME_SECONDS); + + @Inject protected SitePaths sitePaths; + protected Path gitPath; + protected FileBasedConfig config; + + @Override + public void setUpTestPlugin() throws Exception { + gitPath = sitePaths.site_path.resolve("git"); + config = + new FileBasedConfig(sitePaths.etc_dir.resolve("replication.config").toFile(), FS.DETECTED); + config.save(); + setReplicationDestination( + "remote1", + "suffix1", + Optional.of("not-used-project")); // Simulates a full replication.config initialization + super.setUpTestPlugin(); + } + + protected void setReplicationDestination( + String remoteName, String replicaSuffix, Optional<String> project) throws IOException { + setReplicationDestination( + remoteName, Arrays.asList(replicaSuffix), project, TEST_REPLICATION_RETRY_MINUTES); + } + + protected void setReplicationDestination( + String remoteName, String replicaSuffix, Optional<String> project, int replicationDelay) + throws IOException { + setReplicationDestination(remoteName, Arrays.asList(replicaSuffix), project, replicationDelay); + } + + protected FileBasedConfig setReplicationDestination( + String remoteName, + List<String> replicaSuffixes, + Optional<String> project, + int replicationDelay) + throws IOException { + List<String> replicaUrls = + replicaSuffixes.stream() + .map(suffix -> gitPath.resolve("${name}" + suffix + ".git").toString()) + .collect(toList()); + config.setStringList("remote", remoteName, "url", replicaUrls); + config.setInt("remote", remoteName, "replicationDelay", replicationDelay); + config.setInt("remote", remoteName, "replicationRetry", TEST_REPLICATION_RETRY_MINUTES); + project.ifPresent(prj -> config.setString("remote", remoteName, "projects", prj)); + config.save(); + return config; + } + + protected Project.NameKey createTestProject(String name) throws Exception { + return createProject(name); + } + + protected boolean isPushCompleted(Project.NameKey project, String ref, Duration timeOut) { + try (Repository repo = repoManager.openRepository(project)) { + WaitUtil.waitUntil(() -> checkedGetRef(repo, ref) != null, timeOut); + return true; + } catch (InterruptedException e) { + return false; + } catch (Exception e) { + throw new RuntimeException("Cannot open repo for project" + project, e); + } + } + + protected Ref checkedGetRef(Repository repo, String branchName) { + try { + return repo.getRefDatabase().exactRef(branchName); + } catch (Exception e) { + logger.atSevere().withCause(e).log("failed to get ref %s in repo %s", branchName, repo); + return null; + } + } + + protected void reloadConfig() { + plugin.getSysInjector().getInstance(AutoReloadConfigDecorator.class).forceReload(); + } +}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java index 353beb9..706a2c6 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
@@ -17,10 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.gerrit.testing.GerritJUnit.assertThrows; import static com.googlesource.gerrit.plugins.replication.PushResultProcessing.NO_OP; -import static java.util.stream.Collectors.toList; -import com.google.common.flogger.FluentLogger; -import com.google.gerrit.acceptance.LightweightPluginDaemonTest; import com.google.gerrit.acceptance.PushOneCommit.Result; import com.google.gerrit.acceptance.TestPlugin; import com.google.gerrit.acceptance.UseLocalDisk; @@ -30,14 +27,9 @@ import com.google.gerrit.extensions.events.ProjectDeletedListener; import com.google.gerrit.extensions.registration.DynamicSet; import com.google.gerrit.reviewdb.client.Project; -import com.google.gerrit.server.config.SitePaths; import com.google.inject.Inject; import java.io.IOException; -import java.nio.file.Path; import java.time.Duration; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; import java.util.function.Supplier; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; @@ -46,44 +38,24 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; -import org.eclipse.jgit.storage.file.FileBasedConfig; -import org.eclipse.jgit.util.FS; import org.junit.Test; @UseLocalDisk @TestPlugin( name = "replication", sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule") -public class ReplicationIT extends LightweightPluginDaemonTest { - private static final Optional<String> ALL_PROJECTS = Optional.empty(); - private static final FluentLogger logger = FluentLogger.forEnclosingClass(); - private static final int TEST_REPLICATION_DELAY = 1; - private static final int TEST_REPLICATION_RETRY = 1; +public class ReplicationIT extends ReplicationDaemon { private static final int TEST_PROJECT_CREATION_SECONDS = 10; private static final Duration TEST_TIMEOUT = - Duration.ofSeconds((TEST_REPLICATION_DELAY + TEST_REPLICATION_RETRY * 60) + 1); + Duration.ofSeconds( + (TEST_REPLICATION_DELAY_SECONDS + TEST_REPLICATION_RETRY_MINUTES * 60) + 1); private static final Duration TEST_NEW_PROJECT_TIMEOUT = Duration.ofSeconds( - (TEST_REPLICATION_DELAY + TEST_REPLICATION_RETRY * 60) + TEST_PROJECT_CREATION_SECONDS); + (TEST_REPLICATION_DELAY_SECONDS + TEST_REPLICATION_RETRY_MINUTES * 60) + + TEST_PROJECT_CREATION_SECONDS); - @Inject private SitePaths sitePaths; @Inject private DynamicSet<ProjectDeletedListener> deletedListeners; - private Path gitPath; - private FileBasedConfig config; - - @Override - public void setUpTestPlugin() throws Exception { - gitPath = sitePaths.site_path.resolve("git"); - config = - new FileBasedConfig(sitePaths.etc_dir.resolve("replication.config").toFile(), FS.DETECTED); - config.save(); - setReplicationDestination( - "remote1", - "suffix1", - Optional.of("not-used-project")); // Simulates a full replication.config initialization - super.setUpTestPlugin(); - } @Test public void shouldReplicateNewProject() throws Exception { @@ -321,59 +293,10 @@ } } - private Project.NameKey createTestProject(String name) throws Exception { - return createProject(name); - } - - public boolean isPushCompleted(Project.NameKey project, String ref, Duration timeOut) { - try (Repository repo = repoManager.openRepository(project)) { - WaitUtil.waitUntil(() -> checkedGetRef(repo, ref) != null, timeOut); - return true; - } catch (InterruptedException e) { - return false; - } catch (Exception e) { - throw new RuntimeException("Cannot open repo for project" + project, e); - } - } - private Ref getRef(Repository repo, String branchName) throws IOException { return repo.getRefDatabase().exactRef(branchName); } - private Ref checkedGetRef(Repository repo, String branchName) { - try { - return repo.getRefDatabase().exactRef(branchName); - } catch (Exception e) { - logger.atSevere().withCause(e).log("failed to get ref %s in repo %s", branchName, repo); - return null; - } - } - - private void setReplicationDestination( - String remoteName, String replicaSuffix, Optional<String> project) throws IOException { - setReplicationDestination( - remoteName, Arrays.asList(replicaSuffix), project, TEST_REPLICATION_DELAY); - } - - private FileBasedConfig setReplicationDestination( - String remoteName, - List<String> replicaSuffixes, - Optional<String> project, - int replicationDelay) - throws IOException { - - List<String> replicaUrls = - replicaSuffixes.stream() - .map(suffix -> gitPath.resolve("${name}" + suffix + ".git").toString()) - .collect(toList()); - config.setStringList("remote", remoteName, "url", replicaUrls); - config.setInt("remote", remoteName, "replicationDelay", replicationDelay); - config.setInt("remote", remoteName, "replicationRetry", TEST_REPLICATION_RETRY); - project.ifPresent(prj -> config.setString("remote", remoteName, "projects", prj)); - config.save(); - return config; - } - private void setProjectDeletionReplication(String remoteName, boolean replicateProjectDeletion) throws IOException { config.setBoolean("remote", remoteName, "replicateProjectDeletions", replicateProjectDeletion); @@ -384,10 +307,6 @@ WaitUtil.waitUntil(waitCondition, TEST_TIMEOUT); } - private void reloadConfig() { - plugin.getSysInjector().getInstance(AutoReloadConfigDecorator.class).forceReload(); - } - private void shutdownConfig() { plugin.getSysInjector().getInstance(AutoReloadConfigDecorator.class).shutdown(); }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java index da064fb..fdca243 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationStorageIT.java
@@ -18,26 +18,14 @@ import static com.googlesource.gerrit.plugins.replication.PushResultProcessing.NO_OP; import static java.util.stream.Collectors.toList; -import com.google.common.flogger.FluentLogger; -import com.google.gerrit.acceptance.LightweightPluginDaemonTest; import com.google.gerrit.acceptance.TestPlugin; import com.google.gerrit.acceptance.UseLocalDisk; import com.google.gerrit.reviewdb.client.Project; -import com.google.gerrit.server.config.SitePaths; -import com.google.inject.Inject; import com.googlesource.gerrit.plugins.replication.ReplicationTasksStorage.ReplicateRefUpdate; -import java.io.IOException; -import java.nio.file.Path; -import java.time.Duration; import java.util.Arrays; import java.util.List; -import java.util.Optional; import java.util.regex.Pattern; import java.util.stream.Stream; -import org.eclipse.jgit.lib.Ref; -import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.storage.file.FileBasedConfig; -import org.eclipse.jgit.util.FS; import org.junit.Test; /** @@ -50,31 +38,11 @@ @TestPlugin( name = "replication", sysModule = "com.googlesource.gerrit.plugins.replication.ReplicationModule") -public class ReplicationStorageIT extends LightweightPluginDaemonTest { - private static final FluentLogger logger = FluentLogger.forEnclosingClass(); - private static final Optional<String> ALL_PROJECTS = Optional.empty(); - - private static final int TEST_REPLICATION_DELAY_SECONDS = 1; - private static final int TEST_REPLICATION_RETRY_MINUTES = 1; - private static final int TEST_PUSH_TIME_SECONDS = 1; - private static final Duration TEST_PUSH_TIMEOUT = - Duration.ofSeconds(TEST_REPLICATION_DELAY_SECONDS + TEST_PUSH_TIME_SECONDS); - - @Inject private SitePaths sitePaths; - private Path gitPath; - private FileBasedConfig config; +public class ReplicationStorageIT extends ReplicationDaemon { private ReplicationTasksStorage tasksStorage; @Override public void setUpTestPlugin() throws Exception { - gitPath = sitePaths.site_path.resolve("git"); - config = - new FileBasedConfig(sitePaths.etc_dir.resolve("replication.config").toFile(), FS.DETECTED); - config.save(); - setReplicationDestination( - "remote1", - "suffix1", - Optional.of("not-used-project")); // Simulates a full replication.config initialization super.setUpTestPlugin(); tasksStorage = plugin.getSysInjector().getInstance(ReplicationTasksStorage.class); } @@ -239,51 +207,6 @@ } } - private void setReplicationDestination( - String remoteName, String replicaSuffix, Optional<String> project) throws IOException { - setReplicationDestination( - remoteName, Arrays.asList(replicaSuffix), project, TEST_REPLICATION_RETRY_MINUTES); - } - - private void setReplicationDestination( - String remoteName, String replicaSuffix, Optional<String> project, int replicationDelay) - throws IOException { - setReplicationDestination(remoteName, Arrays.asList(replicaSuffix), project, replicationDelay); - } - - private FileBasedConfig setReplicationDestination( - String remoteName, - List<String> replicaSuffixes, - Optional<String> project, - int replicationDelay) - throws IOException { - List<String> replicaUrls = - replicaSuffixes.stream() - .map(suffix -> gitPath.resolve("${name}" + suffix + ".git").toString()) - .collect(toList()); - config.setStringList("remote", remoteName, "url", replicaUrls); - config.setInt("remote", remoteName, "replicationDelay", replicationDelay); - config.setInt("remote", remoteName, "replicationRetry", TEST_REPLICATION_RETRY_MINUTES); - project.ifPresent(prj -> config.setString("remote", remoteName, "projects", prj)); - config.save(); - return config; - } - - private Project.NameKey createTestProject(String name) throws Exception { - return createProject(name); - } - - public boolean isPushCompleted(Project.NameKey project, String ref, Duration timeOut) { - try (Repository repo = repoManager.openRepository(project)) { - WaitUtil.waitUntil(() -> checkedGetRef(repo, ref) != null, timeOut); - return true; - } catch (InterruptedException e) { - return false; - } catch (Exception e) { - throw new RuntimeException("Cannot open repo for project" + project, e); - } - } - private Stream<ReplicateRefUpdate> changeReplicationTasksForRemote( String changeRef, String remote) { return tasksStorage.list().stream() @@ -291,19 +214,6 @@ .filter(task -> remote.equals(task.remote)); } - private Ref checkedGetRef(Repository repo, String branchName) { - try { - return repo.getRefDatabase().exactRef(branchName); - } catch (Exception e) { - logger.atSevere().withCause(e).log("failed to get ref %s in repo %s", branchName, repo); - return null; - } - } - - private void reloadConfig() { - plugin.getSysInjector().getInstance(AutoReloadConfigDecorator.class).forceReload(); - } - private List<ReplicateRefUpdate> listReplicationTasks(String refRegex) { Pattern refmaskPattern = Pattern.compile(refRegex); return tasksStorage.list().stream()