use of com.sequenceiq.common.api.type.ResourceType.AWS_INSTANCE in project cloudbreak by hortonworks.
the class AwsNativeMetadataCollectorTest method collectInstanceMetadataWhenOneOrMoreOfSpecifiedInstanceIdsDoNotExist.
@Test
void collectInstanceMetadataWhenOneOrMoreOfSpecifiedInstanceIdsDoNotExist() {
List<CloudInstance> allInstances = List.of();
String anInstanceId = "anInstanceId";
CloudResource cloudResource = getCloudResource(String.valueOf(1L), "instanceName", anInstanceId, AWS_INSTANCE);
String secondInstanceId = "secondInstanceId";
CloudResource secondCloudResource = getCloudResource(String.valueOf(2L), "secondInstanceName", secondInstanceId, AWS_INSTANCE);
List<CloudResource> resources = List.of(cloudResource, secondCloudResource);
InstanceTemplate instanceTemplate = new InstanceTemplate("flavor", "alma", 1L, Set.of(), CREATED, Map.of(), 1L, "imageid", TemporaryStorage.ATTACHED_VOLUMES, 0L);
CloudInstance cloudInstance = new CloudInstance(anInstanceId, instanceTemplate, null, "subnet-123", "az1");
InstanceTemplate secondInstanceTemplate = new InstanceTemplate("flavor", "alma", 2L, Set.of(), CREATED, Map.of(), 1L, "imageid", TemporaryStorage.ATTACHED_VOLUMES, 0L);
CloudInstance secondCloudInstance = new CloudInstance(secondInstanceId, secondInstanceTemplate, null, "subnet-123", "az1");
List<CloudInstance> cloudInstances = List.of(cloudInstance, secondCloudInstance);
when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
Instance anInstance = getAnInstance(anInstanceId);
Reservation reservation = new Reservation().withInstances(anInstance);
List<Reservation> reservations = List.of(reservation);
DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult().withReservations(reservations);
String instancesNotFoundMessage = String.format("Instance with ID could not be found: '%s'", secondInstanceId);
AmazonServiceException amazonServiceException = new AmazonServiceException(instancesNotFoundMessage);
amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
when(ec2Client.describeInstances(any())).thenThrow(amazonServiceException).thenReturn(describeInstancesResult);
List<CloudVmMetaDataStatus> metaDataStatuses = underTest.collect(authenticatedContext, resources, cloudInstances, allInstances);
verify(ec2Client, times(2)).describeInstances(any());
assertFalse(metaDataStatuses.isEmpty());
assertEquals(2, metaDataStatuses.size());
assertTrue(metaDataStatuses.stream().anyMatch(metaDataStatus -> InstanceStatus.TERMINATED.equals(metaDataStatus.getCloudVmInstanceStatus().getStatus())));
assertTrue(metaDataStatuses.stream().allMatch(vmMetaDataStatus -> isNotEmpty(vmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance().getInstanceId())));
verifyCloudInstance(metaDataStatuses, anInstanceId, "subnet-123", "az1");
verifyCloudInstance(metaDataStatuses, String.valueOf(2L), "subnet-123", "az1");
}
use of com.sequenceiq.common.api.type.ResourceType.AWS_INSTANCE in project cloudbreak by hortonworks.
the class AwsNativeMetadataCollectorTest method collectInstanceMetadataWhenTheSpecifiedInstanceIdsExist.
@Test
void collectInstanceMetadataWhenTheSpecifiedInstanceIdsExist() {
List<CloudInstance> allInstances = List.of();
String anInstanceId = "anInstanceId";
CloudResource cloudResource = getCloudResource(String.valueOf(1L), "instanceName", anInstanceId, AWS_INSTANCE);
String secondInstanceId = "secondInstanceId";
CloudResource secondCloudResource = getCloudResource(String.valueOf(2L), "secondInstanceName", secondInstanceId, AWS_INSTANCE);
List<CloudResource> resources = List.of(cloudResource, secondCloudResource);
InstanceTemplate instanceTemplate = new InstanceTemplate("flavor", "alma", 1L, Set.of(), CREATED, Map.of(), 1L, "imageid", TemporaryStorage.ATTACHED_VOLUMES, 0L);
InstanceTemplate otherInstanceTemplate = new InstanceTemplate("flavor", "alma", 2L, Set.of(), CREATED, Map.of(), 1L, "imageid", TemporaryStorage.ATTACHED_VOLUMES, 0L);
CloudInstance cloudInstance = new CloudInstance(anInstanceId, instanceTemplate, null, "subnet-123", "az1");
CloudInstance secondCloudInstance = new CloudInstance(secondInstanceId, otherInstanceTemplate, null, "subnet-123", "az1");
List<CloudInstance> cloudInstances = List.of(cloudInstance, secondCloudInstance);
when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
Instance anInstance = getAnInstance(anInstanceId);
Instance secondInstance = getAnInstance(secondInstanceId);
Reservation reservation = new Reservation().withInstances(anInstance, secondInstance);
List<Reservation> reservations = List.of(reservation);
DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult().withReservations(reservations);
when(ec2Client.describeInstances(any())).thenReturn(describeInstancesResult);
List<CloudVmMetaDataStatus> metaDataStatuses = underTest.collect(authenticatedContext, resources, cloudInstances, allInstances);
verify(ec2Client, times(1)).describeInstances(any());
assertFalse(metaDataStatuses.isEmpty());
assertEquals(resources.size(), metaDataStatuses.size());
assertTrue(metaDataStatuses.stream().allMatch(vmMetaDataStatus -> isNotEmpty(vmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance().getInstanceId())));
verifyCloudInstance(metaDataStatuses, anInstanceId, "subnet-123", "az1");
verifyCloudInstance(metaDataStatuses, secondInstanceId, "subnet-123", "az1");
}
use of com.sequenceiq.common.api.type.ResourceType.AWS_INSTANCE in project cloudbreak by hortonworks.
the class AwsNativeMetadataCollectorTest method collectInstanceMetadataWhenTheSpecifiedInstanceIdsDoNotExist.
@Test
void collectInstanceMetadataWhenTheSpecifiedInstanceIdsDoNotExist() {
List<CloudInstance> allInstances = List.of();
String anInstanceId = "anInstanceId";
CloudResource cloudResource = getCloudResource(String.valueOf(1L), "instanceName", anInstanceId, AWS_INSTANCE);
String secondInstanceId = "secondInstanceId";
CloudResource secondCloudResource = getCloudResource(String.valueOf(2L), "secondInstanceName", secondInstanceId, AWS_INSTANCE);
List<CloudResource> resources = List.of(cloudResource, secondCloudResource);
InstanceTemplate instanceTemplate = new InstanceTemplate("flavor", "alma", 1L, Set.of(), CREATED, Map.of(), 1L, "imageid", TemporaryStorage.ATTACHED_VOLUMES, 0L);
CloudInstance cloudInstance = new CloudInstance(anInstanceId, instanceTemplate, null, "subnet-123", "az1");
InstanceTemplate secondInstanceTemplate = new InstanceTemplate("flavor", "alma", 2L, Set.of(), CREATED, Map.of(), 1L, "imageid", TemporaryStorage.ATTACHED_VOLUMES, 0L);
CloudInstance secondCloudInstance = new CloudInstance(secondInstanceId, secondInstanceTemplate, null, "subnet-123", "az1");
List<CloudInstance> cloudInstances = List.of(cloudInstance, secondCloudInstance);
when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
String instancesNotFoundMessage = String.format("Instance with id could not be found: '%s, %s'", anInstanceId, secondInstanceId);
AmazonServiceException amazonServiceException = new AmazonServiceException(instancesNotFoundMessage);
amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
when(ec2Client.describeInstances(any())).thenThrow(amazonServiceException);
List<CloudVmMetaDataStatus> metaDataStatuses = underTest.collect(authenticatedContext, resources, cloudInstances, allInstances);
verify(ec2Client, times(1)).describeInstances(any());
assertFalse(metaDataStatuses.isEmpty());
assertTrue(metaDataStatuses.stream().allMatch(cloudVmMetaDataStatus -> InstanceStatus.TERMINATED.equals(cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus())));
assertTrue(metaDataStatuses.stream().allMatch(vmMetaDataStatus -> isNotEmpty(vmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance().getInstanceId())));
}
Aggregations