Merge pull request #15476 from ywelsch/fix/script-service-tests

[TEST] Fix ScriptServiceTests.testFineGrainedSettings that can loop indefinitely
This commit is contained in:
Yannick Welsch 2015-12-16 18:05:55 +01:00
commit 6b49d728ec
1 changed files with 42 additions and 47 deletions

View File

@ -33,6 +33,7 @@ import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -48,7 +49,7 @@ import static org.hamcrest.Matchers.sameInstance;
public class ScriptServiceTests extends ESTestCase { public class ScriptServiceTests extends ESTestCase {
private ResourceWatcherService resourceWatcherService; private ResourceWatcherService resourceWatcherService;
private Set<ScriptEngineService> scriptEngineServices; private ScriptEngineService scriptEngineService;
private Map<String, ScriptEngineService> scriptEnginesByLangMap; private Map<String, ScriptEngineService> scriptEnginesByLangMap;
private ScriptContextRegistry scriptContextRegistry; private ScriptContextRegistry scriptContextRegistry;
private ScriptContext[] scriptContexts; private ScriptContext[] scriptContexts;
@ -72,8 +73,8 @@ public class ScriptServiceTests extends ESTestCase {
.put("path.conf", genericConfigFolder) .put("path.conf", genericConfigFolder)
.build(); .build();
resourceWatcherService = new ResourceWatcherService(baseSettings, null); resourceWatcherService = new ResourceWatcherService(baseSettings, null);
scriptEngineServices = newHashSet(new TestEngineService()); scriptEngineService = new TestEngineService();
scriptEnginesByLangMap = ScriptModesTests.buildScriptEnginesByLangMap(scriptEngineServices); scriptEnginesByLangMap = ScriptModesTests.buildScriptEnginesByLangMap(Collections.singleton(scriptEngineService));
//randomly register custom script contexts //randomly register custom script contexts
int randomInt = randomIntBetween(0, 3); int randomInt = randomIntBetween(0, 3);
//prevent duplicates using map //prevent duplicates using map
@ -100,7 +101,7 @@ public class ScriptServiceTests extends ESTestCase {
private void buildScriptService(Settings additionalSettings) throws IOException { private void buildScriptService(Settings additionalSettings) throws IOException {
Settings finalSettings = Settings.builder().put(baseSettings).put(additionalSettings).build(); Settings finalSettings = Settings.builder().put(baseSettings).put(additionalSettings).build();
Environment environment = new Environment(finalSettings); Environment environment = new Environment(finalSettings);
scriptService = new ScriptService(finalSettings, environment, scriptEngineServices, resourceWatcherService, scriptContextRegistry) { scriptService = new ScriptService(finalSettings, environment, Collections.singleton(scriptEngineService), resourceWatcherService, scriptContextRegistry) {
@Override @Override
String getScriptFromIndex(String scriptLang, String id, HasContextAndHeaders headersContext) { String getScriptFromIndex(String scriptLang, String id, HasContextAndHeaders headersContext) {
//mock the script that gets retrieved from an index //mock the script that gets retrieved from an index
@ -225,13 +226,11 @@ public class ScriptServiceTests extends ESTestCase {
} while (scriptContextSettings.containsKey(scriptContext)); } while (scriptContextSettings.containsKey(scriptContext));
scriptContextSettings.put(scriptContext, randomFrom(ScriptMode.values())); scriptContextSettings.put(scriptContext, randomFrom(ScriptMode.values()));
} }
int numEngineSettings = randomIntBetween(0, 10); int numEngineSettings = randomIntBetween(0, ScriptType.values().length * scriptContexts.length);
Map<String, ScriptMode> engineSettings = new HashMap<>(); Map<String, ScriptMode> engineSettings = new HashMap<>();
for (int i = 0; i < numEngineSettings; i++) { for (int i = 0; i < numEngineSettings; i++) {
String settingKey; String settingKey;
do { do {
ScriptEngineService[] scriptEngineServices = this.scriptEngineServices.toArray(new ScriptEngineService[this.scriptEngineServices.size()]);
ScriptEngineService scriptEngineService = randomFrom(scriptEngineServices);
ScriptType scriptType = randomFrom(ScriptType.values()); ScriptType scriptType = randomFrom(ScriptType.values());
ScriptContext scriptContext = randomFrom(this.scriptContexts); ScriptContext scriptContext = randomFrom(this.scriptContexts);
settingKey = scriptEngineService.types()[0] + "." + scriptType + "." + scriptContext.getKey(); settingKey = scriptEngineService.types()[0] + "." + scriptType + "." + scriptContext.getKey();
@ -288,40 +287,38 @@ public class ScriptServiceTests extends ESTestCase {
buildScriptService(builder.build()); buildScriptService(builder.build());
createFileScripts("groovy", "expression", "mustache", "test"); createFileScripts("groovy", "expression", "mustache", "test");
for (ScriptEngineService scriptEngineService : scriptEngineServices) { for (ScriptType scriptType : ScriptType.values()) {
for (ScriptType scriptType : ScriptType.values()) { //make sure file scripts have a different name than inline ones.
//make sure file scripts have a different name than inline ones. //Otherwise they are always considered file ones as they can be found in the static cache.
//Otherwise they are always considered file ones as they can be found in the static cache. String script = scriptType == ScriptType.FILE ? "file_script" : "script";
String script = scriptType == ScriptType.FILE ? "file_script" : "script"; for (ScriptContext scriptContext : this.scriptContexts) {
for (ScriptContext scriptContext : this.scriptContexts) { //fallback mechanism: 1) engine specific settings 2) op based settings 3) source based settings
//fallback mechanism: 1) engine specific settings 2) op based settings 3) source based settings ScriptMode scriptMode = engineSettings.get(scriptEngineService.types()[0] + "." + scriptType + "." + scriptContext.getKey());
ScriptMode scriptMode = engineSettings.get(scriptEngineService.types()[0] + "." + scriptType + "." + scriptContext.getKey()); if (scriptMode == null) {
if (scriptMode == null) { scriptMode = scriptContextSettings.get(scriptContext.getKey());
scriptMode = scriptContextSettings.get(scriptContext.getKey()); }
} if (scriptMode == null) {
if (scriptMode == null) { scriptMode = scriptSourceSettings.get(scriptType);
scriptMode = scriptSourceSettings.get(scriptType); }
} if (scriptMode == null) {
if (scriptMode == null) { scriptMode = DEFAULT_SCRIPT_MODES.get(scriptType);
scriptMode = DEFAULT_SCRIPT_MODES.get(scriptType); }
}
for (String lang : scriptEngineService.types()) { for (String lang : scriptEngineService.types()) {
switch (scriptMode) { switch (scriptMode) {
case ON: case ON:
assertCompileAccepted(lang, script, scriptType, scriptContext, contextAndHeaders);
break;
case OFF:
assertCompileRejected(lang, script, scriptType, scriptContext, contextAndHeaders);
break;
case SANDBOX:
if (scriptEngineService.sandboxed()) {
assertCompileAccepted(lang, script, scriptType, scriptContext, contextAndHeaders); assertCompileAccepted(lang, script, scriptType, scriptContext, contextAndHeaders);
break; } else {
case OFF:
assertCompileRejected(lang, script, scriptType, scriptContext, contextAndHeaders); assertCompileRejected(lang, script, scriptType, scriptContext, contextAndHeaders);
break; }
case SANDBOX: break;
if (scriptEngineService.sandboxed()) {
assertCompileAccepted(lang, script, scriptType, scriptContext, contextAndHeaders);
} else {
assertCompileRejected(lang, script, scriptType, scriptContext, contextAndHeaders);
}
break;
}
} }
} }
} }
@ -338,15 +335,13 @@ public class ScriptServiceTests extends ESTestCase {
unknownContext = randomAsciiOfLength(randomIntBetween(1, 30)); unknownContext = randomAsciiOfLength(randomIntBetween(1, 30));
} while(scriptContextRegistry.isSupportedContext(new ScriptContext.Plugin(pluginName, unknownContext))); } while(scriptContextRegistry.isSupportedContext(new ScriptContext.Plugin(pluginName, unknownContext)));
for (ScriptEngineService scriptEngineService : scriptEngineServices) { for (String type : scriptEngineService.types()) {
for (String type : scriptEngineService.types()) { try {
try { scriptService.compile(new Script("test", randomFrom(ScriptType.values()), type, null), new ScriptContext.Plugin(
scriptService.compile(new Script("test", randomFrom(ScriptType.values()), type, null), new ScriptContext.Plugin( pluginName, unknownContext), contextAndHeaders);
pluginName, unknownContext), contextAndHeaders); fail("script compilation should have been rejected");
fail("script compilation should have been rejected"); } catch(IllegalArgumentException e) {
} catch(IllegalArgumentException e) { assertThat(e.getMessage(), containsString("script context [" + pluginName + "_" + unknownContext + "] not supported"));
assertThat(e.getMessage(), containsString("script context [" + pluginName + "_" + unknownContext + "] not supported"));
}
} }
} }
} }