|
@@ -57,7 +57,6 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
private Map<String, ScriptEngine> scriptEnginesByLangMap;
|
|
|
private ScriptEngineRegistry scriptEngineRegistry;
|
|
|
private ScriptContextRegistry scriptContextRegistry;
|
|
|
- private ScriptSettings scriptSettings;
|
|
|
private ScriptContext[] scriptContexts;
|
|
|
private ScriptService scriptService;
|
|
|
private Settings baseSettings;
|
|
@@ -80,8 +79,6 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
scriptEngine = new TestEngine();
|
|
|
dangerousScriptEngine = new TestDangerousEngine();
|
|
|
TestEngine defaultScriptServiceEngine = new TestEngine(Script.DEFAULT_SCRIPT_LANG) {};
|
|
|
- scriptEnginesByLangMap = ScriptModesTests.buildScriptEnginesByLangMap(
|
|
|
- new HashSet<>(Arrays.asList(scriptEngine, defaultScriptServiceEngine)));
|
|
|
//randomly register custom script contexts
|
|
|
int randomInt = randomIntBetween(0, 3);
|
|
|
//prevent duplicates using map
|
|
@@ -101,15 +98,13 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
scriptEngineRegistry = new ScriptEngineRegistry(Arrays.asList(scriptEngine, dangerousScriptEngine,
|
|
|
defaultScriptServiceEngine));
|
|
|
scriptContextRegistry = new ScriptContextRegistry(contexts.values());
|
|
|
- scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
|
|
|
scriptContexts = scriptContextRegistry.scriptContexts().toArray(new ScriptContext[scriptContextRegistry.scriptContexts().size()]);
|
|
|
logger.info("--> setup script service");
|
|
|
}
|
|
|
|
|
|
private void buildScriptService(Settings additionalSettings) throws IOException {
|
|
|
Settings finalSettings = Settings.builder().put(baseSettings).put(additionalSettings).build();
|
|
|
- // TODO:
|
|
|
- scriptService = new ScriptService(finalSettings, scriptEngineRegistry, scriptContextRegistry, scriptSettings) {
|
|
|
+ scriptService = new ScriptService(finalSettings, scriptEngineRegistry, scriptContextRegistry) {
|
|
|
@Override
|
|
|
StoredScriptSource getScriptFromClusterState(String id, String lang) {
|
|
|
//mock the script that gets retrieved from an index
|
|
@@ -179,33 +174,25 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
public void testAllowSomeScriptTypeSettings() throws IOException {
|
|
|
Settings.Builder builder = Settings.builder();
|
|
|
builder.put("script.types_allowed", "inline");
|
|
|
- builder.put("script.engine.painless.stored", false);
|
|
|
buildScriptService(builder.build());
|
|
|
|
|
|
assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH);
|
|
|
assertCompileRejected("painless", "script", ScriptType.STORED, ScriptContext.Standard.SEARCH);
|
|
|
-
|
|
|
- assertSettingDeprecationsAndWarnings(
|
|
|
- ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, "script.engine.painless.stored"));
|
|
|
}
|
|
|
|
|
|
public void testAllowSomeScriptContextSettings() throws IOException {
|
|
|
Settings.Builder builder = Settings.builder();
|
|
|
builder.put("script.contexts_allowed", "search, aggs");
|
|
|
- builder.put("script.update", false);
|
|
|
buildScriptService(builder.build());
|
|
|
|
|
|
assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH);
|
|
|
assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.AGGS);
|
|
|
assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.UPDATE);
|
|
|
-
|
|
|
- assertSettingDeprecationsAndWarnings(
|
|
|
- ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, "script.update"));
|
|
|
}
|
|
|
|
|
|
public void testAllowNoScriptTypeSettings() throws IOException {
|
|
|
Settings.Builder builder = Settings.builder();
|
|
|
- builder.put("script.types_allowed", "");
|
|
|
+ builder.put("script.types_allowed", "none");
|
|
|
buildScriptService(builder.build());
|
|
|
|
|
|
assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH);
|
|
@@ -214,7 +201,7 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
|
|
|
public void testAllowNoScriptContextSettings() throws IOException {
|
|
|
Settings.Builder builder = Settings.builder();
|
|
|
- builder.put("script.contexts_allowed", "");
|
|
|
+ builder.put("script.contexts_allowed", "none");
|
|
|
buildScriptService(builder.build());
|
|
|
|
|
|
assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH);
|
|
@@ -223,109 +210,6 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.INGEST);
|
|
|
}
|
|
|
|
|
|
- public void testDefaultBehaviourFineGrainedSettings() throws IOException {
|
|
|
- Settings.Builder builder = Settings.builder();
|
|
|
- buildScriptService(builder.build());
|
|
|
-
|
|
|
- for (ScriptContext scriptContext : scriptContexts) {
|
|
|
- assertCompileRejected("dtest", "script", ScriptType.INLINE, scriptContext);
|
|
|
- assertCompileRejected("dtest", "script", ScriptType.STORED, scriptContext);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void testFineGrainedSettings() throws IOException {
|
|
|
- //collect the fine-grained settings to set for this run
|
|
|
- int numScriptSettings = randomIntBetween(0, ScriptType.values().length);
|
|
|
- Map<ScriptType, Boolean> scriptSourceSettings = new HashMap<>();
|
|
|
- for (int i = 0; i < numScriptSettings; i++) {
|
|
|
- ScriptType scriptType;
|
|
|
- do {
|
|
|
- scriptType = randomFrom(ScriptType.values());
|
|
|
- } while (scriptSourceSettings.containsKey(scriptType));
|
|
|
- scriptSourceSettings.put(scriptType, randomBoolean());
|
|
|
- }
|
|
|
- int numScriptContextSettings = randomIntBetween(0, this.scriptContextRegistry.scriptContexts().size());
|
|
|
- Map<ScriptContext, Boolean> scriptContextSettings = new HashMap<>();
|
|
|
- for (int i = 0; i < numScriptContextSettings; i++) {
|
|
|
- ScriptContext scriptContext;
|
|
|
- do {
|
|
|
- scriptContext = randomFrom(this.scriptContexts);
|
|
|
- } while (scriptContextSettings.containsKey(scriptContext));
|
|
|
- scriptContextSettings.put(scriptContext, randomBoolean());
|
|
|
- }
|
|
|
- int numEngineSettings = randomIntBetween(0, ScriptType.values().length * scriptContexts.length);
|
|
|
- Map<String, Boolean> engineSettings = new HashMap<>();
|
|
|
- for (int i = 0; i < numEngineSettings; i++) {
|
|
|
- String settingKey;
|
|
|
- do {
|
|
|
- ScriptType scriptType = randomFrom(ScriptType.values());
|
|
|
- ScriptContext scriptContext = randomFrom(this.scriptContexts);
|
|
|
- settingKey = scriptEngine.getType() + "." + scriptType + "." + scriptContext.getKey();
|
|
|
- } while (engineSettings.containsKey(settingKey));
|
|
|
- engineSettings.put(settingKey, randomBoolean());
|
|
|
- }
|
|
|
- List<String> deprecated = new ArrayList<>();
|
|
|
- //set the selected fine-grained settings
|
|
|
- Settings.Builder builder = Settings.builder();
|
|
|
- for (Map.Entry<ScriptType, Boolean> entry : scriptSourceSettings.entrySet()) {
|
|
|
- if (entry.getValue()) {
|
|
|
- builder.put("script" + "." + entry.getKey().getName(), "true");
|
|
|
- } else {
|
|
|
- builder.put("script" + "." + entry.getKey().getName(), "false");
|
|
|
- }
|
|
|
- deprecated.add("script" + "." + entry.getKey().getName());
|
|
|
- }
|
|
|
- for (Map.Entry<ScriptContext, Boolean> entry : scriptContextSettings.entrySet()) {
|
|
|
- if (entry.getValue()) {
|
|
|
- builder.put("script" + "." + entry.getKey().getKey(), "true");
|
|
|
- } else {
|
|
|
- builder.put("script" + "." + entry.getKey().getKey(), "false");
|
|
|
- }
|
|
|
- deprecated.add("script" + "." + entry.getKey().getKey());
|
|
|
- }
|
|
|
- for (Map.Entry<String, Boolean> entry : engineSettings.entrySet()) {
|
|
|
- int delimiter = entry.getKey().indexOf('.');
|
|
|
- String part1 = entry.getKey().substring(0, delimiter);
|
|
|
- String part2 = entry.getKey().substring(delimiter + 1);
|
|
|
-
|
|
|
- String lang = randomFrom(scriptEnginesByLangMap.get(part1).getType());
|
|
|
- if (entry.getValue()) {
|
|
|
- builder.put("script.engine" + "." + lang + "." + part2, "true");
|
|
|
- } else {
|
|
|
- builder.put("script.engine" + "." + lang + "." + part2, "false");
|
|
|
- }
|
|
|
- deprecated.add("script.engine" + "." + lang + "." + part2);
|
|
|
- }
|
|
|
-
|
|
|
- buildScriptService(builder.build());
|
|
|
-
|
|
|
- for (ScriptType scriptType : ScriptType.values()) {
|
|
|
- String script = "script";
|
|
|
- for (ScriptContext scriptContext : this.scriptContexts) {
|
|
|
- //fallback mechanism: 1) engine specific settings 2) op based settings 3) source based settings
|
|
|
- Boolean scriptEnabled = engineSettings.get(dangerousScriptEngine.getType() + "." + scriptType + "." + scriptContext.getKey());
|
|
|
- if (scriptEnabled == null) {
|
|
|
- scriptEnabled = scriptContextSettings.get(scriptContext);
|
|
|
- }
|
|
|
- if (scriptEnabled == null) {
|
|
|
- scriptEnabled = scriptSourceSettings.get(scriptType);
|
|
|
- }
|
|
|
- if (scriptEnabled == null) {
|
|
|
- scriptEnabled = DEFAULT_SCRIPT_ENABLED.get(scriptType);
|
|
|
- }
|
|
|
-
|
|
|
- String lang = dangerousScriptEngine.getType();
|
|
|
- if (scriptEnabled) {
|
|
|
- assertCompileAccepted(lang, script, scriptType, scriptContext);
|
|
|
- } else {
|
|
|
- assertCompileRejected(lang, script, scriptType, scriptContext);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- assertSettingDeprecationsAndWarnings(
|
|
|
- ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, deprecated.toArray(new String[] {})));
|
|
|
- }
|
|
|
-
|
|
|
public void testCompileNonRegisteredContext() throws IOException {
|
|
|
buildScriptService(Settings.EMPTY);
|
|
|
String pluginName;
|
|
@@ -378,14 +262,11 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
public void testCompilationStatsOnCacheHit() throws IOException {
|
|
|
Settings.Builder builder = Settings.builder();
|
|
|
builder.put(ScriptService.SCRIPT_CACHE_SIZE_SETTING.getKey(), 1);
|
|
|
- builder.put("script.inline", "true");
|
|
|
buildScriptService(builder.build());
|
|
|
Script script = new Script(ScriptType.INLINE, "test", "1+1", Collections.emptyMap());
|
|
|
scriptService.compile(script, randomFrom(scriptContexts));
|
|
|
scriptService.compile(script, randomFrom(scriptContexts));
|
|
|
assertEquals(1L, scriptService.stats().getCompilations());
|
|
|
- assertSettingDeprecationsAndWarnings(
|
|
|
- ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, "script.inline"));
|
|
|
}
|
|
|
|
|
|
public void testIndexedScriptCountedInCompilationStats() throws IOException {
|
|
@@ -397,25 +278,19 @@ public class ScriptServiceTests extends ESTestCase {
|
|
|
public void testCacheEvictionCountedInCacheEvictionsStats() throws IOException {
|
|
|
Settings.Builder builder = Settings.builder();
|
|
|
builder.put(ScriptService.SCRIPT_CACHE_SIZE_SETTING.getKey(), 1);
|
|
|
- builder.put("script.inline", "true");
|
|
|
buildScriptService(builder.build());
|
|
|
scriptService.compile(new Script(ScriptType.INLINE, "test", "1+1", Collections.emptyMap()), randomFrom(scriptContexts));
|
|
|
scriptService.compile(new Script(ScriptType.INLINE, "test", "2+2", Collections.emptyMap()), randomFrom(scriptContexts));
|
|
|
assertEquals(2L, scriptService.stats().getCompilations());
|
|
|
assertEquals(1L, scriptService.stats().getCacheEvictions());
|
|
|
- assertSettingDeprecationsAndWarnings(
|
|
|
- ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, "script.inline"));
|
|
|
}
|
|
|
|
|
|
public void testDefaultLanguage() throws IOException {
|
|
|
Settings.Builder builder = Settings.builder();
|
|
|
- builder.put("script.inline", "true");
|
|
|
buildScriptService(builder.build());
|
|
|
CompiledScript script = scriptService.compile(
|
|
|
new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, "1 + 1", Collections.emptyMap()), randomFrom(scriptContexts));
|
|
|
assertEquals(script.lang(), Script.DEFAULT_SCRIPT_LANG);
|
|
|
- assertSettingDeprecationsAndWarnings(
|
|
|
- ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, "script.inline"));
|
|
|
}
|
|
|
|
|
|
public void testStoreScript() throws Exception {
|