private void getCompute(Operation op, String hostLink, Consumer<ComputeState> consumer) { Operation.createGet(this, hostLink).setCompletion((o, e) -> { if (e != null) { op.fail(e); return; } consumer.accept(o.getBody(ComputeState.class)); }).sendWith(this); } }
private void getConfigProperty(String propName, Consumer<ConfigurationState> callback) { sendRequest(Operation .createGet(getHost(), UriUtils.buildUriPath(CONFIG_PROPS, propName)) .setCompletion((res, ex) -> { if (ex == null && res.hasBody()) { ConfigurationState body = res.getBody(ConfigurationState.class); callback.accept(body); } })); }
private DeferredResult<ComputeState> requestComputeState(String selfLink) { URI uri = UriUtils.buildUri(host, selfLink); Operation op = Operation.createGet(uri) .setCompletion((o, e) -> { if (e != null) { logger.warning(String.format("Failed to retrieve compute state: [%s]", Utils.toString(e))); return; } }); prepareRequest(op, false); return host.sendWithDeferredResult(op, ComputeState.class); }
@Test public void testCompositeLocaleMessageResolution() { Operation op = Operation.createGet(URI.create("127.0.0.1")) .addRequestHeader(Operation.ACCEPT_LANGUAGE_HEADER, "pt-BR,en-US;q=0.8,en;q=0.6"); Locale locale = LocalizationUtil.resolveLocale(op); assertEquals("pt", locale.getLanguage()); assertEquals("BR", locale.getCountry()); String message = LocalizationUtil.resolveMessage(this.ex, op); assertEquals(ERROR_MESSAGE_BR, message); }
@Test public void testGetKubeConfigWithBearerToken() throws Throwable { String authCredentialsLink = createCredentials(AuthCredentialsType.Bearer, true) .documentSelfLink; String hostLink = createCompute(authCredentialsLink, true, true).documentSelfLink; URI serviceUri = UriUtils.buildUri(host, KubeConfigContentService.SELF_LINK, UriUtils.buildUriQuery("hostLink", hostLink)); verifyOperation(Operation.createGet(serviceUri), o -> { assertEquals("attachment; filename=\"kubeconfig\"", o.getResponseHeader("Content-Disposition")); assertEquals(KUBE_CONFIG_YAML, o.getBody(String.class)); }); }
@Test public void testSimpleServe() { String existingFile = "/serviceHostState.json"; Operation re = this.host.getTestRequestSender() .sendAndWait(Operation.createGet(this.host, SELF_LINK + existingFile)); ServiceHostState hs = re.getBody(ServiceHostState.class); assertEquals(this.host.getId(), hs.id); }
@Test public void systemUser() throws Throwable { URI uri = UriUtils.buildUri(this.host, SystemUserService.SELF_LINK); Operation get = Operation .createGet(uri) .setCompletion(this.host.getCompletion()); this.host.testStart(1); this.host.send(get); this.host.testWait(); } }
@Test public void completeExce() throws InterruptedException { CompletableFuture<Operation> f = this.host.sendWithFuture(Operation .createGet(UriUtils.buildUri(this.host, "/hello")) .setReferer(this.host.getReferer())); // blocking here is OK as it is JUnit's thread that's blocked try { f.get(); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof ServiceNotFoundException); } }
@Test public void completeOk() throws InterruptedException, ExecutionException { CompletableFuture<Operation> f = this.host.sendWithFuture(Operation .createGet(UriUtils.buildUri(this.host, "/"))); // callback with only a success branch ServiceDocumentQueryResult body = f.get().getBody(ServiceDocumentQueryResult.class); assertTrue(body.documentLinks.size() > 0); }
@Test public void testNoHarborProperty() throws Exception { TestContext t = new TestContext(1, Duration.ofSeconds(15)); harborInitRegistryService.handleStart(Operation .createGet(null) .setCompletion(t.getCompletion())); t.await(); RegistryState harborRegistry = getHarborRegistry(false); assertNull(harborRegistry); }
private RegistryState getHarborRegistry(boolean exists) { if (exists) { return sender.sendGetAndWait(UriUtils.buildUri(host, Harbor.DEFAULT_REGISTRY_LINK), RegistryState.class); } else { FailureResponse failureResponse = sender.sendAndWaitFailure( Operation.createGet(host, Harbor.DEFAULT_REGISTRY_LINK)); assertEquals(Operation.STATUS_CODE_NOT_FOUND, failureResponse.op.getStatusCode()); return null; } }
void doHealthCheck(ServiceHost host, String containerDescriptionLink) { host.sendRequest(Operation .createGet(host, containerDescriptionLink) .setReferer(UriUtils.buildUri(host, SERVICE_REFERRER_PATH)) .setCompletion((o, ex) -> { if (ex != null) { host.log(Level.SEVERE, "Failed to fetch %s : %s", containerDescriptionLink, Utils.toJson(ex)); } else { processContainerHealth(host, o.getBody(ContainerDescription.class)); } })); }
/** * Fetch subnet state by document link. * * @param link Document link for the subnet service * @return A deferred result of an operation, that has the subnet state */ private <T> DeferredResult<Operation> getSubnetState(String link) { AssertUtil.assertNotEmpty(link, "Cannot fetch subnet details with an empty subnet link"); URI uri = UriUtils.buildUri(getHost(), link); return sendWithDeferredResult(Operation.createGet(uri)); }
/** * Return list of attached service paths that prefix match to the given path * @return list of service paths */ public List<String> getServicePathsByPrefix(String servicePathPrefix) { Operation dummy = Operation.createGet(null); this.queryServiceUris(UriUtils.buildUriPath(servicePathPrefix, UriUtils.URI_WILDCARD_CHAR), dummy); List<String> links = dummy.getBody(ServiceDocumentQueryResult.class).documentLinks; return links; }
private void verifyDescriptions(List<String> containerDescriptionLinks) throws Throwable { for (String link : containerDescriptionLinks) { verifyOperation(Operation.createGet(UriUtils.buildUri(host, link)), (o) -> { ResourceState cd = o.getBody(ResourceState.class); assertTrue("unexpected component name: " + cd.name, Arrays.asList("wordpress", "mysql", "public-wpnet", "wpnet", "wordpress-mysql-svc", "wordpress-mysql-dpl", "wordpress-svc", "wordpress-dpl", "wordpress-lb", "wordpress-container-lb", "wordpress-secret", "wordpress-mysql-replcontr", "wordpress-mysql-pod").contains(cd.name)); }); } }
private void getExternalPopularImages(Operation get, Runnable notFoundHandler) { sendRequest(Operation.createGet(UriUtils.buildUri(getHost(), EXTERNAL_LINK)) .setCompletion((op, ex) -> { if (ex != null || op.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) { notFoundHandler.run(); } else { get.setBody(op.getBodyRaw()); get.setStatusCode(op.getStatusCode()); get.setContentType(Operation.MEDIA_TYPE_APPLICATION_JSON); get.complete(); } })); }
private void registerClientCredIfNeeded(AuthCredentialsServiceState caCred, String caCert, String caKey, Operation startOp) { Operation.createGet(this, ManagementUriParts.AUTH_CREDENTIALS_CLIENT_LINK) .setCompletion((o, e) -> { if (e != null) { registerClientCred(caCert, caKey, startOp); return; } startOp.complete(); }) .sendWith(this); }
@Test public void testCompositeLocaleMessageResolution() { Operation op = Operation.createGet(URI.create("127.0.0.1")) .addRequestHeader(Operation.ACCEPT_LANGUAGE_HEADER, "pt-BR,en-US;q=0.8,en;q=0.6"); Locale locale = LocalizationUtil.resolveLocale(op); assertEquals("pt", locale.getLanguage()); assertEquals("BR", locale.getCountry()); String message = LocalizationUtil.resolveMessage(this.ex, op); assertEquals(ERROR_MESSAGE_BR, message); }
@Test public void testContainerStatsWrongParam() { TestRequestSender sender = host.getTestRequestSender(); String query = ContainerStatsService.CONTAINER_ID_QUERY_PARAM + "=no-container"; URI uri = UriUtils.buildUri(host, ContainerStatsService.SELF_LINK, query); FailureResponse failureResponse = sender.sendAndWaitFailure(Operation.createGet(uri)); assertEquals(Operation.STATUS_CODE_NOT_FOUND, failureResponse.op.getStatusCode()); }
@Test public void testSimpleServe() { String existingFile = "/serviceHostState.json"; Operation re = this.host.getTestRequestSender() .sendAndWait(Operation.createGet(this.host, SELF_LINK + existingFile)); ServiceHostState hs = re.getBody(ServiceHostState.class); assertEquals(this.host.getId(), hs.id); }