use of org.folio.okapi.bean.Ports in project okapi by folio-org.
the class DockerModuleHandleTest method testGetCreateContainerDoc.
@Test
public void testGetCreateContainerDoc() {
LaunchDescriptor launchDescriptor = new LaunchDescriptor();
launchDescriptor.setEnv(new EnvEntry[] { new EnvEntry("username", "foobar"), new EnvEntry("password", "uvwxyz%p%c") });
launchDescriptor.setDockerArgs(new AnyDescriptor().set("%p", "%p"));
Logger logger = mock(Logger.class);
StringBuilder logMessage = new StringBuilder();
when(logger.isInfoEnabled()).thenReturn(true);
doAnswer(AdditionalAnswers.answerVoid((String msg, Object param) -> logMessage.append(msg).append(param.toString()))).when(logger).info(anyString(), any(Object.class));
DockerModuleHandle dockerModuleHandle = new DockerModuleHandle(Vertx.vertx(), launchDescriptor, "mod-users-5.0.0-SNAPSHOT", new Ports(9232, 9233), "localhost", 9232, new JsonObject(), logger);
assertThat(dockerModuleHandle.getCreateContainerDoc(8000)).contains("8000/tcp").contains(// dockerArgs variable expansion in values, not in keys
"\"%p\" : \"9232\"").contains("foobar").contains(// no %p or %c variable expansion in Env values
"uvwxyz%p%c");
assertThat(logMessage.toString()).contains("8000/tcp").contains("\"%p\" : \"9232\"").doesNotContain(// no env values in the log because they may contain credentials
"foobar").doesNotContain("uvwxyz");
Assert.assertEquals(launchDescriptor.getDockerArgs().properties().get("%p"), "%p");
}
use of org.folio.okapi.bean.Ports in project okapi by folio-org.
the class ProcessModuleHandleTest method testWaitPortClose.
@Test
public void testWaitPortClose(TestContext context) {
final NetServer ns = vertx.createNetServer().connectHandler(res -> {
res.close();
});
ns.listen(9231, context.asyncAssertSuccess(res -> {
LaunchDescriptor desc = new LaunchDescriptor();
desc.setExec("java " + testModuleArgs);
ProcessModuleHandle pmh = new ProcessModuleHandle(vertx, desc, "id", new Ports(9231, 9233), 9231, new JsonObject());
pmh.waitPortToClose(0).onComplete(context.asyncAssertFailure(x -> {
ns.close();
context.assertEquals("port 9231 not shut down", x.getMessage());
}));
}));
}
use of org.folio.okapi.bean.Ports in project okapi by folio-org.
the class DockerModuleHandleTest method testDockerMock.
@Test
public void testDockerMock(TestContext context) {
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
router.routeWithRegex("/.*").handler(this::dockerMockHandle);
Async async1 = context.async();
HttpServerOptions so = new HttpServerOptions().setHandle100ContinueAutomatically(true);
HttpServer listen = vertx.createHttpServer(so).requestHandler(router).listen(MOCK_PORT, context.asyncAssertSuccess(x -> async1.complete()));
async1.await();
LaunchDescriptor ld = new LaunchDescriptor();
ld.setWaitIterations(2);
ld.setDockerImage("folioci/mod-x");
ld.setDockerPull(true);
EnvEntry[] env = new EnvEntry[1];
env[0] = new EnvEntry();
env[0].setName("varName");
env[0].setValue("varValue");
ld.setEnv(env);
dockerPullJson = new JsonObject().put("message", "some message");
dockerPullStatus = 200;
String[] cmd = { "command" };
ld.setDockerCmd(cmd);
Ports ports = new Ports(9232, 9233);
JsonObject conf = new JsonObject().put("dockerUrl", "tcp://localhost:" + MOCK_PORT);
DockerModuleHandle dh = new DockerModuleHandle(vertx, ld, "mod-users-5.0.0-SNAPSHOT", ports, "localhost", // using also mock for virtual module
MOCK_PORT, conf);
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockText = "OK";
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertTrue(cause.getMessage().contains("Failed to decode"), cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockText = "{} 1";
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertTrue(cause.getMessage().contains("Unexpected trailing token"), cause.getMessage());
async.complete();
}));
async.await();
}
dockerPullStatus = 500;
{
Async async = context.async();
dockerMockStatus = 102;
dockerMockText = "Switch";
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertEquals("getImage HTTP error 102\n", cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 404;
dockerMockText = "NotHere";
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertEquals("getImage HTTP error 404\nNotHere", cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockText = "{}\n1";
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertEquals("Missing Config in image", cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockJson = new JsonObject();
dockerMockJson.put("Config", 1);
dh.start().onComplete(context.asyncAssertFailure(cause -> {
assertThat(cause.getMessage()).contains("class java.lang.Integer cannot be cast to class io.vertx.core.json.JsonObject");
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockJson = new JsonObject();
dockerMockJson.put("Config", new JsonObject().put("foo", 1));
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertEquals("Missing EXPOSE in image", cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockJson = new JsonObject();
dockerMockJson.put("Config", new JsonObject().put("foo", 1).put("ExposedPorts", new JsonObject()));
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertEquals("Missing EXPOSE in image", cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockJson = new JsonObject();
dockerMockJson.put("Config", new JsonObject().put("ExposedPorts", new JsonObject().put("notInteger", "a")));
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertEquals("For input string: \"notInteger\"", cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 200;
dockerMockJson = new JsonObject();
dockerMockJson.put("Config", new JsonObject().put("ExposedPorts", new JsonObject().put("1", "a").put("2", "b")));
dh.start().onComplete(context.asyncAssertFailure(cause -> {
context.assertTrue(cause.getMessage().contains("startContainer HTTP error 200"), cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerMockStatus = 400;
dockerMockJson = null;
dockerMockText = "User Error";
dh.createContainer(9232).onComplete(context.asyncAssertFailure(cause -> {
context.assertTrue(cause.getMessage().contains("createContainer HTTP error 400"), cause.getMessage());
async.complete();
}));
async.await();
}
{
Async async = context.async();
dockerEmptyStatus = 204;
dockerMockStatus = 200;
dockerMockJson = new JsonObject();
dockerMockJson.put("Config", new JsonObject().put("ExposedPorts", new JsonObject().put("8000", "a")));
dh.start().onComplete(context.asyncAssertSuccess(res1 -> dh.stop().onComplete(context.asyncAssertSuccess(res2 -> async.complete()))));
async.await();
}
{
Async async = context.async();
dockerEmptyStatus = 204;
dockerMockStatus = 400;
dh.getContainerLog().onComplete(context.asyncAssertFailure(cause -> {
context.assertEquals("getContainerLog HTTP error 400", cause.getMessage());
async.complete();
}));
async.await();
}
listen.close(context.asyncAssertSuccess());
}
Aggregations