From 59ec9302c899aa0ea1a0bbcf93559be83fe9212a Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Sun, 6 Mar 2016 13:18:40 -0800 Subject: [PATCH] Switch cli tests to unified MockTerminal This is the xplugins side of elastic/elasticsearchelastic/elasticsearch#16966 Original commit: elastic/x-pack-elasticsearch@a2e2faf20ad40b450a6234bf5faf5055ddba19ad --- .../tools/LicenseGenerationToolTests.java | 8 +- .../tools/LicenseVerificationToolTests.java | 9 ++- .../authc/esusers/tool/ESUsersToolTests.java | 78 +++++++++---------- 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java b/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java index a001849c54d..d4863ac1a93 100644 --- a/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java +++ b/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java @@ -12,6 +12,7 @@ import java.nio.file.Path; import org.elasticsearch.common.cli.CliTool.Command; import org.elasticsearch.common.cli.CliTool.ExitStatus; import org.elasticsearch.common.cli.CliToolTestCase; +import org.elasticsearch.common.cli.MockTerminal; import org.elasticsearch.common.cli.UserError; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; @@ -124,12 +125,13 @@ public class LicenseGenerationToolTests extends CliToolTestCase { private String runLicenseGenerationTool(Path pubKeyPath, Path priKeyPath, License licenseSpec, ExitStatus expectedExitStatus) throws Exception { - CaptureOutputTerminal outputTerminal = new CaptureOutputTerminal(); + MockTerminal outputTerminal = new MockTerminal(); Settings settings = Settings.builder().put("path.home", createTempDir("LicenseGenerationToolTests")).build(); LicenseGenerator licenseGenerator = new LicenseGenerator(outputTerminal, pubKeyPath, priKeyPath, licenseSpec); assertThat(execute(licenseGenerator, settings), equalTo(expectedExitStatus)); - assertThat(outputTerminal.getTerminalOutput().size(), equalTo(1)); - return outputTerminal.getTerminalOutput().get(0); + String output = outputTerminal.getOutput(); + assertFalse(output, output.isEmpty()); + return output; } private ExitStatus execute(Command cmd, Settings settings) throws Exception { diff --git a/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java b/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java index 5c2e519bdf1..2959e5c6ef2 100644 --- a/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java +++ b/elasticsearch/license/licensor/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java @@ -14,6 +14,7 @@ import java.util.List; import org.elasticsearch.common.cli.CliTool.Command; import org.elasticsearch.common.cli.CliTool.ExitStatus; import org.elasticsearch.common.cli.CliToolTestCase; +import org.elasticsearch.common.cli.MockTerminal; import org.elasticsearch.common.cli.UserError; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; @@ -133,14 +134,14 @@ public class LicenseVerificationToolTests extends CliToolTestCase { } private String runLicenseVerificationTool(License license, Path publicKeyPath, ExitStatus expectedExitStatus) throws Exception { - CaptureOutputTerminal outputTerminal = new CaptureOutputTerminal(); + MockTerminal outputTerminal = new MockTerminal(); Settings settings = Settings.builder().put("path.home", createTempDir("LicenseVerificationToolTests")).build(); LicenseVerifier licenseVerifier = new LicenseVerifier(outputTerminal, license, publicKeyPath); assertThat(execute(licenseVerifier, settings), equalTo(expectedExitStatus)); if (expectedExitStatus == ExitStatus.OK) { - assertThat(outputTerminal.getTerminalOutput().size(), equalTo(1)); - - return outputTerminal.getTerminalOutput().get(0); + String output = outputTerminal.getOutput(); + assertFalse(output, output.isEmpty()); + return output; } else { return null; } diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java index 6d36b38dcf9..f277f9828f5 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersToolTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.shield.authc.esusers.tool; import org.elasticsearch.common.Strings; import org.elasticsearch.common.cli.CliTool; import org.elasticsearch.common.cli.CliToolTestCase; +import org.elasticsearch.common.cli.MockTerminal; import org.elasticsearch.common.cli.Terminal; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; @@ -232,12 +233,12 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - final CaptureOutputTerminal terminal = new CaptureOutputTerminal(); + MockTerminal terminal = new MockTerminal(); ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(terminal, "user1", SecuredStringTests.build("changeme"), "plugin_admin"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(terminal.getTerminalOutput(), hasSize(0)); + assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty()); } public void testUseraddNonExistantRole() throws Exception { @@ -254,13 +255,12 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - final CaptureOutputTerminal terminal = new CaptureOutputTerminal(); + MockTerminal terminal = new MockTerminal(); ESUsersTool.Useradd cmd = new ESUsersTool.Useradd(terminal, "user1", SecuredStringTests.build("changeme"), "plugin_admin_2"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(terminal.getTerminalOutput(), hasSize(1)); - assertThat(terminal.getTerminalOutput().get(0), containsString("[plugin_admin_2]")); + assertThat(terminal.getOutput(), containsString("[plugin_admin_2]")); } public void testUserdelParse() throws Exception { @@ -322,16 +322,14 @@ public class ESUsersToolTests extends CliToolTestCase { .put("shield.authc.realms.esusers.files.users_roles", userRolesFile) .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal terminal = new CaptureOutputTerminal(); + MockTerminal terminal = new MockTerminal(); ESUsersTool.Userdel cmd = new ESUsersTool.Userdel(terminal, "user2"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.NO_USER)); - List output = terminal.getTerminalOutput(); - assertThat(output, hasSize(equalTo(1))); - assertThat(output, hasItem(startsWith("User [user2] doesn't exist"))); + assertThat(terminal.getOutput(), startsWith("User [user2] doesn't exist")); assertFileExists(userFile); List lines = Files.readAllLines(userFile, StandardCharsets.UTF_8); @@ -620,12 +618,12 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.Roles cmd = new ESUsersTool.Roles(catchTerminalOutput, "user", Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("user"), containsString("user,foo,bar")))); + assertThat(catchTerminalOutput.getOutput(), allOf(containsString("user"), containsString("user,foo,bar"))); } public void testRolesCmdRoleCanBeAddedWhenUserIsNotInRolesFile() throws Exception { @@ -641,7 +639,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.Roles cmd = new ESUsersTool.Roles(catchTerminalOutput, "user", new String[]{"myrole"}, Strings.EMPTY_ARRAY); CliTool.ExitStatus status = execute(cmd, settings); @@ -678,14 +676,14 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, null); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(catchTerminalOutput.getTerminalOutput(), hasSize(greaterThanOrEqualTo(2))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(containsString("admin"))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("user"), containsString("user,foo,bar")))); + String output = catchTerminalOutput.getOutput(); + assertThat(output, containsString("admin")); + assertThat(output, allOf(containsString("user"), containsString("user,foo,bar"))); } public void testListUsersAndRolesCmdListAllUsersWithUnknownRoles() throws Exception { @@ -698,14 +696,14 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, null); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(catchTerminalOutput.getTerminalOutput(), hasSize(greaterThanOrEqualTo(2))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(containsString("admin"))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("user"), containsString("user,foo*,bar*")))); + String output = catchTerminalOutput.getOutput(); + assertThat(output, containsString("admin")); + assertThat(output, allOf(containsString("user"), containsString("user,foo*,bar*"))); } public void testListUsersAndRolesCmdListSingleUser() throws Exception { @@ -720,14 +718,13 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, "admin"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(catchTerminalOutput.getTerminalOutput(), hasSize(greaterThanOrEqualTo(1))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(containsString("admin"))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(not(containsString("user")))); + assertThat(catchTerminalOutput.getOutput(), containsString("admin")); + assertThat(catchTerminalOutput.getOutput(), not(containsString("user"))); } public void testListUsersAndRolesCmdNoUsers() throws Exception { @@ -740,14 +737,12 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal terminal = new CaptureOutputTerminal(); + MockTerminal terminal = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(terminal, null); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - List output = terminal.getTerminalOutput(); - assertThat(output, hasSize(1)); - assertThat(output.get(0), equalTo("No users found" + System.lineSeparator())); + assertThat(terminal.getOutput(), equalTo("No users found\n")); } public void testListUsersAndRolesCmdListSingleUserNotFound() throws Exception { @@ -758,7 +753,7 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, "does-not-exist"); CliTool.ExitStatus status = execute(cmd, settings); @@ -777,15 +772,15 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, null); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(catchTerminalOutput.getTerminalOutput(), hasSize(greaterThanOrEqualTo(3))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(containsString("admin"))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("user"), containsString("user,foo,bar")))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("no-roles-user"), containsString("-")))); + String output = catchTerminalOutput.getOutput(); + assertThat(output, containsString("admin")); + assertThat(output, allOf(containsString("user"), containsString("user,foo,bar"))); + assertThat(output, allOf(containsString("no-roles-user"), containsString("-"))); } public void testListUsersAndRolesCmdUsersWithoutRolesAreListed() throws Exception { @@ -800,15 +795,15 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal catchTerminalOutput = new CaptureOutputTerminal(); + MockTerminal catchTerminalOutput = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(catchTerminalOutput, null); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(catchTerminalOutput.getTerminalOutput(), hasSize(greaterThanOrEqualTo(3))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("admin"), containsString("-")))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("user"), containsString("-")))); - assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(allOf(containsString("no-roles-user"), containsString("-")))); + String output = catchTerminalOutput.getOutput(); + assertThat(output, allOf(containsString("admin"), containsString("-"))); + assertThat(output, allOf(containsString("user"), containsString("-"))); + assertThat(output, allOf(containsString("no-roles-user"), containsString("-"))); } public void testListUsersAndRolesCmdUsersWithoutRolesAreListedForSingleUser() throws Exception { @@ -821,13 +816,12 @@ public class ESUsersToolTests extends CliToolTestCase { .put("path.home", createTempDir()) .build(); - CaptureOutputTerminal loggingTerminal = new CaptureOutputTerminal(); + MockTerminal loggingTerminal = new MockTerminal(); ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(loggingTerminal, "admin"); CliTool.ExitStatus status = execute(cmd, settings); assertThat(status, is(CliTool.ExitStatus.OK)); - assertThat(loggingTerminal.getTerminalOutput(), hasSize(greaterThanOrEqualTo(1))); - assertThat(loggingTerminal.getTerminalOutput(), hasItem(allOf(containsString("admin"), containsString("-")))); + assertThat(loggingTerminal.getOutput(), allOf(containsString("admin"), containsString("-"))); } public void testUseraddUsernameWithPeriod() throws Exception {