View Javadoc
1   package it.com.atlassian.plugin.refimpl;
2   
3   import com.atlassian.healthcheck.checks.plugin.PluginHealthCheckConstants;
4   import com.atlassian.healthcheck.testsupport.checks.plugin.PluginHealthCheckTestUtils.PluginHealthCheckResult;
5   import org.codehaus.jackson.JsonNode;
6   import org.junit.Test;
7   
8   import java.util.LinkedHashSet;
9   import java.util.Set;
10  
11  import static com.atlassian.healthcheck.checks.HealthCheckConstants.HEALTH_CHECK_FAILURE_REASON_FIELD;
12  import static com.atlassian.healthcheck.checks.HealthCheckConstants.HEALTH_CHECK_NAME_FIELD;
13  import static com.atlassian.healthcheck.testsupport.checks.plugin.PluginHealthCheckTestUtils.parsePluginHealthCheckResultFailureReason;
14  import static org.hamcrest.MatcherAssert.assertThat;
15  import static org.hamcrest.Matchers.empty;
16  import static org.junit.Assert.fail;
17  
18  /**
19   * Tests plugin and module enablement and startup state, using atlassian-healthcheck.
20   * <p>
21   * In current version of atlassian-healthcheck, these healthchecks don't fail by default, just log warnings
22   * and return a successful result with failure reason (until we've rid Cloud of disabled-plugin snowflakes).
23   * So we assert on the failure reason string here.
24   *
25   * @see TestHealthChecks
26   */
27  public class TestPluginStartup {
28      @Test
29      public void testDisabledPluginsHealthCheck() throws Exception {
30          final String healthCheck = PluginHealthCheckConstants.DISABLED_PLUGINS_HEALTHCHECK;
31          final String failureReason = getHealthCheckFailureReasonByName(healthCheck);
32  
33          final PluginHealthCheckResult result = parsePluginHealthCheckResultFailureReason(failureReason);
34  
35          final Set<String> failedPluginKeys = result.getFailedItems();
36          assertThat(healthCheck + " healthcheck failed because these plugins are unexpectedly disabled: "
37                          + failedPluginKeys
38                          + ".\n\nFull failure reason: '" + failureReason + "'",
39                  failedPluginKeys, empty());
40  
41          final Set<String> whitelistedPluginKeys = mutableCopy(result.getWhitelistedItems());
42          // If you add anything to atlassian-healthcheck-whitelist.txt, empty() will fail; fix by adding expectWhitelistedFailure here.
43          // If they start passing again, expectWhitelistedFailure will fail, helping you come back and clean up here.
44          assertThat("New items have been added to atlassian-healthcheck-whitelist.txt, please add expectWhitelistedFailure for them. ",
45                  whitelistedPluginKeys, empty());
46      }
47  
48      @Test
49      public void testDisabledPluginModulesHealthCheck() throws Exception {
50          final String healthCheck = PluginHealthCheckConstants.DISABLED_PLUGIN_MODULES_HEALTHCHECK;
51          final String failureReason = getHealthCheckFailureReasonByName(healthCheck);
52  
53          final PluginHealthCheckResult result = parsePluginHealthCheckResultFailureReason(failureReason);
54  
55          final Set<String> failedModuleKeys = result.getFailedItems();
56          assertThat(healthCheck + " healthcheck failed because these plugin modules are unexpectedly disabled: "
57                          + failedModuleKeys
58                          + ".\n\nFull failure reason: '" + failureReason + "'",
59                  failedModuleKeys, empty());
60  
61          final Set<String> whitelistedModuleKeys = mutableCopy(result.getWhitelistedItems());
62  
63          // We don't care about these. See CANL-19
64          expectWhitelistedFailure(whitelistedModuleKeys, "com.atlassian.plugins.atlassian-nav-links-plugin:atlassian-nav-links-whitelist");
65          expectWhitelistedFailure(whitelistedModuleKeys, "com.atlassian.applinks.applinks-plugin:applinks-whitelist");
66          expectWhitelistedFailure(whitelistedModuleKeys, "com.atlassian.upm.atlassian-universal-plugin-manager-plugin:analyticsWhitelist");
67  
68          // TODO: PLUGWEB-361
69          expectWhitelistedFailure(whitelistedModuleKeys, "com.atlassian.plugins.atlassian-plugins-webresource-plugin:modules-not-used");
70          expectWhitelistedFailure(whitelistedModuleKeys, "com.atlassian.plugins.atlassian-plugins-webresource-rest:modules-not-used");
71  
72          // If you add anything to atlassian-healthcheck-whitelist.txt, empty() will fail; fix by adding expectWhitelistedFailure above.
73          // If they start passing again, expectWhitelistedFailure will fail, helping you come back and clean up here.
74          assertThat("New items have been added to atlassian-healthcheck-whitelist.txt, please add expectWhitelistedFailure for them. ",
75                  whitelistedModuleKeys, empty());
76      }
77  
78      @Test
79      public void testDisabledByDefaultPluginsHealthCheck() throws Exception {
80          final String healthCheck = PluginHealthCheckConstants.DISABLED_BY_DEFAULT_PLUGINS_HEALTHCHECK;
81          final String failureReason = getHealthCheckFailureReasonByName(healthCheck);
82  
83          final PluginHealthCheckResult result = parsePluginHealthCheckResultFailureReason(failureReason);
84  
85          final Set<String> failedPluginKeys = result.getFailedItems();
86          assertThat(healthCheck + " healthcheck failed because these disabled-by-default plugins are unexpectedly enabled. "
87                          + "Preferred fix: change the plugin to always be enabled and not have \"state='disabled'\" in its atlassian-plugin.xml: "
88                          + failedPluginKeys
89                          + ".\n\nFull failure reason: '" + failureReason + "'",
90                  failedPluginKeys, empty());
91  
92          final Set<String> whitelistedPluginKeys = mutableCopy(result.getWhitelistedItems());
93          // If you add anything to atlassian-healthcheck-whitelist.txt, empty() will fail; fix by adding expectWhitelistedFailure here.
94          // If they start passing again, expectWhitelistedFailure will fail, helping you come back and clean up here.
95          assertThat("New items have been added to atlassian-healthcheck-whitelist.txt, please add expectWhitelistedFailure for them. ",
96                  whitelistedPluginKeys, empty());
97      }
98  
99      @Test
100     public void testDisabledByDefaultPluginModulesHealthCheck() throws Exception {
101         final String healthCheck = PluginHealthCheckConstants.DISABLED_BY_DEFAULT_PLUGIN_MODULES_HEALTHCHECK;
102         final String failureReason = getHealthCheckFailureReasonByName(healthCheck);
103 
104         final PluginHealthCheckResult result = parsePluginHealthCheckResultFailureReason(failureReason);
105 
106         final Set<String> failedModuleKeys = result.getFailedItems();
107         assertThat(healthCheck + " healthcheck failed because these disabled-by-default plugin modules are unexpectedly enabled. "
108                         + "Preferred fix: change the plugin to have all modules always enabled and not have any \"state='disabled'\" modules in its atlassian-plugin.xml: "
109                         + failedModuleKeys
110                         + ".\n\nFull failure reason: '" + failureReason + "'",
111                 failedModuleKeys, empty());
112 
113         final Set<String> whitelistedModuleKeys = mutableCopy(result.getWhitelistedItems());
114         // If you add anything to atlassian-healthcheck-whitelist.txt, empty() will fail; fix by adding expectWhitelistedFailure here.
115         // If they start passing again, expectWhitelistedFailure will fail, helping you come back and clean up here.
116         assertThat("New items have been added to atlassian-healthcheck-whitelist.txt, please add expectWhitelistedFailure for them. ",
117                 whitelistedModuleKeys, empty());
118     }
119 
120     /**
121      * Use this method for each item in atlassian-healthcheck-whitelist.txt that fails consistently every time.
122      * This helps you come back and remove items from the whitelist if they start passing in future.
123      */
124     private static void expectWhitelistedFailure(Set<String> actualWhitelistedItems, String expectedWhitelistedItemToIgnore) {
125         if (!actualWhitelistedItems.remove(expectedWhitelistedItemToIgnore)) {
126             // Someone fixed the failure we were previously ignoring! Force it to be removed from the ignore list.
127             fail("Item is no longer failing (yay!), so please remove its expectWhitelistedFailure call "
128                     + "so that we'll be able to detect future failures: '" + expectedWhitelistedItemToIgnore + "'");
129         }
130     }
131 
132     private static String getHealthCheckFailureReasonByName(String name) {
133         JsonNode statusArray = HealthCheckTestUtils.getHealthChecksStatusArray();
134 
135         for (JsonNode status : statusArray) {
136             if (name.equals(status.get(HEALTH_CHECK_NAME_FIELD).asText())) {
137                 return status.get(HEALTH_CHECK_FAILURE_REASON_FIELD).asText();
138             }
139         }
140 
141         throw new AssertionError("No result for healthcheck: " + name);
142     }
143 
144     private static Set<String> mutableCopy(Set<String> input) {
145         return new LinkedHashSet<>(input);
146     }
147 }