private void tryClient(TestContext tc, ConsulClient client, String expectedExceptionMessageSubstring) { client.agentInfo(tc.asyncAssertFailure(t -> { tc.assertTrue(t.getMessage().contains(expectedExceptionMessageSubstring)); ctx.closeClient(client); })); } }
@Test public void datacenters(TestContext tc) { ctx.readClient().catalogDatacenters(tc.asyncAssertSuccess(datacenters -> { tc.assertEquals(datacenters.size(), 1); tc.assertEquals(datacenters.get(0), ctx.dc().getName()); })); }
@Test public void catalogNodeServicesBlocking() throws InterruptedException { testServicesBlocking(h -> ctx.readClient().catalogNodeServices(ctx.nodeName(), h), (opts, h) -> ctx.readClient().catalogNodeServicesWithOptions(ctx.nodeName(), opts, h)); }
@Test public void createDefaultSession(TestContext tc) { ctx.writeClient().createSession(tc.asyncAssertSuccess(id -> { ctx.writeClient().infoSession(id, tc.asyncAssertSuccess(session -> { tc.assertEquals(id, session.getId()); tc.assertEquals(ctx.nodeName(), session.getNode()); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess()); })); })); }
int requests = MAX_REQUESTS; while (requests --> 0) { nodes1 = getAsync(h -> ctx.readClient().coordinateNodes(h)); if (nodes1.getList().size() == 1) { break; assertEquals(coordinate.getNode(), ctx.nodeName()); ctx.readClient().coordinateNodesWithOptions(opts, h -> { boolean success = h.result().getList().size() == 2; waitComplete(vertx, fut, h.result().getIndex(), success); ConsulAgent attached = ctx.attachAgent("new_node"); latch.await(2, TimeUnit.MINUTES); assertEquals(latch.getCount(), 0); ctx.detachAgent(attached);
@Test public void watchNodes() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); String nodeName = randomAlphaString(10); Watch<NodeList> watch = Watch.nodes(vertx, ctx.readClientOptions()) .setHandler(list -> { if (list.succeeded()) { consumer.consume(list.nextResult().getList() .stream().map(Node::getName).filter(s -> s.equals(nodeName)) .findFirst().orElse("")); } }) .start(); consumer.await(""); ConsulAgent attached = ctx.attachAgent(nodeName); ctx.detachAgent(attached); consumer.await(nodeName); consumer.await(""); consumer.check(); watch.stop(); } }
@Test public void blockingQuery(TestContext tc) throws InterruptedException { ctx.readClient().catalogNodes(tc.asyncAssertSuccess(nodes1 -> { Async async1 = tc.async(); NodeQueryOptions blockingQueryOptions1 = new NodeQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(nodes1.getIndex())); ctx.readClient().catalogNodesWithOptions(blockingQueryOptions1, h -> { System.out.println(">>>>>>> new node event received"); List<String> names = h.result().getList().stream().map(Node::getName).collect(Collectors.toList()); System.out.println(">>>>>>> new node is still not ready"); tc.assertEquals(async1.count(), 1); vertx.<ConsulAgent>executeBlocking(b1 -> b1.complete(ctx.attachAgent("attached_node")), tc.asyncAssertSuccess(attached -> { System.out.println(">>>>>>> new node attached"); async1.handler(v -> { ctx.readClient().catalogNodes(tc.asyncAssertSuccess(nodes2 -> { NodeQueryOptions blockingQueryOptions2 = new NodeQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(nodes2.getIndex())); System.out.println(">>>>>>> wait for new node detaching"); ctx.readClient().catalogNodesWithOptions(blockingQueryOptions2, tc.asyncAssertSuccess()); vertx.executeBlocking(b2 -> ctx.detachAgent(attached), detached -> System.out.println(">>>>>>> new node detached")); })); });
@Test public void closedConnection(TestContext tc) { BrokenHttpServer brokenConsul = new BrokenHttpServer(vertx); ConsulClient client = ctx.createClient(new ConsulClientOptions().setPort(brokenConsul.port())); client.agentInfo(tc.asyncAssertFailure(t -> { ctx.closeClient(client); brokenConsul.close(); tc.assertTrue(t.getMessage().contains("Connection was closed")); })); }
private void go(TestContext tc, boolean trustAll, PemTrustOptions trustOptions) { ConsulClient secureClient = ctx.createSecureClient(trustAll, trustOptions); secureClient.putValue("foo/bars42", "value42", tc.asyncAssertSuccess(b -> { tc.assertTrue(b); secureClient.getValue("foo/bars42", tc.asyncAssertSuccess(pair -> { tc.assertEquals(pair.getValue(), "value42"); ctx.closeClient(secureClient); })); })); } }
@Test public void grpcCheckLifecycle(TestContext tc) { if (ctx.consulVersion().compareTo("1.0.3") < 0) { System.out.println("skip " + ctx.consulVersion() + " version"); return; } GrpcHealthReporter reporter = new GrpcHealthReporter(vertx); Async async = tc.async(); CheckOptions opts = new CheckOptions() .setGrpc("localhost:" + reporter.port() + "/testee") .setInterval("2s") .setName("checkName"); String checkId = createCheck(opts); reporter.start(tc.asyncAssertSuccess(v1 -> { vertx.setTimer(3000, t1 -> { getCheckInfo(tc, checkId, c1 -> { tc.assertEquals(CheckStatus.PASSING, c1.getStatus()); reporter.setStatus(HealthCheck.HealthCheckResponse.ServingStatus.NOT_SERVING); vertx.setTimer(3000, t2 -> { getCheckInfo(tc, checkId, c2 -> { tc.assertEquals(CheckStatus.CRITICAL, c2.getStatus()); reporter.close(tc.asyncAssertSuccess(v2 -> { ctx.writeClient().deregisterCheck(checkId, tc.asyncAssertSuccess(v -> async.complete())); })); }); }); }); }); })); }
@Test public void unknownHost(TestContext tc) { ConsulClient unknownHost = ctx.createClient(new ConsulClientOptions().setHost("unknownConsulHost")); tryClient(tc, unknownHost, "unknownConsulHost"); }
@Test public void lifecycle(TestContext tc) { ctx.masterClient().listAclTokens(tc.asyncAssertSuccess(backupTokens -> { AclToken init = new AclToken() .setName("tokenName") .setRules("key \"bar/\" { policy = \"read\" }"); ctx.masterClient().createAclToken(init, tc.asyncAssertSuccess(id -> { ctx.masterClient().infoAclToken(id, tc.asyncAssertSuccess(token -> { tc.assertEquals(id, token.getId()); tc.assertEquals(init.getName(), token.getName()); tc.assertEquals(init.getRules(), token.getRules()); ctx.masterClient().cloneAclToken(id, tc.asyncAssertSuccess(clonedId -> { ctx.masterClient().infoAclToken(clonedId, tc.asyncAssertSuccess(clonedToken -> { tc.assertEquals(clonedId, clonedToken.getId()); tc.assertEquals(token.getName(), clonedToken.getName()); .setName("updatedName") .setRules("key \"bar/\" { policy = \"write\" }"); ctx.masterClient().updateAclToken(token2, tc.asyncAssertSuccess(updatedId -> { ctx.masterClient().infoAclToken(updatedId, tc.asyncAssertSuccess(updatedToken -> { tc.assertEquals(token2.getId(), updatedToken.getId()); tc.assertEquals(token2.getName(), updatedToken.getName()); tc.assertEquals(token2.getRules(), updatedToken.getRules()); ctx.masterClient().destroyAclToken(id, tc.asyncAssertSuccess(v1 -> { ctx.masterClient().destroyAclToken(clonedId, tc.asyncAssertSuccess(v2 -> { ctx.masterClient().listAclTokens(tc.asyncAssertSuccess(aliveTokens -> { tc.assertEquals(backupTokens.size(), aliveTokens.size()); }));
@Test public void createSessionWithOptionsWithDefaultLockDelay(TestContext tc) { SessionOptions opt = new SessionOptions() .setBehavior(SessionBehavior.DELETE) .setName("optName") .setTtl(442); ctx.writeClient().createSessionWithOptions(opt, tc.asyncAssertSuccess(id -> { ctx.writeClient().infoSession(id, tc.asyncAssertSuccess(session -> { List<String> checks = session.getChecks(); tc.assertEquals(1, checks.size()); tc.assertTrue("serfHealth".equals(checks.get(0))); tc.assertEquals(15L, session.getLockDelay()); tc.assertEquals(ctx.nodeName(), session.getNode()); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess()); })); })); }
@Test public void listNodeSessionsBlocking(TestContext tc) throws InterruptedException { testSessionsBlocking(tc, (opts, h) -> ctx.readClient().listNodeSessionsWithOptions(ctx.nodeName(), opts, h)); }
@Test public void timeout(TestContext tc) { SlowHttpServer slowConsul = new SlowHttpServer(vertx, 10000); ConsulClient client = ctx.createClient(new ConsulClientOptions().setPort(slowConsul.port()).setTimeout(1000)); client.agentInfo(tc.asyncAssertFailure(t -> { ctx.closeClient(client); slowConsul.close(); tc.assertTrue(t.getMessage().contains("The timeout period of 1000ms")); })); }
@Test public void unknownPort(TestContext tc) { ConsulClient unknownPort = ctx.createClient(new ConsulClientOptions().setPort(Utils.getFreePort())); tryClient(tc, unknownPort, "Connection refused"); }
@Test public void createSessionWithOptionsWithZeroLockDelay(TestContext tc) { SessionOptions opt = new SessionOptions() .setBehavior(SessionBehavior.DELETE) .setLockDelay(0L) .setName("optName") .setTtl(442); ctx.writeClient().createSessionWithOptions(opt, tc.asyncAssertSuccess(id -> { ctx.writeClient().infoSession(id, tc.asyncAssertSuccess(session -> { List<String> checks = session.getChecks(); tc.assertEquals(1, checks.size()); tc.assertTrue("serfHealth".equals(checks.get(0))); tc.assertEquals(0L, session.getLockDelay()); tc.assertEquals(ctx.nodeName(), session.getNode()); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess()); })); })); }
@Test public void nodes(TestContext tc) { ctx.readClient().catalogNodes(tc.asyncAssertSuccess(nodes -> { tc.assertEquals(nodes.getList().size(), 1); Node node = nodes.getList().get(0); tc.assertEquals(node.getName(), ctx.nodeName()); })); }
@Test public void info(TestContext tc) { ctx.readClient().agentInfo(tc.asyncAssertSuccess(info -> { JsonObject config = info.getJsonObject("Config"); tc.assertEquals(config.getString("Datacenter"), ctx.dc().getName()); })); } }