Replace assertFalse/assertTrue for file existence (#52092)

In the packaging tests, we added convenience methods for asserting file
existence and file non-existence. This commit replaces the remaining
uses of assertFalse/assertTrue in favor of these dedicated matchers.
This commit is contained in:
Jason Tedor 2020-02-08 12:07:38 -05:00
parent b77ef1f61b
commit 6d4cbe5ded
No known key found for this signature in database
GPG Key ID: FA89F05560F16BC5
8 changed files with 28 additions and 25 deletions

View File

@ -125,7 +125,7 @@ public class ArchiveTests extends PackagingTestCase {
? "gc.log.0.current" ? "gc.log.0.current"
: "gc.log"; : "gc.log";
assertTrue("gc logs exist", Files.exists(installation.logs.resolve(gcLogName))); assertThat(installation.logs.resolve(gcLogName), fileExists());
ServerUtils.runElasticsearchTests(); ServerUtils.runElasticsearchTests();
stopElasticsearch(); stopElasticsearch();

View File

@ -25,7 +25,7 @@ import org.junit.BeforeClass;
import java.nio.file.Paths; import java.nio.file.Paths;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists; import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist; import static org.elasticsearch.packaging.util.FileUtils.assertPathsDoNotExist;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist; import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist;
import static org.elasticsearch.packaging.util.Packages.SYSVINIT_SCRIPT; import static org.elasticsearch.packaging.util.Packages.SYSVINIT_SCRIPT;
import static org.elasticsearch.packaging.util.Packages.assertInstalled; import static org.elasticsearch.packaging.util.Packages.assertInstalled;
@ -76,14 +76,14 @@ public class DebPreservationTests extends PackagingTestCase {
// keystore was removed // keystore was removed
assertPathsDontExist( assertPathsDoNotExist(
installation.config("elasticsearch.keystore"), installation.config("elasticsearch.keystore"),
installation.config(".elasticsearch.keystore.initial_md5sum") installation.config(".elasticsearch.keystore.initial_md5sum")
); );
// doc files were removed // doc files were removed
assertPathsDontExist( assertPathsDoNotExist(
Paths.get("/usr/share/doc/" + distribution().flavor.name), Paths.get("/usr/share/doc/" + distribution().flavor.name),
Paths.get("/usr/share/doc/" + distribution().flavor.name + "/copyright") Paths.get("/usr/share/doc/" + distribution().flavor.name + "/copyright")
); );
@ -100,7 +100,7 @@ public class DebPreservationTests extends PackagingTestCase {
assertRemoved(distribution()); assertRemoved(distribution());
assertPathsDontExist( assertPathsDoNotExist(
installation.config, installation.config,
installation.envFile, installation.envFile,
SYSVINIT_SCRIPT SYSVINIT_SCRIPT

View File

@ -38,7 +38,7 @@ import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist; import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists; import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileUtils.append; import static org.elasticsearch.packaging.util.FileUtils.append;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist; import static org.elasticsearch.packaging.util.FileUtils.assertPathsDoNotExist;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist; import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist;
import static org.elasticsearch.packaging.util.FileUtils.cp; import static org.elasticsearch.packaging.util.FileUtils.cp;
import static org.elasticsearch.packaging.util.FileUtils.fileWithGlobExist; import static org.elasticsearch.packaging.util.FileUtils.fileWithGlobExist;
@ -206,7 +206,7 @@ public class PackageTests extends PackagingTestCase {
} }
assertPathsDontExist( assertPathsDoNotExist(
installation.bin, installation.bin,
installation.lib, installation.lib,
installation.modules, installation.modules,

View File

@ -52,6 +52,8 @@ import java.nio.file.Paths;
import static org.elasticsearch.packaging.util.Cleanup.cleanEverything; import static org.elasticsearch.packaging.util.Cleanup.cleanEverything;
import static org.elasticsearch.packaging.util.Docker.ensureImageIsLoaded; import static org.elasticsearch.packaging.util.Docker.ensureImageIsLoaded;
import static org.elasticsearch.packaging.util.Docker.removeContainer; import static org.elasticsearch.packaging.util.Docker.removeContainer;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeFalse;
@ -317,7 +319,7 @@ public abstract class PackagingTestCase extends Assert {
// If log file exists, then we have bootstrapped our logging and the // If log file exists, then we have bootstrapped our logging and the
// error should be in the logs // error should be in the logs
assertTrue("log file exists", Files.exists(installation.logs.resolve("elasticsearch.log"))); assertThat(installation.logs.resolve("elasticsearch.log"), fileExists());
String logfile = FileUtils.slurp(installation.logs.resolve("elasticsearch.log")); String logfile = FileUtils.slurp(installation.logs.resolve("elasticsearch.log"));
assertThat(logfile, containsString(expectedMessage)); assertThat(logfile, containsString(expectedMessage));

View File

@ -23,14 +23,13 @@ import org.elasticsearch.packaging.util.Distribution;
import org.elasticsearch.packaging.util.Shell; import org.elasticsearch.packaging.util.Shell;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist; import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists; import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileUtils.append; import static org.elasticsearch.packaging.util.FileUtils.append;
import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist; import static org.elasticsearch.packaging.util.FileUtils.assertPathsDoNotExist;
import static org.elasticsearch.packaging.util.Packages.SYSTEMD_SERVICE; import static org.elasticsearch.packaging.util.Packages.SYSTEMD_SERVICE;
import static org.elasticsearch.packaging.util.Packages.SYSVINIT_SCRIPT; import static org.elasticsearch.packaging.util.Packages.SYSVINIT_SCRIPT;
import static org.elasticsearch.packaging.util.Packages.assertInstalled; import static org.elasticsearch.packaging.util.Packages.assertInstalled;
@ -103,7 +102,7 @@ public class RpmPreservationTests extends PackagingTestCase {
assertThat(sh.runIgnoreExitCode("systemctl is-enabled elasticsearch.service").exitCode, is(1)); assertThat(sh.runIgnoreExitCode("systemctl is-enabled elasticsearch.service").exitCode, is(1));
} }
assertPathsDontExist( assertPathsDoNotExist(
installation.bin, installation.bin,
installation.lib, installation.lib,
installation.modules, installation.modules,
@ -137,7 +136,7 @@ public class RpmPreservationTests extends PackagingTestCase {
private void assertConfFilePreserved(String configFile) { private void assertConfFilePreserved(String configFile) {
final Path original = installation.config(configFile); final Path original = installation.config(configFile);
final Path saved = installation.config(configFile + ".rpmsave"); final Path saved = installation.config(configFile + ".rpmsave");
assertFalse(original + " should not exist", Files.exists(original)); assertThat(original, fileDoesNotExist());
assertTrue(saved + " should exist", Files.exists(saved)); assertThat(saved, fileExists());
} }
} }

View File

@ -29,6 +29,8 @@ import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.Directory; import static org.elasticsearch.packaging.util.FileMatcher.Fileness.Directory;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.File; import static org.elasticsearch.packaging.util.FileMatcher.Fileness.File;
import static org.elasticsearch.packaging.util.FileMatcher.file; import static org.elasticsearch.packaging.util.FileMatcher.file;
@ -48,8 +50,6 @@ import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/** /**
* Installation and verification logic for archive distributions * Installation and verification logic for archive distributions
@ -269,7 +269,7 @@ public class Archives {
public static Shell.Result runElasticsearchStartCommand(Installation installation, Shell sh, String keystorePassword) { public static Shell.Result runElasticsearchStartCommand(Installation installation, Shell sh, String keystorePassword) {
final Path pidFile = installation.home.resolve("elasticsearch.pid"); final Path pidFile = installation.home.resolve("elasticsearch.pid");
assertFalse("Pid file doesn't exist when starting Elasticsearch", Files.exists(pidFile)); assertThat(pidFile, fileDoesNotExist());
final Installation.Executables bin = installation.executables(); final Installation.Executables bin = installation.executables();
@ -339,14 +339,14 @@ public class Archives {
final Path pidFile = installation.home.resolve("elasticsearch.pid"); final Path pidFile = installation.home.resolve("elasticsearch.pid");
ServerUtils.waitForElasticsearch(installation); ServerUtils.waitForElasticsearch(installation);
assertTrue("Starting Elasticsearch produced a pid file at " + pidFile, Files.exists(pidFile)); assertThat("Starting Elasticsearch produced a pid file at " + pidFile, pidFile, fileExists());
String pid = slurp(pidFile).trim(); String pid = slurp(pidFile).trim();
assertThat(pid, is(not(emptyOrNullString()))); assertThat(pid, is(not(emptyOrNullString())));
} }
public static void stopElasticsearch(Installation installation) throws Exception { public static void stopElasticsearch(Installation installation) throws Exception {
Path pidFile = installation.home.resolve("elasticsearch.pid"); Path pidFile = installation.home.resolve("elasticsearch.pid");
assertTrue("pid file should exist", Files.exists(pidFile)); assertThat(pidFile, fileExists());
String pid = slurp(pidFile).trim(); String pid = slurp(pidFile).trim();
assertThat(pid, is(not(emptyOrNullString()))); assertThat(pid, is(not(emptyOrNullString())));

View File

@ -52,10 +52,11 @@ import java.util.stream.Stream;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException; import java.util.zip.ZipException;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileExists;
import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/** /**
* Wrappers and convenience methods for common filesystem operations * Wrappers and convenience methods for common filesystem operations
@ -308,8 +309,8 @@ public class FileUtils {
return distribution.path; return distribution.path;
} }
public static void assertPathsExist(Path... paths) { public static void assertPathsExist(final Path... paths) {
Arrays.stream(paths).forEach(path -> assertTrue(path + " should exist", Files.exists(path))); Arrays.stream(paths).forEach(path -> assertThat(path, fileExists()));
} }
public static Matcher<Path> fileWithGlobExist(String glob) throws IOException { public static Matcher<Path> fileWithGlobExist(String glob) throws IOException {
@ -326,8 +327,8 @@ public class FileUtils {
}; };
} }
public static void assertPathsDontExist(Path... paths) { public static void assertPathsDoNotExist(final Path... paths) {
Arrays.stream(paths).forEach(path -> assertFalse(path + " should not exist", Files.exists(path))); Arrays.stream(paths).forEach(path -> assertThat(path, fileDoesNotExist()));
} }
public static void deleteIfExists(Path path) { public static void deleteIfExists(Path path) {

View File

@ -32,6 +32,7 @@ import java.nio.file.StandardOpenOption;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.elasticsearch.packaging.util.FileExistenceMatchers.fileDoesNotExist;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.Directory; import static org.elasticsearch.packaging.util.FileMatcher.Fileness.Directory;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.File; import static org.elasticsearch.packaging.util.FileMatcher.Fileness.File;
import static org.elasticsearch.packaging.util.FileMatcher.file; import static org.elasticsearch.packaging.util.FileMatcher.file;
@ -164,7 +165,7 @@ public class Packages {
final Result passwdResult = sh.run("getent passwd elasticsearch"); final Result passwdResult = sh.run("getent passwd elasticsearch");
final Path homeDir = Paths.get(passwdResult.stdout.trim().split(":")[5]); final Path homeDir = Paths.get(passwdResult.stdout.trim().split(":")[5]);
assertFalse("elasticsearch user home directory must not exist", Files.exists(homeDir)); assertThat("elasticsearch user home directory must not exist", homeDir, fileDoesNotExist());
Stream.of( Stream.of(
es.home, es.home,