Search in sources :

Example 6 with HttpTestClient

use of org.apache.flink.runtime.webmonitor.testutils.HttpTestClient in project flink by apache.

the class WebRuntimeMonitorITCase method testNoEscape.

// ------------------------------------------------------------------------
// Tests that access outside of the web root is not allowed
// ------------------------------------------------------------------------
/**
	 * Files are copied from the flink-dist jar to a temporary directory and
	 * then served from there. Only allow to access files in this temporary
	 * directory.
	 */
@Test
public void testNoEscape() throws Exception {
    final Deadline deadline = TestTimeout.fromNow();
    TestingCluster flink = null;
    WebRuntimeMonitor webMonitor = null;
    try {
        flink = new TestingCluster(new Configuration());
        flink.start(true);
        webMonitor = startWebRuntimeMonitor(flink);
        try (HttpTestClient client = new HttpTestClient("localhost", webMonitor.getServerPort())) {
            String expectedIndex = new Scanner(new File(MAIN_RESOURCES_PATH + "/index.html")).useDelimiter("\\A").next();
            // 1) Request index.html from web server
            client.sendGetRequest("index.html", deadline.timeLeft());
            HttpTestClient.SimpleHttpResponse response = client.getNextResponse(deadline.timeLeft());
            assertEquals(HttpResponseStatus.OK, response.getStatus());
            assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("html"));
            assertEquals(expectedIndex, response.getContent());
            // 2) Request file outside of web root
            // Create a test file in the web base dir (parent of web root)
            File illegalFile = new File(webMonitor.getBaseDir(new Configuration()), "test-file-" + UUID.randomUUID());
            illegalFile.deleteOnExit();
            assertTrue("Failed to create test file", illegalFile.createNewFile());
            // Request the created file from the web server
            client.sendGetRequest("../" + illegalFile.getName(), deadline.timeLeft());
            response = client.getNextResponse(deadline.timeLeft());
            assertEquals("Unexpected status code " + response.getStatus() + " for file outside of web root.", HttpResponseStatus.NOT_FOUND, response.getStatus());
            // 3) Request non-existing file
            client.sendGetRequest("not-existing-resource", deadline.timeLeft());
            response = client.getNextResponse(deadline.timeLeft());
            assertEquals("Unexpected status code " + response.getStatus() + " for file outside of web root.", HttpResponseStatus.NOT_FOUND, response.getStatus());
        }
    } finally {
        if (flink != null) {
            flink.shutdown();
        }
        if (webMonitor != null) {
            webMonitor.stop();
        }
    }
}
Also used : Scanner(java.util.Scanner) HttpTestClient(org.apache.flink.runtime.webmonitor.testutils.HttpTestClient) TestingCluster(org.apache.flink.runtime.testingUtils.TestingCluster) Configuration(org.apache.flink.configuration.Configuration) Deadline(scala.concurrent.duration.Deadline) File(java.io.File) Test(org.junit.Test)

Example 7 with HttpTestClient

use of org.apache.flink.runtime.webmonitor.testutils.HttpTestClient in project flink by apache.

the class WebFrontendITCase method testStop.

@Test
public void testStop() throws Exception {
    // this only works if there is no active job at this point
    assertTrue(cluster.getCurrentlyRunningJobsJava().isEmpty());
    // Create a task
    final JobVertex sender = new JobVertex("Sender");
    sender.setParallelism(2);
    sender.setInvokableClass(StoppableInvokable.class);
    final JobGraph jobGraph = new JobGraph("Stoppable streaming test job", sender);
    final JobID jid = jobGraph.getJobID();
    cluster.submitJobDetached(jobGraph);
    // wait for job to show up
    while (cluster.getCurrentlyRunningJobsJava().isEmpty()) {
        Thread.sleep(10);
    }
    final FiniteDuration testTimeout = new FiniteDuration(2, TimeUnit.MINUTES);
    final Deadline deadline = testTimeout.fromNow();
    while (!cluster.getCurrentlyRunningJobsJava().isEmpty()) {
        try (HttpTestClient client = new HttpTestClient("localhost", port)) {
            // Request the file from the web server
            client.sendDeleteRequest("/jobs/" + jid + "/stop", deadline.timeLeft());
            HttpTestClient.SimpleHttpResponse response = client.getNextResponse(deadline.timeLeft());
            assertEquals(HttpResponseStatus.OK, response.getStatus());
            assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("json"));
            assertEquals("{}", response.getContent());
        }
        Thread.sleep(20);
    }
    // ensure we can access job details when its finished (FLINK-4011)
    try (HttpTestClient client = new HttpTestClient("localhost", port)) {
        FiniteDuration timeout = new FiniteDuration(30, TimeUnit.SECONDS);
        client.sendGetRequest("/jobs/" + jid + "/config", timeout);
        HttpTestClient.SimpleHttpResponse response = client.getNextResponse(timeout);
        assertEquals(HttpResponseStatus.OK, response.getStatus());
        assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("json"));
        assertEquals("{\"jid\":\"" + jid + "\",\"name\":\"Stoppable streaming test job\"," + "\"execution-config\":{\"execution-mode\":\"PIPELINED\",\"restart-strategy\":\"default\"," + "\"job-parallelism\":-1,\"object-reuse-mode\":false,\"user-config\":{}}}", response.getContent());
    }
}
Also used : JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) HttpTestClient(org.apache.flink.runtime.webmonitor.testutils.HttpTestClient) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) Deadline(scala.concurrent.duration.Deadline) FiniteDuration(scala.concurrent.duration.FiniteDuration) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Aggregations

HttpTestClient (org.apache.flink.runtime.webmonitor.testutils.HttpTestClient)7 Test (org.junit.Test)7 Deadline (scala.concurrent.duration.Deadline)7 File (java.io.File)5 Configuration (org.apache.flink.configuration.Configuration)5 Scanner (java.util.Scanner)4 TestingCluster (org.apache.flink.runtime.testingUtils.TestingCluster)3 ActorSystem (akka.actor.ActorSystem)2 Path (java.nio.file.Path)2 TestingServer (org.apache.curator.test.TestingServer)2 JobID (org.apache.flink.api.common.JobID)2 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)2 JobVertex (org.apache.flink.runtime.jobgraph.JobVertex)2 LeaderRetrievalService (org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService)2 FiniteDuration (scala.concurrent.duration.FiniteDuration)2 ActorRef (akka.actor.ActorRef)1 ArrayList (java.util.ArrayList)1 TestingListener (org.apache.flink.runtime.leaderelection.TestingListener)1 Some (scala.Some)1