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"));
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
Aggregations