Search in sources :

Example 6 with Ports

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");
}
Also used : AnyDescriptor(org.folio.okapi.bean.AnyDescriptor) Ports(org.folio.okapi.bean.Ports) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) OkapiLogger(org.folio.okapi.common.OkapiLogger) Logger(org.apache.logging.log4j.Logger) LaunchDescriptor(org.folio.okapi.bean.LaunchDescriptor) EnvEntry(org.folio.okapi.bean.EnvEntry) Test(org.junit.Test)

Example 7 with Ports

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());
        }));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) LaunchDescriptor(org.folio.okapi.bean.LaunchDescriptor) Async(io.vertx.ext.unit.Async) TestRule(org.junit.rules.TestRule) RunWith(org.junit.runner.RunWith) OkapiLogger(org.folio.okapi.common.OkapiLogger) Locale(java.util.Locale) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) Assume(org.junit.Assume) LinkedList(java.util.LinkedList) Ports(org.folio.okapi.bean.Ports) Before(org.junit.Before) GenericCompositeFuture(org.folio.okapi.common.GenericCompositeFuture) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) Description(org.junit.runner.Description) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TestWatcher(org.junit.rules.TestWatcher) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Rule(org.junit.Rule) NetServer(io.vertx.core.net.NetServer) EnvEntry(org.folio.okapi.bean.EnvEntry) ModuleHandle(org.folio.okapi.service.ModuleHandle) Ports(org.folio.okapi.bean.Ports) JsonObject(io.vertx.core.json.JsonObject) NetServer(io.vertx.core.net.NetServer) LaunchDescriptor(org.folio.okapi.bean.LaunchDescriptor) Test(org.junit.Test)

Example 8 with Ports

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());
}
Also used : TestContext(io.vertx.ext.unit.TestContext) LaunchDescriptor(org.folio.okapi.bean.LaunchDescriptor) Async(io.vertx.ext.unit.Async) Json(io.vertx.core.json.Json) HttpServer(io.vertx.core.http.HttpServer) RunWith(org.junit.runner.RunWith) Router(io.vertx.ext.web.Router) WithAssertions(org.assertj.core.api.WithAssertions) RoutingContext(io.vertx.ext.web.RoutingContext) OkapiLogger(org.folio.okapi.common.OkapiLogger) JsonObject(io.vertx.core.json.JsonObject) Assume(org.junit.Assume) Ports(org.folio.okapi.bean.Ports) DOCKER_REGISTRIES_EMPTY_LIST(org.folio.okapi.service.impl.DockerModuleHandle.DOCKER_REGISTRIES_EMPTY_LIST) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) AdditionalAnswers(org.mockito.AdditionalAnswers) JsonArray(io.vertx.core.json.JsonArray) Mockito(org.mockito.Mockito) Base64(java.util.Base64) Logger(org.apache.logging.log4j.Logger) AnyDescriptor(org.folio.okapi.bean.AnyDescriptor) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Assert(org.junit.Assert) EnvEntry(org.folio.okapi.bean.EnvEntry) HttpClient(io.vertx.core.http.HttpClient) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) Ports(org.folio.okapi.bean.Ports) Vertx(io.vertx.core.Vertx) LaunchDescriptor(org.folio.okapi.bean.LaunchDescriptor) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) EnvEntry(org.folio.okapi.bean.EnvEntry) Test(org.junit.Test)

Aggregations

Ports (org.folio.okapi.bean.Ports)8 LaunchDescriptor (org.folio.okapi.bean.LaunchDescriptor)7 JsonObject (io.vertx.core.json.JsonObject)6 Test (org.junit.Test)6 Vertx (io.vertx.core.Vertx)5 Logger (org.apache.logging.log4j.Logger)5 EnvEntry (org.folio.okapi.bean.EnvEntry)5 OkapiLogger (org.folio.okapi.common.OkapiLogger)5 Future (io.vertx.core.Future)4 Async (io.vertx.ext.unit.Async)4 TestContext (io.vertx.ext.unit.TestContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 AnyDescriptor (org.folio.okapi.bean.AnyDescriptor)4 Assume (org.junit.Assume)4 RunWith (org.junit.runner.RunWith)4 VertxOptions (io.vertx.core.VertxOptions)3 HttpClient (io.vertx.core.http.HttpClient)3 HttpMethod (io.vertx.core.http.HttpMethod)3 HttpServer (io.vertx.core.http.HttpServer)3 HttpServerOptions (io.vertx.core.http.HttpServerOptions)3