public virtual void TestOverwriteEmptyDirectory() { Describe("verify trying to create a file over an empty dir fails"); Path path = Path("testOverwriteEmptyDirectory"); Mkdirs(path); AssertIsDirectory(path); byte[] data = ContractTestUtils.Dataset(256, 'a', 'z'); try { ContractTestUtils.WriteDataset(GetFileSystem(), path, data, data.Length, 1024, true ); AssertIsDirectory(path); NUnit.Framework.Assert.Fail("write of file over empty dir succeeded"); } catch (FileAlreadyExistsException expected) { //expected HandleExpectedException(expected); } catch (FileNotFoundException e) { HandleRelaxedException("overwriting a dir with a file ", "FileAlreadyExistsException" , e); } catch (IOException e) { HandleRelaxedException("overwriting a dir with a file ", "FileAlreadyExistsException" , e); } AssertIsDirectory(path); }
public virtual void TestOpenFileTwice() { Describe("verify that two opened file streams are independent"); Path path = Path("testopenfiletwice.txt"); byte[] block = ContractTestUtils.Dataset(TestFileLen, 0, 255); //this file now has a simple rule: offset => value ContractTestUtils.CreateFile(GetFileSystem(), path, false, block); //open first FSDataInputStream instream1 = GetFileSystem().Open(path); int c = instream1.Read(); Assert.Equal(0, c); FSDataInputStream instream2 = null; try { instream2 = GetFileSystem().Open(path); Assert.Equal("first read of instream 2", 0, instream2.Read()); Assert.Equal("second read of instream 1", 1, instream1.Read()); instream1.Close(); Assert.Equal("second read of instream 2", 1, instream2.Read()); //close instream1 again instream1.Close(); } finally { IOUtils.CloseStream(instream1); IOUtils.CloseStream(instream2); } }
public virtual void TestRenameFileNonexistentDir() { Describe("rename a file into a new file in the same directory"); Path renameSrc = Path("testRenameSrc"); Path renameTarget = Path("subdir/testRenameTarget"); byte[] data = ContractTestUtils.Dataset(256, 'a', 'z'); ContractTestUtils.WriteDataset(GetFileSystem(), renameSrc, data, data.Length, 1024 * 1024, false); bool renameCreatesDestDirs = IsSupported(RenameCreatesDestDirs); try { bool rename = Rename(renameSrc, renameTarget); if (renameCreatesDestDirs) { Assert.True(rename); ContractTestUtils.VerifyFileContents(GetFileSystem(), renameTarget, data); } else { NUnit.Framework.Assert.IsFalse(rename); ContractTestUtils.VerifyFileContents(GetFileSystem(), renameSrc, data); } } catch (FileNotFoundException) { // allowed unless that rename flag is set NUnit.Framework.Assert.IsFalse(renameCreatesDestDirs); } }
public virtual void TestRenameDirIntoExistingDir() { Describe("Verify renaming a dir into an existing dir puts it underneath" + " and leaves existing files alone" ); FileSystem fs = GetFileSystem(); string sourceSubdir = "source"; Path srcDir = Path(sourceSubdir); Path srcFilePath = new Path(srcDir, "source-256.txt"); byte[] srcDataset = ContractTestUtils.Dataset(256, 'a', 'z'); ContractTestUtils.WriteDataset(fs, srcFilePath, srcDataset, srcDataset.Length, 1024 , false); Path destDir = Path("dest"); Path destFilePath = new Path(destDir, "dest-512.txt"); byte[] destDateset = ContractTestUtils.Dataset(512, 'A', 'Z'); ContractTestUtils.WriteDataset(fs, destFilePath, destDateset, destDateset.Length, 1024, false); AssertIsFile(destFilePath); bool rename = Rename(srcDir, destDir); Path renamedSrc = new Path(destDir, sourceSubdir); AssertIsFile(destFilePath); AssertIsDirectory(renamedSrc); ContractTestUtils.VerifyFileContents(fs, destFilePath, destDateset); Assert.True("rename returned false though the contents were copied" , rename); }
public virtual void TestCreateFileOverExistingFileNoOverwrite() { Describe("Verify overwriting an existing file fails"); Path path = Path("testCreateFileOverExistingFileNoOverwrite"); byte[] data = ContractTestUtils.Dataset(256, 'a', 'z'); ContractTestUtils.WriteDataset(GetFileSystem(), path, data, data.Length, 1024, false ); byte[] data2 = ContractTestUtils.Dataset(10 * 1024, 'A', 'Z'); try { ContractTestUtils.WriteDataset(GetFileSystem(), path, data2, data2.Length, 1024, false); NUnit.Framework.Assert.Fail("writing without overwrite unexpectedly succeeded"); } catch (FileAlreadyExistsException expected) { //expected HandleExpectedException(expected); } catch (IOException relaxed) { HandleRelaxedException("Creating a file over a file with overwrite==false", "FileAlreadyExistsException" , relaxed); } }
public virtual void TestSeekBigFile() { Describe("Seek round a large file and verify the bytes are what is expected"); Path testSeekFile = Path("bigseekfile.txt"); byte[] block = ContractTestUtils.Dataset(65536, 0, 255); ContractTestUtils.CreateFile(GetFileSystem(), testSeekFile, false, block); instream = GetFileSystem().Open(testSeekFile); Assert.Equal(0, instream.GetPos()); //expect that seek to 0 works instream.Seek(0); int result = instream.Read(); Assert.Equal(0, result); Assert.Equal(1, instream.Read()); Assert.Equal(2, instream.Read()); //do seek 32KB ahead instream.Seek(32768); Assert.Equal("@32768", block[32768], unchecked ((byte)instream. Read())); instream.Seek(40000); Assert.Equal("@40000", block[40000], unchecked ((byte)instream. Read())); instream.Seek(8191); Assert.Equal("@8191", block[8191], unchecked ((byte)instream.Read ())); instream.Seek(0); Assert.Equal("@0", 0, unchecked ((byte)instream.Read())); }
public virtual void TestOverwriteNonEmptyDirectory() { Describe("verify trying to create a file over a non-empty dir fails"); Path path = Path("testOverwriteNonEmptyDirectory"); Mkdirs(path); try { AssertIsDirectory(path); } catch (Exception failure) { if (IsSupported(IsBlobstore)) { // file/directory hack surfaces here throw Extensions.InitCause(new AssumptionViolatedException(failure.ToString ()), failure); } // else: rethrow throw; } Path child = new Path(path, "child"); ContractTestUtils.WriteTextFile(GetFileSystem(), child, "child file", true); byte[] data = ContractTestUtils.Dataset(256, 'a', 'z'); try { ContractTestUtils.WriteDataset(GetFileSystem(), path, data, data.Length, 1024, true ); FileStatus status = GetFileSystem().GetFileStatus(path); bool isDir = status.IsDirectory(); if (!isDir && IsSupported(IsBlobstore)) { // object store: downgrade to a skip so that the failure is visible // in test results ContractTestUtils.Skip("Object store allows a file to overwrite a directory"); } NUnit.Framework.Assert.Fail("write of file over dir succeeded"); } catch (FileAlreadyExistsException expected) { //expected HandleExpectedException(expected); } catch (FileNotFoundException e) { HandleRelaxedException("overwriting a dir with a file ", "FileAlreadyExistsException" , e); } catch (IOException e) { HandleRelaxedException("overwriting a dir with a file ", "FileAlreadyExistsException" , e); } AssertIsDirectory(path); AssertIsFile(child); }
public virtual void TestConcatFileOnFile() { byte[] block = ContractTestUtils.Dataset(TestFileLen, 0, 255); ContractTestUtils.CreateFile(GetFileSystem(), target, false, block); GetFileSystem().Concat(target, new Path[] { srcFile }); ContractTestUtils.AssertFileHasLength(GetFileSystem(), target, TestFileLen * 2); ContractTestUtils.ValidateFileContent(ContractTestUtils.ReadDataset(GetFileSystem (), target, TestFileLen * 2), new byte[][] { block, block }); }
public virtual void TestCreateNewFile() { Describe("Foundational 'create a file' test"); Path path = Path("testCreateNewFile"); byte[] data = ContractTestUtils.Dataset(256, 'a', 'z'); ContractTestUtils.WriteDataset(GetFileSystem(), path, data, data.Length, 1024 * 1024 , false); ContractTestUtils.VerifyFileContents(GetFileSystem(), path, data); }
/// <exception cref="System.Exception"/> public override void Setup() { base.Setup(); SkipIfUnsupported(SupportsConcat); //delete the test directory testPath = Path("test"); srcFile = new Path(testPath, "small.txt"); zeroByteFile = new Path(testPath, "zero.txt"); target = new Path(testPath, "target"); byte[] block = ContractTestUtils.Dataset(TestFileLen, 0, 255); ContractTestUtils.CreateFile(GetFileSystem(), srcFile, false, block); ContractTestUtils.Touch(GetFileSystem(), zeroByteFile); }
public virtual void TestAppendToExistingFile() { byte[] original = ContractTestUtils.Dataset(8192, 'A', 'Z'); byte[] appended = ContractTestUtils.Dataset(8192, '0', '9'); ContractTestUtils.CreateFile(GetFileSystem(), target, false, original); FSDataOutputStream outputStream = GetFileSystem().Append(target); outputStream.Write(appended); outputStream.Close(); byte[] bytes = ContractTestUtils.ReadDataset(GetFileSystem(), target, original.Length + appended.Length); ContractTestUtils.ValidateFileContent(bytes, new byte[][] { original, appended }); }
/// <exception cref="System.Exception"/> public override void Setup() { base.Setup(); SkipIfUnsupported(SupportsSeek); //delete the test directory testPath = GetContract().GetTestPath(); smallSeekFile = Path("seekfile.txt"); zeroByteFile = Path("zero.txt"); byte[] block = ContractTestUtils.Dataset(TestFileLen, 0, 255); //this file now has a simple rule: offset => value ContractTestUtils.CreateFile(GetFileSystem(), smallSeekFile, false, block); ContractTestUtils.Touch(GetFileSystem(), zeroByteFile); }
public virtual void TestConcatOnSelf() { byte[] block = ContractTestUtils.Dataset(TestFileLen, 0, 255); ContractTestUtils.CreateFile(GetFileSystem(), target, false, block); try { GetFileSystem().Concat(target, new Path[] { target }); } catch (Exception e) { //expected HandleExpectedException(e); } }
public virtual void TestRenameFileOverExistingFile() { Describe("Verify renaming a file onto an existing file matches expectations"); Path srcFile = Path("source-256.txt"); byte[] srcData = ContractTestUtils.Dataset(256, 'a', 'z'); ContractTestUtils.WriteDataset(GetFileSystem(), srcFile, srcData, srcData.Length, 1024, false); Path destFile = Path("dest-512.txt"); byte[] destData = ContractTestUtils.Dataset(512, 'A', 'Z'); ContractTestUtils.WriteDataset(GetFileSystem(), destFile, destData, destData.Length , 1024, false); AssertIsFile(destFile); bool renameOverwritesDest = IsSupported(RenameOverwritesDest); bool renameReturnsFalseOnRenameDestExists = !IsSupported(RenameReturnsFalseIfDestExists ); bool destUnchanged = true; try { bool renamed = Rename(srcFile, destFile); if (renameOverwritesDest) { // the filesystem supports rename(file, file2) by overwriting file2 Assert.True("Rename returned false", renamed); destUnchanged = false; } else { // rename is rejected by returning 'false' or throwing an exception if (renamed && !renameReturnsFalseOnRenameDestExists) { //expected an exception string destDirLS = GenerateAndLogErrorListing(srcFile, destFile); GetLog().Error("dest dir {}", destDirLS); NUnit.Framework.Assert.Fail("expected rename(" + srcFile + ", " + destFile + " ) to fail," + " but got success and destination of " + destDirLS); } } } catch (FileAlreadyExistsException e) { HandleExpectedException(e); } // verify that the destination file is as expected based on the expected // outcome ContractTestUtils.VerifyFileContents(GetFileSystem(), destFile, destUnchanged ? destData : srcData); }
public virtual void TestOverwriteExistingFile() { Describe("Overwrite an existing file and verify the new data is there"); Path path = Path("testOverwriteExistingFile"); byte[] data = ContractTestUtils.Dataset(256, 'a', 'z'); ContractTestUtils.WriteDataset(GetFileSystem(), path, data, data.Length, 1024, false ); ContractTestUtils.VerifyFileContents(GetFileSystem(), path, data); byte[] data2 = ContractTestUtils.Dataset(10 * 1024, 'A', 'Z'); ContractTestUtils.WriteDataset(GetFileSystem(), path, data2, data2.Length, 1024, true); ContractTestUtils.VerifyFileContents(GetFileSystem(), path, data2); }
public virtual void TestRenameNewFileSameDir() { Describe("rename a file into a new file in the same directory"); Path renameSrc = Path("rename_src"); Path renameTarget = Path("rename_dest"); byte[] data = ContractTestUtils.Dataset(256, 'a', 'z'); ContractTestUtils.WriteDataset(GetFileSystem(), renameSrc, data, data.Length, 1024 * 1024, false); bool rename = Rename(renameSrc, renameTarget); Assert.True("rename(" + renameSrc + ", " + renameTarget + ") returned false" , rename); ContractTestUtils.AssertListStatusFinds(GetFileSystem(), renameTarget.GetParent() , renameTarget); ContractTestUtils.VerifyFileContents(GetFileSystem(), renameTarget, data); }
public virtual void TestRandomSeeks() { int limit = GetContract().GetLimit(TestRandomSeekCount, DefaultRandomSeekCount); Describe("Testing " + limit + " random seeks"); int filesize = 10 * 1024; byte[] buf = ContractTestUtils.Dataset(filesize, 0, 255); Path randomSeekFile = Path("testrandomseeks.bin"); ContractTestUtils.CreateFile(GetFileSystem(), randomSeekFile, false, buf); Random r = new Random(); FSDataInputStream stm = GetFileSystem().Open(randomSeekFile); // Record the sequence of seeks and reads which trigger a failure. int[] seeks = new int[10]; int[] reads = new int[10]; try { for (int i = 0; i < limit; i++) { int seekOff = r.Next(buf.Length); int toRead = r.Next(Math.Min(buf.Length - seekOff, 32000)); seeks[i % seeks.Length] = seekOff; reads[i % reads.Length] = toRead; ContractTestUtils.VerifyRead(stm, buf, seekOff, toRead); } } catch (Exception afe) { StringBuilder sb = new StringBuilder(); sb.Append("Sequence of actions:\n"); for (int j = 0; j < seeks.Length; j++) { sb.Append("seek @ ").Append(seeks[j]).Append(" ").Append("read ").Append(reads[j ]).Append("\n"); } Log.Error(sb.ToString()); throw; } finally { stm.Close(); } }
public virtual void TestAppendToEmptyFile() { ContractTestUtils.Touch(GetFileSystem(), target); byte[] dataset = ContractTestUtils.Dataset(256, 'a', 'z'); FSDataOutputStream outputStream = GetFileSystem().Append(target); try { outputStream.Write(dataset); } finally { outputStream.Close(); } byte[] bytes = ContractTestUtils.ReadDataset(GetFileSystem(), target, dataset.Length ); ContractTestUtils.CompareByteArrays(dataset, bytes, dataset.Length); }
public virtual void TestCreateFileOverRoot() { Path root = new Path("/"); byte[] dataset = ContractTestUtils.Dataset(1024, ' ', 'z'); try { ContractTestUtils.CreateFile(GetFileSystem(), root, false, dataset); NUnit.Framework.Assert.Fail("expected an exception, got a file created over root: " + Ls(root)); } catch (IOException e) { //expected HandleExpectedException(e); } AssertIsDirectory(root); }
public virtual void TestSequentialRead() { Describe("verify that sequential read() operations return values"); Path path = Path("testsequentialread.txt"); int len = 4; int @base = unchecked ((int)(0x40)); // 64 byte[] block = ContractTestUtils.Dataset(len, @base, @base + len); //this file now has a simple rule: offset => (value | 0x40) ContractTestUtils.CreateFile(GetFileSystem(), path, false, block); //open first instream = GetFileSystem().Open(path); Assert.Equal(@base, instream.Read()); Assert.Equal(@base + 1, instream.Read()); Assert.Equal(@base + 2, instream.Read()); Assert.Equal(@base + 3, instream.Read()); // and now, failures Assert.Equal(-1, instream.Read()); Assert.Equal(-1, instream.Read()); instream.Close(); }
public virtual void TestRenameFileBeingAppended() { ContractTestUtils.Touch(GetFileSystem(), target); AssertPathExists("original file does not exist", target); byte[] dataset = ContractTestUtils.Dataset(256, 'a', 'z'); FSDataOutputStream outputStream = GetFileSystem().Append(target); outputStream.Write(dataset); Path renamed = new Path(testPath, "renamed"); outputStream.Close(); string listing = Ls(testPath); //expected: the stream goes to the file that was being renamed, not //the original path AssertPathExists("renamed destination file does not exist", renamed); AssertPathDoesNotExist("Source file found after rename during append:\n" + listing , target); byte[] bytes = ContractTestUtils.ReadDataset(GetFileSystem(), renamed, dataset.Length ); ContractTestUtils.CompareByteArrays(dataset, bytes, dataset.Length); }
public virtual void TestPositionedBulkReadDoesntChangePosition() { Describe("verify that a positioned read does not change the getPos() value"); Path testSeekFile = Path("bigseekfile.txt"); byte[] block = ContractTestUtils.Dataset(65536, 0, 255); ContractTestUtils.CreateFile(GetFileSystem(), testSeekFile, false, block); instream = GetFileSystem().Open(testSeekFile); instream.Seek(39999); Assert.True(-1 != instream.Read()); Assert.Equal(40000, instream.GetPos()); byte[] readBuffer = new byte[256]; instream.Read(128, readBuffer, 0, readBuffer.Length); //have gone back Assert.Equal(40000, instream.GetPos()); //content is the same too Assert.Equal("@40000", block[40000], unchecked ((byte)instream. Read())); //now verify the picked up data for (int i = 0; i < 256; i++) { Assert.Equal("@" + i, block[i + 128], readBuffer[i]); } }
public virtual void TestMkdirOverParentFile() { Describe("try to mkdir where a parent is a file"); FileSystem fs = GetFileSystem(); Path path = Path("testMkdirOverParentFile"); byte[] dataset = ContractTestUtils.Dataset(1024, ' ', 'z'); ContractTestUtils.CreateFile(GetFileSystem(), path, false, dataset); Path child = new Path(path, "child-to-mkdir"); try { bool made = fs.Mkdirs(child); NUnit.Framework.Assert.Fail("mkdirs did not fail over a file but returned " + made + "; " + Ls(path)); } catch (ParentNotDirectoryException e) { //parent is a directory HandleExpectedException(e); } catch (FileAlreadyExistsException e) { HandleExpectedException(e); } catch (IOException e) { HandleRelaxedException("mkdirs", "ParentNotDirectoryException", e); } AssertIsFile(path); byte[] bytes = ContractTestUtils.ReadDataset(GetFileSystem(), path, dataset.Length ); ContractTestUtils.CompareByteArrays(dataset, bytes, dataset.Length); AssertPathExists("mkdir failed", path); AssertDeleted(path, true); }