Author: rangadi
Date: Fri Jan 11 19:27:25 2008
New Revision: 611361

URL: http://svn.apache.org/viewvc?rev=611361&view=rev
Log:
HADOOP-2464. Unit tests for chmod, chown, and chgrp using DFS. (Raghu Angadi)

Modified:
    lucene/hadoop/trunk/CHANGES.txt
    lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestDFSShell.java

Modified: lucene/hadoop/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/lucene/hadoop/trunk/CHANGES.txt?rev=611361&r1=611360&r2=611361&view=diff
==============================================================================
--- lucene/hadoop/trunk/CHANGES.txt (original)
+++ lucene/hadoop/trunk/CHANGES.txt Fri Jan 11 19:27:25 2008
@@ -213,6 +213,9 @@
     Mapper.map method. This is done by splitting the 'io.sort.mb' buffer into
     two and using one half for collecting map-outputs and the other half for
     sort/spill. (Amar Kamat via acmurthy)
+    
+    HADOOP-2464. Unit tests for chmod, chown, and chgrp using DFS.
+    (Raghu Angadi)
 
   OPTIMIZATIONS
 

Modified: lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestDFSShell.java
URL: 
http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestDFSShell.java?rev=611361&r1=611360&r2=611361&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestDFSShell.java 
(original)
+++ lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestDFSShell.java Fri 
Jan 11 19:27:25 2008
@@ -301,58 +301,121 @@
     }
   }
 
+  //throws IOException instead of Exception as shell.run() does.
+  private int runCmd(FsShell shell, String... args) throws IOException {
+    try {
+      return shell.run(args);
+    } catch (IOException e) {
+      throw e;
+    } catch (RuntimeException e) {
+      throw e;
+    } catch (Exception e) {
+      throw new IOException(StringUtils.stringifyException(e));
+    }
+  }
+  
   /**
-   * Test chmod. How do we test chown and chgrp?
+   * Test chmod.
    */
-  public void testFilePermissions() throws IOException {
-    Configuration conf = new Configuration();
-    //Temperorily use LocalFileSystem until HADOOP-1298 is committed
-    conf.set("fs.default.name", "local");
-    FileSystem fs = FileSystem.getLocal(conf);
-    
+  void testChmod(Configuration conf, FileSystem fs, String chmodDir) 
+                                                    throws IOException {
     FsShell shell = new FsShell();
     shell.setConf(conf);
     
     try {
-     String chmodDir = (new File(TEST_ROOT_DIR, 
"chmodTest")).getAbsolutePath(); 
-     
      //first make dir
      Path dir = new Path(chmodDir);
      fs.delete(dir);
      fs.mkdirs(dir);
 
-     shell.run(new String[]{ "-chmod", "u+rwx,g=rw,o-rwx", chmodDir });
+     runCmd(shell, "-chmod", "u+rwx,g=rw,o-rwx", chmodDir);
      assertEquals("rwxrw----",
                   fs.getFileStatus(dir).getPermission().toString());
 
      //create an empty file
      Path file = new Path(chmodDir, "file");
-     TestDFSShell.createLocalFile(new File(file.toString()));
+     TestDFSShell.writeFile(fs, file);
 
      //test octal mode
-     shell.run(new String[]{ "-chmod", "644", file.toString()});
+     runCmd(shell, "-chmod", "644", file.toString());
      assertEquals("rw-r--r--",
                   fs.getFileStatus(file).getPermission().toString());
 
      //test recursive
-     shell.run(new String[]{ "-chmod", "-R", "a+rwX", chmodDir });
+     runCmd(shell, "-chmod", "-R", "a+rwX", chmodDir);
      assertEquals("rwxrwxrwx",
                   fs.getFileStatus(dir).getPermission().toString()); 
      assertEquals("rw-rw-rw-",
                   fs.getFileStatus(file).getPermission().toString());
      
      fs.delete(dir);     
-    } catch (IOException e) {
-      throw e;
-    } catch (RuntimeException e) {
-      throw e;
-    } catch (Exception e) {
-      throw new IOException(StringUtils.stringifyException(e));
     } finally {
-      shell.close();
-      fs.close();
+      try {
+        fs.close();
+        shell.close();
+      } catch (IOException ignored) {}
     }
+  }
+  
+  private void confirmOwner(String owner, String group, 
+                            FileSystem fs, Path... paths) throws IOException {
+    for(Path path : paths) {
+      if (owner != null) {
+        assertEquals(owner, fs.getFileStatus(path).getOwner());
+      }
+      if (group != null) {
+        assertEquals(group, fs.getFileStatus(path).getGroup());
+      }
+    }
+  }
+  
+  public void testFilePermissions() throws IOException {
+    Configuration conf = new Configuration();
+    
+    //test chnmod on local fs
+    FileSystem fs = FileSystem.getLocal(conf);
+    testChmod(conf, fs, 
+              (new File(TEST_ROOT_DIR, "chmodTest")).getAbsolutePath());
+    
+    conf.set("dfs.permissions", "true");
+    
+    //test chmod on DFS
+    MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null);
+    fs = cluster.getFileSystem();
+    testChmod(conf, fs, "/tmp/chmodTest");
+    
+    // test chown and chgrp on DFS:
+    
+    FsShell shell = new FsShell();
+    shell.setConf(conf);
+    fs = cluster.getFileSystem();
+    
+    /* For dfs, I am the super user and I can change ower of any file to
+     * anything. "-R" option is already tested by chmod test above.
+     */
+    
+    String file = "/tmp/chownTest";
+    Path path = new Path(file);
+    Path parent = new Path("/tmp");
+    Path root = new Path("/");
+    TestDFSShell.writeFile(fs, path);
+    
+    runCmd(shell, "-chgrp", "-R", "herbivores", "/*", "unknownFile*");
+    confirmOwner(null, "herbivores", fs, parent, path);
+    
+    runCmd(shell, "-chgrp", "mammals", file);
+    confirmOwner(null, "mammals", fs, path);
+    
+    runCmd(shell, "-chown", "-R", ":reptiles", "/");
+    confirmOwner(null, "reptiles", fs, root, parent, path);
+    
+    runCmd(shell, "-chown", "python:", "/nonExistentFile", file);
+    confirmOwner("python", "reptiles", fs, path);
+
+    runCmd(shell, "-chown", "-R", "hadoop:toys", "unknownFile", "/");
+    confirmOwner("hadoop", "toys", fs, root, parent, path);
     
+    cluster.shutdown();
   }
   /**
    * Tests various options of DFSShell.


Reply via email to