Search in sources :

Example 1 with INSTANCE_NOT_FOUND_ERROR_CODE

use of com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE 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");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) INSTANCE_NOT_FOUND_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Reservation(com.amazonaws.services.ec2.model.Reservation) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) AmazonServiceException(com.amazonaws.AmazonServiceException) AwsNativeLbMetadataCollector(com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeLbMetadataCollector) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) TemporaryStorage(com.sequenceiq.cloudbreak.common.type.TemporaryStorage) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) ELASTIC_LOAD_BALANCER(com.sequenceiq.common.api.type.ResourceType.ELASTIC_LOAD_BALANCER) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) AWS_INSTANCE(com.sequenceiq.common.api.type.ResourceType.AWS_INSTANCE) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) LoadBalancerSchemeEnum(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerSchemeEnum) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) AwsLifeCycleMapper(com.sequenceiq.cloudbreak.cloud.aws.common.util.AwsLifeCycleMapper) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) ArrayList(java.util.ArrayList) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) LoadBalancerNotFoundException(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerNotFoundException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) InstanceLifecycle(com.amazonaws.services.ec2.model.InstanceLifecycle) LOAD_BALANCER_NOT_FOUND_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeMetadataCollector.LOAD_BALANCER_NOT_FOUND_ERROR_CODE) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Instance(com.amazonaws.services.ec2.model.Instance) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Mockito.verify(org.mockito.Mockito.verify) AwsNativeMetadataCollector(com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeMetadataCollector) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Assertions(org.junit.jupiter.api.Assertions) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) CREATED(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus.CREATED) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 2 with INSTANCE_NOT_FOUND_ERROR_CODE

use of com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE 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())));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) INSTANCE_NOT_FOUND_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Reservation(com.amazonaws.services.ec2.model.Reservation) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) AmazonServiceException(com.amazonaws.AmazonServiceException) AwsNativeLbMetadataCollector(com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeLbMetadataCollector) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) TemporaryStorage(com.sequenceiq.cloudbreak.common.type.TemporaryStorage) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) ELASTIC_LOAD_BALANCER(com.sequenceiq.common.api.type.ResourceType.ELASTIC_LOAD_BALANCER) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) AWS_INSTANCE(com.sequenceiq.common.api.type.ResourceType.AWS_INSTANCE) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) LoadBalancerSchemeEnum(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerSchemeEnum) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) AwsLifeCycleMapper(com.sequenceiq.cloudbreak.cloud.aws.common.util.AwsLifeCycleMapper) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) ArrayList(java.util.ArrayList) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) LoadBalancerNotFoundException(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerNotFoundException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) InstanceLifecycle(com.amazonaws.services.ec2.model.InstanceLifecycle) LOAD_BALANCER_NOT_FOUND_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeMetadataCollector.LOAD_BALANCER_NOT_FOUND_ERROR_CODE) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Instance(com.amazonaws.services.ec2.model.Instance) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Mockito.verify(org.mockito.Mockito.verify) AwsNativeMetadataCollector(com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeMetadataCollector) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Assertions(org.junit.jupiter.api.Assertions) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) CREATED(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus.CREATED) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)2 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)2 Instance (com.amazonaws.services.ec2.model.Instance)2 InstanceLifecycle (com.amazonaws.services.ec2.model.InstanceLifecycle)2 Reservation (com.amazonaws.services.ec2.model.Reservation)2 DescribeLoadBalancersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult)2 LoadBalancer (com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer)2 LoadBalancerNotFoundException (com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerNotFoundException)2 LoadBalancerSchemeEnum (com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancerSchemeEnum)2 INSTANCE_NOT_FOUND_ERROR_CODE (com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE)2 CommonAwsClient (com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient)2 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)2 AmazonElasticLoadBalancingClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient)2 LoadBalancerTypeConverter (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter)2 AwsLifeCycleMapper (com.sequenceiq.cloudbreak.cloud.aws.common.util.AwsLifeCycleMapper)2 AwsNativeLbMetadataCollector (com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeLbMetadataCollector)2 AwsNativeMetadataCollector (com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeMetadataCollector)2 LOAD_BALANCER_NOT_FOUND_ERROR_CODE (com.sequenceiq.cloudbreak.cloud.aws.metadata.AwsNativeMetadataCollector.LOAD_BALANCER_NOT_FOUND_ERROR_CODE)2 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)2 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2