Fix failing CLI tests

This commit fixes some failing CLI tests. The failure here is that a
guard against the system property es.path.conf was added yet these tests
were not adapted for this change. This commit implements this adapation
which overrides the createEnv method where the problematic guard is
invoked. We do this to avoid having to set es.path.conf in tests.

Original commit: elastic/x-pack-elasticsearch@20e1724823
This commit is contained in:
Jason Tedor 2017-07-28 17:00:25 +09:00
parent af7ec7a213
commit aade36eff3
5 changed files with 73 additions and 8 deletions

View File

@ -81,7 +81,11 @@ public class ESNativeRealmMigrateTool extends MultiCommand {
public ESNativeRealmMigrateTool() {
super("Imports file-based users and roles to the native security realm");
subcommands.put("native", new MigrateUserOrRoles());
subcommands.put("native", newMigrateUserOrRoles());
}
protected MigrateUserOrRoles newMigrateUserOrRoles() {
return new MigrateUserOrRoles();
}
/**

View File

@ -59,12 +59,20 @@ public class SetupPasswordTool extends MultiCommand {
SetupPasswordTool(Function<Environment, CommandLineHttpClient> clientFunction,
CheckedFunction<Environment, KeyStoreWrapper, Exception> keyStoreFunction) {
super("Sets the passwords for reserved users");
subcommands.put("auto", new AutoSetup());
subcommands.put("interactive", new InteractiveSetup());
subcommands.put("auto", newAutoSetup());
subcommands.put("interactive", newInteractiveSetup());
this.clientFunction = clientFunction;
this.keyStoreFunction = keyStoreFunction;
}
protected AutoSetup newAutoSetup() {
return new AutoSetup();
}
protected InteractiveSetup newInteractiveSetup() {
return new InteractiveSetup();
}
public static void main(String[] args) throws Exception {
exit(new SetupPasswordTool().main(args, Terminal.DEFAULT));
}
@ -73,7 +81,7 @@ public class SetupPasswordTool extends MultiCommand {
* This class sets the passwords using automatically generated random passwords. The passwords will be
* printed to the console.
*/
private class AutoSetup extends SetupCommand {
class AutoSetup extends SetupCommand {
AutoSetup() {
super("Uses randomly generated passwords");
@ -116,7 +124,7 @@ public class SetupPasswordTool extends MultiCommand {
/**
* This class sets the passwords using password entered manually by the user from the console.
*/
private class InteractiveSetup extends SetupCommand {
class InteractiveSetup extends SetupCommand {
InteractiveSetup() {
super("Uses passwords entered by a user");

View File

@ -11,7 +11,9 @@ import org.apache.logging.log4j.Logger;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.cli.MockTerminal;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.Terminal.Verbosity;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
@ -24,6 +26,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@ -36,7 +39,19 @@ public class ESNativeRealmMigrateToolTests extends CommandTestCase {
@Override
protected Command newCommand() {
return new ESNativeRealmMigrateTool();
return new ESNativeRealmMigrateTool() {
@Override
protected MigrateUserOrRoles newMigrateUserOrRoles() {
return new MigrateUserOrRoles() {
@Override
protected Environment createEnv(Terminal terminal, Map<String, String> settings) throws UserException {
return new Environment(Settings.builder().put(settings).build());
}
};
}
};
}
public void testUserJson() throws Exception {

View File

@ -8,12 +8,15 @@ package org.elasticsearch.xpack.security.authc.esnative.tool;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.env.Environment;
import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm;
import org.elasticsearch.xpack.security.user.ElasticUser;
import org.elasticsearch.xpack.security.user.KibanaUser;
@ -25,6 +28,7 @@ import org.mockito.Mockito;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Map;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.contains;
@ -60,7 +64,29 @@ public class SetupPasswordToolTests extends CommandTestCase {
@Override
protected Command newCommand() {
return new SetupPasswordTool((e) -> httpClient, (e) -> keyStore);
return new SetupPasswordTool((e) -> httpClient, (e) -> keyStore) {
@Override
protected AutoSetup newAutoSetup() {
return new AutoSetup() {
@Override
protected Environment createEnv(Terminal terminal, Map<String, String> settings) throws UserException {
return new Environment(Settings.builder().put(settings).build());
}
};
}
@Override
protected InteractiveSetup newInteractiveSetup() {
return new InteractiveSetup() {
@Override
protected Environment createEnv(Terminal terminal, Map<String, String> settings) throws UserException {
return new Environment(Settings.builder().put(settings).build());
}
};
}
};
}
public void testAutoSetup() throws Exception {

View File

@ -10,7 +10,11 @@ import com.google.common.jimfs.Jimfs;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.io.PathUtilsForTesting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.security.crypto.CryptoService;
import org.junit.After;
@ -19,6 +23,7 @@ import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Map;
import java.util.Set;
public class SystemKeyToolTests extends CommandTestCase {
@ -41,7 +46,14 @@ public class SystemKeyToolTests extends CommandTestCase {
@Override
protected Command newCommand() {
return new SystemKeyTool();
return new SystemKeyTool() {
@Override
protected Environment createEnv(Terminal terminal, Map<String, String> settings) throws UserException {
return new Environment(Settings.builder().put(settings).build());
}
};
}
public void testGenerate() throws Exception {