Tests: Ensure watcher is enabled/disabled during tests (elastic/x-pack-elasticsearch#2392)

The method to check if watcher was enabled was returning
`randomBoolean()` and thus could change during test runs.

This fixes the test to ensure that always the same value
is returned and documents this requirement.

relates elastic/x-pack-elasticsearch#1783

Original commit: elastic/x-pack-elasticsearch@97bf3cfc29
This commit is contained in:
Alexander Reelsen 2017-08-31 12:55:05 +02:00 committed by GitHub
parent 4cada797d7
commit 2033b027ed
2 changed files with 16 additions and 26 deletions

View File

@ -28,6 +28,8 @@ import static org.hamcrest.Matchers.greaterThan;
@ClusterScope(scope = Scope.TEST, numClientNodes = 0, transportClientRatio = 0.0) @ClusterScope(scope = Scope.TEST, numClientNodes = 0, transportClientRatio = 0.0)
public class NodeStatsTests extends MonitoringIntegTestCase { public class NodeStatsTests extends MonitoringIntegTestCase {
private static Boolean WATCHER_ENABLED = null;
@Override @Override
protected Settings nodeSettings(int nodeOrdinal) { protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder() return Settings.builder()
@ -67,7 +69,7 @@ public class NodeStatsTests extends MonitoringIntegTestCase {
for (SearchHit searchHit : response.getHits().getHits()) { for (SearchHit searchHit : response.getHits().getHits()) {
Map<String, Object> fields = searchHit.getSourceAsMap(); Map<String, Object> fields = searchHit.getSourceAsMap();
for (String filter : nodeStatsFilters(watcherEnabled)) { for (String filter : nodeStatsFilters()) {
if (Constants.WINDOWS) { if (Constants.WINDOWS) {
// load average is unavailable on Windows // load average is unavailable on Windows
if (filter.startsWith("node_stats.os.cpu.load_average")) { if (filter.startsWith("node_stats.os.cpu.load_average")) {
@ -94,12 +96,10 @@ public class NodeStatsTests extends MonitoringIntegTestCase {
/** /**
* Optionally exclude {@link NodeStatsResolver#FILTERS} that require Watcher to be enabled. * Optionally exclude {@link NodeStatsResolver#FILTERS} that require Watcher to be enabled.
* *
* @param includeWatcher {@code true} to keep watcher filters.
* @return Never {@code null} or empty. * @return Never {@code null} or empty.
* @see #watcherEnabled
*/ */
private static Set<String> nodeStatsFilters(boolean includeWatcher) { private Set<String> nodeStatsFilters() {
if (includeWatcher) { if (enableWatcher()) {
return NodeStatsResolver.FILTERS; return NodeStatsResolver.FILTERS;
} }
@ -108,8 +108,11 @@ public class NodeStatsTests extends MonitoringIntegTestCase {
@Override @Override
protected boolean enableWatcher() { protected boolean enableWatcher() {
if (WATCHER_ENABLED == null) {
WATCHER_ENABLED = randomBoolean();
}
// currently this is the only Monitoring test that expects Watcher to be enabled. // currently this is the only Monitoring test that expects Watcher to be enabled.
// Once this becomes the default, then this should be removed. // Once this becomes the default, then this should be removed.
return randomBoolean(); return WATCHER_ENABLED;
} }
} }

View File

@ -85,18 +85,6 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase {
* Per test run this is enabled or disabled. * Per test run this is enabled or disabled.
*/ */
protected static Boolean securityEnabled; protected static Boolean securityEnabled;
/**
* Enables individual tests to control the behavior.
* <p>
* Control this by overriding {@link #enableWatcher()}, which defaults to disabling it (this will change!).
*/
protected Boolean watcherEnabled;
private void randomizeSettings() {
if (watcherEnabled == null) {
watcherEnabled = enableWatcher();
}
}
@Override @Override
protected TestCluster buildTestCluster(Scope scope, long seed) throws IOException { protected TestCluster buildTestCluster(Scope scope, long seed) throws IOException {
@ -109,11 +97,9 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase {
@Override @Override
protected Settings nodeSettings(int nodeOrdinal) { protected Settings nodeSettings(int nodeOrdinal) {
randomizeSettings();
Settings.Builder builder = Settings.builder() Settings.Builder builder = Settings.builder()
.put(super.nodeSettings(nodeOrdinal)) .put(super.nodeSettings(nodeOrdinal))
.put(XPackSettings.WATCHER_ENABLED.getKey(), watcherEnabled) .put(XPackSettings.WATCHER_ENABLED.getKey(), enableWatcher())
// Disable native ML autodetect_process as the c++ controller won't be available // Disable native ML autodetect_process as the c++ controller won't be available
.put(MachineLearning.AUTODETECT_PROCESS.getKey(), false) .put(MachineLearning.AUTODETECT_PROCESS.getKey(), false)
.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false) .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false)
@ -152,8 +138,6 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase {
@Override @Override
protected Settings transportClientSettings() { protected Settings transportClientSettings() {
randomizeSettings();
if (securityEnabled) { if (securityEnabled) {
return Settings.builder() return Settings.builder()
.put(super.transportClientSettings()) .put(super.transportClientSettings())
@ -161,12 +145,12 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase {
.put(Security.USER_SETTING.getKey(), "test:" + SecuritySettings.TEST_PASSWORD) .put(Security.USER_SETTING.getKey(), "test:" + SecuritySettings.TEST_PASSWORD)
.put(NetworkModule.TRANSPORT_TYPE_KEY, Security.NAME4) .put(NetworkModule.TRANSPORT_TYPE_KEY, Security.NAME4)
.put(NetworkModule.HTTP_TYPE_KEY, Security.NAME4) .put(NetworkModule.HTTP_TYPE_KEY, Security.NAME4)
.put(XPackSettings.WATCHER_ENABLED.getKey(), watcherEnabled) .put(XPackSettings.WATCHER_ENABLED.getKey(), enableWatcher())
.build(); .build();
} }
return Settings.builder().put(super.transportClientSettings()) return Settings.builder().put(super.transportClientSettings())
.put(XPackSettings.SECURITY_ENABLED.getKey(), false) .put(XPackSettings.SECURITY_ENABLED.getKey(), false)
.put(XPackSettings.WATCHER_ENABLED.getKey(), watcherEnabled) .put(XPackSettings.WATCHER_ENABLED.getKey(), enableWatcher())
.build(); .build();
} }
@ -222,7 +206,7 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (watcherEnabled != null && watcherEnabled) { if (enableWatcher()) {
internalCluster().getInstances(WatcherLifeCycleService.class) internalCluster().getInstances(WatcherLifeCycleService.class)
.forEach(w -> w.stop("tearing down watcher as part of monitoring test case")); .forEach(w -> w.stop("tearing down watcher as part of monitoring test case"));
} }
@ -232,6 +216,9 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase {
/** /**
* Override and return {@code false} to force running without Watcher. * Override and return {@code false} to force running without Watcher.
*
* Ensure that this method always returns the same value during a test run, do not put randomBoolean() in here
* as it is called more than once
*/ */
protected boolean enableWatcher() { protected boolean enableWatcher() {
// Once randomDefault() becomes the default again, then this should only be actively disabled when // Once randomDefault() becomes the default again, then this should only be actively disabled when