Merge pull request #13550 from rmuir/this_test_is_fucked_up

NewPathForShardTests is broken every which way from Sunday.
This commit is contained in:
Robert Muir 2015-09-14 04:13:55 -04:00
commit d0deb28336
2 changed files with 19 additions and 41 deletions

View File

@ -30,7 +30,11 @@ public class PathUtilsForTesting {
/** Sets a new default filesystem for testing */ /** Sets a new default filesystem for testing */
public static void setup() { public static void setup() {
FileSystem mock = LuceneTestCase.getBaseTempDirForTestClass().getFileSystem(); installMock(LuceneTestCase.getBaseTempDirForTestClass().getFileSystem());
}
/** Installs a mock filesystem for testing */
public static void installMock(FileSystem mock) {
PathUtils.DEFAULT = mock; PathUtils.DEFAULT = mock;
} }

View File

@ -18,15 +18,11 @@
*/ */
package org.elasticsearch.index.shard; package org.elasticsearch.index.shard;
import com.carrotsearch.randomizedtesting.annotations.Repeat;
import org.apache.lucene.mockfile.FilterFileSystem;
import org.apache.lucene.mockfile.FilterFileSystemProvider; import org.apache.lucene.mockfile.FilterFileSystemProvider;
import org.apache.lucene.mockfile.FilterPath;
import org.apache.lucene.util.Constants; import org.apache.lucene.util.Constants;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.SuppressForbidden;
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.env.NodeEnvironment.NodePath; import org.elasticsearch.env.NodeEnvironment.NodePath;
@ -34,76 +30,53 @@ import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.FileStore; import java.nio.file.FileStore;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.FileAttributeView; import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileStoreAttributeView; import java.nio.file.attribute.FileStoreAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
/** Separate test class from ShardPathTests because we need static (BeforeClass) setup to install mock filesystems... */ /** Separate test class from ShardPathTests because we need static (BeforeClass) setup to install mock filesystems... */
@SuppressForbidden(reason = "ProviderMismatchException if I try to use PathUtils.getDefault instead")
public class NewPathForShardTests extends ESTestCase { public class NewPathForShardTests extends ESTestCase {
// Sneakiness to install mock file stores so we can pretend how much free space we have on each path.data: // Sneakiness to install mock file stores so we can pretend how much free space we have on each path.data:
private static MockFileStore aFileStore = new MockFileStore("mocka"); private static MockFileStore aFileStore = new MockFileStore("mocka");
private static MockFileStore bFileStore = new MockFileStore("mockb"); private static MockFileStore bFileStore = new MockFileStore("mockb");
private static FileSystem origFileSystem; private static String aPathPart;
private static String aPathPart = File.separator + 'a' + File.separator; private static String bPathPart;
private static String bPathPart = File.separator + 'b' + File.separator;
@BeforeClass @BeforeClass
public static void installMockUsableSpaceFS() throws Exception { public static void installMockUsableSpaceFS() throws Exception {
// Necessary so when Environment.clinit runs, to gather all FileStores, it sees ours: FileSystem current = PathUtils.getDefaultFileSystem();
origFileSystem = FileSystems.getDefault(); aPathPart = current.getSeparator() + 'a' + current.getSeparator();
bPathPart = current.getSeparator() + 'b' + current.getSeparator();
Field field = PathUtils.class.getDeclaredField("DEFAULT"); FileSystemProvider mock = new MockUsableSpaceFileSystemProvider(current);
field.setAccessible(true); PathUtilsForTesting.installMock(mock.getFileSystem(null));
FileSystem mock = new MockUsableSpaceFileSystemProvider().getFileSystem(getBaseTempDirForTestClass().toUri());
field.set(null, mock);
assertEquals(mock, PathUtils.getDefaultFileSystem());
} }
@AfterClass @AfterClass
public static void removeMockUsableSpaceFS() throws Exception { public static void removeMockUsableSpaceFS() throws Exception {
Field field = PathUtils.class.getDeclaredField("DEFAULT"); PathUtilsForTesting.teardown();
field.setAccessible(true);
field.set(null, origFileSystem);
origFileSystem = null;
aFileStore = null; aFileStore = null;
bFileStore = null; bFileStore = null;
} }
/** Mock file system that fakes usable space for each FileStore */ /** Mock file system that fakes usable space for each FileStore */
@SuppressForbidden(reason = "ProviderMismatchException if I try to use PathUtils.getDefault instead")
static class MockUsableSpaceFileSystemProvider extends FilterFileSystemProvider { static class MockUsableSpaceFileSystemProvider extends FilterFileSystemProvider {
public MockUsableSpaceFileSystemProvider() { public MockUsableSpaceFileSystemProvider(FileSystem inner) {
super("mockusablespace://", FileSystems.getDefault()); super("mockusablespace://", inner);
final List<FileStore> fileStores = new ArrayList<>(); final List<FileStore> fileStores = new ArrayList<>();
fileStores.add(aFileStore); fileStores.add(aFileStore);
fileStores.add(bFileStore); fileStores.add(bFileStore);
fileSystem = new FilterFileSystem(this, origFileSystem) {
@Override
public Iterable<FileStore> getFileStores() {
return fileStores;
}
};
} }
@Override @Override
@ -183,7 +156,6 @@ public class NewPathForShardTests extends ESTestCase {
} }
public void testSelectNewPathForShard() throws Exception { public void testSelectNewPathForShard() throws Exception {
assumeFalse("Consistenty fails on windows ('could not remove the following files')", Constants.WINDOWS);
Path path = PathUtils.get(createTempDir().toString()); Path path = PathUtils.get(createTempDir().toString());
// Use 2 data paths: // Use 2 data paths:
@ -232,5 +204,7 @@ public class NewPathForShardTests extends ESTestCase {
// had the most free space, never using the other drive unless new shards arrive // had the most free space, never using the other drive unless new shards arrive
// after the first shards started using storage: // after the first shards started using storage:
assertNotEquals(result1.getDataPath(), result2.getDataPath()); assertNotEquals(result1.getDataPath(), result2.getDataPath());
nodeEnv.close();
} }
} }