rewrite custom scriptcontext tests to be unit tests

This commit is contained in:
Ryan Ernst 2015-09-22 21:15:44 -07:00
parent b65a631d9b
commit 05957cc4f3
3 changed files with 163 additions and 135 deletions

View File

@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@ -36,7 +37,7 @@ public final class ScriptContextRegistry {
private final ImmutableMap<String, ScriptContext> scriptContexts;
public ScriptContextRegistry(Iterable<ScriptContext.Plugin> customScriptContexts) {
public ScriptContextRegistry(Collection<ScriptContext.Plugin> customScriptContexts) {
Map<String, ScriptContext> scriptContexts = new HashMap<>();
for (ScriptContext.Standard scriptContext : ScriptContext.Standard.values()) {
scriptContexts.put(scriptContext.getKey(), scriptContext);

View File

@ -1,134 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.script;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.ContextAndHeaderHolder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.groovy.GroovyScriptEngineService;
import org.elasticsearch.script.mustache.MustacheScriptEngineService;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Test;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.notNullValue;
// TODO: this needs to be done with mock engines.
public class CustomScriptContextIT extends ESIntegTestCase {
private static final ImmutableSet<String> LANG_SET = ImmutableSet.of(GroovyScriptEngineService.NAME, MustacheScriptEngineService.NAME);
private static final String PLUGIN_NAME = "testplugin";
@Override
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder().put(super.nodeSettings(nodeOrdinal))
.put("script." + PLUGIN_NAME + "_custom_globally_disabled_op", "off")
.put("script.engine.expression.inline." + PLUGIN_NAME + "_custom_exp_disabled_op", "off")
.build();
}
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return pluginList(CustomScriptContextPlugin.class);
}
@Test
public void testCustomScriptContextsSettings() {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
ScriptService scriptService = internalCluster().getInstance(ScriptService.class);
for (String lang : LANG_SET) {
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
try {
scriptService.compile(new Script("test", scriptType, lang, null), new ScriptContext.Plugin(PLUGIN_NAME,
"custom_globally_disabled_op"), contextAndHeaders);
fail("script compilation should have been rejected");
} catch(ScriptException e) {
assertThat(e.getMessage(), containsString("scripts of type [" + scriptType + "], operation [" + PLUGIN_NAME + "_custom_globally_disabled_op] and lang [" + lang + "] are disabled"));
}
}
}
CompiledScript compiledScript = scriptService.compile(new Script("1", ScriptService.ScriptType.INLINE, "mustache", null),
new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"), contextAndHeaders);
assertThat(compiledScript, notNullValue());
for (String lang : LANG_SET) {
compiledScript = scriptService.compile(new Script("1", ScriptService.ScriptType.INLINE, lang, null), new ScriptContext.Plugin(
PLUGIN_NAME, "custom_op"), contextAndHeaders);
assertThat(compiledScript, notNullValue());
}
}
@Test
public void testCompileNonRegisteredPluginContext() {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
ScriptService scriptService = internalCluster().getInstance(ScriptService.class);
try {
scriptService.compile(
new Script("test", randomFrom(ScriptService.ScriptType.values()), randomFrom(LANG_SET.toArray(new String[LANG_SET
.size()])), null), new ScriptContext.Plugin("test", "unknown"), contextAndHeaders);
fail("script compilation should have been rejected");
} catch(IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("script context [test_unknown] not supported"));
}
}
@Test
public void testCompileNonRegisteredScriptContext() {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
ScriptService scriptService = internalCluster().getInstance(ScriptService.class);
try {
scriptService.compile(
new Script("test", randomFrom(ScriptService.ScriptType.values()), randomFrom(LANG_SET.toArray(new String[LANG_SET
.size()])), null), new ScriptContext() {
@Override
public String getKey() {
return "test";
}
}, contextAndHeaders);
fail("script compilation should have been rejected");
} catch(IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("script context [test] not supported"));
}
}
public static class CustomScriptContextPlugin extends Plugin {
@Override
public String name() {
return "custom_script_context_plugin";
}
@Override
public String description() {
return "Custom script context plugin";
}
public void onModule(ScriptModule scriptModule) {
scriptModule.registerScriptContext(new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"));
scriptModule.registerScriptContext(new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"));
scriptModule.registerScriptContext(new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"));
}
}
}

View File

@ -0,0 +1,161 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.script;
import org.elasticsearch.common.ContextAndHeaderHolder;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CustomScriptContextTests extends ESTestCase {
private static final String PLUGIN_NAME = "testplugin";
private static final String SCRIPT_LANG = "customlang";
ScriptService makeScriptService() throws Exception {
Settings settings = Settings.builder()
.put("path.home", createTempDir())
// no file watching, so we don't need a ResourceWatcherService
.put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING, false)
.put("script." + PLUGIN_NAME + "_custom_globally_disabled_op", false)
.put("script.engine." + SCRIPT_LANG + ".inline." + PLUGIN_NAME + "_custom_exp_disabled_op", false)
.build();
Set<ScriptEngineService> engines = new HashSet<>(Collections.singletonList(new MockScriptEngine()));
List<ScriptContext.Plugin> customContexts = Arrays.asList(
new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"),
new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"),
new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"));
return new ScriptService(settings, new Environment(settings), engines, null, new ScriptContextRegistry(customContexts));
}
public void testCustomGlobalScriptContextSettings() throws Exception {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
ScriptService scriptService = makeScriptService();
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
try {
Script script = new Script("1", scriptType, SCRIPT_LANG, null);
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"), contextAndHeaders);
fail("script compilation should have been rejected");
} catch (ScriptException e) {
assertTrue(e.getMessage(), e.getMessage().contains("scripts of type [" + scriptType + "], operation [" + PLUGIN_NAME + "_custom_globally_disabled_op] and lang [" + SCRIPT_LANG + "] are disabled"));
}
}
}
public void testCustomScriptContextSettings() throws Exception {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
ScriptService scriptService = makeScriptService();
Script script = new Script("1", ScriptService.ScriptType.INLINE, SCRIPT_LANG, null);
try {
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"), contextAndHeaders);
fail("script compilation should have been rejected");
} catch (ScriptException e) {
assertTrue(e.getMessage(), e.getMessage().contains("scripts of type [inline], operation [" + PLUGIN_NAME + "_custom_exp_disabled_op] and lang [" + SCRIPT_LANG + "] are disabled"));
}
// still works for other script contexts
assertNotNull(scriptService.compile(script, ScriptContext.Standard.AGGS, contextAndHeaders));
assertNotNull(scriptService.compile(script, ScriptContext.Standard.SEARCH, contextAndHeaders));
assertNotNull(scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"), contextAndHeaders));
}
public void testUnknownPluginScriptContext() throws Exception {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
ScriptService scriptService = makeScriptService();
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
try {
Script script = new Script("1", scriptType, SCRIPT_LANG, null);
scriptService.compile(script, new ScriptContext.Plugin(PLUGIN_NAME, "unknown"), contextAndHeaders);
fail("script compilation should have been rejected");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("script context [" + PLUGIN_NAME + "_unknown] not supported"));
}
}
}
public void testUnknownCustomScriptContext() throws Exception {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
ScriptContext context = new ScriptContext() {
@Override
public String getKey() {
return "test";
}
};
ScriptService scriptService = makeScriptService();
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
try {
Script script = new Script("1", scriptType, SCRIPT_LANG, null);
scriptService.compile(script, context, contextAndHeaders);
fail("script compilation should have been rejected");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("script context [test] not supported"));
}
}
}
static class MockScriptEngine implements ScriptEngineService {
@Override
public String[] types() {
return new String[] {SCRIPT_LANG};
}
@Override
public String[] extensions() {
return types();
}
@Override
public boolean sandboxed() {
return true;
}
@Override
public Object compile(String script) {
return Integer.parseInt(script);
}
@Override
public ExecutableScript executable(CompiledScript compiledScript, @Nullable Map<String, Object> vars) {
return null;
}
@Override
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, @Nullable Map<String, Object> vars) {
return null;
}
@Override
public Object execute(CompiledScript compiledScript, Map<String, Object> vars) {
return null;
}
@Override
public Object unwrap(Object value) {
return null;
}
@Override
public void scriptRemoved(@Nullable CompiledScript script) {}
@Override
public void close() throws IOException {}
}
}