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.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
/** /**
* Utilities for creating a Path from names, * Utilities for creating a Path from names,
* or accessing the default FileSystem. * or accessing the default FileSystem.
* <p> * <p>
@ -39,14 +39,14 @@ import java.nio.file.Paths;
public final class PathUtils { public final class PathUtils {
/** no instantiation */ /** no instantiation */
private PathUtils() {} private PathUtils() {}
/** the actual JDK default */ /** the actual JDK default */
static final FileSystem ACTUAL_DEFAULT = FileSystems.getDefault(); static final FileSystem ACTUAL_DEFAULT = FileSystems.getDefault();
/** can be changed by tests */ /** can be changed by tests */
static volatile FileSystem DEFAULT = ACTUAL_DEFAULT; static volatile FileSystem DEFAULT = ACTUAL_DEFAULT;
/** /**
* Returns a {@code Path} from name components. * Returns a {@code Path} from name components.
* <p> * <p>
* This works just like {@code Paths.get()}. * This works just like {@code Paths.get()}.
@ -57,30 +57,10 @@ public final class PathUtils {
* a path against an existing one! * a path against an existing one!
*/ */
public static Path get(String first, String... more) { 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 * Returns a {@code Path} from a URI
* <p> * <p>
* This works just like {@code Paths.get()}. * 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 org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;

View File

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

View File

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