use of com.vmware.xenon.common.ServiceDocumentQueryResult in project photon-model by vmware.
the class TestAWSEnumerationTask method testEnumerationPreserveLocalStates.
@Test
public void testEnumerationPreserveLocalStates() throws Throwable {
this.host.log("Running test: " + this.currentTestName.getMethodName());
ComputeState vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, TestAWSSetupUtils.class, zoneId, regionId, null, this.singleNicSpec, this.awsTestContext);
if (this.isMock) {
// Just make a call to the enumeration service and make sure that the adapter patches
// the parent with completion.
enumerateResourcesPreserveMissing(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_MOCK_MODE);
return;
}
// Overriding the page size to test the pagination logic with limited instances on AWS.
// This is a functional test
// so the latency numbers maybe higher from this test due to low page size.
setQueryPageSize(DEFAULT_TEST_PAGE_SIZE);
setQueryResultLimit(DEFAULT_TEST_PAGE_SIZE);
// Provision a single VM . Check initial state.
vmState = provisionMachine(this.host, vmState, this.isMock, this.instancesToCleanUp);
queryComputeInstances(this.host, count2);
queryDocumentsAndAssertExpectedCount(this.host, count2, ComputeDescriptionService.FACTORY_LINK, false);
// CREATION directly on AWS
List<String> instanceIdsToDelete = provisionAWSVMWithEC2Client(this.client, this.host, count4, T2_MICRO_INSTANCE_TYPE, this.subnetId, this.securityGroupId);
List<String> instanceIds = provisionAWSVMWithEC2Client(this.client, this.host, count1, instanceType, this.subnetId, this.securityGroupId);
instanceIdsToDelete.addAll(instanceIds);
this.instancesToCleanUp.addAll(instanceIdsToDelete);
waitForProvisioningToComplete(instanceIdsToDelete, this.host, this.client, ZERO);
// Xenon does not know about the new instances.
ProvisioningUtils.queryComputeInstances(this.host, count2);
enumerateResourcesPreserveMissing(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
// 5 new resources should be discovered. Mapping to 2 new compute description and 5 new
// compute states.
// Even though the "t2.micro" is common to the VM provisioned from Xenon
// service and the one directly provisioned on EC2, there is no Compute description
// linking of discovered resources to user defined compute descriptions. So a new system
// generated compute description will be created for "t2.micro"
queryDocumentsAndAssertExpectedCount(this.host, count4, ComputeDescriptionService.FACTORY_LINK, false);
queryDocumentsAndAssertExpectedCount(this.host, count7, ComputeService.FACTORY_LINK, false);
// Verify Deletion flow
// Delete 5 VMs spawned above of type T2_NANO
deleteVMsUsingEC2Client(this.client, this.host, instanceIdsToDelete);
enumerateResourcesPreserveMissing(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_DELETE_VMS);
// Counts should go down 5 compute states.
ServiceDocumentQueryResult queryResult = queryDocumentsAndAssertExpectedCount(this.host, count7, ComputeService.FACTORY_LINK, false);
List<ComputeState> localInstances = queryResult.documents.values().stream().map(d -> Utils.fromJson(d, ComputeState.class)).filter(c -> instanceIdsToDelete.contains(c.id)).collect(Collectors.toList());
assertEquals(instanceIdsToDelete.size(), localInstances.size());
for (ComputeState c : localInstances) {
assertEquals(LifecycleState.RETIRED, c.lifecycleState);
}
}
use of com.vmware.xenon.common.ServiceDocumentQueryResult in project photon-model by vmware.
the class TestAWSEnumerationTask method testTagEnumeration.
@Test
public void testTagEnumeration() throws Throwable {
if (this.isMock) {
return;
}
setUpTestVolume(this.host, this.client, this.awsTestContext, this.isMock);
this.snapshotId = (String) this.awsTestContext.get(TestAWSSetupUtils.SNAPSHOT_KEY);
this.ebsBlockDevice = new EbsBlockDevice().withSnapshotId(this.snapshotId);
this.blockDeviceMapping = new BlockDeviceMapping().withDeviceName(BLOCK_DEVICE_NAME).withEbs(this.ebsBlockDevice);
this.diskId = (String) this.awsTestContext.get(TestAWSSetupUtils.DISK_KEY);
this.host.log("Running test: " + this.currentTestName.getMethodName());
// VM tags
Tag tag1 = new Tag(VM_TAG_KEY_1, VM_TAG_VALUE_1);
Tag tag2 = new Tag(VM_TAG_KEY_2, VM_TAG_VALUE_2);
Tag tag3 = new Tag(VM_TAG_KEY_3, VM_TAG_VALUE_3);
List<Tag> vmTags = Arrays.asList(tag1, tag2, tag3);
// SG tag
List<Tag> sgTags = new ArrayList<>();
sgTags.add(new Tag(INITIAL_SG_TAG, INITIAL_SG_TAG));
// Network tag
List<Tag> networkTags = new ArrayList<>();
networkTags.add(new Tag(INITIAL_VPC_TAG, INITIAL_VPC_TAG));
// Subnet tag
List<Tag> subnetTags = new ArrayList<>();
subnetTags.add(new Tag(INITIAL_SUBNET_TAG, INITIAL_SUBNET_TAG));
// Disk tag
List<Tag> diskTags = new ArrayList<>();
diskTags.add(new Tag(INITIAL_DISK_TAG, INITIAL_DISK_TAG));
try {
String linuxVMId1 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
this.instancesToCleanUp.add(linuxVMId1);
waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
// Tag the first VM with a name and add some additional tags
tagResourcesWithName(this.client, VM_NAME, linuxVMId1);
List<Tag> linuxVMId1Tags = Arrays.asList(tag1, tag2);
// tag vm, default SG, VPC, Subnet and Disk
tagResources(this.client, linuxVMId1Tags, linuxVMId1);
tagResources(this.client, sgTags, this.securityGroupId);
tagResources(this.client, networkTags, this.vpcId);
tagResources(this.client, subnetTags, this.subnetId);
tagResources(this.client, diskTags, this.diskId);
enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
String linuxVMId2 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
this.instancesToCleanUp.add(linuxVMId2);
waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
// Name the second VM and add some tags
tagResourcesWithName(this.client, VM_UPDATED_NAME, linuxVMId2);
List<Tag> linuxVMId2Tags = Arrays.asList(tag2, tag3);
tagResources(this.client, linuxVMId2Tags, linuxVMId2);
// Un-tag the resources
unTagResources(this.client, sgTags, this.securityGroupId);
unTagResources(this.client, networkTags, this.vpcId);
unTagResources(this.client, subnetTags, this.subnetId);
unTagResources(this.client, diskTags, this.diskId);
// re-init tag arrays
sgTags = new ArrayList<>();
networkTags = new ArrayList<>();
subnetTags = new ArrayList<>();
diskTags = new ArrayList<>();
// new key-value set remotely should result in a new tag state created locally
// and a new tag link added to the SecurityGroupState, NetworkState, SubnetState and
// DiskState
sgTags.add(new Tag(SECONDARY_SG_TAG, SECONDARY_SG_TAG));
networkTags.add(new Tag(SECONDARY_VPC_TAG, SECONDARY_VPC_TAG));
subnetTags.add(new Tag(SECONDARY_SUBNET_TAG, SECONDARY_SUBNET_TAG));
diskTags.add(new Tag(SECONDARY_DISK_TAG, SECONDARY_DISK_TAG));
// tag again default SG, VPC, Subnet and Disk
tagResources(this.client, diskTags, this.diskId);
tagResources(this.client, sgTags, this.securityGroupId);
tagResources(this.client, networkTags, this.vpcId);
tagResources(this.client, subnetTags, this.subnetId);
enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
validateComputeName(linuxVMId1, VM_NAME);
validateComputeName(linuxVMId2, VM_UPDATED_NAME);
// Validate tag states number
int allTagsNumber = vmTags.size() + sgTags.size() + networkTags.size() + subnetTags.size() + diskTags.size();
queryDocumentsAndAssertExpectedCount(this.host, allTagsNumber, TagService.FACTORY_LINK, false);
ServiceDocumentQueryResult serviceDocumentQueryResult = queryAllFactoryResources(this.host, TagService.FACTORY_LINK);
Map<String, TagState> tagsMap = new HashMap<>();
for (Entry<String, Object> entry : serviceDocumentQueryResult.documents.entrySet()) {
tagsMap.put(entry.getKey(), Utils.fromJson(entry.getValue(), TagState.class));
}
// validate security group tags
Map<String, SecurityGroupState> allSecurityGroupStatesMap = ProvisioningUtils.<SecurityGroupState>getResourceStates(this.host, SecurityGroupService.FACTORY_LINK, SecurityGroupState.class);
SecurityGroupState defaultSgState = allSecurityGroupStatesMap.get(this.securityGroupId);
// ensure one link is deleted and one new is added to the sg state. One additional
// link is an internal tag.
assertNotNull(defaultSgState.tagLinks);
assertEquals("Wrong number of security-group tag links found.", 1 + internalTagsCount1, defaultSgState.tagLinks.size());
// validate vpc tags
Map<String, NetworkState> allNetworkStatesMap = ProvisioningUtils.<NetworkState>getResourceStates(this.host, NetworkService.FACTORY_LINK, NetworkState.class);
NetworkState defaultNetworkState = allNetworkStatesMap.get(this.vpcId);
// ensure one link is deleted and one new is added to the network state. One additional
// link is an internal tag.
assertEquals("Wrong number of network tag links found.", 1 + internalTagsCount1, defaultNetworkState.tagLinks.size());
// validate subnet tags
Map<String, SubnetState> allSubnetStatesMap = ProvisioningUtils.<SubnetState>getResourceStates(this.host, SubnetService.FACTORY_LINK, SubnetState.class);
SubnetState defaultSubnetState = allSubnetStatesMap.get(this.subnetId);
// ensure one link is deleted and one new is added to the subnet state. One additional
// link is an internal tag.
assertEquals("Wrong number of subnet tag links found.", 1 + internalTagsCount1, defaultSubnetState.tagLinks.size());
// validate disk tags
Map<String, DiskState> allDiskStatesMap = ProvisioningUtils.<DiskState>getResourceStates(this.host, DiskService.FACTORY_LINK, DiskState.class);
DiskState defaultDiskState = allDiskStatesMap.get(this.diskId);
// ensure one link is deleted and one new is added to the disk state
assertEquals("Wrong number of disk tag links found.", 1 + internalTagsCount1, defaultDiskState.tagLinks.size());
// ensure EBS disk has an internal type tag set
assertTrue(defaultDiskState.tagLinks.contains(TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.ebs_block.toString(), false, this.endpointState.tenantLinks).documentSelfLink));
// validate vm tags
Map<Tag, String> vmTagLinks = new HashMap<>();
for (Tag tag : vmTags) {
for (TagState tagState : tagsMap.values()) {
if (tagState.key.equals(tag.getKey())) {
vmTagLinks.put(tag, tagState.documentSelfLink);
}
}
}
ComputeState linuxVMId1ComputeState = getComputeByAWSId(this.host, linuxVMId1);
// compute has 2 remote tags + 1 local tag
assertEquals(linuxVMId1Tags.size() + internalTagsCount1, linuxVMId1ComputeState.tagLinks.size());
for (Tag tag : linuxVMId1Tags) {
assertTrue(linuxVMId1ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
}
ComputeState linuxVMId2ComputeState = getComputeByAWSId(this.host, linuxVMId2);
assertEquals(linuxVMId2Tags.size() + internalTagsCount1, linuxVMId2ComputeState.tagLinks.size());
for (Tag tag : linuxVMId2Tags) {
assertTrue(linuxVMId2ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
}
} catch (Throwable t) {
this.host.log("Exception occurred during test execution: %s", t.getMessage());
if (t instanceof AssertionError) {
fail("Assert exception occurred during test execution: " + t.getMessage());
}
} finally {
// un-tag default SG
unTagResources(this.client, sgTags, this.securityGroupId);
// un-tag default VPC
unTagResources(this.client, networkTags, this.vpcId);
// un-tag default Subnet
unTagResources(this.client, subnetTags, this.subnetId);
// un-tag default Disk
unTagResources(this.client, diskTags, this.diskId);
tearDownTestDisk(this.client, this.host, this.awsTestContext, this.isMock);
}
}
use of com.vmware.xenon.common.ServiceDocumentQueryResult in project photon-model by vmware.
the class TestAWSImageEnumerationTask method testImageEnumeration_delete.
private void testImageEnumeration_delete(boolean isPublic, String imageFilter) throws Throwable {
Assume.assumeFalse(this.isMock);
EndpointState endpointState = createEndpointState();
// Those images should not be touched by this image enum. {{
//
// Pre-create public and private image in different end-point
EndpointState azureEndpointState = createDummyEndpointState(EndpointType.azure);
ImageState publicImageState_diffEP = createImageState(azureEndpointState, true, PUBLIC);
ImageState privateImageState_diffEP = createImageState(azureEndpointState, true, PRIVATE);
// Pre-create public and private image in same end-point but different region
ImageState publicImageState_diffRegion = createImageState(endpointState, false, PUBLIC);
ImageState privateImageState_diffRegion = createImageState(endpointState, false, PRIVATE);
// }}
// Create one stale image that should be deleted by this enumeration
ImageState staleImageState = createImageState(endpointState, true, isPublic);
// Validate the 3 image states are preCREATED: 1 stale and 2 vSphere
int preCreatedCount = 1 + 2 + 2;
queryDocumentsAndAssertExpectedCount(getHost(), preCreatedCount, ImageService.FACTORY_LINK, EXACT_COUNT);
// Under TESTING
kickOffImageEnumeration(endpointState, isPublic, imageFilter);
// Validate 1 image state is CREATED and the 2 vSphere are UNtouched
int postEnumCount = 1 + 2 + 2;
// since we are not deleting stale resource anymore, just disassociating
postEnumCount++;
// them
ServiceDocumentQueryResult imagesAfterEnum = queryDocumentsAndAssertExpectedCount(getHost(), postEnumCount, ImageService.FACTORY_LINK, EXACT_COUNT);
// Validate 1 stale image state is DISASSOCIATED
ImageState staleImage = Utils.fromJson(imagesAfterEnum.documents.get(staleImageState.documentSelfLink), ImageState.class);
Assert.assertTrue("Dummy image should have been disassociated.", staleImage.endpointLinks.isEmpty());
// Validate vSphere images are untouched
Assert.assertTrue("Private images from other endpoints should not have been deleted.", imagesAfterEnum.documentLinks.contains(privateImageState_diffEP.documentSelfLink));
Assert.assertTrue("Public images from other endpoints should not have been deleted.", imagesAfterEnum.documentLinks.contains(publicImageState_diffEP.documentSelfLink));
Assert.assertTrue("Private images from same endpoints but different region should not have been deleted.", imagesAfterEnum.documentLinks.contains(privateImageState_diffRegion.documentSelfLink));
Assert.assertTrue("Public images from other endpoints should not have been deleted.", imagesAfterEnum.documentLinks.contains(publicImageState_diffRegion.documentSelfLink));
}
use of com.vmware.xenon.common.ServiceDocumentQueryResult in project photon-model by vmware.
the class TestAWSImageEnumerationTask method testPublicImageEnumeration_single.
@Test
public void testPublicImageEnumeration_single() throws Throwable {
// Important: MUST share same Endpoint between the two enum runs.
final EndpointState endpointState = createEndpointState();
ServiceDocumentQueryResult imagesAfterFirstEnum = null;
ImageState imageAfterFirstEnum = null;
ServiceDocumentQueryResult imagesAfterSecondEnum = null;
{
getHost().log(Level.INFO, "=== First enumeration should create a single '%s' image", AMAZON_PUBLIC_IMAGE_FILTER_SINGLE);
kickOffImageEnumeration(endpointState, PUBLIC, AMAZON_PUBLIC_IMAGE_FILTER_SINGLE);
if (!this.isMock) {
// Validate 1 image state is CREATED
imagesAfterFirstEnum = queryDocumentsAndAssertExpectedCount(getHost(), 1, ImageService.FACTORY_LINK, EXACT_COUNT);
imageAfterFirstEnum = Utils.fromJson(imagesAfterFirstEnum.documents.values().iterator().next(), ImageState.class);
// Validate created image is correctly populated
Assert.assertNotNull("Public image must have endpointType set.", imageAfterFirstEnum.endpointType);
Assert.assertNull("Public image must NOT have endpointLink set.", imageAfterFirstEnum.endpointLink);
Assert.assertNull("Public image must NOT have tenantLinks set.", imageAfterFirstEnum.tenantLinks);
Assert.assertNotNull("Disk configurations should not be null", imageAfterFirstEnum.diskConfigs);
Assert.assertTrue("There should be at least one disk configuration for boot disk", imageAfterFirstEnum.diskConfigs.size() > 0);
}
}
{
getHost().log(Level.INFO, "=== Second enumeration should update the single '%s' image", AMAZON_PUBLIC_IMAGE_FILTER_SINGLE);
if (!this.isMock) {
// Update local image state
updateImageState(imagesAfterFirstEnum.documentLinks.get(0));
}
kickOffImageEnumeration(endpointState, PUBLIC, AMAZON_PUBLIC_IMAGE_FILTER_SINGLE);
if (!this.isMock) {
// Validate 1 image state is UPDATED (and the local update above is overridden)
imagesAfterSecondEnum = queryDocumentsAndAssertExpectedCount(getHost(), 1, ImageService.FACTORY_LINK, EXACT_COUNT);
Assert.assertEquals("Images should be the same after the two enums", imagesAfterFirstEnum.documentLinks, imagesAfterSecondEnum.documentLinks);
ImageState imageAfterSecondEnum = Utils.fromJson(imagesAfterSecondEnum.documents.values().iterator().next(), ImageState.class);
Assert.assertNotEquals("Images timestamp should differ after the two enums", imageAfterFirstEnum.documentUpdateTimeMicros, imageAfterSecondEnum.documentUpdateTimeMicros);
Assert.assertTrue("Image name is not updated correctly after second enum.", !imageAfterSecondEnum.name.contains("OVERRIDE"));
Assert.assertNotNull("Disk configurations should not be null", imageAfterSecondEnum.diskConfigs);
Assert.assertTrue("There should be at least one disk configuration for boot disk", imageAfterSecondEnum.diskConfigs.size() > 0);
}
}
}
use of com.vmware.xenon.common.ServiceDocumentQueryResult in project photon-model by vmware.
the class TestAWSSetupUtils method deleteVMs.
/**
* Deletes the VM that is present on an endpoint and represented by the passed in ID.
*
* @param documentSelfLink
* @param isMock
* @param host
* @param deleteDocumentOnly
* @throws Throwable
*/
public static void deleteVMs(String documentSelfLink, boolean isMock, VerificationHost host, boolean deleteDocumentOnly) throws Throwable {
ResourceRemovalTaskState deletionState = new ResourceRemovalTaskState();
QuerySpecification resourceQuerySpec = new QueryTask.QuerySpecification();
// query all ComputeState resources for the cluster
resourceQuerySpec.query.setTermPropertyName(ServiceDocument.FIELD_NAME_SELF_LINK).setTermMatchValue(documentSelfLink);
deletionState.resourceQuerySpec = resourceQuerySpec;
deletionState.isMockRequest = isMock;
// Waiting for default request timeout in minutes for the machine to be turned OFF on AWS.
deletionState.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.MINUTES.toMicros(AWS_VM_REQUEST_TIMEOUT_MINUTES);
if (deleteDocumentOnly) {
deletionState.options = EnumSet.of(TaskOption.DOCUMENT_CHANGES_ONLY);
}
deletionState = TestUtils.doPost(host, deletionState, ResourceRemovalTaskState.class, UriUtils.buildUri(host, ResourceRemovalTaskService.FACTORY_LINK));
ProvisioningUtils.waitForTaskCompletion(host, deletionState.documentSelfLink, ResourceRemovalTaskState.class);
// check that the VMs are gone
ServiceDocumentQueryResult serviceDocumentQueryResult = ProvisioningUtils.queryAllFactoryResources(host, ComputeService.FACTORY_LINK);
List<String> documentLinks = serviceDocumentQueryResult.documentLinks;
assertFalse(documentLinks.contains(documentSelfLink));
}
Aggregations