hadoop-hdfs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject svn commit: r1212060 [6/8] - in /hadoop/common/trunk/hadoop-hdfs-project: ./ hadoop-hdfs-httpfs/ hadoop-hdfs-httpfs/src/ hadoop-hdfs-httpfs/src/main/ hadoop-hdfs-httpfs/src/main/conf/ hadoop-hdfs-httpfs/src/main/java/ hadoop-hdfs-httpfs/src/main/java/o...
Date Thu, 08 Dec 2011 19:25:33 GMT
Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServer.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServer.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServer.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServer.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,790 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.server;
+
+import junit.framework.Assert;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.IOUtils;
+import org.apache.hadoop.lib.lang.XException;
+import org.apache.hadoop.test.HTestCase;
+import org.apache.hadoop.test.TestDir;
+import org.apache.hadoop.test.TestDirHelper;
+import org.apache.hadoop.test.TestException;
+import org.apache.hadoop.util.StringUtils;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class TestServer extends HTestCase {
+
+  @Test
+  @TestDir
+  public void constructorsGetters() throws Exception {
+    Server server = new Server("server", "/a", "/b", "/c", "/d", new Configuration(false));
+    Assert.assertEquals(server.getHomeDir(), "/a");
+    Assert.assertEquals(server.getConfigDir(), "/b");
+    Assert.assertEquals(server.getLogDir(), "/c");
+    Assert.assertEquals(server.getTempDir(), "/d");
+    Assert.assertEquals(server.getName(), "server");
+    Assert.assertEquals(server.getPrefix(), "server");
+    Assert.assertEquals(server.getPrefixedName("name"), "server.name");
+    Assert.assertNotNull(server.getConfig());
+
+    server = new Server("server", "/a", "/b", "/c", "/d");
+    Assert.assertEquals(server.getHomeDir(), "/a");
+    Assert.assertEquals(server.getConfigDir(), "/b");
+    Assert.assertEquals(server.getLogDir(), "/c");
+    Assert.assertEquals(server.getTempDir(), "/d");
+    Assert.assertEquals(server.getName(), "server");
+    Assert.assertEquals(server.getPrefix(), "server");
+    Assert.assertEquals(server.getPrefixedName("name"), "server.name");
+    Assert.assertNull(server.getConfig());
+
+    server = new Server("server", TestDirHelper.getTestDir().getAbsolutePath(), new Configuration(false));
+    Assert.assertEquals(server.getHomeDir(), TestDirHelper.getTestDir().getAbsolutePath());
+    Assert.assertEquals(server.getConfigDir(), TestDirHelper.getTestDir() + "/conf");
+    Assert.assertEquals(server.getLogDir(), TestDirHelper.getTestDir() + "/log");
+    Assert.assertEquals(server.getTempDir(), TestDirHelper.getTestDir() + "/temp");
+    Assert.assertEquals(server.getName(), "server");
+    Assert.assertEquals(server.getPrefix(), "server");
+    Assert.assertEquals(server.getPrefixedName("name"), "server.name");
+    Assert.assertNotNull(server.getConfig());
+
+    server = new Server("server", TestDirHelper.getTestDir().getAbsolutePath());
+    Assert.assertEquals(server.getHomeDir(), TestDirHelper.getTestDir().getAbsolutePath());
+    Assert.assertEquals(server.getConfigDir(), TestDirHelper.getTestDir() + "/conf");
+    Assert.assertEquals(server.getLogDir(), TestDirHelper.getTestDir() + "/log");
+    Assert.assertEquals(server.getTempDir(), TestDirHelper.getTestDir() + "/temp");
+    Assert.assertEquals(server.getName(), "server");
+    Assert.assertEquals(server.getPrefix(), "server");
+    Assert.assertEquals(server.getPrefixedName("name"), "server.name");
+    Assert.assertNull(server.getConfig());
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S01.*")
+  @TestDir
+  public void initNoHomeDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S02.*")
+  @TestDir
+  public void initHomeDirNotDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    new FileOutputStream(homeDir).close();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S01.*")
+  @TestDir
+  public void initNoConfigDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    Assert.assertTrue(homeDir.mkdir());
+    Assert.assertTrue(new File(homeDir, "log").mkdir());
+    Assert.assertTrue(new File(homeDir, "temp").mkdir());
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S02.*")
+  @TestDir
+  public void initConfigDirNotDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    Assert.assertTrue(homeDir.mkdir());
+    Assert.assertTrue(new File(homeDir, "log").mkdir());
+    Assert.assertTrue(new File(homeDir, "temp").mkdir());
+    File configDir = new File(homeDir, "conf");
+    new FileOutputStream(configDir).close();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S01.*")
+  @TestDir
+  public void initNoLogDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    Assert.assertTrue(homeDir.mkdir());
+    Assert.assertTrue(new File(homeDir, "conf").mkdir());
+    Assert.assertTrue(new File(homeDir, "temp").mkdir());
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S02.*")
+  @TestDir
+  public void initLogDirNotDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    Assert.assertTrue(homeDir.mkdir());
+    Assert.assertTrue(new File(homeDir, "conf").mkdir());
+    Assert.assertTrue(new File(homeDir, "temp").mkdir());
+    File logDir = new File(homeDir, "log");
+    new FileOutputStream(logDir).close();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S01.*")
+  @TestDir
+  public void initNoTempDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    Assert.assertTrue(homeDir.mkdir());
+    Assert.assertTrue(new File(homeDir, "conf").mkdir());
+    Assert.assertTrue(new File(homeDir, "log").mkdir());
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S02.*")
+  @TestDir
+  public void initTempDirNotDir() throws Exception {
+    File homeDir = new File(TestDirHelper.getTestDir(), "home");
+    Assert.assertTrue(homeDir.mkdir());
+    Assert.assertTrue(new File(homeDir, "conf").mkdir());
+    Assert.assertTrue(new File(homeDir, "log").mkdir());
+    File tempDir = new File(homeDir, "temp");
+    new FileOutputStream(tempDir).close();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = new Server("server", homeDir.getAbsolutePath(), conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S05.*")
+  @TestDir
+  public void siteFileNotAFile() throws Exception {
+    String homeDir = TestDirHelper.getTestDir().getAbsolutePath();
+    File siteFile = new File(homeDir, "server-site.xml");
+    Assert.assertTrue(siteFile.mkdir());
+    Server server = new Server("server", homeDir, homeDir, homeDir, homeDir);
+    server.init();
+  }
+
+  private Server createServer(Configuration conf) {
+    return new Server("server", TestDirHelper.getTestDir().getAbsolutePath(),
+                      TestDirHelper.getTestDir().getAbsolutePath(),
+                      TestDirHelper.getTestDir().getAbsolutePath(), TestDirHelper.getTestDir().getAbsolutePath(), conf);
+  }
+
+  @Test
+  @TestDir
+  public void log4jFile() throws Exception {
+    InputStream is = Server.getResource("default-log4j.properties");
+    OutputStream os = new FileOutputStream(new File(TestDirHelper.getTestDir(), "server-log4j.properties"));
+    IOUtils.copyBytes(is, os, 1024, true);
+    Configuration conf = new Configuration(false);
+    Server server = createServer(conf);
+    server.init();
+  }
+
+  public static class LifeCycleService extends BaseService {
+
+    public LifeCycleService() {
+      super("lifecycle");
+    }
+
+    @Override
+    protected void init() throws ServiceException {
+      Assert.assertEquals(getServer().getStatus(), Server.Status.BOOTING);
+    }
+
+    @Override
+    public void destroy() {
+      Assert.assertEquals(getServer().getStatus(), Server.Status.SHUTTING_DOWN);
+      super.destroy();
+    }
+
+    @Override
+    public Class getInterface() {
+      return LifeCycleService.class;
+    }
+  }
+
+  @Test
+  @TestDir
+  public void lifeCycle() throws Exception {
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", LifeCycleService.class.getName());
+    Server server = createServer(conf);
+    Assert.assertEquals(server.getStatus(), Server.Status.UNDEF);
+    server.init();
+    Assert.assertNotNull(server.get(LifeCycleService.class));
+    Assert.assertEquals(server.getStatus(), Server.Status.NORMAL);
+    server.destroy();
+    Assert.assertEquals(server.getStatus(), Server.Status.SHUTDOWN);
+  }
+
+  @Test
+  @TestDir
+  public void startWithStatusNotNormal() throws Exception {
+    Configuration conf = new Configuration(false);
+    conf.set("server.startup.status", "ADMIN");
+    Server server = createServer(conf);
+    server.init();
+    Assert.assertEquals(server.getStatus(), Server.Status.ADMIN);
+    server.destroy();
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  @TestDir
+  public void nonSeteableStatus() throws Exception {
+    Configuration conf = new Configuration(false);
+    Server server = createServer(conf);
+    server.init();
+    server.setStatus(Server.Status.SHUTDOWN);
+  }
+
+  public static class TestService implements Service {
+    static List<String> LIFECYCLE = new ArrayList<String>();
+
+    @Override
+    public void init(Server server) throws ServiceException {
+      LIFECYCLE.add("init");
+    }
+
+    @Override
+    public void postInit() throws ServiceException {
+      LIFECYCLE.add("postInit");
+    }
+
+    @Override
+    public void destroy() {
+      LIFECYCLE.add("destroy");
+    }
+
+    @Override
+    public Class[] getServiceDependencies() {
+      return new Class[0];
+    }
+
+    @Override
+    public Class getInterface() {
+      return TestService.class;
+    }
+
+    @Override
+    public void serverStatusChange(Server.Status oldStatus, Server.Status newStatus) throws ServiceException {
+      LIFECYCLE.add("serverStatusChange");
+    }
+  }
+
+  public static class TestServiceExceptionOnStatusChange extends TestService {
+
+    @Override
+    public void serverStatusChange(Server.Status oldStatus, Server.Status newStatus) throws ServiceException {
+      throw new RuntimeException();
+    }
+  }
+
+  @Test
+  @TestDir
+  public void changeStatus() throws Exception {
+    TestService.LIFECYCLE.clear();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = createServer(conf);
+    server.init();
+    server.setStatus(Server.Status.ADMIN);
+    Assert.assertTrue(TestService.LIFECYCLE.contains("serverStatusChange"));
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S11.*")
+  @TestDir
+  public void changeStatusServiceException() throws Exception {
+    TestService.LIFECYCLE.clear();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestServiceExceptionOnStatusChange.class.getName());
+    Server server = createServer(conf);
+    server.init();
+  }
+
+  @Test
+  @TestDir
+  public void setSameStatus() throws Exception {
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = createServer(conf);
+    server.init();
+    TestService.LIFECYCLE.clear();
+    server.setStatus(server.getStatus());
+    Assert.assertFalse(TestService.LIFECYCLE.contains("serverStatusChange"));
+  }
+
+  @Test
+  @TestDir
+  public void serviceLifeCycle() throws Exception {
+    TestService.LIFECYCLE.clear();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", TestService.class.getName());
+    Server server = createServer(conf);
+    server.init();
+    Assert.assertNotNull(server.get(TestService.class));
+    server.destroy();
+    Assert.assertEquals(TestService.LIFECYCLE, Arrays.asList("init", "postInit", "serverStatusChange", "destroy"));
+  }
+
+  @Test
+  @TestDir
+  public void loadingDefaultConfig() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Server server = new Server("testserver", dir, dir, dir, dir);
+    server.init();
+    Assert.assertEquals(server.getConfig().get("testserver.a"), "default");
+  }
+
+  @Test
+  @TestDir
+  public void loadingSiteConfig() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    File configFile = new File(dir, "testserver-site.xml");
+    Writer w = new FileWriter(configFile);
+    w.write("<configuration><property><name>testserver.a</name><value>site</value></property></configuration>");
+    w.close();
+    Server server = new Server("testserver", dir, dir, dir, dir);
+    server.init();
+    Assert.assertEquals(server.getConfig().get("testserver.a"), "site");
+  }
+
+  @Test
+  @TestDir
+  public void loadingSysPropConfig() throws Exception {
+    try {
+      System.setProperty("testserver.a", "sysprop");
+      String dir = TestDirHelper.getTestDir().getAbsolutePath();
+      File configFile = new File(dir, "testserver-site.xml");
+      Writer w = new FileWriter(configFile);
+      w.write("<configuration><property><name>testserver.a</name><value>site</value></property></configuration>");
+      w.close();
+      Server server = new Server("testserver", dir, dir, dir, dir);
+      server.init();
+      Assert.assertEquals(server.getConfig().get("testserver.a"), "sysprop");
+    } finally {
+      System.getProperties().remove("testserver.a");
+    }
+  }
+
+  @Test(expected = IllegalStateException.class)
+  @TestDir
+  public void illegalState1() throws Exception {
+    Server server = new Server("server", TestDirHelper.getTestDir().getAbsolutePath(), new Configuration(false));
+    server.destroy();
+  }
+
+  @Test(expected = IllegalStateException.class)
+  @TestDir
+  public void illegalState2() throws Exception {
+    Server server = new Server("server", TestDirHelper.getTestDir().getAbsolutePath(), new Configuration(false));
+    server.get(Object.class);
+  }
+
+  @Test(expected = IllegalStateException.class)
+  @TestDir
+  public void illegalState3() throws Exception {
+    Server server = new Server("server", TestDirHelper.getTestDir().getAbsolutePath(), new Configuration(false));
+    server.setService(null);
+  }
+
+  @Test(expected = IllegalStateException.class)
+  @TestDir
+  public void illegalState4() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Server server = new Server("server", dir, dir, dir, dir, new Configuration(false));
+    server.init();
+    server.init();
+  }
+
+  private static List<String> ORDER = new ArrayList<String>();
+
+  public abstract static class MyService implements Service, XException.ERROR {
+    private String id;
+    private Class serviceInterface;
+    private Class[] dependencies;
+    private boolean failOnInit;
+    private boolean failOnDestroy;
+
+    protected MyService(String id, Class serviceInterface, Class[] dependencies, boolean failOnInit,
+                        boolean failOnDestroy) {
+      this.id = id;
+      this.serviceInterface = serviceInterface;
+      this.dependencies = dependencies;
+      this.failOnInit = failOnInit;
+      this.failOnDestroy = failOnDestroy;
+    }
+
+
+    @Override
+    public void init(Server server) throws ServiceException {
+      ORDER.add(id + ".init");
+      if (failOnInit) {
+        throw new ServiceException(this);
+      }
+    }
+
+    @Override
+    public void postInit() throws ServiceException {
+      ORDER.add(id + ".postInit");
+    }
+
+    @Override
+    public String getTemplate() {
+      return "";
+    }
+
+    @Override
+    public void destroy() {
+      ORDER.add(id + ".destroy");
+      if (failOnDestroy) {
+        throw new RuntimeException();
+      }
+    }
+
+    @Override
+    public Class[] getServiceDependencies() {
+      return dependencies;
+    }
+
+    @Override
+    public Class getInterface() {
+      return serviceInterface;
+    }
+
+    @Override
+    public void serverStatusChange(Server.Status oldStatus, Server.Status newStatus) throws ServiceException {
+    }
+  }
+
+  public static class MyService1 extends MyService {
+
+    public MyService1() {
+      super("s1", MyService1.class, null, false, false);
+    }
+
+    protected MyService1(String id, Class serviceInterface, Class[] dependencies, boolean failOnInit,
+                         boolean failOnDestroy) {
+      super(id, serviceInterface, dependencies, failOnInit, failOnDestroy);
+    }
+  }
+
+  public static class MyService2 extends MyService {
+    public MyService2() {
+      super("s2", MyService2.class, null, true, false);
+    }
+  }
+
+
+  public static class MyService3 extends MyService {
+    public MyService3() {
+      super("s3", MyService3.class, null, false, false);
+    }
+  }
+
+  public static class MyService1a extends MyService1 {
+    public MyService1a() {
+      super("s1a", MyService1.class, null, false, false);
+    }
+  }
+
+  public static class MyService4 extends MyService1 {
+
+    public MyService4() {
+      super("s4a", String.class, null, false, false);
+    }
+  }
+
+  public static class MyService5 extends MyService {
+
+    public MyService5() {
+      super("s5", MyService5.class, null, false, true);
+    }
+
+    protected MyService5(String id, Class serviceInterface, Class[] dependencies, boolean failOnInit,
+                         boolean failOnDestroy) {
+      super(id, serviceInterface, dependencies, failOnInit, failOnDestroy);
+    }
+  }
+
+  public static class MyService5a extends MyService5 {
+
+    public MyService5a() {
+      super("s5a", MyService5.class, null, false, false);
+    }
+  }
+
+  public static class MyService6 extends MyService {
+
+    public MyService6() {
+      super("s6", MyService6.class, new Class[]{MyService1.class}, false, false);
+    }
+  }
+
+  public static class MyService7 extends MyService {
+
+    @SuppressWarnings({"UnusedParameters"})
+    public MyService7(String foo) {
+      super("s6", MyService7.class, new Class[]{MyService1.class}, false, false);
+    }
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S08.*")
+  @TestDir
+  public void invalidSservice() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", "foo");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S07.*")
+  @TestDir
+  public void serviceWithNoDefaultConstructor() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", MyService7.class.getName());
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S04.*")
+  @TestDir
+  public void serviceNotImplementingServiceInterface() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", MyService4.class.getName());
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServerException.class, msgRegExp = "S10.*")
+  @TestDir
+  public void serviceWithMissingDependency() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    String services = StringUtils.join(",", Arrays.asList(MyService3.class.getName(), MyService6.class.getName())
+    );
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestDir
+  public void services() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf;
+    Server server;
+
+    // no services
+    ORDER.clear();
+    conf = new Configuration(false);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Assert.assertEquals(ORDER.size(), 0);
+
+    // 2 services init/destroy
+    ORDER.clear();
+    String services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName())
+    );
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Assert.assertEquals(server.get(MyService1.class).getInterface(), MyService1.class);
+    Assert.assertEquals(server.get(MyService3.class).getInterface(), MyService3.class);
+    Assert.assertEquals(ORDER.size(), 4);
+    Assert.assertEquals(ORDER.get(0), "s1.init");
+    Assert.assertEquals(ORDER.get(1), "s3.init");
+    Assert.assertEquals(ORDER.get(2), "s1.postInit");
+    Assert.assertEquals(ORDER.get(3), "s3.postInit");
+    server.destroy();
+    Assert.assertEquals(ORDER.size(), 6);
+    Assert.assertEquals(ORDER.get(4), "s3.destroy");
+    Assert.assertEquals(ORDER.get(5), "s1.destroy");
+
+    // 3 services, 2nd one fails on init
+    ORDER.clear();
+    services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService2.class.getName(),
+                                                   MyService3.class.getName()));
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+
+    server = new Server("server", dir, dir, dir, dir, conf);
+    try {
+      server.init();
+      Assert.fail();
+    } catch (ServerException ex) {
+      Assert.assertEquals(MyService2.class, ex.getError().getClass());
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+    Assert.assertEquals(ORDER.size(), 3);
+    Assert.assertEquals(ORDER.get(0), "s1.init");
+    Assert.assertEquals(ORDER.get(1), "s2.init");
+    Assert.assertEquals(ORDER.get(2), "s1.destroy");
+
+    // 2 services one fails on destroy
+    ORDER.clear();
+    services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService5.class.getName()));
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Assert.assertEquals(ORDER.size(), 4);
+    Assert.assertEquals(ORDER.get(0), "s1.init");
+    Assert.assertEquals(ORDER.get(1), "s5.init");
+    Assert.assertEquals(ORDER.get(2), "s1.postInit");
+    Assert.assertEquals(ORDER.get(3), "s5.postInit");
+    server.destroy();
+    Assert.assertEquals(ORDER.size(), 6);
+    Assert.assertEquals(ORDER.get(4), "s5.destroy");
+    Assert.assertEquals(ORDER.get(5), "s1.destroy");
+
+
+    // service override via ext
+    ORDER.clear();
+    services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName()));
+    String servicesExt = StringUtils.join(",", Arrays.asList(MyService1a.class.getName()));
+
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.services.ext", servicesExt);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+
+    Assert.assertEquals(server.get(MyService1.class).getClass(), MyService1a.class);
+    Assert.assertEquals(ORDER.size(), 4);
+    Assert.assertEquals(ORDER.get(0), "s1a.init");
+    Assert.assertEquals(ORDER.get(1), "s3.init");
+    Assert.assertEquals(ORDER.get(2), "s1a.postInit");
+    Assert.assertEquals(ORDER.get(3), "s3.postInit");
+    server.destroy();
+    Assert.assertEquals(ORDER.size(), 6);
+    Assert.assertEquals(ORDER.get(4), "s3.destroy");
+    Assert.assertEquals(ORDER.get(5), "s1a.destroy");
+
+    // service override via setService
+    ORDER.clear();
+    services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName()));
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+
+    server.setService(MyService1a.class);
+    Assert.assertEquals(ORDER.size(), 6);
+    Assert.assertEquals(ORDER.get(4), "s1.destroy");
+    Assert.assertEquals(ORDER.get(5), "s1a.init");
+
+    Assert.assertEquals(server.get(MyService1.class).getClass(), MyService1a.class);
+
+    server.destroy();
+    Assert.assertEquals(ORDER.size(), 8);
+    Assert.assertEquals(ORDER.get(6), "s3.destroy");
+    Assert.assertEquals(ORDER.get(7), "s1a.destroy");
+
+    // service add via setService
+    ORDER.clear();
+    services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName()));
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+
+    server.setService(MyService5.class);
+    Assert.assertEquals(ORDER.size(), 5);
+    Assert.assertEquals(ORDER.get(4), "s5.init");
+
+    Assert.assertEquals(server.get(MyService5.class).getClass(), MyService5.class);
+
+    server.destroy();
+    Assert.assertEquals(ORDER.size(), 8);
+    Assert.assertEquals(ORDER.get(5), "s5.destroy");
+    Assert.assertEquals(ORDER.get(6), "s3.destroy");
+    Assert.assertEquals(ORDER.get(7), "s1.destroy");
+
+    // service add via setService exception
+    ORDER.clear();
+    services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName()));
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    try {
+      server.setService(MyService7.class);
+      Assert.fail();
+    } catch (ServerException ex) {
+      Assert.assertEquals(ServerException.ERROR.S09, ex.getError());
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+    Assert.assertEquals(ORDER.size(), 6);
+    Assert.assertEquals(ORDER.get(4), "s3.destroy");
+    Assert.assertEquals(ORDER.get(5), "s1.destroy");
+
+    // service with dependency
+    ORDER.clear();
+    services = StringUtils.join(",", Arrays.asList(MyService1.class.getName(), MyService6.class.getName()));
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Assert.assertEquals(server.get(MyService1.class).getInterface(), MyService1.class);
+    Assert.assertEquals(server.get(MyService6.class).getInterface(), MyService6.class);
+    server.destroy();
+  }
+
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServerConstructor.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServerConstructor.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServerConstructor.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/server/TestServerConstructor.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.server;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.test.HTestCase;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+@RunWith(value = Parameterized.class)
+public class TestServerConstructor extends HTestCase {
+
+  @Parameterized.Parameters
+  public static Collection constructorFailParams() {
+    return Arrays.asList(new Object[][]{
+      {null, null, null, null, null, null},
+      {"", null, null, null, null, null},
+      {null, null, null, null, null, null},
+      {"server", null, null, null, null, null},
+      {"server", "", null, null, null, null},
+      {"server", "foo", null, null, null, null},
+      {"server", "/tmp", null, null, null, null},
+      {"server", "/tmp", "", null, null, null},
+      {"server", "/tmp", "foo", null, null, null},
+      {"server", "/tmp", "/tmp", null, null, null},
+      {"server", "/tmp", "/tmp", "", null, null},
+      {"server", "/tmp", "/tmp", "foo", null, null},
+      {"server", "/tmp", "/tmp", "/tmp", null, null},
+      {"server", "/tmp", "/tmp", "/tmp", "", null},
+      {"server", "/tmp", "/tmp", "/tmp", "foo", null}});
+  }
+
+  private String name;
+  private String homeDir;
+  private String configDir;
+  private String logDir;
+  private String tempDir;
+  private Configuration conf;
+
+  public TestServerConstructor(String name, String homeDir, String configDir, String logDir, String tempDir,
+                               Configuration conf) {
+    this.name = name;
+    this.homeDir = homeDir;
+    this.configDir = configDir;
+    this.logDir = logDir;
+    this.tempDir = tempDir;
+    this.conf = conf;
+  }
+
+
+  @Test(expected = IllegalArgumentException.class)
+  public void constructorFail() {
+    new Server(name, homeDir, configDir, logDir, tempDir, conf);
+  }
+
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/hadoop/TestFileSystemAccessService.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/hadoop/TestFileSystemAccessService.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/hadoop/TestFileSystemAccessService.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/hadoop/TestFileSystemAccessService.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,306 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.service.hadoop;
+
+import junit.framework.Assert;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.lib.server.Server;
+import org.apache.hadoop.lib.server.ServiceException;
+import org.apache.hadoop.lib.service.FileSystemAccess;
+import org.apache.hadoop.lib.service.FileSystemAccessException;
+import org.apache.hadoop.lib.service.instrumentation.InstrumentationService;
+import org.apache.hadoop.test.HFSTestCase;
+import org.apache.hadoop.test.TestDir;
+import org.apache.hadoop.test.TestDirHelper;
+import org.apache.hadoop.test.TestException;
+import org.apache.hadoop.test.TestHdfs;
+import org.apache.hadoop.test.TestHdfsHelper;
+import org.apache.hadoop.util.StringUtils;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+public class TestFileSystemAccessService extends HFSTestCase {
+
+  @Test
+  @TestDir
+  public void simpleSecurity() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Assert.assertNotNull(server.get(FileSystemAccess.class));
+    server.destroy();
+  }
+
+  @Test
+  @TestException(exception = ServiceException.class, msgRegExp = "H01.*")
+  @TestDir
+  public void noKerberosKeytabProperty() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.authentication.type", "kerberos");
+    conf.set("server.hadoop.authentication.kerberos.keytab", " ");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServiceException.class, msgRegExp = "H01.*")
+  @TestDir
+  public void noKerberosPrincipalProperty() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.authentication.type", "kerberos");
+    conf.set("server.hadoop.authentication.kerberos.keytab", "/tmp/foo");
+    conf.set("server.hadoop.authentication.kerberos.principal", " ");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServiceException.class, msgRegExp = "H02.*")
+  @TestDir
+  public void kerberosInitializationFailure() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.authentication.type", "kerberos");
+    conf.set("server.hadoop.authentication.kerberos.keytab", "/tmp/foo");
+    conf.set("server.hadoop.authentication.kerberos.principal", "foo@FOO");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServiceException.class, msgRegExp = "H09.*")
+  @TestDir
+  public void invalidSecurity() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.authentication.type", "foo");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestDir
+  public void serviceHadoopConf() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.conf:foo", "FOO");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    FileSystemAccessService fsAccess = (FileSystemAccessService) server.get(FileSystemAccess.class);
+    Assert.assertEquals(fsAccess.serviceHadoopConf.get("foo"), "FOO");
+    server.destroy();
+  }
+
+  @Test
+  @TestDir
+  public void inWhitelists() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    FileSystemAccessService fsAccess = (FileSystemAccessService) server.get(FileSystemAccess.class);
+    fsAccess.validateNamenode("NN");
+    server.destroy();
+
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.name.node.whitelist", "*");
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    fsAccess = (FileSystemAccessService) server.get(FileSystemAccess.class);
+    fsAccess.validateNamenode("NN");
+    server.destroy();
+
+    conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.name.node.whitelist", "NN");
+    server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    fsAccess = (FileSystemAccessService) server.get(FileSystemAccess.class);
+    fsAccess.validateNamenode("NN");
+    server.destroy();
+  }
+
+  @Test
+  @TestException(exception = FileSystemAccessException.class, msgRegExp = "H05.*")
+  @TestDir
+  public void NameNodeNotinWhitelists() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    conf.set("server.hadoop.name.node.whitelist", "NN");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    FileSystemAccessService fsAccess = (FileSystemAccessService) server.get(FileSystemAccess.class);
+    fsAccess.validateNamenode("NNx");
+  }
+
+  @Test
+  @TestDir
+  @TestHdfs
+  public void createFileSystem() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    FileSystemAccess hadoop = server.get(FileSystemAccess.class);
+    FileSystem fs = hadoop.createFileSystem("u", TestHdfsHelper.getHdfsConf());
+    Assert.assertNotNull(fs);
+    fs.mkdirs(new Path("/tmp/foo"));
+    hadoop.releaseFileSystem(fs);
+    try {
+      fs.mkdirs(new Path("/tmp/foo"));
+      Assert.fail();
+    } catch (IOException ex) {
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+    server.destroy();
+  }
+
+  @Test
+  @TestDir
+  @TestHdfs
+  public void fileSystemExecutor() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    FileSystemAccess hadoop = server.get(FileSystemAccess.class);
+
+    final FileSystem fsa[] = new FileSystem[1];
+
+    hadoop.execute("u", TestHdfsHelper.getHdfsConf(), new FileSystemAccess.FileSystemExecutor<Void>() {
+      @Override
+      public Void execute(FileSystem fs) throws IOException {
+        fs.mkdirs(new Path("/tmp/foo"));
+        fsa[0] = fs;
+        return null;
+      }
+    });
+    try {
+      fsa[0].mkdirs(new Path("/tmp/foo"));
+      Assert.fail();
+    } catch (IOException ex) {
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+    server.destroy();
+  }
+
+  @Test
+  @TestException(exception = FileSystemAccessException.class, msgRegExp = "H06.*")
+  @TestDir
+  @TestHdfs
+  public void fileSystemExecutorNoNameNode() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    FileSystemAccess fsAccess = server.get(FileSystemAccess.class);
+
+    Configuration hdfsConf = TestHdfsHelper.getHdfsConf();
+    hdfsConf.set("fs.default.name", "");
+    fsAccess.execute("u", hdfsConf, new FileSystemAccess.FileSystemExecutor<Void>() {
+      @Override
+      public Void execute(FileSystem fs) throws IOException {
+        return null;
+      }
+    });
+  }
+
+  @Test
+  @TestDir
+  @TestHdfs
+  public void fileSystemExecutorException() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          FileSystemAccessService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    FileSystemAccess hadoop = server.get(FileSystemAccess.class);
+
+    final FileSystem fsa[] = new FileSystem[1];
+    try {
+      hadoop.execute("u", TestHdfsHelper.getHdfsConf(), new FileSystemAccess.FileSystemExecutor<Void>() {
+        @Override
+        public Void execute(FileSystem fs) throws IOException {
+          fsa[0] = fs;
+          throw new IOException();
+        }
+      });
+      Assert.fail();
+    } catch (FileSystemAccessException ex) {
+      Assert.assertEquals(ex.getError(), FileSystemAccessException.ERROR.H03);
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+
+    try {
+      fsa[0].mkdirs(new Path("/tmp/foo"));
+      Assert.fail();
+    } catch (IOException ex) {
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+    server.destroy();
+  }
+
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/instrumentation/TestInstrumentationService.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/instrumentation/TestInstrumentationService.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/instrumentation/TestInstrumentationService.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/instrumentation/TestInstrumentationService.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,404 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.service.instrumentation;
+
+import junit.framework.Assert;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.lib.server.Server;
+import org.apache.hadoop.lib.service.Instrumentation;
+import org.apache.hadoop.lib.service.scheduler.SchedulerService;
+import org.apache.hadoop.test.HTestCase;
+import org.apache.hadoop.test.TestDir;
+import org.apache.hadoop.test.TestDirHelper;
+import org.apache.hadoop.util.StringUtils;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.junit.Test;
+
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class TestInstrumentationService extends HTestCase {
+
+  @Override
+  protected float getWaitForRatio() {
+    return 1;
+  }
+
+  @Test
+  public void cron() {
+    InstrumentationService.Cron cron = new InstrumentationService.Cron();
+    Assert.assertEquals(cron.start, 0);
+    Assert.assertEquals(cron.lapStart, 0);
+    Assert.assertEquals(cron.own, 0);
+    Assert.assertEquals(cron.total, 0);
+    long begin = System.currentTimeMillis();
+    Assert.assertEquals(cron.start(), cron);
+    Assert.assertEquals(cron.start(), cron);
+    Assert.assertEquals(cron.start, begin, 20);
+    Assert.assertEquals(cron.start, cron.lapStart);
+    sleep(100);
+    Assert.assertEquals(cron.stop(), cron);
+    long end = System.currentTimeMillis();
+    long delta = end - begin;
+    Assert.assertEquals(cron.own, delta, 20);
+    Assert.assertEquals(cron.total, 0);
+    Assert.assertEquals(cron.lapStart, 0);
+    sleep(100);
+    long reStart = System.currentTimeMillis();
+    cron.start();
+    Assert.assertEquals(cron.start, begin, 20);
+    Assert.assertEquals(cron.lapStart, reStart, 20);
+    sleep(100);
+    cron.stop();
+    long reEnd = System.currentTimeMillis();
+    delta += reEnd - reStart;
+    Assert.assertEquals(cron.own, delta, 20);
+    Assert.assertEquals(cron.total, 0);
+    Assert.assertEquals(cron.lapStart, 0);
+    cron.end();
+    Assert.assertEquals(cron.total, reEnd - begin, 20);
+
+    try {
+      cron.start();
+      Assert.fail();
+    } catch (IllegalStateException ex) {
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+
+    try {
+      cron.stop();
+      Assert.fail();
+    } catch (IllegalStateException ex) {
+    } catch (Exception ex) {
+      Assert.fail();
+    }
+  }
+
+  @Test
+  public void timer() throws Exception {
+    InstrumentationService.Timer timer = new InstrumentationService.Timer(2);
+    InstrumentationService.Cron cron = new InstrumentationService.Cron();
+
+    long ownStart;
+    long ownEnd;
+    long totalStart;
+    long totalEnd;
+    long ownDelta;
+    long totalDelta;
+    long avgTotal;
+    long avgOwn;
+
+    cron.start();
+    ownStart = System.currentTimeMillis();
+    totalStart = ownStart;
+    ownDelta = 0;
+    sleep(100);
+
+    cron.stop();
+    ownEnd = System.currentTimeMillis();
+    ownDelta += ownEnd - ownStart;
+    sleep(100);
+
+    cron.start();
+    ownStart = System.currentTimeMillis();
+    sleep(100);
+
+    cron.stop();
+    ownEnd = System.currentTimeMillis();
+    ownDelta += ownEnd - ownStart;
+    totalEnd = ownEnd;
+    totalDelta = totalEnd - totalStart;
+
+    avgTotal = totalDelta;
+    avgOwn = ownDelta;
+
+    timer.addCron(cron);
+    long[] values = timer.getValues();
+    Assert.assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);
+
+    cron = new InstrumentationService.Cron();
+
+    cron.start();
+    ownStart = System.currentTimeMillis();
+    totalStart = ownStart;
+    ownDelta = 0;
+    sleep(200);
+
+    cron.stop();
+    ownEnd = System.currentTimeMillis();
+    ownDelta += ownEnd - ownStart;
+    sleep(200);
+
+    cron.start();
+    ownStart = System.currentTimeMillis();
+    sleep(200);
+
+    cron.stop();
+    ownEnd = System.currentTimeMillis();
+    ownDelta += ownEnd - ownStart;
+    totalEnd = ownEnd;
+    totalDelta = totalEnd - totalStart;
+
+    avgTotal = (avgTotal * 1 + totalDelta) / 2;
+    avgOwn = (avgOwn * 1 + ownDelta) / 2;
+
+    timer.addCron(cron);
+    values = timer.getValues();
+    Assert.assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);
+
+    avgTotal = totalDelta;
+    avgOwn = ownDelta;
+
+    cron = new InstrumentationService.Cron();
+
+    cron.start();
+    ownStart = System.currentTimeMillis();
+    totalStart = ownStart;
+    ownDelta = 0;
+    sleep(300);
+
+    cron.stop();
+    ownEnd = System.currentTimeMillis();
+    ownDelta += ownEnd - ownStart;
+    sleep(300);
+
+    cron.start();
+    ownStart = System.currentTimeMillis();
+    sleep(300);
+
+    cron.stop();
+    ownEnd = System.currentTimeMillis();
+    ownDelta += ownEnd - ownStart;
+    totalEnd = ownEnd;
+    totalDelta = totalEnd - totalStart;
+
+    avgTotal = (avgTotal * 1 + totalDelta) / 2;
+    avgOwn = (avgOwn * 1 + ownDelta) / 2;
+
+    cron.stop();
+    timer.addCron(cron);
+    values = timer.getValues();
+    Assert.assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
+    Assert.assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);
+
+    JSONObject json = (JSONObject) new JSONParser().parse(timer.toJSONString());
+    Assert.assertEquals(json.size(), 4);
+    Assert.assertEquals(json.get("lastTotal"), values[InstrumentationService.Timer.LAST_TOTAL]);
+    Assert.assertEquals(json.get("lastOwn"), values[InstrumentationService.Timer.LAST_OWN]);
+    Assert.assertEquals(json.get("avgTotal"), values[InstrumentationService.Timer.AVG_TOTAL]);
+    Assert.assertEquals(json.get("avgOwn"), values[InstrumentationService.Timer.AVG_OWN]);
+
+    StringWriter writer = new StringWriter();
+    timer.writeJSONString(writer);
+    writer.close();
+    json = (JSONObject) new JSONParser().parse(writer.toString());
+    Assert.assertEquals(json.size(), 4);
+    Assert.assertEquals(json.get("lastTotal"), values[InstrumentationService.Timer.LAST_TOTAL]);
+    Assert.assertEquals(json.get("lastOwn"), values[InstrumentationService.Timer.LAST_OWN]);
+    Assert.assertEquals(json.get("avgTotal"), values[InstrumentationService.Timer.AVG_TOTAL]);
+    Assert.assertEquals(json.get("avgOwn"), values[InstrumentationService.Timer.AVG_OWN]);
+  }
+
+  @Test
+  public void sampler() throws Exception {
+    final long value[] = new long[1];
+    Instrumentation.Variable<Long> var = new Instrumentation.Variable<Long>() {
+      @Override
+      public Long getValue() {
+        return value[0];
+      }
+    };
+
+    InstrumentationService.Sampler sampler = new InstrumentationService.Sampler();
+    sampler.init(4, var);
+    Assert.assertEquals(sampler.getRate(), 0f, 0.0001);
+    sampler.sample();
+    Assert.assertEquals(sampler.getRate(), 0f, 0.0001);
+    value[0] = 1;
+    sampler.sample();
+    Assert.assertEquals(sampler.getRate(), (0d + 1) / 2, 0.0001);
+    value[0] = 2;
+    sampler.sample();
+    Assert.assertEquals(sampler.getRate(), (0d + 1 + 2) / 3, 0.0001);
+    value[0] = 3;
+    sampler.sample();
+    Assert.assertEquals(sampler.getRate(), (0d + 1 + 2 + 3) / 4, 0.0001);
+    value[0] = 4;
+    sampler.sample();
+    Assert.assertEquals(sampler.getRate(), (4d + 1 + 2 + 3) / 4, 0.0001);
+
+    JSONObject json = (JSONObject) new JSONParser().parse(sampler.toJSONString());
+    Assert.assertEquals(json.size(), 2);
+    Assert.assertEquals(json.get("sampler"), sampler.getRate());
+    Assert.assertEquals(json.get("size"), 4L);
+
+    StringWriter writer = new StringWriter();
+    sampler.writeJSONString(writer);
+    writer.close();
+    json = (JSONObject) new JSONParser().parse(writer.toString());
+    Assert.assertEquals(json.size(), 2);
+    Assert.assertEquals(json.get("sampler"), sampler.getRate());
+    Assert.assertEquals(json.get("size"), 4L);
+  }
+
+  @Test
+  public void variableHolder() throws Exception {
+    InstrumentationService.VariableHolder<String> variableHolder =
+      new InstrumentationService.VariableHolder<String>();
+
+    variableHolder.var = new Instrumentation.Variable<String>() {
+      @Override
+      public String getValue() {
+        return "foo";
+      }
+    };
+
+    JSONObject json = (JSONObject) new JSONParser().parse(variableHolder.toJSONString());
+    Assert.assertEquals(json.size(), 1);
+    Assert.assertEquals(json.get("value"), "foo");
+
+    StringWriter writer = new StringWriter();
+    variableHolder.writeJSONString(writer);
+    writer.close();
+    json = (JSONObject) new JSONParser().parse(writer.toString());
+    Assert.assertEquals(json.size(), 1);
+    Assert.assertEquals(json.get("value"), "foo");
+  }
+
+  @Test
+  @TestDir
+  @SuppressWarnings("unchecked")
+  public void service() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+
+    Instrumentation instrumentation = server.get(Instrumentation.class);
+    Assert.assertNotNull(instrumentation);
+    instrumentation.incr("g", "c", 1);
+    instrumentation.incr("g", "c", 2);
+    instrumentation.incr("g", "c1", 2);
+
+    Instrumentation.Cron cron = instrumentation.createCron();
+    cron.start();
+    sleep(100);
+    cron.stop();
+    instrumentation.addCron("g", "t", cron);
+    cron = instrumentation.createCron();
+    cron.start();
+    sleep(200);
+    cron.stop();
+    instrumentation.addCron("g", "t", cron);
+
+    Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {
+      @Override
+      public String getValue() {
+        return "foo";
+      }
+    };
+    instrumentation.addVariable("g", "v", var);
+
+    Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
+      @Override
+      public Long getValue() {
+        return 1L;
+      }
+    };
+    instrumentation.addSampler("g", "s", 10, varToSample);
+
+    Map<String, ?> snapshot = instrumentation.getSnapshot();
+    Assert.assertNotNull(snapshot.get("os-env"));
+    Assert.assertNotNull(snapshot.get("sys-props"));
+    Assert.assertNotNull(snapshot.get("jvm"));
+    Assert.assertNotNull(snapshot.get("counters"));
+    Assert.assertNotNull(snapshot.get("timers"));
+    Assert.assertNotNull(snapshot.get("variables"));
+    Assert.assertNotNull(snapshot.get("samplers"));
+    Assert.assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
+    Assert.assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
+    Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
+    Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
+    Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
+    Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));
+
+    StringWriter writer = new StringWriter();
+    JSONObject.writeJSONString(snapshot, writer);
+    writer.close();
+    server.destroy();
+  }
+
+  @Test
+  @TestDir
+  @SuppressWarnings("unchecked")
+  public void sampling() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                          SchedulerService.class.getName()));
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", services);
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Instrumentation instrumentation = server.get(Instrumentation.class);
+
+    final AtomicInteger count = new AtomicInteger();
+
+    Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
+      @Override
+      public Long getValue() {
+        return (long) count.incrementAndGet();
+      }
+    };
+    instrumentation.addSampler("g", "s", 10, varToSample);
+
+    sleep(2000);
+    int i = count.get();
+    Assert.assertTrue(i > 0);
+
+    Map<String, Map<String, ?>> snapshot = instrumentation.getSnapshot();
+    Map<String, Map<String, Object>> samplers = (Map<String, Map<String, Object>>) snapshot.get("samplers");
+    InstrumentationService.Sampler sampler = (InstrumentationService.Sampler) samplers.get("g").get("s");
+    Assert.assertTrue(sampler.getRate() > 0);
+
+    server.destroy();
+  }
+
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/scheduler/TestSchedulerService.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/scheduler/TestSchedulerService.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/scheduler/TestSchedulerService.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/scheduler/TestSchedulerService.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.service.scheduler;
+
+import junit.framework.Assert;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.lib.server.Server;
+import org.apache.hadoop.lib.service.Scheduler;
+import org.apache.hadoop.lib.service.instrumentation.InstrumentationService;
+import org.apache.hadoop.test.HTestCase;
+import org.apache.hadoop.test.TestDir;
+import org.apache.hadoop.test.TestDirHelper;
+import org.apache.hadoop.util.StringUtils;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+public class TestSchedulerService extends HTestCase {
+
+  @Test
+  @TestDir
+  public void service() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName(),
+                                                                    SchedulerService.class.getName())));
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Assert.assertNotNull(server.get(Scheduler.class));
+    server.destroy();
+  }
+
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestGroupsService.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestGroupsService.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestGroupsService.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestGroupsService.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.service.security;
+
+import junit.framework.Assert;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.lib.server.Server;
+import org.apache.hadoop.lib.service.Groups;
+import org.apache.hadoop.test.HTestCase;
+import org.apache.hadoop.test.TestDir;
+import org.apache.hadoop.test.TestDirHelper;
+import org.apache.hadoop.util.StringUtils;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class TestGroupsService extends HTestCase {
+
+  @Test
+  @TestDir
+  public void service() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName())));
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Groups groups = server.get(Groups.class);
+    Assert.assertNotNull(groups);
+    List<String> g = groups.getGroups(System.getProperty("user.name"));
+    Assert.assertNotSame(g.size(), 0);
+    server.destroy();
+  }
+
+  @Test(expected = RuntimeException.class)
+  @TestDir
+  public void invalidGroupsMapping() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName())));
+    conf.set("server.groups.hadoop.security.group.mapping", String.class.getName());
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestProxyUserService.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestProxyUserService.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestProxyUserService.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/service/security/TestProxyUserService.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,225 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.service.security;
+
+import junit.framework.Assert;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.lib.server.Server;
+import org.apache.hadoop.lib.server.ServiceException;
+import org.apache.hadoop.lib.service.Groups;
+import org.apache.hadoop.lib.service.ProxyUser;
+import org.apache.hadoop.test.HTestCase;
+import org.apache.hadoop.test.TestDir;
+import org.apache.hadoop.test.TestDirHelper;
+import org.apache.hadoop.test.TestException;
+import org.apache.hadoop.util.StringUtils;
+import org.junit.Test;
+
+import java.security.AccessControlException;
+import java.util.Arrays;
+import java.util.List;
+
+public class TestProxyUserService extends HTestCase {
+
+  @Test
+  @TestDir
+  public void service() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    server.destroy();
+  }
+
+  @Test
+  @TestException(exception = ServiceException.class, msgRegExp = "PRXU02.*")
+  @TestDir
+  public void wrongConfigGroups() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServiceException.class, msgRegExp = "PRXU01.*")
+  @TestDir
+  public void wrongHost() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "otherhost");
+    conf.set("server.proxyuser.foo.groups", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestException(exception = ServiceException.class, msgRegExp = "PRXU02.*")
+  @TestDir
+  public void wrongConfigHosts() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.groups", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+  }
+
+  @Test
+  @TestDir
+  public void validateAnyHostAnyUser() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "*");
+    conf.set("server.proxyuser.foo.groups", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    proxyUser.validate("foo", "localhost", "bar");
+    server.destroy();
+  }
+
+  @Test(expected = AccessControlException.class)
+  @TestDir
+  public void invalidProxyUser() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "*");
+    conf.set("server.proxyuser.foo.groups", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    proxyUser.validate("bar", "localhost", "foo");
+    server.destroy();
+  }
+
+  @Test
+  @TestDir
+  public void validateHost() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "localhost");
+    conf.set("server.proxyuser.foo.groups", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    proxyUser.validate("foo", "localhost", "bar");
+    server.destroy();
+  }
+
+  private String getGroup() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName())));
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    Groups groups = server.get(Groups.class);
+    List<String> g = groups.getGroups(System.getProperty("user.name"));
+    server.destroy();
+    return g.get(0);
+  }
+
+  @Test
+  @TestDir
+  public void validateGroup() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "*");
+    conf.set("server.proxyuser.foo.groups", getGroup());
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    proxyUser.validate("foo", "localhost", System.getProperty("user.name"));
+    server.destroy();
+  }
+
+
+  @Test(expected = AccessControlException.class)
+  @TestDir
+  public void unknownHost() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "localhost");
+    conf.set("server.proxyuser.foo.groups", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    proxyUser.validate("foo", "unknownhost.bar.foo", "bar");
+    server.destroy();
+  }
+
+  @Test(expected = AccessControlException.class)
+  @TestDir
+  public void invalidHost() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "localhost");
+    conf.set("server.proxyuser.foo.groups", "*");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    proxyUser.validate("foo", "www.yahoo.com", "bar");
+    server.destroy();
+  }
+
+  @Test(expected = AccessControlException.class)
+  @TestDir
+  public void invalidGroup() throws Exception {
+    String dir = TestDirHelper.getTestDir().getAbsolutePath();
+    Configuration conf = new Configuration(false);
+    conf.set("server.services", StringUtils.join(",", Arrays.asList(GroupsService.class.getName(),
+                                                                    ProxyUserService.class.getName())));
+    conf.set("server.proxyuser.foo.hosts", "localhost");
+    conf.set("server.proxyuser.foo.groups", "nobody");
+    Server server = new Server("server", dir, dir, dir, dir, conf);
+    server.init();
+    ProxyUser proxyUser = server.get(ProxyUser.class);
+    Assert.assertNotNull(proxyUser);
+    proxyUser.validate("foo", "localhost", System.getProperty("user.name"));
+    server.destroy();
+  }
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.servlet;
+
+import junit.framework.Assert;
+import org.apache.hadoop.test.HTestCase;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+
+public class TestHostnameFilter extends HTestCase {
+
+  @Test
+  public void hostname() throws Exception {
+    ServletRequest request = Mockito.mock(ServletRequest.class);
+    Mockito.when(request.getRemoteAddr()).thenReturn("localhost");
+
+    ServletResponse response = Mockito.mock(ServletResponse.class);
+
+    final AtomicBoolean invoked = new AtomicBoolean();
+
+    FilterChain chain = new FilterChain() {
+      @Override
+      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
+        throws IOException, ServletException {
+        Assert.assertEquals(HostnameFilter.get(), "localhost");
+        invoked.set(true);
+      }
+    };
+
+    Filter filter = new HostnameFilter();
+    filter.init(null);
+    Assert.assertNull(HostnameFilter.get());
+    filter.doFilter(request, response, chain);
+    Assert.assertTrue(invoked.get());
+    Assert.assertNull(HostnameFilter.get());
+    filter.destroy();
+  }
+
+}

Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestMDCFilter.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestMDCFilter.java?rev=1212060&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestMDCFilter.java (added)
+++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestMDCFilter.java Thu Dec  8 19:25:28 2011
@@ -0,0 +1,117 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.lib.servlet;
+
+import junit.framework.Assert;
+import org.apache.hadoop.test.HTestCase;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.slf4j.MDC;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.security.Principal;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+
+public class TestMDCFilter extends HTestCase {
+
+  @Test
+  public void mdc() throws Exception {
+    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+    Mockito.when(request.getUserPrincipal()).thenReturn(null);
+    Mockito.when(request.getMethod()).thenReturn("METHOD");
+    Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");
+
+    ServletResponse response = Mockito.mock(ServletResponse.class);
+
+    final AtomicBoolean invoked = new AtomicBoolean();
+
+    FilterChain chain = new FilterChain() {
+      @Override
+      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
+        throws IOException, ServletException {
+        Assert.assertEquals(MDC.get("hostname"), null);
+        Assert.assertEquals(MDC.get("user"), null);
+        Assert.assertEquals(MDC.get("method"), "METHOD");
+        Assert.assertEquals(MDC.get("path"), "/pathinfo");
+        invoked.set(true);
+      }
+    };
+
+    MDC.clear();
+    Filter filter = new MDCFilter();
+    filter.init(null);
+
+    filter.doFilter(request, response, chain);
+    Assert.assertTrue(invoked.get());
+    Assert.assertNull(MDC.get("hostname"));
+    Assert.assertNull(MDC.get("user"));
+    Assert.assertNull(MDC.get("method"));
+    Assert.assertNull(MDC.get("path"));
+
+    Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {
+      @Override
+      public String getName() {
+        return "name";
+      }
+    });
+
+    invoked.set(false);
+    chain = new FilterChain() {
+      @Override
+      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
+        throws IOException, ServletException {
+        Assert.assertEquals(MDC.get("hostname"), null);
+        Assert.assertEquals(MDC.get("user"), "name");
+        Assert.assertEquals(MDC.get("method"), "METHOD");
+        Assert.assertEquals(MDC.get("path"), "/pathinfo");
+        invoked.set(true);
+      }
+    };
+    filter.doFilter(request, response, chain);
+    Assert.assertTrue(invoked.get());
+
+    HostnameFilter.HOSTNAME_TL.set("HOST");
+
+    invoked.set(false);
+    chain = new FilterChain() {
+      @Override
+      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
+        throws IOException, ServletException {
+        Assert.assertEquals(MDC.get("hostname"), "HOST");
+        Assert.assertEquals(MDC.get("user"), "name");
+        Assert.assertEquals(MDC.get("method"), "METHOD");
+        Assert.assertEquals(MDC.get("path"), "/pathinfo");
+        invoked.set(true);
+      }
+    };
+    filter.doFilter(request, response, chain);
+    Assert.assertTrue(invoked.get());
+
+    HostnameFilter.HOSTNAME_TL.remove();
+
+    filter.destroy();
+  }
+}



Mime
View raw message