Moved MockTerminal and cleaned up some tests
Original commit: elastic/x-pack-elasticsearch@ffb873c826
This commit is contained in:
parent
b54e6a7ae6
commit
bafbcd9ed3
|
@ -11,7 +11,7 @@ import java.nio.file.Path;
|
|||
|
||||
import org.elasticsearch.cli.ExitCodes;
|
||||
import org.elasticsearch.cli.UserError;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.cli.MockTerminal;
|
||||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.license.core.License;
|
||||
import org.elasticsearch.license.licensor.TestUtils;
|
||||
|
|
|
@ -95,14 +95,9 @@ public class ESUsersTool extends MultiCommand {
|
|||
@Override
|
||||
protected int execute(Terminal terminal, OptionSet options) throws Exception {
|
||||
String username = arguments.value(options);
|
||||
String password = passwordOption.value(options);
|
||||
String roles = rolesOption.value(options);
|
||||
execute(terminal, username, password, roles);
|
||||
return ExitCodes.OK;
|
||||
}
|
||||
String passwordStr = passwordOption.value(options);
|
||||
String rolesCsv = rolesOption.value(options);
|
||||
|
||||
// pkg private for testing
|
||||
void execute(Terminal terminal, String username, String passwordStr, String rolesCsv) throws Exception {
|
||||
Validation.Error validationError = Validation.ESUsers.validateUsername(username);
|
||||
if (validationError != null) {
|
||||
throw new UserError(ExitCodes.DATA_ERROR, "Invalid username [" + username + "]... " + validationError);
|
||||
|
@ -156,6 +151,7 @@ public class ESUsersTool extends MultiCommand {
|
|||
}
|
||||
|
||||
attributesChecker.check(terminal);
|
||||
return ExitCodes.OK;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ public class ShieldFiles {
|
|||
tempFileView.setPermissions(attributes.permissions());
|
||||
|
||||
// Make an attempt to set the username and group to match. If it fails, silently ignore the failure as the user
|
||||
// will be notified by the CheckFileCommand that the ownership has changed and needs to be corrected
|
||||
// will be notified by the FileAttributeChecker that the ownership has changed and needs to be corrected
|
||||
try {
|
||||
tempFileView.setOwner(attributes.owner());
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -14,16 +14,20 @@ import java.util.Locale;
|
|||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.elasticsearch.cli.Command;
|
||||
import org.elasticsearch.cli.CommandTestCase;
|
||||
import org.elasticsearch.cli.ExitCodes;
|
||||
import org.elasticsearch.cli.MockTerminal;
|
||||
import org.elasticsearch.cli.UserError;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.cli.CliTool;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.shield.authc.esusers.FileUserRolesStore;
|
||||
import org.elasticsearch.shield.authc.support.Hasher;
|
||||
import org.elasticsearch.shield.authc.support.SecuredStringTests;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.junit.Before;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
|
@ -37,74 +41,60 @@ import static org.hamcrest.Matchers.hasSize;
|
|||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
public class ESUsersToolTests extends ESTestCase {
|
||||
public void testUseraddParseAllOptions() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("useradd", args("username -p changeme -r r1,r2,r3"));
|
||||
assertThat(command, instanceOf(ESUsersTool.Useradd.class));
|
||||
ESUsersTool.Useradd cmd = (ESUsersTool.Useradd) command;
|
||||
assertThat(cmd.username, equalTo("username"));
|
||||
assertThat(new String(cmd.passwd.internalChars()), equalTo("changeme"));
|
||||
assertThat(cmd.roles, notNullValue());
|
||||
assertThat(cmd.roles, arrayContaining("r1", "r2", "r3"));
|
||||
public class ESUsersToolTests extends CommandTestCase {
|
||||
|
||||
// settings used to create an Environment for tools
|
||||
Settings.Builder settingsBuilder;
|
||||
|
||||
@Before
|
||||
public void resetSettings() {
|
||||
// TODO: use jimfs so we can run without SM...
|
||||
settingsBuilder = Settings.builder().put("path.home", createTempDir());
|
||||
}
|
||||
|
||||
public void testUseraddExtraArgs() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("useradd", args("username -p changeme -r r1,r2,r3 r4 r6"));
|
||||
assertThat(command, instanceOf(CliTool.Command.Exit.class));
|
||||
CliTool.Command.Exit exit = (CliTool.Command.Exit) command;
|
||||
assertThat(exit.status(), is(CliTool.ExitStatus.USAGE));
|
||||
@Override
|
||||
protected Command newCommand() {
|
||||
return new ESUsersTool(new Environment(settingsBuilder.build()));
|
||||
}
|
||||
|
||||
public void testUseraddParseInvalidUsername() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("useradd", args("$34dkl -p changeme -r r1,r2,r3"));
|
||||
assertThat(command, instanceOf(CliTool.Command.Exit.class));
|
||||
CliTool.Command.Exit exit = (CliTool.Command.Exit) command;
|
||||
assertThat(exit.status(), is(CliTool.ExitStatus.DATA_ERROR));
|
||||
public void testUseraddInvalidUsername() throws Exception {
|
||||
UserError e = expectThrows(UserError.class, () -> {
|
||||
execute("useradd", "$34dkl", "-p", "changeme", "-r", "r1");
|
||||
});
|
||||
assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("Invalid username"));
|
||||
}
|
||||
|
||||
public void testUseradd_Parse_InvalidRoleName() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("useradd", args("username -p changeme -r $343,r2,r3"));
|
||||
assertThat(command, instanceOf(CliTool.Command.Exit.class));
|
||||
CliTool.Command.Exit exit = (CliTool.Command.Exit) command;
|
||||
assertThat(exit.status(), is(CliTool.ExitStatus.DATA_ERROR));
|
||||
public void testUseraddInvalidRoleName() throws Exception {
|
||||
UserError e = expectThrows(UserError.class, () -> {
|
||||
execute("useradd", "username", "-p", "changeme", "-r", "$343");
|
||||
});
|
||||
assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("Invalid role"));
|
||||
}
|
||||
|
||||
public void testUseraddParseInvalidPassword() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("useradd", args("username -p 123 -r r1,r2,r3"));
|
||||
assertThat(command, instanceOf(CliTool.Command.Exit.class));
|
||||
CliTool.Command.Exit exit = (CliTool.Command.Exit) command;
|
||||
assertThat(exit.status(), is(CliTool.ExitStatus.DATA_ERROR));
|
||||
public void testUseraddInvalidPassword() throws Exception {
|
||||
UserError e = expectThrows(UserError.class, () -> {
|
||||
execute("useradd", "username", "-p", "123", "-r", "r1");
|
||||
});
|
||||
assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("Invalid password"));
|
||||
}
|
||||
|
||||
public void testUseraddParseNoUsername() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("useradd", args("-p test123"));
|
||||
assertThat(command, instanceOf(CliTool.Command.Exit.class));
|
||||
assertThat(((CliTool.Command.Exit) command).status(), is(CliTool.ExitStatus.USAGE));
|
||||
public void testUseraddNoUsername() throws Exception {
|
||||
UserError e = expectThrows(UserError.class, () -> {
|
||||
execute("useradd");
|
||||
});
|
||||
assertEquals(ExitCodes.USAGE, e.exitCode);
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("TBD"));
|
||||
}
|
||||
|
||||
public void testUseraddParseNoPassword() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool(new MockTerminal() {
|
||||
@Override
|
||||
public char[] readSecret(String text) {
|
||||
return "changeme".toCharArray();
|
||||
}
|
||||
});
|
||||
CliTool.Command command = tool.parse("useradd", args("username"));
|
||||
assertThat(command, instanceOf(ESUsersTool.Useradd.class));
|
||||
ESUsersTool.Useradd cmd = (ESUsersTool.Useradd) command;
|
||||
assertThat(cmd.username, equalTo("username"));
|
||||
assertThat(new String(cmd.passwd.internalChars()), equalTo("changeme"));
|
||||
assertThat(cmd.roles, notNullValue());
|
||||
assertThat(cmd.roles.length, is(0));
|
||||
terminal.addSecretInput("changeme");
|
||||
execute("useradd", "username");
|
||||
assertUser("username", "changeme");
|
||||
}
|
||||
|
||||
public void testUseraddCmdCreate() throws Exception {
|
||||
|
@ -855,11 +845,6 @@ public class ESUsersToolTests extends ESTestCase {
|
|||
assertThat(lines, containsInAnyOrder("r1:john.doe", "r2:john.doe", "r3:john.doe"));
|
||||
}
|
||||
|
||||
private CliTool.ExitStatus execute(CliTool.Command cmd, Settings settings) throws Exception {
|
||||
Environment env = new Environment(settings);
|
||||
return cmd.execute(settings, env);
|
||||
}
|
||||
|
||||
private Path writeFile(String content) throws IOException {
|
||||
Path file = createTempFile();
|
||||
Files.write(file, content.getBytes(StandardCharsets.UTF_8));
|
||||
|
|
|
@ -11,7 +11,7 @@ import java.nio.file.attribute.PosixFileAttributeView;
|
|||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.util.Set;
|
||||
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.cli.MockTerminal;
|
||||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
|
@ -26,7 +26,7 @@ public class SystemKeyToolTests extends ESTestCase {
|
|||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
terminal = new MockTerminal();
|
||||
Settings settings = Settings.builder()
|
||||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
|
||||
env = new Environment(settings);
|
||||
|
|
|
@ -13,12 +13,11 @@ import java.nio.file.attribute.PosixFileAttributeView;
|
|||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.nio.file.attribute.UserPrincipal;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.jimfs.Configuration;
|
||||
import com.google.common.jimfs.Jimfs;
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.cli.MockTerminal;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
public class FileAttributesCheckerTests extends ESTestCase {
|
||||
|
@ -26,9 +25,9 @@ public class FileAttributesCheckerTests extends ESTestCase {
|
|||
public void testNonExistentFile() throws Exception {
|
||||
Path path = createTempDir().resolve("dne");
|
||||
FileAttributesChecker checker = new FileAttributesChecker(path);
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
checker.check(terminal);
|
||||
assertTrue(terminal.getTerminalOutput().isEmpty());
|
||||
assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty());
|
||||
}
|
||||
|
||||
public void testNoPosix() throws Exception {
|
||||
|
@ -36,9 +35,9 @@ public class FileAttributesCheckerTests extends ESTestCase {
|
|||
try (FileSystem fs = Jimfs.newFileSystem(conf)) {
|
||||
Path path = fs.getPath("temp");
|
||||
FileAttributesChecker checker = new FileAttributesChecker(path);
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
checker.check(terminal);
|
||||
assertTrue(terminal.getTerminalOutput().isEmpty());
|
||||
assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,9 +48,9 @@ public class FileAttributesCheckerTests extends ESTestCase {
|
|||
Files.createFile(path);
|
||||
FileAttributesChecker checker = new FileAttributesChecker(path);
|
||||
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
checker.check(terminal);
|
||||
assertTrue(terminal.getTerminalOutput().isEmpty());
|
||||
assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,11 +69,10 @@ public class FileAttributesCheckerTests extends ESTestCase {
|
|||
perms.add(PosixFilePermission.GROUP_READ);
|
||||
attrs.setPermissions(perms);
|
||||
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
checker.check(terminal);
|
||||
List<String> output = terminal.getTerminalOutput();
|
||||
assertEquals(output.toString(), 2, output.size());
|
||||
assertTrue(output.toString(), output.get(0).contains("permissions of [" + path + "] have changed"));
|
||||
String output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("permissions of [" + path + "] have changed"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,11 +87,10 @@ public class FileAttributesCheckerTests extends ESTestCase {
|
|||
PosixFileAttributeView attrs = Files.getFileAttributeView(path, PosixFileAttributeView.class);
|
||||
attrs.setOwner(newOwner);
|
||||
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
checker.check(terminal);
|
||||
List<String> output = terminal.getTerminalOutput();
|
||||
assertEquals(output.toString(), 1, output.size());
|
||||
assertTrue(output.toString(), output.get(0).contains("Owner of file [" + path + "] used to be"));
|
||||
String output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("Owner of file [" + path + "] used to be"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,11 +105,10 @@ public class FileAttributesCheckerTests extends ESTestCase {
|
|||
PosixFileAttributeView attrs = Files.getFileAttributeView(path, PosixFileAttributeView.class);
|
||||
attrs.setGroup(newGroup);
|
||||
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
checker.check(terminal);
|
||||
List<String> output = terminal.getTerminalOutput();
|
||||
assertEquals(output.toString(), 1, output.size());
|
||||
assertTrue(output.toString(), output.get(0).contains("Group of file [" + path + "] used to be"));
|
||||
String output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("Group of file [" + path + "] used to be"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue