From ef26df4f42e639dab54081c29d532c1c93dd42bb Mon Sep 17 00:00:00 2001 From: David Pilato Date: Wed, 4 Sep 2013 19:00:43 +0200 Subject: [PATCH] Add integration test for PluginManager We want to make sure that Plugin Manager still downloading plugins from internet. New tests requires internet access (`@Network` annotation has been added). By default, tests annotated with `@Network` are not launched. If you need to run these tests, use `-Dtests.network=true` option. Closes #3894. --- .../junit/annotations/Network.java | 34 +++++++ .../plugin/PluginManagerTests.java | 86 +++++++++++++++--- .../plugin/plugin_with_classfile.zip | Bin 0 -> 191 bytes 3 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 src/test/java/org/elasticsearch/junit/annotations/Network.java create mode 100644 src/test/resources/org/elasticsearch/plugin/plugin_with_classfile.zip diff --git a/src/test/java/org/elasticsearch/junit/annotations/Network.java b/src/test/java/org/elasticsearch/junit/annotations/Network.java new file mode 100644 index 00000000000..85ebb823071 --- /dev/null +++ b/src/test/java/org/elasticsearch/junit/annotations/Network.java @@ -0,0 +1,34 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.junit.annotations; + +import com.carrotsearch.randomizedtesting.annotations.TestGroup; + +import java.lang.annotation.*; + +/** + * Annotation used to set if internet network connectivity is required to run the test. + * By default, tests annotated with @Network won't be executed. + * Set -Dtests.network=true when running test to launch network tests + */ +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@TestGroup(enabled = false, sysProperty = "tests.network") +public @interface Network { +} diff --git a/src/test/java/org/elasticsearch/plugin/PluginManagerTests.java b/src/test/java/org/elasticsearch/plugin/PluginManagerTests.java index b266603f09d..8a525918ba2 100644 --- a/src/test/java/org/elasticsearch/plugin/PluginManagerTests.java +++ b/src/test/java/org/elasticsearch/plugin/PluginManagerTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.http.HttpServerTransport; +import org.elasticsearch.junit.annotations.Network; import org.elasticsearch.node.internal.InternalSettingsPreparer; import org.elasticsearch.plugins.PluginManager; import org.elasticsearch.rest.RestStatus; @@ -47,18 +48,26 @@ import static org.hamcrest.CoreMatchers.*; @ClusterScope(scope=Scope.TEST, numNodes=0) public class PluginManagerTests extends AbstractIntegrationTest { private static final Settings SETTINGS = ImmutableSettings.settingsBuilder() - .put("discovery.zen.ping.multicast.enabled", false).build(); + .put("discovery.zen.ping.multicast.enabled", false).build(); private static final String PLUGIN_DIR = "plugins"; - - + @After + public void afterTest() { + deletePluginsFolder(); + } + + @Before + public void beforeTest() { + deletePluginsFolder(); + } + @Test public void testLocalPluginInstallSingleFolder() throws Exception { //When we have only a folder in top-level (no files either) we remove that folder while extracting String pluginName = "plugin-test"; URL url = PluginManagerTests.class.getResource("plugin_single_folder.zip"); downloadAndExtract(pluginName, "file://" + url.getFile()); - + String nodeName = cluster().startNode(SETTINGS); assertPluginLoaded(pluginName); @@ -146,34 +155,81 @@ public class PluginManagerTests extends AbstractIntegrationTest { assertThat(response.errorCode(), equalTo(RestStatus.OK.getStatus())); } - @Before - public void beforeTest() { - deletePluginsFolder(); + @Test + public void testListInstalledEmpty() throws IOException { + File[] plugins = pluginManager(null).getListInstalledPlugins(); + assertThat(plugins, notNullValue()); + assertThat(plugins.length, is(0)); } - @After - public void afterTest() { - deletePluginsFolder(); + @Test(expected = IOException.class) + public void testInstallPluginNull() throws IOException { + pluginManager(null).downloadAndExtract(""); } - private void deletePluginsFolder() { - FileSystemUtils.deleteRecursively(new File(PLUGIN_DIR)); + + @Test + public void testInstallPlugin() throws IOException { + PluginManager pluginManager = pluginManager("file://".concat(PluginManagerTests.class.getResource("plugin_with_classfile.zip").getFile())); + + pluginManager.downloadAndExtract("plugin"); + File[] plugins = pluginManager.getListInstalledPlugins(); + assertThat(plugins, notNullValue()); + assertThat(plugins.length, is(1)); } - private void singlePluginInstallAndRemove(String pluginName, String pluginCoordinates) throws IOException { + @Test + public void testInstallSitePlugin() throws IOException { + PluginManager pluginManager = pluginManager("file://".concat(PluginManagerTests.class.getResource("plugin_without_folders.zip").getFile())); + + pluginManager.downloadAndExtract("plugin-site"); + File[] plugins = pluginManager.getListInstalledPlugins(); + assertThat(plugins, notNullValue()); + assertThat(plugins.length, is(1)); + + // We want to check that Plugin Manager moves content to _site + String pluginDir = PLUGIN_DIR.concat("/plugin-site/_site"); + assertThat(FileSystemUtils.exists(new File(pluginDir)), is(true)); + } + + + private void singlePluginInstallAndRemove(String pluginShortName, String pluginCoordinates) throws IOException { PluginManager pluginManager = pluginManager(pluginCoordinates); - pluginManager.downloadAndExtract(pluginName); + pluginManager.downloadAndExtract(pluginShortName); File[] plugins = pluginManager.getListInstalledPlugins(); assertThat(plugins, notNullValue()); assertThat(plugins.length, is(1)); // We remove it - pluginManager.removePlugin(pluginName); + pluginManager.removePlugin(pluginShortName); plugins = pluginManager.getListInstalledPlugins(); assertThat(plugins, notNullValue()); assertThat(plugins.length, is(0)); } + /** + * We are ignoring by default these tests as they require to have an internet access + * To activate the test, use -Dtests.network=true + */ + @Test @Network + public void testInstallPluginWithInternet() throws IOException { + // We test regular form: username/reponame/version + // It should find it in download.elasticsearch.org service + singlePluginInstallAndRemove("elasticsearch/elasticsearch-transport-thrift/1.5.0", null); + + // We test regular form: groupId/artifactId/version + // It should find it in maven central service + singlePluginInstallAndRemove("org.elasticsearch/elasticsearch-transport-thrift/1.5.0", null); + + // We test site plugins from github: userName/repoName + // It should find it on github + singlePluginInstallAndRemove("elasticsearch/kibana", null); + } + + private void deletePluginsFolder() { + FileSystemUtils.deleteRecursively(new File(PLUGIN_DIR)); + } + @Test public void testRemovePlugin() throws Exception { // We want to remove plugin with plugin short name diff --git a/src/test/resources/org/elasticsearch/plugin/plugin_with_classfile.zip b/src/test/resources/org/elasticsearch/plugin/plugin_with_classfile.zip new file mode 100644 index 0000000000000000000000000000000000000000..29bedba1fbf09ee59beada23ae8ebef638eb8da2 GIT binary patch literal 191 zcmWIWW@Zs#-~hs-vl*QkkN^*Z07HSrUUE)iadBt_4})RK??A=bzXHE9iZF0{ z>gt_5;~lPXLeuyBncx$?XS{v