Search in sources :

Example 66 with ClientResponse

use of com.sun.jersey.api.client.ClientResponse in project hadoop by apache.

the class TestRMWebServicesApps method testAppsQueryStates.

@Test
public void testAppsQueryStates() throws JSONException, Exception {
    rm.start();
    MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
    rm.submitApp(CONTAINER_MB);
    RMApp killedApp = rm.submitApp(CONTAINER_MB);
    rm.killApp(killedApp.getApplicationId());
    amNodeManager.nodeHeartbeat(true);
    WebResource r = resource();
    MultivaluedMapImpl params = new MultivaluedMapImpl();
    params.add("states", YarnApplicationState.ACCEPTED.toString());
    ClientResponse response = r.path("ws").path("v1").path("cluster").path("apps").queryParams(params).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
    JSONObject json = response.getEntity(JSONObject.class);
    assertEquals("incorrect number of elements", 1, json.length());
    JSONObject apps = json.getJSONObject("apps");
    assertEquals("incorrect number of elements", 1, apps.length());
    JSONArray array = apps.getJSONArray("app");
    assertEquals("incorrect number of elements", 1, array.length());
    assertEquals("state not equal to ACCEPTED", "ACCEPTED", array.getJSONObject(0).getString("state"));
    r = resource();
    params = new MultivaluedMapImpl();
    params.add("states", YarnApplicationState.ACCEPTED.toString());
    params.add("states", YarnApplicationState.KILLED.toString());
    response = r.path("ws").path("v1").path("cluster").path("apps").queryParams(params).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
    json = response.getEntity(JSONObject.class);
    assertEquals("incorrect number of elements", 1, json.length());
    apps = json.getJSONObject("apps");
    assertEquals("incorrect number of elements", 1, apps.length());
    array = apps.getJSONArray("app");
    assertEquals("incorrect number of elements", 2, array.length());
    assertTrue("both app states of ACCEPTED and KILLED are not present", (array.getJSONObject(0).getString("state").equals("ACCEPTED") && array.getJSONObject(1).getString("state").equals("KILLED")) || (array.getJSONObject(0).getString("state").equals("KILLED") && array.getJSONObject(1).getString("state").equals("ACCEPTED")));
    rm.stop();
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) RMApp(org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp) JSONObject(org.codehaus.jettison.json.JSONObject) MockNM(org.apache.hadoop.yarn.server.resourcemanager.MockNM) JSONArray(org.codehaus.jettison.json.JSONArray) WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Test(org.junit.Test)

Example 67 with ClientResponse

use of com.sun.jersey.api.client.ClientResponse in project hadoop by apache.

the class TestRMWebServicesApps method testAppsQueryUser.

@Test
public void testAppsQueryUser() throws JSONException, Exception {
    rm.start();
    MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
    rm.submitApp(CONTAINER_MB);
    rm.submitApp(CONTAINER_MB);
    amNodeManager.nodeHeartbeat(true);
    WebResource r = resource();
    ClientResponse response = r.path("ws").path("v1").path("cluster").path("apps").queryParam("user", UserGroupInformation.getCurrentUser().getShortUserName()).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
    JSONObject json = response.getEntity(JSONObject.class);
    assertEquals("incorrect number of elements", 1, json.length());
    JSONObject apps = json.getJSONObject("apps");
    assertEquals("incorrect number of elements", 1, apps.length());
    JSONArray array = apps.getJSONArray("app");
    assertEquals("incorrect number of elements", 2, array.length());
    rm.stop();
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) JSONObject(org.codehaus.jettison.json.JSONObject) MockNM(org.apache.hadoop.yarn.server.resourcemanager.MockNM) JSONArray(org.codehaus.jettison.json.JSONArray) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Example 68 with ClientResponse

use of com.sun.jersey.api.client.ClientResponse in project hadoop by apache.

the class TestRMWebServicesApps method testAppsXMLMulti.

@Test
public void testAppsXMLMulti() throws JSONException, Exception {
    rm.start();
    MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
    rm.submitApp(CONTAINER_MB, "testwordcount", "user1");
    rm.submitApp(2048, "testwordcount2", "user1");
    amNodeManager.nodeHeartbeat(true);
    WebResource r = resource();
    ClientResponse response = r.path("ws").path("v1").path("cluster").path("apps").accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
    String xml = response.getEntity(String.class);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);
    NodeList nodesApps = dom.getElementsByTagName("apps");
    assertEquals("incorrect number of elements", 1, nodesApps.getLength());
    NodeList nodes = dom.getElementsByTagName("app");
    assertEquals("incorrect number of elements", 2, nodes.getLength());
    rm.stop();
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) MockNM(org.apache.hadoop.yarn.server.resourcemanager.MockNM) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) WebResource(com.sun.jersey.api.client.WebResource) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 69 with ClientResponse

use of com.sun.jersey.api.client.ClientResponse in project hadoop by apache.

the class TestRMWebServicesAppsModification method testSingleAppKillInvalidId.

@Test
public void testSingleAppKillInvalidId() throws Exception {
    rm.start();
    MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
    amNodeManager.nodeHeartbeat(true);
    String[] testAppIds = { "application_1391705042196_0001", "random_string" };
    for (int i = 0; i < testAppIds.length; i++) {
        AppState info = new AppState("KILLED");
        ClientResponse response = this.constructWebResource("apps", testAppIds[i], "state").accept(MediaType.APPLICATION_XML).entity(info, MediaType.APPLICATION_XML).put(ClientResponse.class);
        if (!isAuthenticationEnabled()) {
            assertResponseStatusCode(Status.UNAUTHORIZED, response.getStatusInfo());
            continue;
        }
        if (i == 0) {
            assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo());
        } else {
            assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo());
        }
    }
    rm.stop();
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) MockNM(org.apache.hadoop.yarn.server.resourcemanager.MockNM) RMAppState(org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppState) AppState(org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.AppState) Test(org.junit.Test)

Example 70 with ClientResponse

use of com.sun.jersey.api.client.ClientResponse in project hadoop by apache.

the class TestRMWebServicesDelegationTokens method testRenewDelegationToken.

// Test to verify renew functionality - create a token and then try to renew
// it. The renewer should succeed; owner and third user should fail
@Test
public void testRenewDelegationToken() throws Exception {
    client().addFilter(new LoggingFilter(System.out));
    rm.start();
    final String renewer = "client2";
    this.client().addFilter(new LoggingFilter(System.out));
    final DelegationToken dummyToken = new DelegationToken();
    dummyToken.setRenewer(renewer);
    String[] mediaTypes = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML };
    for (final String mediaType : mediaTypes) {
        for (final String contentType : mediaTypes) {
            if (isKerberosAuth == false) {
                verifySimpleAuthRenew(mediaType, contentType);
                continue;
            }
            // test "client" and client2" trying to renew "client" token
            final DelegationToken responseToken = KerberosTestUtils.doAsClient(new Callable<DelegationToken>() {

                @Override
                public DelegationToken call() throws Exception {
                    ClientResponse response = resource().path("ws").path("v1").path("cluster").path("delegation-token").accept(contentType).entity(dummyToken, mediaType).post(ClientResponse.class);
                    assertResponseStatusCode(Status.OK, response.getStatusInfo());
                    DelegationToken tok = getDelegationTokenFromResponse(response);
                    assertFalse(tok.getToken().isEmpty());
                    String body = generateRenewTokenBody(mediaType, tok.getToken());
                    response = resource().path("ws").path("v1").path("cluster").path("delegation-token").path("expiration").header(yarnTokenHeader, tok.getToken()).accept(contentType).entity(body, mediaType).post(ClientResponse.class);
                    assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo());
                    return tok;
                }
            });
            KerberosTestUtils.doAs(renewer, new Callable<DelegationToken>() {

                @Override
                public DelegationToken call() throws Exception {
                    // renew twice so that we can confirm that the
                    // expiration time actually changes
                    long oldExpirationTime = Time.now();
                    assertValidRMToken(responseToken.getToken());
                    String body = generateRenewTokenBody(mediaType, responseToken.getToken());
                    ClientResponse response = resource().path("ws").path("v1").path("cluster").path("delegation-token").path("expiration").header(yarnTokenHeader, responseToken.getToken()).accept(contentType).entity(body, mediaType).post(ClientResponse.class);
                    assertResponseStatusCode(Status.OK, response.getStatusInfo());
                    DelegationToken tok = getDelegationTokenFromResponse(response);
                    String message = "Expiration time not as expected: old = " + oldExpirationTime + "; new = " + tok.getNextExpirationTime();
                    assertTrue(message, tok.getNextExpirationTime() > oldExpirationTime);
                    oldExpirationTime = tok.getNextExpirationTime();
                    // artificial sleep to ensure we get a different expiration time
                    Thread.sleep(1000);
                    response = resource().path("ws").path("v1").path("cluster").path("delegation-token").path("expiration").header(yarnTokenHeader, responseToken.getToken()).accept(contentType).entity(body, mediaType).post(ClientResponse.class);
                    assertResponseStatusCode(Status.OK, response.getStatusInfo());
                    tok = getDelegationTokenFromResponse(response);
                    message = "Expiration time not as expected: old = " + oldExpirationTime + "; new = " + tok.getNextExpirationTime();
                    assertTrue(message, tok.getNextExpirationTime() > oldExpirationTime);
                    return tok;
                }
            });
            // test unauthorized user renew attempt
            KerberosTestUtils.doAs("client3", new Callable<DelegationToken>() {

                @Override
                public DelegationToken call() throws Exception {
                    String body = generateRenewTokenBody(mediaType, responseToken.getToken());
                    ClientResponse response = resource().path("ws").path("v1").path("cluster").path("delegation-token").path("expiration").header(yarnTokenHeader, responseToken.getToken()).accept(contentType).entity(body, mediaType).post(ClientResponse.class);
                    assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo());
                    return null;
                }
            });
            // test bad request - incorrect format, empty token string and random
            // token string
            KerberosTestUtils.doAsClient(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    String token = "TEST_TOKEN_STRING";
                    String body = "";
                    if (mediaType.equals(MediaType.APPLICATION_JSON)) {
                        body = "{\"token\": \"" + token + "\" }";
                    } else {
                        body = "<delegation-token><token>" + token + "</token></delegation-token>";
                    }
                    // missing token header
                    ClientResponse response = resource().path("ws").path("v1").path("cluster").path("delegation-token").path("expiration").accept(contentType).entity(body, mediaType).post(ClientResponse.class);
                    assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo());
                    return null;
                }
            });
        }
    }
    rm.stop();
    return;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) DelegationToken(org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.DelegationToken) LoggingFilter(com.sun.jersey.api.client.filter.LoggingFilter) ServletException(javax.servlet.ServletException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JSONException(org.codehaus.jettison.json.JSONException) Test(org.junit.Test)

Aggregations

ClientResponse (com.sun.jersey.api.client.ClientResponse)1970 Test (org.junit.Test)1136 WebResource (com.sun.jersey.api.client.WebResource)674 JSONObject (org.codehaus.jettison.json.JSONObject)412 URI (java.net.URI)292 Client (com.sun.jersey.api.client.Client)117 HashMap (java.util.HashMap)112 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)110 JobId (org.apache.hadoop.mapreduce.v2.api.records.JobId)103 ArrayList (java.util.ArrayList)97 JSONArray (org.codehaus.jettison.json.JSONArray)81 IOException (java.io.IOException)78 InputStream (java.io.InputStream)77 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)76 JSONException (org.codehaus.jettison.json.JSONException)58 List (java.util.List)56 Gson (com.google.gson.Gson)52 JavaResult (org.milyn.payload.JavaResult)52 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)49 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)47