Search in sources :

Example 1 with NoteJobStatus

use of org.apache.zeppelin.rest.message.NoteJobStatus in project zeppelin by apache.

the class ZeppelinRestApiTest method testRegressionZEPPELIN_527.

@Test
public void testRegressionZEPPELIN_527() throws Exception {
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testRegressionZEPPELIN_527", anonymous);
        // use write lock because name is overwritten
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            note.setName("note for run test");
            Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            paragraph.setText("%spark\nval param = z.input(\"param\").toString\nprintln(param)");
            return null;
        });
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            try {
                note.runAll(AuthenticationInfo.ANONYMOUS, true, false, new HashMap<>());
                TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            } catch (Exception e) {
                fail();
            }
            return null;
        });
        CloseableHttpResponse getNoteJobs = httpGet("/notebook/job/" + noteId);
        assertThat("test note jobs run:", getNoteJobs, isAllowed());
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(getNoteJobs.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        NoteJobStatus noteJobStatus = NoteJobStatus.fromJson(gson.toJson(resp.get("body")));
        assertNotNull(noteJobStatus.getParagraphJobStatusList().get(0).getStarted());
        assertNotNull(noteJobStatus.getParagraphJobStatusList().get(0).getFinished());
        getNoteJobs.close();
    } finally {
        // cleanup
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
    }
}
Also used : NoteJobStatus(org.apache.zeppelin.rest.message.NoteJobStatus) Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 2 with NoteJobStatus

use of org.apache.zeppelin.rest.message.NoteJobStatus in project zeppelin by apache.

the class ZeppelinRestApiTest method testGetNoteJob.

@Test
public void testGetNoteJob() throws Exception {
    LOG.info("testGetNoteJob");
    String noteId = null;
    try {
        // Create note to run test.
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testGetNoteJob", anonymous);
        // use write lock because name is overwritten
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            assertNotNull("can't create new note", note);
            note.setName("note for run test");
            Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map<String, Object> config = paragraph.getConfig();
            config.put("enabled", true);
            paragraph.setConfig(config);
            paragraph.setText("%sh sleep 1");
            paragraph.setAuthenticationInfo(anonymous);
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return null;
        });
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            try {
                note.runAll(anonymous, true, false, new HashMap<>());
            } catch (Exception e) {
                fail();
            }
            return null;
        });
        // assume that status of the paragraph is running
        CloseableHttpResponse get = httpGet("/notebook/job/" + noteId);
        assertThat("test get note job: ", get, isAllowed());
        String responseBody = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
        get.close();
        LOG.info("test get note job: \n" + responseBody);
        Map<String, Object> resp = gson.fromJson(responseBody, new TypeToken<Map<String, Object>>() {
        }.getType());
        NoteJobStatus noteJobStatus = NoteJobStatus.fromJson(gson.toJson(resp.get("body")));
        assertEquals(1, noteJobStatus.getParagraphJobStatusList().size());
        int progress = Integer.parseInt(noteJobStatus.getParagraphJobStatusList().get(0).getProgress());
        assertTrue(progress >= 0 && progress <= 100);
        // wait until job is finished or timeout.
        int timeout = 1;
        boolean terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
        while (!terminated) {
            Thread.sleep(100);
            terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
            if (timeout++ > 10) {
                LOG.info("testGetNoteJob timeout job.");
                break;
            }
        }
    } finally {
        // cleanup
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
    }
}
Also used : MethodSorters(org.junit.runners.MethodSorters) TestUtils(org.apache.zeppelin.utils.TestUtils) Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) BeforeClass(org.junit.BeforeClass) NoteJobStatus(org.apache.zeppelin.rest.message.NoteJobStatus) HashMap(java.util.HashMap) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) StringUtils(org.apache.commons.lang3.StringUtils) EntityUtils(org.apache.http.util.EntityUtils) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) Gson(com.google.gson.Gson) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Map(java.util.Map) ConfVars(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) Paragraph(org.apache.zeppelin.notebook.Paragraph) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) Note(org.apache.zeppelin.notebook.Note) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Notebook(org.apache.zeppelin.notebook.Notebook) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) FixMethodOrder(org.junit.FixMethodOrder) Assert.assertEquals(org.junit.Assert.assertEquals) Notebook(org.apache.zeppelin.notebook.Notebook) IOException(java.io.IOException) Paragraph(org.apache.zeppelin.notebook.Paragraph) NoteJobStatus(org.apache.zeppelin.rest.message.NoteJobStatus) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Aggregations

TypeToken (com.google.gson.reflect.TypeToken)2 IOException (java.io.IOException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 Notebook (org.apache.zeppelin.notebook.Notebook)2 Paragraph (org.apache.zeppelin.notebook.Paragraph)2 NoteJobStatus (org.apache.zeppelin.rest.message.NoteJobStatus)2 Test (org.junit.Test)2 Gson (com.google.gson.Gson)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 StringUtils (org.apache.commons.lang3.StringUtils)1 EntityUtils (org.apache.http.util.EntityUtils)1 ConfVars (org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars)1 AuthorizationService (org.apache.zeppelin.notebook.AuthorizationService)1 Note (org.apache.zeppelin.notebook.Note)1