CloudStoreConstraintChecker implements ConstraintChecker { public Map<String, ServerAddress> getCandidatesSync( List<ResourceConstraint> resourceConstraints, int numCandidates) { List<Map<String, ServerAddress>> candidates = new ArrayList<Map<String, ServerAddress>>(); CountDownLatch latch = new CountDownLatch(1); getCandidates(resourceConstraints, numCandidates, (c, ex) -> { candidates.add(c); latch.countDown(); }); boolean done = false; while (!done) { try { latch.await(); done = true; } catch (InterruptedException ex) { } } return candidates.get(0); } CloudStoreConstraintChecker(CloudStoreHelper cloudStoreHelper, ServiceHost xenonHost); Map<String, ServerAddress> getCandidatesSync(
      List<ResourceConstraint> resourceConstraints,
      int numCandidates); @Override void getCandidates(
      List<ResourceConstraint> resourceConstraints,
      int numCandidates,
      GetCandidatesCompletion completion);  }
@Test(dataProvider = "environment") private void testHostTypeQueries( String environmentName, TestEnvironment cloudStoreEnvironment, CloudStoreConstraintChecker checker) throws Throwable { logger.info("Testing host type boundaries with {}", environmentName); List<DatastoreService.State> datastores = createDatastoreDescriptions(10); List<HostService.State> cloudHosts = createHostDescriptions(10, false, datastores); List<HostService.State> managementHosts = createHostDescriptions(10, true, datastores); logger.info("Making 10 datastores..."); createDatastores(cloudStoreEnvironment, datastores); logger.info("Making 10 cloud hosts..."); createHosts(cloudStoreEnvironment, cloudHosts); logger.info("Making 10 management hosts..."); createHosts(cloudStoreEnvironment, managementHosts); Map<String, ServerAddress> selectedHosts; ResourceConstraint constraint; constraint = new ResourceConstraint(ResourceConstraintType.MANAGEMENT_ONLY, null); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(2)); for (String hostId : selectedHosts.keySet()) { assertThat(hostId, startsWith(MGMT_HOST_PREFIX)); } constraint = new ResourceConstraint(ResourceConstraintType.MANAGEMENT_ONLY, null); constraint.setNegative(true); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(2)); for (String hostId : selectedHosts.keySet()) { assertThat(hostId, startsWith(CLOUD_HOST_PREFIX)); } selectedHosts = checker.getCandidatesSync(null, 2); assertThat(selectedHosts.size(), equalTo(2)); for (String hostId : selectedHosts.keySet()) { assertThat(hostId, startsWith(CLOUD_HOST_PREFIX)); } List<String> datastoreList = new ArrayList<>(); datastoreList.addAll(cloudHosts.get(0).reportedDatastores); constraint = new ResourceConstraint(ResourceConstraintType.DATASTORE, datastoreList); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 1); assertThat(selectedHosts.size(), equalTo(1)); for (String hostId : selectedHosts.keySet()) { assertThat(hostId, startsWith(CLOUD_HOST_PREFIX)); } deleteDatastores(cloudStoreEnvironment, datastores); deleteHosts(cloudStoreEnvironment, cloudHosts); deleteHosts(cloudStoreEnvironment, managementHosts); }
@Test(dataProvider = "environment") private void testMultipleValues( String environmentName, TestEnvironment cloudStoreEnvironment, CloudStoreConstraintChecker checker) throws Throwable { logger.info("Testing multiple values with {}", environmentName); List<DatastoreService.State> datastores = createDatastoreDescriptions(10); List<HostService.State> hosts = createHostDescriptions(10, false, datastores); logger.info("Making 10 datastores..."); createDatastores(cloudStoreEnvironment, datastores); logger.info("Making 10 cloud hosts..."); createHosts(cloudStoreEnvironment, hosts); HostService.State host0 = hosts.get(0); HostService.State host1 = hosts.get(1); ServerAddress host0Address = new ServerAddress(host0.hostAddress, host0.agentPort); ServerAddress host1Address = new ServerAddress(host1.hostAddress, host1.agentPort); DatastoreService.State datastore0 = datastores.get(0); DatastoreService.State datastore1 = datastores.get(1); logger.info("Testing multiple values for networks..."); ResourceConstraint constraint = new ResourceConstraint( ResourceConstraintType.NETWORK, Arrays.asList(getHostNetwork(host0), getHostNetwork(host1))); Map<String, ServerAddress> selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), hasItem(host0Address)); assertThat(selectedHosts.values(), hasItem(host1Address)); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.NETWORK, Arrays.asList(getHostNetwork(host0), getHostNetwork(host1))); constraint.setNegative(true); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), not(hasItem(host0Address))); assertThat(selectedHosts.values(), not(hasItem(host1Address))); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.NETWORK, Arrays.asList("non_existent_network")); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(0)); logger.info("Testing multiple values for availability zones..."); constraint = new ResourceConstraint( ResourceConstraintType.AVAILABILITY_ZONE, Arrays.asList(host0.availabilityZoneId, host1.availabilityZoneId)); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), hasItem(host0Address)); assertThat(selectedHosts.values(), hasItem(host1Address)); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.AVAILABILITY_ZONE, Arrays.asList(host0.availabilityZoneId, host1.availabilityZoneId)); constraint.setNegative(true); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), not(hasItem(host0Address))); assertThat(selectedHosts.values(), not(hasItem(host1Address))); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.AVAILABILITY_ZONE, Arrays.asList("non_existent_availability_zone")); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(0)); logger.info("Testing multiple values for allowed hosts..."); constraint = new ResourceConstraint( ResourceConstraintType.HOST, Arrays.asList(host0.documentSelfLink, host1.documentSelfLink)); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), hasItem(host0Address)); assertThat(selectedHosts.values(), hasItem(host1Address)); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.HOST, Arrays.asList(host0.documentSelfLink, host1.documentSelfLink)); constraint.setNegative(true); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), not(hasItem(host0Address))); assertThat(selectedHosts.values(), not(hasItem(host1Address))); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.HOST, Arrays.asList("non_existent_host")); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(0)); logger.info("Testing multiple values for datastores..."); constraint = new ResourceConstraint( ResourceConstraintType.DATASTORE, Arrays.asList(getHostDatastore(host0), getHostDatastore(host1))); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), hasItem(host0Address)); assertThat(selectedHosts.values(), hasItem(host1Address)); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.DATASTORE, Arrays.asList(getHostDatastore(host0), getHostDatastore(host1))); constraint.setNegative(true); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), not(hasItem(host0Address))); assertThat(selectedHosts.values(), not(hasItem(host1Address))); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.DATASTORE, Arrays.asList("non_existent_datastore")); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(0)); logger.info("Testing multiple values for datastore tags..."); constraint = new ResourceConstraint( ResourceConstraintType.DATASTORE_TAG, Arrays.asList(getDatastoreTag(datastore0), getDatastoreTag(datastore1))); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), hasItem(host0Address)); assertThat(selectedHosts.values(), hasItem(host1Address)); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.DATASTORE_TAG, Arrays.asList(getDatastoreTag(datastore0), getDatastoreTag(datastore1))); constraint.setNegative(true); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.values(), not(hasItem(host0Address))); assertThat(selectedHosts.values(), not(hasItem(host1Address))); assertThat(selectedHosts.size(), equalTo(2)); constraint = new ResourceConstraint( ResourceConstraintType.DATASTORE_TAG, Arrays.asList("non_existent_tag")); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(0)); constraint = new ResourceConstraint( ResourceConstraintType.VIRTUAL_NETWORK, Arrays.asList("network-1", "network-2")); selectedHosts = checker.getCandidatesSync(Arrays.asList(constraint), 2); assertThat(selectedHosts.size(), equalTo(2)); deleteDatastores(cloudStoreEnvironment, datastores); deleteHosts(cloudStoreEnvironment, hosts); }
@Test(dataProvider = "environment") private void testAllHostsPicked( String environmentName, TestEnvironment cloudStoreEnvironment, CloudStoreConstraintChecker checker) throws Throwable { logger.info("Testing that all hosts are selected in {}", environmentName); List<DatastoreService.State> datastores = createDatastoreDescriptions(10); List<HostService.State> hosts = createHostDescriptions(10, false, datastores); logger.info("Making 10 datastores..."); createDatastores(cloudStoreEnvironment, datastores); logger.info("Making 10 cloud hosts..."); createHosts(cloudStoreEnvironment, hosts); List<ResourceConstraint> constraints = new LinkedList<>(); Boolean[] selectedHost = new Boolean[10]; boolean foundAll = false; int numAttempts = 0; Arrays.fill(selectedHost, false); for (int i = 0; i < 10000; i++) { Map<String, ServerAddress> candidate = checker.getCandidatesSync(constraints, 1); assertThat(candidate.size(), is(1)); for (String hostname : candidate.keySet()) { String[] hostnameSubstrings = hostname.split("-"); int hostIndex = Integer.parseInt(hostnameSubstrings[hostnameSubstrings.length - 1]); selectedHost[hostIndex] = true; } if (Arrays.stream(selectedHost).allMatch(selected -> selected == true)) { foundAll = true; numAttempts = i + 1; break; } } assertEquals(foundAll, true); if (foundAll) { logger.info("Took {} attempts to select all hosts", numAttempts); } deleteDatastores(cloudStoreEnvironment, datastores); deleteHosts(cloudStoreEnvironment, hosts); }