Use mock filesystem during install plugins tests

This commit sets up the default filesystem used during install plugins
tests. A hack is neeeded to handle the temporary directory because the
system property "java.io.tmpdir" will have been initialized to a value
that is sensible for the default filesystem, but not necessarily to a
value that makes sense for the mock filesystem in use during the
tests. This property is restored after each test.
This commit is contained in:
Jason Tedor 2016-03-22 09:48:06 -04:00
parent e6eefcb142
commit 5dc48e71d0
5 changed files with 38 additions and 49 deletions

View File

@ -27,7 +27,7 @@ import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
/**
* Utilities for creating a Path from names,
* or accessing the default FileSystem.
* <p>
@ -39,14 +39,14 @@ import java.nio.file.Paths;
public final class PathUtils {
/** no instantiation */
private PathUtils() {}
/** the actual JDK default */
static final FileSystem ACTUAL_DEFAULT = FileSystems.getDefault();
/** can be changed by tests */
static volatile FileSystem DEFAULT = ACTUAL_DEFAULT;
/**
/**
* Returns a {@code Path} from name components.
* <p>
* This works just like {@code Paths.get()}.
@ -57,30 +57,10 @@ public final class PathUtils {
* a path against an existing one!
*/
public static Path get(String first, String... more) {
return get(DEFAULT, first, more);
return DEFAULT.getPath(first, more);
}
/**
* Returns a {@code Path} from name components against the given
* {@code FileSystem}.
* <p>
* This works just like {@code Paths.get()}.
* Remember: just like {@code Paths.get()} this is NOT A STRING CONCATENATION
* UTILITY FUNCTION.
* <p>
* Remember: this should almost never be used. Usually resolve
* a path against an existing one!
*
* @param fs the given {@code FileSystem}
* @param first the first path component
* @param more the remaining path components
* @return a path
*/
public static Path get(FileSystem fs, String first, String... more) {
return fs.getPath(first, more);
}
/**
/**
* Returns a {@code Path} from a URI
* <p>
* This works just like {@code Paths.get()}.

View File

@ -38,7 +38,6 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

View File

@ -31,7 +31,6 @@ import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@ -109,32 +108,28 @@ public class Environment {
}
public Environment(Settings settings) {
this(PathUtils.getDefaultFileSystem(), settings);
}
public Environment(FileSystem fs, Settings settings) {
this.settings = settings;
final Path homeFile;
if (PATH_HOME_SETTING.exists(settings)) {
homeFile = PathUtils.get(fs, PATH_HOME_SETTING.get(settings));
homeFile = PathUtils.get(cleanPath(PATH_HOME_SETTING.get(settings)));
} else {
throw new IllegalStateException(PATH_HOME_SETTING.getKey() + " is not configured");
}
if (PATH_CONF_SETTING.exists(settings)) {
configFile = PathUtils.get(fs, cleanPath(PATH_CONF_SETTING.get(settings)));
configFile = PathUtils.get(cleanPath(PATH_CONF_SETTING.get(settings)));
} else {
configFile = homeFile.resolve("config");
}
if (PATH_SCRIPTS_SETTING.exists(settings)) {
scriptsFile = PathUtils.get(fs, cleanPath(PATH_SCRIPTS_SETTING.get(settings)));
scriptsFile = PathUtils.get(cleanPath(PATH_SCRIPTS_SETTING.get(settings)));
} else {
scriptsFile = configFile.resolve("scripts");
}
if (PATH_PLUGINS_SETTING.exists(settings)) {
pluginsFile = PathUtils.get(fs, cleanPath(PATH_PLUGINS_SETTING.get(settings)));
pluginsFile = PathUtils.get(cleanPath(PATH_PLUGINS_SETTING.get(settings)));
} else {
pluginsFile = homeFile.resolve("plugins");
}
@ -144,7 +139,7 @@ public class Environment {
dataFiles = new Path[dataPaths.size()];
dataWithClusterFiles = new Path[dataPaths.size()];
for (int i = 0; i < dataPaths.size(); i++) {
dataFiles[i] = PathUtils.get(fs, dataPaths.get(i));
dataFiles[i] = PathUtils.get(dataPaths.get(i));
dataWithClusterFiles[i] = dataFiles[i].resolve(ClusterName.clusterNameFromSettings(settings).value());
}
} else {

View File

@ -21,7 +21,6 @@ package org.elasticsearch.node.internal;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;

View File

@ -23,14 +23,17 @@ import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.SuppressForbidden;
import org.elasticsearch.Version;
import org.elasticsearch.cli.MockTerminal;
import org.elasticsearch.cli.UserError;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.PathUtilsForTesting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.PosixPermissionsResetter;
import org.junit.After;
import java.io.IOException;
import java.io.InputStream;
@ -71,12 +74,25 @@ public class InstallPluginCommandTests extends ESTestCase {
private final FileSystem fs;
private final boolean isPosix;
private final boolean isReal;
private final String javaIoTmpdir;
@SuppressForbidden(reason = "sets java.io.tmpdir")
public InstallPluginCommandTests(FileSystem fs, Function<String, Path> temp) {
this.fs = fs;
this.temp = temp;
this.isPosix = fs.supportedFileAttributeViews().contains("posix");
this.isReal = fs == PathUtils.getDefaultFileSystem();
PathUtilsForTesting.installMock(fs);
javaIoTmpdir = System.getProperty("java.io.tmpdir");
System.setProperty("java.io.tmpdir", temp.apply("tmpdir").toString());
}
@After
@SuppressForbidden(reason = "resets java.io.tmpdir")
public void tearDown() throws Exception {
System.setProperty("java.io.tmpdir", javaIoTmpdir);
PathUtilsForTesting.teardown();
super.tearDown();
}
@ParametersFactory
@ -85,26 +101,26 @@ public class InstallPluginCommandTests extends ESTestCase {
private final FileSystem fileSystem;
private final Function<String, Path> temp;
public Parameter(FileSystem fileSystem, Supplier<String> root) {
this(fileSystem, root, s -> {
public Parameter(FileSystem fileSystem, String root) {
this(fileSystem, s -> {
try {
return Files.createTempDirectory(fileSystem.getPath(root.get()), s);
return Files.createTempDirectory(fileSystem.getPath(root), s);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
public Parameter(FileSystem fileSystem, Supplier<String> root, Function<String, Path> temp) {
public Parameter(FileSystem fileSystem, Function<String, Path> temp) {
this.fileSystem = fileSystem;
this.temp = temp;
}
}
List<Parameter> parameters = new ArrayList<>();
parameters.add(new Parameter(Jimfs.newFileSystem(Configuration.windows()), () -> "c:\\"));
parameters.add(new Parameter(Jimfs.newFileSystem(toPosix(Configuration.osX())), () -> "/"));
parameters.add(new Parameter(Jimfs.newFileSystem(toPosix(Configuration.unix())), () -> "/"));
parameters.add(new Parameter(PathUtils.getDefaultFileSystem(), () -> createTempDir().toString(), LuceneTestCase::createTempDir ));
parameters.add(new Parameter(Jimfs.newFileSystem(Configuration.windows()), "c:\\"));
parameters.add(new Parameter(Jimfs.newFileSystem(toPosix(Configuration.osX())), "/"));
parameters.add(new Parameter(Jimfs.newFileSystem(toPosix(Configuration.unix())), "/"));
parameters.add(new Parameter(PathUtils.getDefaultFileSystem(), LuceneTestCase::createTempDir ));
return parameters.stream().map(p -> new Object[] { p.fileSystem, p.temp }).collect(Collectors.toList());
}
@ -124,7 +140,7 @@ public class InstallPluginCommandTests extends ESTestCase {
Settings settings = Settings.builder()
.put("path.home", home)
.build();
return new Environment(fs, settings);
return new Environment(settings);
}
static Path createPluginDir(Function<String, Path> temp) throws IOException {