Skip to content

Commit

Permalink
Fix test for Windows line endings (#86312)
Browse files Browse the repository at this point in the history
* Fix test for Windows line endings
* Make more tests line-separator agnostic
* Skip some tests where we need to fix production code

Co-authored-by: Rory Hunter <roryhunter2@gmail.com>
  • Loading branch information
williamrandolph and pugnascotia committed May 3, 2022
1 parent 5f82235 commit 9b0b410
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import org.elasticsearch.cli.UserException;
import org.elasticsearch.env.Environment;

import java.util.List;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class ListKeyStoreCommandTests extends KeyStoreCommandTestCase {

Expand All @@ -42,23 +45,23 @@ public void testEmpty() throws Exception {
createKeystore(password);
terminal.addSecretInput(password);
execute();
assertEquals("keystore.seed\n", terminal.getOutput());
assertThat(terminal.getOutput(), containsString("keystore.seed"));
}

public void testOne() throws Exception {
String password = getPossibleKeystorePassword();
createKeystore(password, "foo", "bar");
terminal.addSecretInput(password);
execute();
assertEquals("foo\nkeystore.seed\n", terminal.getOutput());
assertThat(terminal.getOutput().lines().toList(), equalTo(List.of("foo", "keystore.seed")));
}

public void testMultiple() throws Exception {
String password = getPossibleKeystorePassword();
createKeystore(password, "foo", "1", "baz", "2", "bar", "3");
terminal.addSecretInput(password);
execute();
assertEquals("bar\nbaz\nfoo\nkeystore.seed\n", terminal.getOutput()); // sorted
assertThat(terminal.getOutput().lines().toList(), equalTo(List.of("bar", "baz", "foo", "keystore.seed"))); // sorted
}

public void testListWithIncorrectPassword() throws Exception {
Expand All @@ -85,6 +88,6 @@ public void testListWithUnprotectedKeystore() throws Exception {
createKeystore("", "foo", "bar");
execute();
// Not prompted for a password
assertEquals("foo\nkeystore.seed\n", terminal.getOutput());
assertThat(terminal.getOutput().lines().toList(), equalTo(List.of("foo", "keystore.seed")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.env.Environment;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

Expand Down Expand Up @@ -63,7 +64,7 @@ public void testPrintSingleValueToTerminal() throws Exception {
createKeystore(password, "reindex.ssl.keystore.password", value);
terminal.addSecretInput(password);
execute("reindex.ssl.keystore.password");
assertEquals(value + "\n", terminal.getOutput());
assertThat(terminal.getOutput().lines().toList(), contains(value));
}

public void testShowBinaryValue() throws Exception {
Expand Down Expand Up @@ -126,9 +127,9 @@ public void testRetrieveFromUnprotectedKeystore() throws Exception {
// Not prompted for a password

if (console) {
assertEquals(value + "\n", terminal.getOutput());
assertThat(terminal.getOutput().lines().toList(), contains(value));
} else {
assertEquals(value, terminal.getOutput());
assertThat(terminal.getOutput(), equalTo(value));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void initEnv() throws Exception {
}

private static String buildMultiline(String... args) {
return Arrays.stream(args).collect(Collectors.joining("\n", "", "\n"));
return Arrays.stream(args).collect(Collectors.joining(System.lineSeparator(), "", System.lineSeparator()));
}

private static void buildFakePlugin(final Environment env, final String description, final String name, final String classname)
Expand Down Expand Up @@ -102,6 +102,7 @@ public void testTwoPlugins() throws Exception {
assertEquals(buildMultiline("fake1", "fake2"), terminal.getOutput());
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86352")
public void testPluginWithVerbose() throws Exception {
buildFakePlugin(env, "fake desc", "fake_plugin", "org.fake");
execute("-v");
Expand All @@ -125,6 +126,7 @@ public void testPluginWithVerbose() throws Exception {
);
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86352")
public void testPluginWithNativeController() throws Exception {
buildFakePlugin(env, "fake desc 1", "fake_plugin1", "org.fake", true);
execute("-v");
Expand All @@ -148,6 +150,7 @@ public void testPluginWithNativeController() throws Exception {
);
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86352")
public void testPluginWithVerboseMultiplePlugins() throws Exception {
buildFakePlugin(env, "fake desc 1", "fake_plugin1", "org.fake");
buildFakePlugin(env, "fake desc 2", "fake_plugin2", "org.fake2");
Expand Down Expand Up @@ -206,6 +209,7 @@ public void testPluginWithWrongDescriptorFile() throws Exception {
assertEquals("property [name] is missing in [" + descriptorPath.toString() + "]", e.getMessage());
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86352")
public void testExistingIncompatiblePlugin() throws Exception {
PluginTestUtil.writePluginProperties(
env.pluginsFile().resolve("fake_plugin1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

public class MultiCommandTests extends CommandTestCase {
Expand Down Expand Up @@ -221,7 +221,7 @@ public void testErrorDisplayedWithDefault() throws Exception {
multiCommand.subcommands.put("throw", new ErrorThrowingSubCommand());
executeMain("throw", "--silent");
assertThat(terminal.getOutput(), is(emptyString()));
assertThat(terminal.getErrorOutput(), equalTo("ERROR: Dummy error\n"));
assertThat(terminal.getErrorOutput().lines().toList(), contains("ERROR: Dummy error"));
}

public void testNullErrorMessageSuppressesErrorOutput() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,7 @@ public void testResetNativeUser() throws Exception {
}

private String readPasswordFromOutput(String output) {
String[] lines = output.split("\\n");
for (String line : lines) {
if (line.startsWith("New value: ")) {
return line.substring(11);
}
}
return null;
return output.lines().filter(line -> line.startsWith("New value: ")).map(line -> line.substring(11)).findFirst().orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ public void testAutoInteractiveModesMutuallyExclusive() throws Exception {

public void testAutoBatchSilent() throws Exception {
execute(randomFrom("--silent", "-s"), randomFrom("--batch", "-b"), randomFrom("-u", "--username"), user);
String output = terminal.getOutput();
assertThat(output, hasLength(21)); // password + new line char
assertThat(terminal.getOutput(), hasLength(20 + System.lineSeparator().length())); // password + new line char
assertThat(terminal.getErrorOutput(), is(emptyString()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;

import static org.elasticsearch.test.SecurityIntegTestCase.getFastStoredHashAlgoForTests;
import static org.hamcrest.Matchers.containsInRelativeOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -262,20 +263,20 @@ public void testDeleteTokenIncorrect() throws IOException {

public void testListTokens() throws Exception {
execute("list", pathHomeParameter);
final String output = terminal.getOutput();
assertThat(output, containsString("""
elastic/fleet-server/server_1
elastic/fleet-server/server_2
elastic/fleet-server/server_3"""));
final List<String> output = terminal.getOutput().lines().toList();
assertThat(
output,
containsInRelativeOrder("elastic/fleet-server/server_1", "elastic/fleet-server/server_2", "elastic/fleet-server/server_3")
);
}

public void testListTokensByPrincipal() throws Exception {
execute("list", pathHomeParameter, "elastic/fleet-server");
final String output = terminal.getOutput();
assertThat(output, containsString("""
elastic/fleet-server/server_1
elastic/fleet-server/server_2
elastic/fleet-server/server_3"""));
final List<String> output = terminal.getOutput().lines().toList();
assertThat(
output,
containsInRelativeOrder("elastic/fleet-server/server_1", "elastic/fleet-server/server_2", "elastic/fleet-server/server_3")
);
}

public void testListTokensNonExist() throws Exception {
Expand Down

0 comments on commit 9b0b410

Please sign in to comment.