Browse Source

Removing plugin that isn't installed shouldn't trigger usage information

The usage information for `elasticsearch-plugin` is quiet verbose and makes the
actual error message that is shown when trying to remove an non-existing plugin
hard to spot. This changes the error code to not trigger printing the usage
information.

Closes #21250
Christoph Büscher 9 years ago
parent
commit
f4594d4302

+ 10 - 9
core/src/main/java/org/elasticsearch/plugins/RemovePluginCommand.java

@@ -19,25 +19,26 @@
 
 package org.elasticsearch.plugins;
 
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardCopyOption;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
 import joptsimple.OptionSet;
 import joptsimple.OptionSpec;
+
 import org.apache.lucene.util.IOUtils;
 import org.elasticsearch.cli.ExitCodes;
 import org.elasticsearch.cli.SettingCommand;
+import org.elasticsearch.cli.Terminal;
 import org.elasticsearch.cli.UserException;
 import org.elasticsearch.common.Strings;
-import org.elasticsearch.cli.Terminal;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.env.Environment;
 import org.elasticsearch.node.internal.InternalSettingsPreparer;
 
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
 import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE;
 
 /**
@@ -67,7 +68,7 @@ final class RemovePluginCommand extends SettingCommand {
         final Path pluginDir = env.pluginsFile().resolve(pluginName);
         if (Files.exists(pluginDir) == false) {
             throw new UserException(
-                    ExitCodes.USAGE,
+                    ExitCodes.CONFIG,
                     "plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
         }
 

+ 20 - 0
qa/evil-tests/src/test/java/org/elasticsearch/plugins/RemovePluginCommandTests.java

@@ -20,6 +20,7 @@
 package org.elasticsearch.plugins;
 
 import org.apache.lucene.util.LuceneTestCase;
+import org.elasticsearch.cli.ExitCodes;
 import org.elasticsearch.cli.MockTerminal;
 import org.elasticsearch.cli.UserException;
 import org.elasticsearch.common.settings.Settings;
@@ -27,7 +28,9 @@ import org.elasticsearch.env.Environment;
 import org.elasticsearch.test.ESTestCase;
 import org.junit.Before;
 
+import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.StringReader;
 import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -43,6 +46,7 @@ public class RemovePluginCommandTests extends ESTestCase {
     private Path home;
     private Environment env;
 
+    @Override
     @Before
     public void setUp() throws Exception {
         super.setUp();
@@ -130,8 +134,24 @@ public class RemovePluginCommandTests extends ESTestCase {
         assertThat(terminal.getOutput(), not(containsString(expectedConfigDirPreservedMessage(configDir))));
     }
 
+    public void testRemoveUninstalledPluginErrors() throws Exception {
+        UserException e = expectThrows(UserException.class, () -> removePlugin("fake", home));
+        assertEquals(ExitCodes.CONFIG, e.exitCode);
+        assertEquals("plugin fake not found; run 'elasticsearch-plugin list' to get list of installed plugins", e.getMessage());
+
+        MockTerminal terminal = new MockTerminal();
+        new RemovePluginCommand().main(new String[] { "-Epath.home=" + home, "fake" }, terminal);
+        try (BufferedReader reader = new BufferedReader(new StringReader(terminal.getOutput()))) {
+            assertEquals("-> Removing fake...", reader.readLine());
+            assertEquals("ERROR: plugin fake not found; run 'elasticsearch-plugin list' to get list of installed plugins",
+                    reader.readLine());
+            assertNull(reader.readLine());
+        }
+    }
+
     private String expectedConfigDirPreservedMessage(final Path configDir) {
         return "-> Preserving plugin config files [" + configDir + "] in case of upgrade, delete manually if not needed";
     }
 
 }
+