Search in sources :

Example 91 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsNativeInstanceResourceBuilderTest method testBuildWhenInstanceExistAndNotRunningButNotTerminated.

@Test
public void testBuildWhenInstanceExistAndNotRunningButNotTerminated() throws Exception {
    CloudResource cloudResource = CloudResource.builder().name("name").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).params(emptyMap()).build();
    long privateId = 0;
    Instance instance = new Instance().withInstanceId("instanceId").withState(new InstanceState().withCode(0));
    when(awsMethodExecutor.execute(any(), eq(Optional.empty()))).thenReturn(Optional.of(instance));
    when(awsContext.getAmazonEc2Client()).thenReturn(amazonEc2Client);
    List<CloudResource> actual = underTest.build(awsContext, cloudInstance, privateId, ac, group, Collections.singletonList(cloudResource), cloudStack);
    Assertions.assertEquals(actual.get(0).getInstanceId(), "instanceId");
    verify(amazonEc2Client, times(1)).startInstances(any());
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 92 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsTaggingServiceTest method tesTagRootVolumesForInstancesMoreThanSingleRequestLimitAndNotAllVolumesFound.

@Test
public void tesTagRootVolumesForInstancesMoreThanSingleRequestLimitAndNotAllVolumesFound() {
    int instanceCount = 1200;
    CloudResource instance = CloudResource.builder().type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build();
    Instance awsInstance = new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sda1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1");
    Instance awsInstanceWithInvalidRootDisk = new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sdb1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1");
    List<CloudResource> instanceList = new ArrayList<>(instanceCount);
    for (int i = 0; i < instanceCount; i++) {
        instanceList.add(instance);
    }
    List<Instance> awsInstances = new ArrayList<>(instanceCount);
    for (int i = 0; i < 1100; i++) {
        awsInstances.add(awsInstance);
    }
    for (int i = 0; i < 100; i++) {
        awsInstances.add(awsInstanceWithInvalidRootDisk);
    }
    DescribeInstancesResult describeResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(awsInstances));
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    when(ec2Client.describeInstances(any())).thenReturn(describeResult);
    awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, instanceList, Map.of());
    verify(ec2Client, times(2)).createTags(tagRequestCaptor.capture());
    List<CreateTagsRequest> requests = tagRequestCaptor.getAllValues();
    assertEquals(1000, requests.get(0).getResources().size());
    assertEquals(100, requests.get(1).getResources().size());
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) Test(org.junit.Test)

Example 93 with Instance

use of com.google.api.services.notebooks.v1.model.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");
}
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 94 with Instance

use of com.google.api.services.notebooks.v1.model.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");
}
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) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 95 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsNativeMetadataCollectorTest method collectInstanceMetadataWhenOneOrMoreOfSpecifiedInstanceIdsDoNotExistAndMultipleBatchRequestsAreNecessary.

@Test
void collectInstanceMetadataWhenOneOrMoreOfSpecifiedInstanceIdsDoNotExistAndMultipleBatchRequestsAreNecessary() {
    List<CloudInstance> allInstances = List.of();
    List<CloudResource> resources = new ArrayList<>();
    List<CloudInstance> cloudInstances = new ArrayList<>();
    List<Instance> ec2Instances = new ArrayList<>();
    for (int i = 0; i < 15; i++) {
        String anInstanceId = "anInstanceId" + i;
        InstanceTemplate instanceTemplate = new InstanceTemplate("flavor", "alma", (long) i, Set.of(), CREATED, Map.of(), 1L, "imageid", TemporaryStorage.ATTACHED_VOLUMES, 0L);
        resources.add(getCloudResource(String.valueOf(i), "instanceName" + i, anInstanceId, AWS_INSTANCE));
        cloudInstances.add(new CloudInstance(anInstanceId, instanceTemplate, null, "subnet-123", "az1"));
        ec2Instances.add(getAnInstance(anInstanceId));
    }
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
    AmazonServiceException amazonServiceException = new AmazonServiceException("Instance with id could not be found: 'anInstanceId0'");
    amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    DescribeInstancesResult describeInstancesResult1 = new DescribeInstancesResult().withReservations(List.of(new Reservation().withInstances(ec2Instances.subList(1, 5))));
    AmazonServiceException amazonServiceException2 = new AmazonServiceException("Instance with id could not be found: 'anInstanceId5'");
    amazonServiceException2.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    DescribeInstancesResult describeInstancesResult2 = new DescribeInstancesResult().withReservations(List.of(new Reservation().withInstances(ec2Instances.subList(6, 10))));
    DescribeInstancesResult describeInstancesResult3 = new DescribeInstancesResult().withReservations(List.of(new Reservation().withInstances(ec2Instances.subList(10, ec2Instances.size()))));
    when(ec2Client.describeInstances(any())).thenThrow(amazonServiceException).thenReturn(describeInstancesResult1).thenThrow(amazonServiceException2).thenReturn(describeInstancesResult2).thenReturn(describeInstancesResult3);
    List<CloudVmMetaDataStatus> metaDataStatuses = underTest.collect(authenticatedContext, resources, cloudInstances, allInstances);
    verify(ec2Client, times(5)).describeInstances(any());
    assertFalse(metaDataStatuses.isEmpty());
    assertEquals(cloudInstances.size(), metaDataStatuses.size());
    for (int i = 0; i < 15; i++) {
        String instanceId = i == 0 || i == 5 ? String.valueOf(i) : "anInstanceId" + i;
        verifyCloudInstance(metaDataStatuses, instanceId, "subnet-123", "az1");
    }
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) 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)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)185 Reservation (com.amazonaws.services.ec2.model.Reservation)84 ArrayList (java.util.ArrayList)82 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)71 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)48 List (java.util.List)48 Tag (com.amazonaws.services.ec2.model.Tag)41 Test (org.junit.jupiter.api.Test)38 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)36 Map (java.util.Map)36 Collectors (java.util.stream.Collectors)32 HashMap (java.util.HashMap)26 Filter (com.amazonaws.services.ec2.model.Filter)25 Set (java.util.Set)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)22 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)18 InstanceState (com.amazonaws.services.ec2.model.InstanceState)18 Volume (com.amazonaws.services.ec2.model.Volume)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18