use of com.vmware.xenon.common.test.VerificationHost in project photon-model by vmware.
the class TestAWSSetupUtils method getAwsInstancesByIds.
/**
* Method to get Instance details directly from Amazon
*
* @throws Throwable
*/
public static List<Instance> getAwsInstancesByIds(AmazonEC2AsyncClient client, VerificationHost host, List<String> instanceIds) throws Throwable {
host.log("Getting instances with ids " + instanceIds + " from the AWS endpoint using the EC2 client.");
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceIds);
DescribeInstancesResult describeInstancesResult = client.describeInstances(describeInstancesRequest);
return describeInstancesResult.getReservations().stream().flatMap(r -> r.getInstances().stream()).collect(Collectors.toList());
}
use of com.vmware.xenon.common.test.VerificationHost in project photon-model by vmware.
the class GCPTestUtil method queryComputeStatesWithPowerState.
/**
* Method to query the number of local compute states with given power state.
* @param host The test service host.
* @param resourcePool The default resource pool.
* @param parentCompute The default compute host.
* @param powerState The given power state.
* @param instanceNames The assumed names of compute states.
*/
private static void queryComputeStatesWithPowerState(VerificationHost host, ResourcePoolState resourcePool, ComputeState parentCompute, PowerState powerState, Set<String> instanceNames) {
Query query = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, resourcePool.documentSelfLink).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, parentCompute.documentSelfLink).build();
QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).setQuery(query).build();
host.sendRequest(QueryUtils.createQueryTaskOperation(host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(host.getUri()).setCompletion((o, e) -> {
if (e != null) {
host.log(Level.WARNING, String.format("Error: %s", e.getMessage()));
return;
}
QueryTask queryTask = o.getBody(QueryTask.class);
if (queryTask.results.documentCount > 0) {
queryTask.results.documents.values().forEach(s -> {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
if (computeState.powerState == powerState) {
instanceNames.remove(computeState.name);
}
});
}
}));
}
use of com.vmware.xenon.common.test.VerificationHost in project photon-model by vmware.
the class AzureLifecycleOperationServiceTest method createTaskResultListener.
private void createTaskResultListener(VerificationHost host, String taskLink, Function<Operation, Boolean> h) {
StatelessService service = new StatelessService() {
@Override
public void handleRequest(Operation update) {
if (!h.apply(update)) {
super.handleRequest(update);
}
}
};
TestContext ctx = this.host.testCreate(1);
Operation startOp = Operation.createPost(host, taskLink).setCompletion((o, e) -> {
if (e != null) {
ctx.failIteration(e);
return;
}
ctx.completeIteration();
}).setReferer(this.host.getReferer());
this.host.startService(startOp, service);
ctx.await();
}
use of com.vmware.xenon.common.test.VerificationHost in project photon-model by vmware.
the class StatsCollectionTaskServiceTest method testStatsCollection.
private void testStatsCollection(boolean testOnCluster) throws Throwable {
VerificationHost metricHost = null;
if (testOnCluster) {
metricHost = this.setupMetricHost();
}
// Use this.host if metricHost is null.
VerificationHost verificationHost = (metricHost == null ? this.host : metricHost);
// create a metric host
// create a compute description for all the computes
ComputeDescription cDesc = new ComputeDescription();
cDesc.name = UUID.randomUUID().toString();
cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
// create multiple computes
ComputeState computeState = new ComputeState();
computeState.name = UUID.randomUUID().toString();
computeState.descriptionLink = descReturnState.documentSelfLink;
List<String> computeLinks = new ArrayList<>(this.numResources);
for (int i = 0; i < this.numResources; i++) {
ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
computeLinks.add(res.documentSelfLink);
}
// create a resource pool including all the created computes
ResourcePoolState rpState = new ResourcePoolState();
rpState.name = UUID.randomUUID().toString();
rpState.properties = EnumSet.of(ResourcePoolProperty.ELASTIC);
rpState.query = Query.Builder.create().addKindFieldClause(ComputeState.class).addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, computeLinks).build();
ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
// create a stats collection scheduler task
StatsCollectionTaskState statCollectionState = new StatsCollectionTaskState();
statCollectionState.resourcePoolLink = rpReturnState.documentSelfLink;
statCollectionState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
ScheduledTaskState statsCollectionTaskState = new ScheduledTaskState();
statsCollectionTaskState.factoryLink = StatsCollectionTaskService.FACTORY_LINK;
statsCollectionTaskState.initialStateJson = Utils.toJson(statCollectionState);
statsCollectionTaskState.intervalMicros = TimeUnit.SECONDS.toMicros(2);
statsCollectionTaskState = postServiceSynchronously(ScheduledTaskService.FACTORY_LINK, statsCollectionTaskState, ScheduledTaskState.class);
ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.host, ScheduledTaskService.FACTORY_LINK)));
assertTrue(res.documents.size() > 0);
// the last successful collection time should be populated as an in memory stat.
for (int i = 0; i < this.numResources; i++) {
String statsUriPath = UriUtils.buildUriPath(computeLinks.get(i), ServiceHost.SERVICE_URI_SUFFIX_STATS);
this.host.waitFor("Error waiting for in memory stats", () -> {
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
boolean returnStatus = false;
for (ServiceStat stat : resStats.entries.values()) {
if (stat.latestValue > 0) {
returnStatus = true;
break;
}
}
return returnStatus;
});
}
host.log(Level.INFO, "Successfully verified that all the last collection time is available in memory.");
// persisted at a per metric level along with the last collection run time
for (String computeLink : computeLinks) {
ResourceMetrics metric = getResourceMetrics(verificationHost, computeLink, MockStatsAdapter.KEY_1);
assertNotNull("The resource metric for" + MockStatsAdapter.KEY_1 + " should not be null ", metric);
assertEquals(metric.entries.size(), 1);
assertEquals(metric.customProperties.get("prop1"), "val1");
ResourceMetrics metric2 = getResourceMetrics(verificationHost, computeLink, MockStatsAdapter.KEY_2);
assertNotNull("The resource metric for" + MockStatsAdapter.KEY_2 + "should not be null ", metric2);
assertEquals(metric2.entries.size(), 1);
String lastSuccessfulRunMetricKey = UriUtils.getLastPathSegment(MockStatsAdapter.SELF_LINK) + StatsUtil.SEPARATOR + PhotonModelConstants.LAST_SUCCESSFUL_STATS_COLLECTION_TIME;
ResourceMetrics metricLastRun = getResourceMetrics(verificationHost, computeLink, lastSuccessfulRunMetricKey);
assertNotNull("The resource metric for" + lastSuccessfulRunMetricKey + " should not be null ", metricLastRun);
}
host.log(Level.INFO, "Successfully verified that the required resource metrics are persisted in the resource metrics table");
// Verify sorted order of the metrics versions by timestamp
for (String computeLink : computeLinks) {
// get all versions
QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.SORT).orderAscending(ServiceDocument.FIELD_NAME_SELF_LINK, TypeName.STRING).setQuery(Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, UriUtils.getLastPathSegment(computeLink)), MatchType.PREFIX).addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, MockStatsAdapter.KEY_1), NumericRange.createDoubleRange(Double.MIN_VALUE, Double.MAX_VALUE, true, true)).build()).build();
verificationHost.createQueryTaskService(qt, false, true, qt, null);
ResourceMetrics prevMetric = null;
for (String documentLink : qt.results.documentLinks) {
ResourceMetrics metric = Utils.fromJson(qt.results.documents.get(documentLink), ResourceMetrics.class);
if (prevMetric == null) {
prevMetric = metric;
continue;
}
assertTrue(prevMetric.timestampMicrosUtc < metric.timestampMicrosUtc);
}
}
// verify that the aggregation tasks have been deleted
this.host.waitFor("Timeout waiting for task to expire", () -> {
ServiceDocumentQueryResult collectRes = this.host.getFactoryState(UriUtils.buildUri(this.host, StatsCollectionTaskService.FACTORY_LINK));
if (collectRes.documentLinks.size() == 0) {
return true;
}
return false;
});
if (testOnCluster) {
this.cleanUpMetricHost(metricHost);
}
}
use of com.vmware.xenon.common.test.VerificationHost in project photon-model by vmware.
the class AzureTestUtil method createDefaultVMResource.
public static ComputeState createDefaultVMResource(VerificationHost host, String azureVMName, ComputeState computeHost, EndpointState endpointState, AzureNicSpecs nicSpecs, String networkRGLink, ImageSource imageSource, int numberOfAdditionalDisks, ComputeDescription azureVMDesc, String azureRgName) throws Throwable {
if (azureRgName == null) {
azureRgName = azureVMName;
}
final ResourceGroupState defaultVmRG = createDefaultResourceGroupState(host, azureRgName, computeHost, endpointState, ResourceGroupStateType.AzureResourceGroup);
final String defaultVmRGLink = defaultVmRG.documentSelfLink;
if (networkRGLink == null) {
// The RG where the VM is deployed is also used as RG for the Network!
networkRGLink = defaultVmRGLink;
}
// The RG where the VM is deployed is also used as RG for the SecurityGroup!
final String sgRGLink = defaultVmRGLink;
// Create resource group with a different type. It should be filtered out.
ResourceGroupState azureStorageContainerRG = createDefaultResourceGroupState(host, AZURE_STORAGE_CONTAINER_RG_NAME, computeHost, endpointState, ResourceGroupStateType.AzureStorageContainer);
final Set<String> networkRGLinks = new HashSet<>();
networkRGLinks.add(networkRGLink);
networkRGLinks.add(azureStorageContainerRG.documentSelfLink);
final Set<String> sgRGLinks = new HashSet<>();
sgRGLinks.add(sgRGLink);
sgRGLinks.add(azureStorageContainerRG.documentSelfLink);
if (azureVMDesc == null) {
azureVMDesc = buildComputeDescription(host, computeHost, endpointState, null);
}
List<String> vmDisks = new ArrayList<>();
DiskState rootDisk = new DiskState();
rootDisk.name = azureVMName + "-boot-disk";
rootDisk.id = UUID.randomUUID().toString();
rootDisk.documentSelfLink = rootDisk.id;
rootDisk.type = DiskType.HDD;
// Custom OSDisk size of 32 GBs
rootDisk.capacityMBytes = AZURE_CUSTOM_OSDISK_SIZE;
if (imageSource.type == Type.PRIVATE_IMAGE || imageSource.type == Type.PUBLIC_IMAGE) {
rootDisk.imageLink = imageSource.asImageState().documentSelfLink;
} else if (imageSource.type == Type.IMAGE_REFERENCE) {
rootDisk.sourceImageReference = URI.create(imageSource.asRef());
}
rootDisk.bootOrder = 1;
rootDisk.endpointLink = endpointState.documentSelfLink;
rootDisk.endpointLinks = new HashSet<>();
rootDisk.endpointLinks.add(endpointState.documentSelfLink);
rootDisk.computeHostLink = computeHost.documentSelfLink;
rootDisk.tenantLinks = endpointState.tenantLinks;
rootDisk.storageType = AZURE_STORAGE_DISKS;
rootDisk.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_vhd.name());
rootDisk.customProperties = new HashMap<>();
rootDisk.customProperties.put(AZURE_OSDISK_CACHING, DEFAULT_OS_DISK_CACHING.name());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_NAME, (azureVMName + "sa").replaceAll("[_-]", "").toLowerCase());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_RG_NAME, azureRgName);
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_TYPE, AZURE_STORAGE_ACCOUNT_TYPE);
rootDisk = TestUtils.doPost(host, rootDisk, DiskState.class, UriUtils.buildUri(host, DiskService.FACTORY_LINK));
vmDisks.add(rootDisk.documentSelfLink);
// create additional disks
vmDisks.addAll(createAdditionalDisks(host, azureVMName, endpointState, numberOfAdditionalDisks, VMResourceSpec.PersistentDisks.NONE, false));
// Create NICs
List<String> nicLinks = createDefaultNicStates(host, computeHost, endpointState, networkRGLinks, sgRGLinks, nicSpecs, azureVMName).stream().map(nic -> nic.documentSelfLink).collect(Collectors.toList());
// Finally create the compute resource state to provision using all constructs above.
ComputeState computeState = new ComputeState();
computeState.id = UUID.randomUUID().toString();
computeState.name = azureVMName;
computeState.parentLink = computeHost.documentSelfLink;
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
computeState.descriptionLink = azureVMDesc.documentSelfLink;
computeState.resourcePoolLink = computeHost.resourcePoolLink;
computeState.diskLinks = vmDisks;
computeState.networkInterfaceLinks = nicLinks;
computeState.customProperties = Collections.singletonMap(RESOURCE_GROUP_NAME, azureRgName);
computeState.groupLinks = Collections.singleton(defaultVmRGLink);
computeState.endpointLink = endpointState.documentSelfLink;
computeState.endpointLinks = new HashSet<>();
computeState.endpointLinks.add(endpointState.documentSelfLink);
computeState.computeHostLink = computeHost.documentSelfLink;
computeState.tenantLinks = endpointState.tenantLinks;
computeState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, AzureConstants.AzureResourceType.azure_vm.toString());
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
return TestUtils.doPost(host, computeState, ComputeState.class, UriUtils.buildUri(host, ComputeService.FACTORY_LINK));
}
Aggregations