Merge branch 'stable-2.15' into stable-2.16

* stable-2.15:
  Add ability to disable healthchecks
  Fix typo when getting auth username from configuration file

Change-Id: Iaddf663b7f3feeab6ff09a6db0b74317f758d200
diff --git a/README.md b/README.md
index fe38bc8..f375030 100644
--- a/README.md
+++ b/README.md
@@ -46,8 +46,9 @@
 - ts: epoch timestamp in millis of the individual check
 - elapsed: elapsed time in millis to complete the check
 - result: result of the health check
-  
+
   - passed: the check passed successfully
+  - disabled: the check was disabled
   - failed: the check failed with an error
   - timeout: the check took too long and timed out
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
index 5e1c7ea..b92588b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
@@ -14,6 +14,8 @@
 
 package com.googlesource.gerrit.plugins.healthcheck;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Strings;
@@ -40,6 +42,7 @@
   private static final int LIMIT_DEFAULT = 10;
   private static final String USERNAME_DEFAULT = "healthcheck";
   private static final String PASSWORD_DEFAULT = "";
+  private static final boolean HEALTH_CHECK_ENABLED_DEFAULT = true;
   private final AllProjectsName allProjectsName;
   private final AllUsersName allUsersName;
 
@@ -100,13 +103,18 @@
   }
 
   public String getUsername(String healthCheckName) {
-    return getStringWithFallback("userame", healthCheckName, USERNAME_DEFAULT);
+    return getStringWithFallback("username", healthCheckName, USERNAME_DEFAULT);
   }
 
   public String getPassword(String healthCheckName) {
     return getStringWithFallback("password", healthCheckName, PASSWORD_DEFAULT);
   }
 
+  public boolean healthCheckEnabled(String healthCheckName) {
+    return config.getBoolean(
+        HEALTHCHECK, checkNotNull(healthCheckName), "enabled", HEALTH_CHECK_ENABLED_DEFAULT);
+  }
+
   private String getStringWithFallback(
       String parameter, String healthCheckName, String defaultValue) {
     String fallbackDefault =
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
index 26ef1ee..e8aeb95 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
@@ -29,12 +29,14 @@
   private final String name;
   private final ListeningExecutorService executor;
   protected StatusSummary latestStatus;
+  protected boolean enabled;
 
   protected AbstractHealthCheck(
       ListeningExecutorService executor, HealthCheckConfig config, String name) {
     this.executor = executor;
     this.name = name;
     this.timeout = config.getTimeout(name);
+    this.enabled = config.healthCheckEnabled(name);
     this.latestStatus = StatusSummary.INITIAL_STATUS;
   }
 
@@ -51,7 +53,7 @@
             () -> {
               Result healthy;
               try {
-                healthy = doCheck();
+                healthy = enabled ? doCheck() : Result.DISABLED;
               } catch (Exception e) {
                 log.warn("Check {} failed", name, e);
                 healthy = Result.FAILED;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java
index 4f6a532..858e142 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java
@@ -15,6 +15,9 @@
 package com.googlesource.gerrit.plugins.healthcheck.check;
 
 import com.google.gson.annotations.SerializedName;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 
 public interface HealthCheck {
 
@@ -24,7 +27,9 @@
     @SerializedName("failed")
     FAILED,
     @SerializedName("timeout")
-    TIMEOUT;
+    TIMEOUT,
+    @SerializedName("disabled")
+    DISABLED;
   }
 
   public class StatusSummary {
@@ -33,6 +38,8 @@
     public final Result result;
     public final long ts;
     public final long elapsed;
+    public static final Set<Result> failingResults =
+        new HashSet(Arrays.asList(Result.FAILED, Result.TIMEOUT));
 
     public StatusSummary(Result result, long ts, long elapsed) {
       this.result = result;
@@ -41,7 +48,7 @@
     }
 
     public Boolean isFailure() {
-      return this.result != Result.PASSED;
+      return failingResults.contains(this.result);
     }
   }
 
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index c150f93..2b7a224 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -49,7 +49,10 @@
 - `projectslist` : check the ability to list projects with their descriptions
 - `auth`: check the ability to authenticate with username and password
 
-The follwing parameters are available:
+Each check name can be disabled by setting the `enabled` parameter to **false**,
+by default this parameter is set to **true**
+
+The following parameters are available:
 
 - `healthcheck.<checkName>.timeout` : Specific timeout (msec) for the
   healthcheck to complete. Zero means that there is no timeout.
@@ -71,11 +74,11 @@
    to the default ones that are always included.
 
   Default: All-Projects, All-Users
-  
+
  - `healthcheck.auth.username` : Username to use for authentication
- 
+
    Default: healthcheck
-   
+
  - `healthcheck.auth.password` : Password to use for authentication
- 
+
    Default: no password
\ No newline at end of file
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java
index fc215ff..264db94 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java
@@ -37,6 +37,22 @@
   }
 
   @Test
+  public void shouldHaveAuthUsername() {
+    HealthCheckConfig config =
+        new HealthCheckConfig("[healthcheck \"auth\"]\n" + "username=test_user");
+
+    assertThat(config.getUsername("auth")).isEqualTo("test_user");
+  }
+
+  @Test
+  public void shouldHaveAuthPassword() {
+    HealthCheckConfig config =
+        new HealthCheckConfig("[healthcheck \"auth\"]\n" + "password=secret");
+
+    assertThat(config.getPassword("auth")).isEqualTo("secret");
+  }
+
+  @Test
   public void shouldHaveCheckOverriddenTimeout() {
     HealthCheckConfig config =
         new HealthCheckConfig(
@@ -45,4 +61,28 @@
     assertThat(config.getTimeout("fooCheck")).isEqualTo(1000);
     assertThat(config.getTimeout("barCheck")).isEqualTo(2000);
   }
+
+  @Test
+  public void shouldHaveAnEnabledValue() {
+    HealthCheckConfig config =
+        new HealthCheckConfig("[healthcheck \"fooCheck\"]\n" + "enabled=false");
+
+    assertThat(config.healthCheckEnabled("fooCheck")).isEqualTo(false);
+  }
+
+  @Test
+  public void shouldHaveEnabledAndDisabledValue() {
+    HealthCheckConfig config =
+        new HealthCheckConfig(
+            "[healthcheck \"fooCheck\"]\n"
+                + "enabled=false\n"
+                + "[healthcheck \"barCheck\"]\n"
+                + "timeout=1000"
+                + "[healthcheck \"bazCheck\"]\n"
+                + "enabled=true\n");
+
+    assertThat(config.healthCheckEnabled("fooCheck")).isEqualTo(false);
+    assertThat(config.healthCheckEnabled("barCheck")).isEqualTo(true);
+    assertThat(config.healthCheckEnabled("bazCheck")).isEqualTo(true);
+  }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
index 3adee84..cb41000 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
@@ -27,6 +27,7 @@
 import com.googlesource.gerrit.plugins.healthcheck.check.ProjectsListHealthCheck;
 import java.util.SortedMap;
 import java.util.TreeMap;
+import org.eclipse.jgit.lib.Config;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -61,7 +62,7 @@
   }
 
   private ListProjects getFailingProjectList() {
-    return new ListProjects(null, null, null, null, null, null, null, null, null) {
+    return new ListProjects(null, null, null, null, null, null, null, null, null, new Config()) {
       @Override
       public SortedMap<String, ProjectInfo> apply() throws BadRequestException {
         throw new IllegalArgumentException("Unable to return project list");
@@ -70,7 +71,7 @@
   }
 
   private ListProjects getWorkingProjectList(long execTime) {
-    return new ListProjects(null, null, null, null, null, null, null, null, null) {
+    return new ListProjects(null, null, null, null, null, null, null, null, null, new Config()) {
       @Override
       public SortedMap<String, ProjectInfo> apply() throws BadRequestException {
         SortedMap<String, ProjectInfo> projects = new TreeMap<>();