Honour plugin name in URI on Gerrit replica When the healthcheck plugin is running on a Gerrit replica, it needs to manage the REST-API through a filter because directly of lack of support for API infrastructure. However, when the plugin name is amended, the URI matched healthcheck plugin name hardcoded, without taking into consideration the actual name of the plugin, which could be different if amended by the Gerrit admin during setup. Use the injected @PluginName string into the filter so that it can respect the same URI as it was running on Gerrit primary node. Bug: Issue 14370 Change-Id: I7a5b13f3e31b4071ba717bb7786b80668bb23ec5
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/filter/HealthCheckStatusFilter.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/filter/HealthCheckStatusFilter.java index b2e91b1..75e48fd 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/filter/HealthCheckStatusFilter.java +++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/filter/HealthCheckStatusFilter.java
@@ -14,6 +14,7 @@ package com.googlesource.gerrit.plugins.healthcheck.filter; +import com.google.gerrit.extensions.annotations.PluginName; import com.google.gerrit.extensions.restapi.Response; import com.google.gerrit.httpd.AllRequestFilter; import com.google.gerrit.httpd.restapi.RestApiServlet; @@ -35,11 +36,14 @@ public class HealthCheckStatusFilter extends AllRequestFilter { private final HealthCheckStatusEndpoint statusEndpoint; private final Gson gson; + private final String pluginName; @Inject - public HealthCheckStatusFilter(HealthCheckStatusEndpoint statusEndpoint) { + public HealthCheckStatusFilter( + HealthCheckStatusEndpoint statusEndpoint, @PluginName String pluginName) { this.statusEndpoint = statusEndpoint; this.gson = OutputFormat.JSON.newGsonBuilder().create(); + this.pluginName = pluginName; } @Override @@ -61,7 +65,9 @@ } private boolean isStatusCheck(HttpServletRequest httpServletRequest) { - return httpServletRequest.getRequestURI().matches("(?:/a)?/config/server/healthcheck~status"); + return httpServletRequest + .getRequestURI() + .matches("(?:/a)?/config/server/" + pluginName + "~status"); } private void doStatusCheck(HttpServletResponse httpResponse) throws ServletException {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java index 16c6739..d14153f 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
@@ -26,8 +26,10 @@ import com.google.gerrit.acceptance.RestResponse; import com.google.gerrit.acceptance.Sandboxed; import com.google.gerrit.acceptance.TestPlugin; +import com.google.gerrit.extensions.annotations.PluginName; import com.google.gson.Gson; import com.google.gson.JsonObject; +import com.google.inject.Key; import com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames; import java.io.IOException; import org.eclipse.jgit.errors.ConfigInvalidException; @@ -35,13 +37,14 @@ import org.junit.Test; @TestPlugin( - name = "healthcheck", + name = "healthcheck-test", sysModule = "com.googlesource.gerrit.plugins.healthcheck.Module", httpModule = "com.googlesource.gerrit.plugins.healthcheck.HttpModule") @Sandboxed public class HealthCheckIT extends LightweightPluginDaemonTest { Gson gson = new Gson(); HealthCheckConfig config; + String healthCheckUriPath; @Override @Before @@ -54,6 +57,11 @@ createChange("refs/for/master"); } accountCreator.create(config.getUsername(HealthCheckNames.AUTH)); + + healthCheckUriPath = + String.format( + "/config/server/%s~status", + plugin.getSysInjector().getInstance(Key.get(String.class, PluginName.class))); } @Test @@ -182,11 +190,11 @@ } private RestResponse getHealthCheckStatus() throws IOException { - return adminRestSession.get("/config/server/healthcheck~status"); + return adminRestSession.get(healthCheckUriPath); } private RestResponse getHealthCheckStatusAnonymously() throws IOException { - return anonymousRestSession.get("/config/server/healthcheck~status"); + return anonymousRestSession.get(healthCheckUriPath); } private void assertCheckResult(JsonObject respPayload, String checkName, String result) {