private void validateAllocatedContainers(boolean isReplicated, String containerTemplateLink) throws Throwable { QueryTask.Query kindClause = new QueryTask.Query() .setTermPropertyName(ServiceDocument.FIELD_NAME_KIND) .setTermMatchValue(Utils.buildKind(ContainerService.State.class)); QueryTask.Query containerTemplateClause = new QueryTask.Query() .setTermPropertyName( ContainerService.State.FIELD_NAME_CONTAINER_TEMPLATE_SERVICE_LINK) .setTermMatchValue(containerTemplateLink); QueryTask.QuerySpecification querySpecification = new QueryTask.QuerySpecification(); querySpecification.query.addBooleanClause(kindClause); querySpecification.query.addBooleanClause(containerTemplateClause); QueryTask query = QueryTask.create(querySpecification).setDirect(true); NodeGroupBroadcastResponse queryResponse = testEnvironment.sendBroadcastQueryAndWait(query); Set<String> documentLinks = QueryTaskUtils.getBroadcastQueryResults(queryResponse); // Verify that count(replicas) == count(dockerVms) i.e. 1 container per vm int expectedReplicaCount = isReplicated ? dockerVms.size() : 1; assertThat(documentLinks.size(), is(expectedReplicaCount)); // Verify that each container was assigned to a unique docker vm Set<String> uniqueVmLinks = new HashSet<>(); for (String documentLink : documentLinks) { ContainerService.State state = testEnvironment.getServiceState(documentLink, ContainerService.State.class); uniqueVmLinks.add(state.vmServiceLink); } assertThat(uniqueVmLinks.size(), is(expectedReplicaCount)); }
@Test public void testWaitForQuery() throws Throwable { ExampleService exampleService = new ExampleService(); final ExampleService.ExampleServiceState exampleServiceState = new ExampleService.ExampleServiceState(); exampleServiceState.name = UUID.randomUUID().toString(); QueryTask.Query kindClause = new QueryTask.Query() .setTermPropertyName(ServiceDocument.FIELD_NAME_KIND) .setTermMatchValue(Utils.buildKind(ExampleService.ExampleServiceState.class)); QueryTask.Query nameClause = new QueryTask.Query() .setTermPropertyName("name") .setTermMatchValue(exampleServiceState.name); QueryTask.QuerySpecification spec = new QueryTask.QuerySpecification(); spec.query.addBooleanClause(kindClause); spec.query.addBooleanClause(nameClause); spec.options = EnumSet.of(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT); QueryTask query = QueryTask.create(spec).setDirect(true); try { host.waitForQuery( query, new Predicate<QueryTask>() { @Override public boolean test(QueryTask queryTask) { return queryTask.results.documentLinks.size() > 0; } }); Assert.fail("waitForQuery should not have succeeded before documents are created"); } catch (RuntimeException runtimeException) { assertThat( runtimeException.getMessage(), is(equalToIgnoringCase("timeout waiting for query result."))); } Operation post = host.startServiceSynchronously(exampleService, exampleServiceState); assertThat(post.getStatusCode(), is(200)); ExampleService.ExampleServiceState result = host.getServiceState(ExampleService.ExampleServiceState.class); assertThat(result.name, is(exampleServiceState.name)); QueryTask response = host.waitForQuery( query, new Predicate<QueryTask>() { @Override public boolean test(QueryTask queryTask) { return queryTask.results.documentLinks.size() > 0; } }); assertThat(response.results.documentLinks.size(), is(1)); // verify fields are passed down correctly for (Map.Entry<String, Object> document : response.results.documents.entrySet()) { ExampleService.ExampleServiceState docState = Utils.fromJson(document.getValue(), ExampleService.ExampleServiceState.class); assertThat(docState.name, is(equalTo(exampleServiceState.name))); } }