Search in sources :

Example 71 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class ShiroAuthenticationServiceTest method setupPrincipalName.

private void setupPrincipalName(String expectedName) {
    PowerMockito.mockStatic(org.apache.shiro.SecurityUtils.class);
    when(org.apache.shiro.SecurityUtils.getSubject()).thenReturn(subject);
    when(subject.isAuthenticated()).thenReturn(true);
    when(subject.getPrincipal()).thenReturn(new TestPrincipal(expectedName));
    Notebook notebook = Mockito.mock(Notebook.class);
    when(notebook.getConf()).thenReturn(ZeppelinConfiguration.create("zeppelin-site.xml"));
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook)

Example 72 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class RecoveryTest method testRecovery_2.

@Test
public void testRecovery_2() throws Exception {
    LOG.info("Test testRecovery_2");
    String note1Id = null;
    try {
        note1Id = notebook.createNote("note2", AuthenticationInfo.ANONYMOUS);
        // run python interpreter and create new variable `user`
        notebook.processNote(note1Id, note1 -> {
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%python user='abc'");
            return null;
        });
        CloseableHttpResponse post = httpPost("/notebook/job/" + note1Id + "?blocking=true", "");
        assertThat(post, isAllowed());
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        assertEquals("OK", resp.get("status"));
        post.close();
        notebook.processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            assertEquals(Job.Status.FINISHED, p1.getStatus());
            TestUtils.getInstance(Notebook.class).saveNote(note1, AuthenticationInfo.ANONYMOUS);
            // restart the python interpreter
            try {
                TestUtils.getInstance(Notebook.class).getInterpreterSettingManager().restart(((ManagedInterpreterGroup) p1.getBindedInterpreter().getInterpreterGroup()).getInterpreterSetting().getId());
            } catch (InterpreterException e) {
                fail();
            }
            return null;
        });
        // shutdown zeppelin and restart it
        shutDown();
        startUp(RecoveryTest.class.getSimpleName(), false);
        Thread.sleep(5 * 1000);
        // run the paragraph again, but change the text to print variable `user`.
        // can not recover the python interpreter, because it has been shutdown.
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            p1.setText("%python print(user)");
            return null;
        });
        post = httpPost("/notebook/job/" + note1Id + "?blocking=true", "");
        assertEquals("OK", resp.get("status"));
        post.close();
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            assertEquals(Job.Status.ERROR, p1.getStatus());
            return null;
        });
    } catch (Exception e) {
        LOG.error(e.toString(), e);
        throw e;
    } finally {
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) Paragraph(org.apache.zeppelin.notebook.Paragraph) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) Test(org.junit.Test)

Example 73 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class RecoveryTest method testRecovery_Running_Paragraph_sh.

@Test
public void testRecovery_Running_Paragraph_sh() throws Exception {
    LOG.info("Test testRecovery_Running_Paragraph_sh");
    String note1Id = null;
    try {
        note1Id = TestUtils.getInstance(Notebook.class).createNote("note4", AuthenticationInfo.ANONYMOUS);
        Paragraph p1 = TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            return note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        });
        p1.setText("%sh sleep 10\necho 'hello'");
        // run sh paragraph async, print 'hello' after 10 seconds
        CloseableHttpResponse post = httpPost("/notebook/job/" + note1Id + "/" + p1.getId(), "");
        assertThat(post, isAllowed());
        post.close();
        long start = System.currentTimeMillis();
        // wait until paragraph is RUNNING
        while ((System.currentTimeMillis() - start) < 10 * 1000) {
            if (p1.getStatus() == Job.Status.RUNNING) {
                break;
            }
            Thread.sleep(1000);
        }
        if (p1.getStatus() != Job.Status.RUNNING) {
            fail("Fail to run paragraph: " + p1.getReturn());
        }
        // shutdown zeppelin and restart it
        shutDown();
        startUp(RecoveryTest.class.getSimpleName(), false);
        // wait until paragraph is finished
        start = System.currentTimeMillis();
        while ((System.currentTimeMillis() - start) < 10 * 1000) {
            if (p1.isTerminated()) {
                break;
            }
            Thread.sleep(1000);
        }
        assertEquals(Job.Status.FINISHED, p1.getStatus());
        assertEquals("hello\n", p1.getReturn().message().get(0).getData());
        Thread.sleep(5 * 1000);
    } catch (Exception e) {
        LOG.error(e.toString(), e);
        throw e;
    } finally {
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 74 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class NotebookRestApiTest method testRunParagraphJob.

@Test
public void testRunParagraphJob() throws Exception {
    LOG.info("Running testRunParagraphJob");
    String note1Id = null;
    try {
        note1Id = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
        Paragraph p = TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            return note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        });
        // run blank paragraph
        CloseableHttpResponse post = httpPost("/notebook/job/" + note1Id + "/" + p.getId(), "");
        assertThat(post, isAllowed());
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        assertEquals("OK", resp.get("status"));
        post.close();
        p.waitUntilFinished();
        assertEquals(Job.Status.FINISHED, p.getStatus());
        // run non-blank paragraph
        p.setText("test");
        post = httpPost("/notebook/job/" + note1Id + "/" + p.getId(), "");
        assertThat(post, isAllowed());
        resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        assertEquals("OK", resp.get("status"));
        post.close();
        p.waitUntilFinished();
        assertNotEquals(Job.Status.FINISHED, p.getStatus());
    } finally {
        // cleanup
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HashMap(java.util.HashMap) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 75 with Notebook

use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.

the class NotebookRestApiTest method testRunWithServerRestart.

@Test
public void testRunWithServerRestart() throws Exception {
    LOG.info("Running testRunWithServerRestart");
    String note1Id = null;
    try {
        note1Id = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
        // 2 paragraphs
        // P1:
        // %python
        // from __future__ import print_function
        // import time
        // time.sleep(1)
        // user='abc'
        // P2:
        // %python
        // print(user)
        // 
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Paragraph p2 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%python from __future__ import print_function\nimport time\ntime.sleep(1)\nuser='abc'");
            p2.setText("%python print(user)");
            return null;
        });
        CloseableHttpResponse post1 = httpPost("/notebook/job/" + note1Id + "?blocking=true", "");
        assertThat(post1, isAllowed());
        post1.close();
        CloseableHttpResponse put = httpPut("/notebook/" + note1Id + "/clear", "");
        LOG.info("test clear paragraph output response\n" + EntityUtils.toString(put.getEntity(), StandardCharsets.UTF_8));
        assertThat(put, isAllowed());
        put.close();
        // restart server (while keeping interpreter configuration)
        AbstractTestRestApi.shutDown(false);
        startUp(NotebookRestApiTest.class.getSimpleName(), false);
        CloseableHttpResponse post2 = httpPost("/notebook/job/" + note1Id + "?blocking=true", "");
        assertThat(post2, isAllowed());
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post2.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        assertEquals("OK", resp.get("status"));
        post2.close();
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            Paragraph p2 = note1.getParagraph(1);
            assertEquals(Job.Status.FINISHED, p1.getStatus());
            assertEquals(p2.getReturn().toString(), Job.Status.FINISHED, p2.getStatus());
            assertNotNull(p2.getReturn());
            assertEquals("abc\n", p2.getReturn().message().get(0).getData());
            return null;
        });
    } finally {
        // cleanup
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Aggregations

Notebook (org.apache.zeppelin.notebook.Notebook)79 Test (org.junit.Test)53 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)48 Paragraph (org.apache.zeppelin.notebook.Paragraph)43 TypeToken (com.google.gson.reflect.TypeToken)33 Map (java.util.Map)25 HashMap (java.util.HashMap)24 Note (org.apache.zeppelin.notebook.Note)20 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)12 AuthorizationService (org.apache.zeppelin.notebook.AuthorizationService)12 Before (org.junit.Before)12 ZeppelinConfiguration (org.apache.zeppelin.conf.ZeppelinConfiguration)11 List (java.util.List)10 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)10 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)9 BeforeClass (org.junit.BeforeClass)9 InterpreterSettingManager (org.apache.zeppelin.interpreter.InterpreterSettingManager)8 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)7 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)7