Fix list X-Pack extension command tests

This commit fixes the list X-Pack extension command tests which were
broken because of a guard added that es.path.conf is set. We do not want
to set this system property in tests so instead we override createEnv
where the problematic guard exists.

Original commit: elastic/x-pack-elasticsearch@b1bc4ddcb8
This commit is contained in:
Jason Tedor 2017-07-28 17:15:23 +09:00
parent aade36eff3
commit d8277942ac
1 changed files with 37 additions and 24 deletions

View File

@ -9,6 +9,8 @@ import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.MockTerminal; import org.elasticsearch.cli.MockTerminal;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
@ -19,6 +21,7 @@ import java.nio.file.Files;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@LuceneTestCase.SuppressFileSystems("*") @LuceneTestCase.SuppressFileSystems("*")
@ -38,6 +41,26 @@ public class ListXPackExtensionCommandTests extends ESTestCase {
Files.createDirectories(extensionsFile(env)); Files.createDirectories(extensionsFile(env));
} }
private static class MockListXPackExtensionCommand extends ListXPackExtensionCommand {
private final Environment env;
private MockListXPackExtensionCommand(final Environment env) {
this.env = env;
}
@Override
protected Environment createEnv(Terminal terminal, Map<String, String> settings) throws UserException {
return env;
}
@Override
protected boolean addShutdownHook() {
return false;
}
}
static String buildMultiline(String... args){ static String buildMultiline(String... args){
return Arrays.asList(args).stream().collect(Collectors.joining("\n", "", "\n")); return Arrays.asList(args).stream().collect(Collectors.joining("\n", "", "\n"));
} }
@ -56,67 +79,57 @@ public class ListXPackExtensionCommandTests extends ESTestCase {
return env.pluginsFile().resolve("x-pack").resolve("extensions"); return env.pluginsFile().resolve("x-pack").resolve("extensions");
} }
static MockTerminal listExtensions(Path home) throws Exception { static MockTerminal listExtensions(Path home, Environment env) throws Exception {
MockTerminal terminal = new MockTerminal(); MockTerminal terminal = new MockTerminal();
int status = new ListXPackExtensionCommand() { int status = new MockListXPackExtensionCommand(env).main(new String[] { "-Epath.home=" + home }, terminal);
@Override
protected boolean addShutdownHook() {
return false;
}
}.main(new String[] { "-Epath.home=" + home }, terminal);
assertEquals(ExitCodes.OK, status); assertEquals(ExitCodes.OK, status);
return terminal; return terminal;
} }
static MockTerminal listExtensions(Path home, String[] args) throws Exception { static MockTerminal listExtensions(Path home, Environment env, String[] args) throws Exception {
String[] argsAndHome = new String[args.length + 1]; String[] argsAndHome = new String[args.length + 1];
System.arraycopy(args, 0, argsAndHome, 0, args.length); System.arraycopy(args, 0, argsAndHome, 0, args.length);
argsAndHome[args.length] = "-Epath.home=" + home; argsAndHome[args.length] = "-Epath.home=" + home;
MockTerminal terminal = new MockTerminal(); MockTerminal terminal = new MockTerminal();
int status = new ListXPackExtensionCommand() { int status = new MockListXPackExtensionCommand(env).main(argsAndHome, terminal);
@Override
protected boolean addShutdownHook() {
return false;
}
}.main(argsAndHome, terminal);
assertEquals(ExitCodes.OK, status); assertEquals(ExitCodes.OK, status);
return terminal; return terminal;
} }
public void testExtensionsDirMissing() throws Exception { public void testExtensionsDirMissing() throws Exception {
Files.delete(extensionsFile(env)); Files.delete(extensionsFile(env));
IOException e = expectThrows(IOException.class, () -> listExtensions(home)); IOException e = expectThrows(IOException.class, () -> listExtensions(home, env));
assertTrue(e.getMessage(), e.getMessage().contains("Extensions directory missing")); assertTrue(e.getMessage(), e.getMessage().contains("Extensions directory missing"));
} }
public void testNoExtensions() throws Exception { public void testNoExtensions() throws Exception {
MockTerminal terminal = listExtensions(home); MockTerminal terminal = listExtensions(home, env);
assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty()); assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty());
} }
public void testNoExtensionsVerbose() throws Exception { public void testNoExtensionsVerbose() throws Exception {
String[] params = { "-v" }; String[] params = { "-v" };
MockTerminal terminal = listExtensions(home, params); MockTerminal terminal = listExtensions(home, env, params);
assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env))); assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env)));
} }
public void testOneExtension() throws Exception { public void testOneExtension() throws Exception {
buildFakeExtension(env, "", "fake", "org.fake"); buildFakeExtension(env, "", "fake", "org.fake");
MockTerminal terminal = listExtensions(home); MockTerminal terminal = listExtensions(home, env);
assertEquals(terminal.getOutput(), buildMultiline("fake")); assertEquals(terminal.getOutput(), buildMultiline("fake"));
} }
public void testTwoExtensions() throws Exception { public void testTwoExtensions() throws Exception {
buildFakeExtension(env, "", "fake1", "org.fake1"); buildFakeExtension(env, "", "fake1", "org.fake1");
buildFakeExtension(env, "", "fake2", "org.fake2"); buildFakeExtension(env, "", "fake2", "org.fake2");
MockTerminal terminal = listExtensions(home); MockTerminal terminal = listExtensions(home, env);
assertEquals(terminal.getOutput(), buildMultiline("fake1", "fake2")); assertEquals(terminal.getOutput(), buildMultiline("fake1", "fake2"));
} }
public void testExtensionWithVerbose() throws Exception { public void testExtensionWithVerbose() throws Exception {
buildFakeExtension(env, "fake desc", "fake_extension", "org.fake"); buildFakeExtension(env, "fake desc", "fake_extension", "org.fake");
String[] params = { "-v" }; String[] params = { "-v" };
MockTerminal terminal = listExtensions(home, params); MockTerminal terminal = listExtensions(home, env, params);
assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env), assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env),
"fake_extension", "- XPack Extension information:", "Name: fake_extension", "fake_extension", "- XPack Extension information:", "Name: fake_extension",
"Description: fake desc", "Version: 1.0", " * Classname: org.fake")); "Description: fake desc", "Version: 1.0", " * Classname: org.fake"));
@ -126,7 +139,7 @@ public class ListXPackExtensionCommandTests extends ESTestCase {
buildFakeExtension(env, "fake desc 1", "fake_extension1", "org.fake"); buildFakeExtension(env, "fake desc 1", "fake_extension1", "org.fake");
buildFakeExtension(env, "fake desc 2", "fake_extension2", "org.fake2"); buildFakeExtension(env, "fake desc 2", "fake_extension2", "org.fake2");
String[] params = { "-v" }; String[] params = { "-v" };
MockTerminal terminal = listExtensions(home, params); MockTerminal terminal = listExtensions(home, env, params);
assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env), assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env),
"fake_extension1", "- XPack Extension information:", "Name: fake_extension1", "fake_extension1", "- XPack Extension information:", "Name: fake_extension1",
"Description: fake desc 1", "Version: 1.0", " * Classname: org.fake", "Description: fake desc 1", "Version: 1.0", " * Classname: org.fake",
@ -137,14 +150,14 @@ public class ListXPackExtensionCommandTests extends ESTestCase {
public void testExtensionWithoutVerboseMultipleExtensions() throws Exception { public void testExtensionWithoutVerboseMultipleExtensions() throws Exception {
buildFakeExtension(env, "fake desc 1", "fake_extension1", "org.fake"); buildFakeExtension(env, "fake desc 1", "fake_extension1", "org.fake");
buildFakeExtension(env, "fake desc 2", "fake_extension2", "org.fake2"); buildFakeExtension(env, "fake desc 2", "fake_extension2", "org.fake2");
MockTerminal terminal = listExtensions(home, new String[0]); MockTerminal terminal = listExtensions(home, env, new String[0]);
String output = terminal.getOutput(); String output = terminal.getOutput();
assertEquals(output, buildMultiline("fake_extension1", "fake_extension2")); assertEquals(output, buildMultiline("fake_extension1", "fake_extension2"));
} }
public void testExtensionWithoutDescriptorFile() throws Exception{ public void testExtensionWithoutDescriptorFile() throws Exception{
Files.createDirectories(extensionsFile(env).resolve("fake1")); Files.createDirectories(extensionsFile(env).resolve("fake1"));
NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> listExtensions(home)); NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> listExtensions(home, env));
assertEquals(e.getFile(), assertEquals(e.getFile(),
extensionsFile(env).resolve("fake1").resolve(XPackExtensionInfo.XPACK_EXTENSION_PROPERTIES).toString()); extensionsFile(env).resolve("fake1").resolve(XPackExtensionInfo.XPACK_EXTENSION_PROPERTIES).toString());
} }
@ -152,7 +165,7 @@ public class ListXPackExtensionCommandTests extends ESTestCase {
public void testExtensionWithWrongDescriptorFile() throws Exception{ public void testExtensionWithWrongDescriptorFile() throws Exception{
XPackExtensionTestUtil.writeProperties(extensionsFile(env).resolve("fake1"), XPackExtensionTestUtil.writeProperties(extensionsFile(env).resolve("fake1"),
"description", "fake desc"); "description", "fake desc");
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> listExtensions(home)); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> listExtensions(home, env));
assertEquals(e.getMessage(), "Property [name] is missing in [" + assertEquals(e.getMessage(), "Property [name] is missing in [" +
extensionsFile(env).resolve("fake1") extensionsFile(env).resolve("fake1")
.resolve(XPackExtensionInfo.XPACK_EXTENSION_PROPERTIES).toString() + "]"); .resolve(XPackExtensionInfo.XPACK_EXTENSION_PROPERTIES).toString() + "]");