Search in sources :

Example 51 with TestDir

use of org.apache.hadoop.test.TestDir in project hadoop by apache.

the class TestServer method services.

@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();
    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();
    assertEquals(server.get(MyService1.class).getInterface(), MyService1.class);
    assertEquals(server.get(MyService3.class).getInterface(), MyService3.class);
    assertEquals(ORDER.size(), 4);
    assertEquals(ORDER.get(0), "s1.init");
    assertEquals(ORDER.get(1), "s3.init");
    assertEquals(ORDER.get(2), "s1.postInit");
    assertEquals(ORDER.get(3), "s3.postInit");
    server.destroy();
    assertEquals(ORDER.size(), 6);
    assertEquals(ORDER.get(4), "s3.destroy");
    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();
        fail();
    } catch (ServerException ex) {
        assertEquals(MyService2.class, ex.getError().getClass());
    } catch (Exception ex) {
        fail();
    }
    assertEquals(ORDER.size(), 3);
    assertEquals(ORDER.get(0), "s1.init");
    assertEquals(ORDER.get(1), "s2.init");
    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();
    assertEquals(ORDER.size(), 4);
    assertEquals(ORDER.get(0), "s1.init");
    assertEquals(ORDER.get(1), "s5.init");
    assertEquals(ORDER.get(2), "s1.postInit");
    assertEquals(ORDER.get(3), "s5.postInit");
    server.destroy();
    assertEquals(ORDER.size(), 6);
    assertEquals(ORDER.get(4), "s5.destroy");
    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();
    assertEquals(server.get(MyService1.class).getClass(), MyService1a.class);
    assertEquals(ORDER.size(), 4);
    assertEquals(ORDER.get(0), "s1a.init");
    assertEquals(ORDER.get(1), "s3.init");
    assertEquals(ORDER.get(2), "s1a.postInit");
    assertEquals(ORDER.get(3), "s3.postInit");
    server.destroy();
    assertEquals(ORDER.size(), 6);
    assertEquals(ORDER.get(4), "s3.destroy");
    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);
    assertEquals(ORDER.size(), 6);
    assertEquals(ORDER.get(4), "s1.destroy");
    assertEquals(ORDER.get(5), "s1a.init");
    assertEquals(server.get(MyService1.class).getClass(), MyService1a.class);
    server.destroy();
    assertEquals(ORDER.size(), 8);
    assertEquals(ORDER.get(6), "s3.destroy");
    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);
    assertEquals(ORDER.size(), 5);
    assertEquals(ORDER.get(4), "s5.init");
    assertEquals(server.get(MyService5.class).getClass(), MyService5.class);
    server.destroy();
    assertEquals(ORDER.size(), 8);
    assertEquals(ORDER.get(5), "s5.destroy");
    assertEquals(ORDER.get(6), "s3.destroy");
    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);
        fail();
    } catch (ServerException ex) {
        assertEquals(ServerException.ERROR.S09, ex.getError());
    } catch (Exception ex) {
        fail();
    }
    assertEquals(ORDER.size(), 6);
    assertEquals(ORDER.get(4), "s3.destroy");
    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();
    assertEquals(server.get(MyService1.class).getInterface(), MyService1.class);
    assertEquals(server.get(MyService6.class).getInterface(), MyService6.class);
    server.destroy();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) TestException(org.apache.hadoop.test.TestException) XException(org.apache.hadoop.lib.lang.XException) TestDir(org.apache.hadoop.test.TestDir) Test(org.junit.Test)

Example 52 with TestDir

use of org.apache.hadoop.test.TestDir in project hadoop by apache.

the class TestServer method setSameStatus.

@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());
    assertFalse(TestService.LIFECYCLE.contains("serverStatusChange"));
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) TestDir(org.apache.hadoop.test.TestDir) Test(org.junit.Test)

Example 53 with TestDir

use of org.apache.hadoop.test.TestDir in project hadoop by apache.

the class TestServer method loadingSiteConfig.

@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();
    assertEquals(server.getConfig().get("testserver.a"), "site");
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TestDir(org.apache.hadoop.test.TestDir) Test(org.junit.Test)

Example 54 with TestDir

use of org.apache.hadoop.test.TestDir in project hadoop by apache.

the class TestServer method initNoTempDir.

@Test
@TestException(exception = ServerException.class, msgRegExp = "S01.*")
@TestDir
public void initNoTempDir() throws Exception {
    File homeDir = new File(TestDirHelper.getTestDir(), "home");
    assertTrue(homeDir.mkdir());
    assertTrue(new File(homeDir, "conf").mkdir());
    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();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) File(java.io.File) TestException(org.apache.hadoop.test.TestException) TestDir(org.apache.hadoop.test.TestDir) Test(org.junit.Test)

Example 55 with TestDir

use of org.apache.hadoop.test.TestDir in project hadoop by apache.

the class TestServer method lifeCycle.

@Test
@TestDir
public void lifeCycle() throws Exception {
    Configuration conf = new Configuration(false);
    conf.set("server.services", LifeCycleService.class.getName());
    Server server = createServer(conf);
    assertEquals(server.getStatus(), Server.Status.UNDEF);
    server.init();
    assertNotNull(server.get(LifeCycleService.class));
    assertEquals(server.getStatus(), Server.Status.NORMAL);
    server.destroy();
    assertEquals(server.getStatus(), Server.Status.SHUTDOWN);
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) TestDir(org.apache.hadoop.test.TestDir) Test(org.junit.Test)

Aggregations

TestDir (org.apache.hadoop.test.TestDir)67 Test (org.junit.Test)67 Configuration (org.apache.hadoop.conf.Configuration)46 TestException (org.apache.hadoop.test.TestException)25 TestHdfs (org.apache.hadoop.test.TestHdfs)21 Server (org.apache.hadoop.lib.server.Server)19 TestJetty (org.apache.hadoop.test.TestJetty)18 FileSystem (org.apache.hadoop.fs.FileSystem)14 File (java.io.File)13 Path (org.apache.hadoop.fs.Path)13 FileOutputStream (java.io.FileOutputStream)10 FileSystemAccess (org.apache.hadoop.lib.service.FileSystemAccess)10 HttpURLConnection (java.net.HttpURLConnection)9 URL (java.net.URL)9 AuthenticatedURL (org.apache.hadoop.security.authentication.client.AuthenticatedURL)9 IOException (java.io.IOException)6 OutputStream (java.io.OutputStream)6 InputStreamReader (java.io.InputStreamReader)5 ServiceException (org.apache.hadoop.lib.server.ServiceException)4 FileSystemAccessException (org.apache.hadoop.lib.service.FileSystemAccessException)4