|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.script; |
| 20 | + |
| 21 | +import com.carrotsearch.ant.tasks.junit4.dependencies.com.google.common.collect.ImmutableSet; |
| 22 | +import org.elasticsearch.ElasticsearchIllegalArgumentException; |
| 23 | +import org.elasticsearch.common.Nullable; |
| 24 | +import org.elasticsearch.common.io.Streams; |
| 25 | +import org.elasticsearch.common.settings.Settings; |
| 26 | +import org.elasticsearch.env.Environment; |
| 27 | +import org.elasticsearch.search.lookup.SearchLookup; |
| 28 | +import org.elasticsearch.test.ElasticsearchTestCase; |
| 29 | +import org.elasticsearch.watcher.ResourceWatcherService; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; |
| 37 | +import static org.hamcrest.Matchers.containsString; |
| 38 | +import static org.hamcrest.Matchers.equalTo; |
| 39 | + |
| 40 | +/** |
| 41 | + * |
| 42 | + */ |
| 43 | +public class ScriptServiceTests extends ElasticsearchTestCase { |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testScriptsWithoutExtensions() throws IOException { |
| 47 | + File homeFolder = newTempDir(); |
| 48 | + File genericConfigFolder = newTempDir(); |
| 49 | + |
| 50 | + Settings settings = settingsBuilder() |
| 51 | + .put("path.conf", genericConfigFolder) |
| 52 | + .put("path.home", homeFolder) |
| 53 | + .build(); |
| 54 | + Environment environment = new Environment(settings); |
| 55 | + |
| 56 | + ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, null); |
| 57 | + |
| 58 | + logger.info("--> setup script service"); |
| 59 | + ScriptService scriptService = new ScriptService(settings, environment, ImmutableSet.of(new TestEngineService()), resourceWatcherService); |
| 60 | + File scriptsFile = new File(genericConfigFolder, "scripts"); |
| 61 | + assertThat(scriptsFile.mkdir(), equalTo(true)); |
| 62 | + resourceWatcherService.notifyNow(); |
| 63 | + |
| 64 | + logger.info("--> setup two test files one with extension and another without"); |
| 65 | + File testFileNoExt = new File(scriptsFile, "test_no_ext"); |
| 66 | + File testFileWithExt = new File(scriptsFile, "test_script.tst"); |
| 67 | + Streams.copy("test_file_no_ext".getBytes("UTF-8"), testFileNoExt); |
| 68 | + Streams.copy("test_file".getBytes("UTF-8"), testFileWithExt); |
| 69 | + resourceWatcherService.notifyNow(); |
| 70 | + |
| 71 | + logger.info("--> verify that file with extension was correctly processed"); |
| 72 | + CompiledScript compiledScript = scriptService.compile("test", "test_script", ScriptService.ScriptType.FILE); |
| 73 | + assertThat(compiledScript.compiled(), equalTo((Object) "compiled_test_file")); |
| 74 | + |
| 75 | + logger.info("--> delete both files"); |
| 76 | + assertThat(testFileNoExt.delete(), equalTo(true)); |
| 77 | + assertThat(testFileWithExt.delete(), equalTo(true)); |
| 78 | + resourceWatcherService.notifyNow(); |
| 79 | + |
| 80 | + logger.info("--> verify that file with extension was correctly removed"); |
| 81 | + try { |
| 82 | + scriptService.compile("test", "test_script", ScriptService.ScriptType.FILE); |
| 83 | + fail("the script test_script should no longe exist"); |
| 84 | + } catch (ElasticsearchIllegalArgumentException ex) { |
| 85 | + assertThat(ex.getMessage(), containsString("Unable to find on disk script test_script")); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static class TestEngineService implements ScriptEngineService { |
| 90 | + |
| 91 | + @Override |
| 92 | + public String[] types() { |
| 93 | + return new String[] {"test"}; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String[] extensions() { |
| 98 | + return new String[] {"test", "tst"}; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean sandboxed() { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Object compile(String script) { |
| 108 | + return "compiled_" + script; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public ExecutableScript executable(final Object compiledScript, @Nullable Map<String, Object> vars) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public SearchScript search(Object compiledScript, SearchLookup lookup, @Nullable Map<String, Object> vars) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Object execute(Object compiledScript, Map<String, Object> vars) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Object unwrap(Object value) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void close() { |
| 133 | + |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments