Search in sources :

Example 81 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsDownscaleServiceTest method downscaleASG.

@Test
void downscaleASG() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    cloudInstances.add(workerInstance2);
    cloudInstances.add(workerInstance3);
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
    AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
    Waiter waiter = mock(Waiter.class);
    when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    ArgumentCaptor<DetachInstancesRequest> detachInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(DetachInstancesRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    when(amazonAutoScalingClient.detachInstances(detachInstancesRequestArgumentCaptor.capture())).thenReturn(new DetachInstancesResult());
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    List<DetachInstancesRequest> allValues = detachInstancesRequestArgumentCaptor.getAllValues();
    assertThat(allValues.get(0).getInstanceIds(), contains("i-worker1"));
    verify(amazonAutoScalingClient, times(1)).detachInstances(any());
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.autoscaling.model.Instance) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Waiter(com.amazonaws.waiters.Waiter) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 82 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsDownscaleServiceTest method downscaleNoWaiterIfNoInstanceLeftOnAWS.

@Test
public void downscaleNoWaiterIfNoInstanceLeftOnAWS() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-4").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance4 = new CloudInstance("i-worker4", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    cloudInstances.add(workerInstance2);
    cloudInstances.add(workerInstance3);
    cloudInstances.add(workerInstance4);
    CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    Waiter waiter = mock(Waiter.class);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    AmazonServiceException amazonServiceException = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker1\",\"i-worker2\",\"i-worker3\",\"i-worker4\"");
    amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    when(amazonEC2Client.terminateInstances(any())).thenThrow(amazonServiceException).thenReturn(new TerminateInstancesResult());
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of());
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    verify(amazonAutoScalingClient, never()).detachInstances(any());
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    ArgumentCaptor<TerminateInstancesRequest> terminateInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(TerminateInstancesRequest.class);
    verify(amazonEC2Client, times(1)).terminateInstances(terminateInstancesRequestArgumentCaptor.capture());
    List<TerminateInstancesRequest> allValues = terminateInstancesRequestArgumentCaptor.getAllValues();
    List<String> firstTerminateInstanceIds = allValues.get(0).getInstanceIds();
    assertEquals(4, firstTerminateInstanceIds.size());
    assertTrue(firstTerminateInstanceIds.contains("i-worker1"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker2"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
    verify(waiter, times(0)).run(any());
}
Also used : ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Waiter(com.amazonaws.waiters.Waiter) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 83 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsDownscaleServiceTest method mockDescribeInstances.

private void mockDescribeInstances(AmazonEc2Client amazonEC2Client) {
    when(amazonEC2Client.describeInstances(any(DescribeInstancesRequest.class))).thenAnswer(a -> {
        DescribeInstancesRequest request = a.getArgument(0, DescribeInstancesRequest.class);
        List<com.amazonaws.services.ec2.model.Instance> instances = request.getInstanceIds().stream().map(i -> new com.amazonaws.services.ec2.model.Instance().withInstanceId(i)).collect(Collectors.toList());
        return new DescribeInstancesResult().withReservations(new Reservation().withInstances(instances));
    });
}
Also used : LoadBalancerService(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerService) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Mockito.doThrow(org.mockito.Mockito.doThrow) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) INSTANCE_NOT_FOUND_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Waiter(com.amazonaws.waiters.Waiter) Reservation(com.amazonaws.services.ec2.model.Reservation) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) AmazonServiceException(com.amazonaws.AmazonServiceException) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Mockito.doNothing(org.mockito.Mockito.doNothing) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Matchers.contains(org.hamcrest.Matchers.contains) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) WaiterParameters(com.amazonaws.waiters.WaiterParameters) ArrayList(java.util.ArrayList) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) ArgumentCaptor(org.mockito.ArgumentCaptor) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) InOrder(org.mockito.InOrder) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Mockito.verify(org.mockito.Mockito.verify) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Instance(com.amazonaws.services.autoscaling.model.Instance) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) Collections(java.util.Collections) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.autoscaling.model.Instance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 84 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsDownscaleServiceTest method downscaleButInstanceNotFoundOnAWS.

@Test
public void downscaleButInstanceNotFoundOnAWS() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-4").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance4 = new CloudInstance("i-worker4", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    cloudInstances.add(workerInstance2);
    cloudInstances.add(workerInstance3);
    cloudInstances.add(workerInstance4);
    CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
    Waiter waiter = mock(Waiter.class);
    when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    AmazonServiceException amazonServiceException = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker1\",\"i-worker2\"");
    amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    when(amazonEC2Client.terminateInstances(any())).thenThrow(amazonServiceException).thenReturn(new TerminateInstancesResult());
    AmazonServiceException amazonServiceExceptionForWaiter = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker3\"");
    amazonServiceExceptionForWaiter.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    doThrow(amazonServiceExceptionForWaiter).doNothing().when(waiter).run(any());
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of());
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    verify(amazonAutoScalingClient, never()).detachInstances(any());
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    ArgumentCaptor<TerminateInstancesRequest> terminateInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(TerminateInstancesRequest.class);
    verify(amazonEC2Client, times(2)).terminateInstances(terminateInstancesRequestArgumentCaptor.capture());
    List<TerminateInstancesRequest> allValues = terminateInstancesRequestArgumentCaptor.getAllValues();
    List<String> firstTerminateInstanceIds = allValues.get(0).getInstanceIds();
    assertEquals(4, firstTerminateInstanceIds.size());
    assertTrue(firstTerminateInstanceIds.contains("i-worker1"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker2"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
    List<String> secondTerminateInstanceIds = allValues.get(1).getInstanceIds();
    assertEquals(2, secondTerminateInstanceIds.size());
    assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
    ArgumentCaptor<WaiterParameters> waiterParametersArgumentCaptor = ArgumentCaptor.forClass(WaiterParameters.class);
    verify(waiter, times(2)).run(waiterParametersArgumentCaptor.capture());
    List<WaiterParameters> waiterParametersList = waiterParametersArgumentCaptor.getAllValues();
    DescribeInstancesRequest firstDescribeInstancesRequest = (DescribeInstancesRequest) waiterParametersList.get(0).getRequest();
    assertEquals(2, firstDescribeInstancesRequest.getInstanceIds().size());
    assertTrue(firstDescribeInstancesRequest.getInstanceIds().contains("i-worker3"));
    assertTrue(firstDescribeInstancesRequest.getInstanceIds().contains("i-worker4"));
    DescribeInstancesRequest secondDescribeInstancesRequest = (DescribeInstancesRequest) waiterParametersList.get(1).getRequest();
    assertEquals(1, secondDescribeInstancesRequest.getInstanceIds().size());
    assertTrue(secondDescribeInstancesRequest.getInstanceIds().contains("i-worker4"));
    assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
Also used : ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) WaiterParameters(com.amazonaws.waiters.WaiterParameters) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Waiter(com.amazonaws.waiters.Waiter) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 85 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsNetworkConnectorTest method testGetNetworkCidrWithoutResult.

@Test
public void testGetNetworkCidrWithoutResult() {
    String existingVpc = "vpc-1";
    Network network = new Network(null, Map.of(NetworkConstants.VPC_ID, existingVpc, "region", "us-west-2"));
    CloudCredential credential = new CloudCredential();
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    DescribeVpcsResult describeVpcsResult = describeVpcsResult();
    when(awsClient.createEc2Client(any(AwsCredentialView.class), eq("us-west-2"))).thenReturn(amazonEC2Client);
    when(amazonEC2Client.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc))).thenReturn(describeVpcsResult);
    thrown.expect(BadRequestException.class);
    thrown.expectMessage("VPC cidr could not fetch from AWS: " + existingVpc);
    underTest.getNetworkCidr(network, credential);
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Test(org.junit.Test)

Aggregations

AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)97 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)44 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)41 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)30 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)29 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)29 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)28 HashMap (java.util.HashMap)28 Group (com.sequenceiq.cloudbreak.cloud.model.Group)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)23 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)23 Network (com.sequenceiq.cloudbreak.cloud.model.Network)23 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)22 AmazonServiceException (com.amazonaws.AmazonServiceException)21 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)21 List (java.util.List)21 Vpc (com.amazonaws.services.ec2.model.Vpc)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)20