Search in sources :

Example 91 with FileStatus

use of org.apache.hadoop.fs.FileStatus in project hadoop by apache.

the class TestPermission method checkPermission.

static FsPermission checkPermission(FileSystem fs, String path, FsPermission expected) throws IOException {
    FileStatus s = fs.getFileStatus(new Path(path));
    LOG.info(s.getPath() + ": " + s.isDirectory() + " " + s.getPermission() + ":" + s.getOwner() + ":" + s.getGroup());
    if (expected != null) {
        assertEquals(expected, s.getPermission());
        assertEquals(expected.toShort(), s.getPermission().toShort());
    }
    return s.getPermission();
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus)

Example 92 with FileStatus

use of org.apache.hadoop.fs.FileStatus in project hadoop by apache.

the class TestRollingFileSystemSinkWithHdfs method testFlushThread.

/**
   * This test specifically checks whether the flusher thread is automatically
   * flushing the files.  It unfortunately can only test with the alternative
   * flushing schedule (because of test timing), but it's better than nothing.
   *
   * @throws Exception thrown if something breaks
   */
@Test
public void testFlushThread() throws Exception {
    // Cause the sink's flush thread to be run immediately after the second
    // metrics log is written
    RollingFileSystemSink.forceFlush = true;
    String path = "hdfs://" + cluster.getNameNode().getHostAndPort() + "/tmp";
    MetricsSystem ms = initMetricsSystem(path, true, false, false);
    new MyMetrics1().registerWith(ms);
    // Publish the metrics
    ms.publishMetricsNow();
    // Pubish again because the first write seems to get properly flushed
    // regardless.
    ms.publishMetricsNow();
    int count = 0;
    try {
        // sleep, but the sleep is here to make sure this test isn't flakey.
        while (!RollingFileSystemSink.hasFlushed) {
            Thread.sleep(10L);
            if (++count > 1000) {
                fail("Flush thread did not run within 10 seconds");
            }
        }
        Calendar now = Calendar.getInstance();
        Path currentDir = new Path(path, DATE_FORMAT.format(now.getTime()) + "00");
        FileSystem fs = FileSystem.newInstance(new URI(path), new Configuration());
        Path currentFile = findMostRecentLogFile(fs, new Path(currentDir, getLogFilename()));
        FileStatus status = fs.getFileStatus(currentFile);
        // Each metrics record is 118+ bytes, depending on hostname
        assertTrue("The flusher thread didn't flush the log contents. Expected " + "at least 236 bytes in the log file, but got " + status.getLen(), status.getLen() >= 236);
    } finally {
        RollingFileSystemSink.forceFlush = false;
        try {
            ms.stop();
        } finally {
            ms.shutdown();
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) MyMetrics1(org.apache.hadoop.metrics2.sink.RollingFileSystemSinkTestBase.MyMetrics1) FileStatus(org.apache.hadoop.fs.FileStatus) Configuration(org.apache.hadoop.conf.Configuration) Calendar(java.util.Calendar) FileSystem(org.apache.hadoop.fs.FileSystem) MetricsSystem(org.apache.hadoop.metrics2.MetricsSystem) URI(java.net.URI) Test(org.junit.Test)

Example 93 with FileStatus

use of org.apache.hadoop.fs.FileStatus in project hadoop by apache.

the class TestMove method testMoveTargetExistsWithoutExplicitRename.

@Test
public void testMoveTargetExistsWithoutExplicitRename() throws Exception {
    Path srcPath = new Path("mockfs:/file");
    Path targetPath = new Path("mockfs:/fold0");
    Path dupPath = new Path("mockfs:/fold0/file");
    Path srcPath2 = new Path("mockfs://user/file");
    Path targetPath2 = new Path("mockfs://user/fold0");
    Path dupPath2 = new Path("mockfs://user/fold0/file");
    InstrumentedRenameCommand cmd;
    String[] cmdargs = new String[] { "mockfs:/file", "mockfs:/fold0" };
    FileStatus src_fileStat, target_fileStat, dup_fileStat;
    URI myuri;
    src_fileStat = mock(FileStatus.class);
    target_fileStat = mock(FileStatus.class);
    dup_fileStat = mock(FileStatus.class);
    myuri = new URI("mockfs://user");
    when(src_fileStat.isDirectory()).thenReturn(false);
    when(target_fileStat.isDirectory()).thenReturn(true);
    when(dup_fileStat.isDirectory()).thenReturn(false);
    when(src_fileStat.getPath()).thenReturn(srcPath2);
    when(target_fileStat.getPath()).thenReturn(targetPath2);
    when(dup_fileStat.getPath()).thenReturn(dupPath2);
    when(mockFs.getFileStatus(eq(srcPath))).thenReturn(src_fileStat);
    when(mockFs.getFileStatus(eq(targetPath))).thenReturn(target_fileStat);
    when(mockFs.getFileStatus(eq(dupPath))).thenReturn(dup_fileStat);
    when(mockFs.getFileStatus(eq(srcPath2))).thenReturn(src_fileStat);
    when(mockFs.getFileStatus(eq(targetPath2))).thenReturn(target_fileStat);
    when(mockFs.getFileStatus(eq(dupPath2))).thenReturn(dup_fileStat);
    when(mockFs.getUri()).thenReturn(myuri);
    cmd = new InstrumentedRenameCommand();
    cmd.setConf(conf);
    cmd.setOverwrite(true);
    cmd.run(cmdargs);
    // make sure command failed with the proper exception
    assertTrue("Rename should have failed with path exists exception", cmd.error instanceof PathExistsException);
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) PathExistsException(org.apache.hadoop.fs.PathExistsException) URI(java.net.URI) Test(org.junit.Test)

Example 94 with FileStatus

use of org.apache.hadoop.fs.FileStatus in project hadoop by apache.

the class AbstractContractGetFileStatusTest method testGetFileStatusNonexistentFile.

@Test
public void testGetFileStatusNonexistentFile() throws Throwable {
    try {
        FileStatus status = getFileSystem().getFileStatus(target);
        //got here: trouble
        fail("expected a failure, got " + status);
    } catch (FileNotFoundException e) {
        //expected
        handleExpectedException(e);
    }
}
Also used : LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) FileStatus(org.apache.hadoop.fs.FileStatus) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 95 with FileStatus

use of org.apache.hadoop.fs.FileStatus in project hadoop by apache.

the class AbstractContractGetFileStatusTest method verifyStatusArrayMatchesFile.

/**
   * Verify a returned status array matches a single named file.
   * @param f filename
   * @param status status array
   */
private void verifyStatusArrayMatchesFile(Path f, FileStatus[] status) {
    assertEquals(1, status.length);
    FileStatus fileStatus = status[0];
    assertIsNamedFile(f, fileStatus);
}
Also used : LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) FileStatus(org.apache.hadoop.fs.FileStatus)

Aggregations

FileStatus (org.apache.hadoop.fs.FileStatus)1156 Path (org.apache.hadoop.fs.Path)910 FileSystem (org.apache.hadoop.fs.FileSystem)417 Test (org.junit.Test)372 IOException (java.io.IOException)296 Configuration (org.apache.hadoop.conf.Configuration)187 ArrayList (java.util.ArrayList)175 FileNotFoundException (java.io.FileNotFoundException)136 LocatedFileStatus (org.apache.hadoop.fs.LocatedFileStatus)105 FsPermission (org.apache.hadoop.fs.permission.FsPermission)86 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)67 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)65 HashMap (java.util.HashMap)54 File (java.io.File)41 URI (java.net.URI)41 PathFilter (org.apache.hadoop.fs.PathFilter)38 BufferedReader (java.io.BufferedReader)30 InputStreamReader (java.io.InputStreamReader)30 BlockLocation (org.apache.hadoop.fs.BlockLocation)30 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)30