use of com.amazonaws.services.ec2.waiters.AmazonEC2Waiters in project cloudbreak by hortonworks.
the class AwsRepairTest method downscaleStack.
private void downscaleStack() throws IOException {
when(amazonEC2Client.describeVolumes(any())).thenReturn(new DescribeVolumesResult().withVolumes(new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_1).withState(VolumeState.InUse), new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_2).withState(VolumeState.InUse)));
when(amazonCloudFormationClient.describeStackResource(any())).thenReturn(new DescribeStackResourceResult().withStackResourceDetail(new StackResourceDetail().withPhysicalResourceId(AUTOSCALING_GROUP_NAME)));
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));
});
AmazonEC2Waiters mockWaiter = mock(AmazonEC2Waiters.class);
when(amazonEC2Client.waiters()).thenReturn(mockWaiter);
when(mockWaiter.instanceTerminated()).thenReturn(mock(Waiter.class));
DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
autoScalingGroup.setInstances(List.of(new Instance().withInstanceId(INSTANCE_ID_1), new Instance().withInstanceId(INSTANCE_ID_2), new Instance().withInstanceId(INSTANCE_ID_3)));
describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
when(amazonAutoScalingClient.describeAutoScalingGroups(any())).thenReturn(describeAutoScalingGroupsResult);
List<Volume> volumes = List.of();
InstanceTemplate instanceTemplate = new InstanceTemplate("", WORKER_GROUP, 0L, volumes, InstanceStatus.STARTED, Map.of(), 0L, IMAGE_ID, TemporaryStorage.ATTACHED_VOLUMES, 0L);
InstanceAuthentication authentication = new InstanceAuthentication("publicKey", "publicKeyId", "cloudbreak");
CloudInstance firstCloudInstance = new CloudInstance(INSTANCE_ID_1, instanceTemplate, authentication, "subnet-1", "az1");
CloudInstance secondCloudInstance = new CloudInstance(INSTANCE_ID_2, instanceTemplate, authentication, "subnet-1", "az1");
List<CloudInstance> cloudInstancesToRemove = List.of(firstCloudInstance, secondCloudInstance);
CloudResource instance1VolumeResource = createVolumeResource(VOLUME_ID_1, INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1, CommonStatus.CREATED);
CloudResource instance2VolumeResource = createVolumeResource(VOLUME_ID_2, INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2, CommonStatus.CREATED);
List<CloudResource> resources = List.of(instance1VolumeResource, instance2VolumeResource);
AuthenticatedContext authenticatedContext = componentTestUtil.getAuthenticatedContext();
CloudStack cloudStack = componentTestUtil.getStack(InstanceStatus.DELETE_REQUESTED, InstanceStatus.CREATE_REQUESTED);
underTest.downscale(authenticatedContext, cloudStack, resources, cloudInstancesToRemove, null);
verify(persistenceNotifier).notifyUpdate(argThat(cloudResource -> ResourceType.AWS_VOLUMESET.equals(cloudResource.getType()) && VOLUME_ID_1.equals(cloudResource.getName()) && INSTANCE_ID_1.equals(cloudResource.getInstanceId()) && CommonStatus.DETACHED.equals(cloudResource.getStatus()) && cloudResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class).getDeleteOnTermination()), eq(authenticatedContext.getCloudContext()));
verify(persistenceNotifier).notifyUpdate(argThat(cloudResource -> ResourceType.AWS_VOLUMESET.equals(cloudResource.getType()) && VOLUME_ID_2.equals(cloudResource.getName()) && INSTANCE_ID_2.equals(cloudResource.getInstanceId()) && CommonStatus.DETACHED.equals(cloudResource.getStatus()) && cloudResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class).getDeleteOnTermination()), eq(authenticatedContext.getCloudContext()));
verify(amazonAutoScalingClient).detachInstances(argThat(argument -> argument.getAutoScalingGroupName().equals(AUTOSCALING_GROUP_NAME) && argument.getShouldDecrementDesiredCapacity() && argument.getInstanceIds().size() == 2 && argument.getInstanceIds().contains(INSTANCE_ID_1) && argument.getInstanceIds().contains(INSTANCE_ID_2)));
verify(amazonEC2Client).terminateInstances(argThat(argument -> argument.getInstanceIds().size() == 2 && argument.getInstanceIds().contains(INSTANCE_ID_1) && argument.getInstanceIds().contains(INSTANCE_ID_2)));
verify(amazonAutoScalingClient).updateAutoScalingGroup(argThat(argument -> argument.getAutoScalingGroupName().equals(AUTOSCALING_GROUP_NAME) && argument.getMaxSize().equals(1)));
}
use of com.amazonaws.services.ec2.waiters.AmazonEC2Waiters in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method downscaleASGWhenAllInstancesHaveBeenRemovedFromASG.
@Test
void downscaleASGWhenAllInstancesHaveBeenRemovedFromASG() {
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);
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");
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());
assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
use of com.amazonaws.services.ec2.waiters.AmazonEC2Waiters in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method downscaleOrderTest.
@Test
void downscaleOrderTest() {
// We need to invoke detach, terminate and update ASG in this order othervise a strange sporadic concurrency issue can occure on AWS side
// The proper order is:
// amazonASClient.detachInstances(...);
// amazonEC2Client.terminateInstances(...);
// amazonASClient.updateAutoScalingGroup(...);
CloudStack stack = mock(CloudStack.class);
List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").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");
cloudInstances.add(workerInstance1);
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);
DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
when(amazonAutoScalingClient.describeAutoScalingGroups(any())).thenReturn(describeAutoScalingGroupsResult);
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(amazonAutoScalingClient.detachInstances(any())).thenReturn(new DetachInstancesResult());
when(amazonEC2Client.terminateInstances(any())).thenReturn(new TerminateInstancesResult());
when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
mockDescribeInstances(amazonEC2Client);
// create inOrder object passing any mocks that need to be verified in order
InOrder inOrder = Mockito.inOrder(amazonAutoScalingClient, amazonEC2Client);
underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
// Following will make sure that detach, ivoked before terminate and terminate invoked before update ASG!
inOrder.verify(amazonAutoScalingClient).detachInstances(any());
inOrder.verify(amazonEC2Client).terminateInstances(any());
inOrder.verify(amazonAutoScalingClient).updateAutoScalingGroup(any());
}
use of com.amazonaws.services.ec2.waiters.AmazonEC2Waiters in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method downscaleWithLoadBalancers.
@Test
void downscaleWithLoadBalancers() {
CloudStack stack = mock(CloudStack.class);
List<CloudResource> resources = Collections.emptyList();
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
when(workerTemplate.getGroupName()).thenReturn("worker");
List<CloudInstance> cloudInstances = List.of(new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1"));
CloudLoadBalancer privateLoadBalancer = new CloudLoadBalancer(LoadBalancerType.PRIVATE);
CloudLoadBalancer publicLoadBalancer = new CloudLoadBalancer(LoadBalancerType.PUBLIC);
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");
when(stack.getLoadBalancers()).thenReturn(List.of(privateLoadBalancer, publicLoadBalancer));
doNothing().when(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
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);
verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
}
use of com.amazonaws.services.ec2.waiters.AmazonEC2Waiters in project cloudbreak by hortonworks.
the class AwsRepairTest method upscaleStack.
private void upscaleStack() throws Exception {
AuthenticatedContext authenticatedContext = componentTestUtil.getAuthenticatedContext();
CloudStack stack = componentTestUtil.getStack(InstanceStatus.CREATE_REQUESTED, InstanceStatus.STARTED);
List<CloudResource> cloudResources = List.of(CloudResource.builder().name(AWS_SUBNET_ID).type(ResourceType.AWS_SUBNET).build(), createVolumeResource(VOLUME_ID_1, INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1, CommonStatus.DETACHED), createVolumeResource(VOLUME_ID_2, INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2, CommonStatus.DETACHED), createVolumeResource(VOLUME_ID_3, INSTANCE_ID_3, SIZE_DISK_2, FSTAB_2, CommonStatus.CREATED));
InMemoryStateStore.putStack(1L, PollGroup.POLLABLE);
when(amazonCloudFormationClient.describeStackResource(any())).thenReturn(new DescribeStackResourceResult().withStackResourceDetail(new StackResourceDetail().withPhysicalResourceId(AUTOSCALING_GROUP_NAME)));
when(amazonAutoScalingClient.describeAutoScalingGroups(any())).thenReturn(new DescribeAutoScalingGroupsResult().withAutoScalingGroups(new AutoScalingGroup().withAutoScalingGroupName(AUTOSCALING_GROUP_NAME).withInstances(List.of(new Instance().withInstanceId(INSTANCE_ID_1).withLifecycleState(LifecycleState.InService), new Instance().withInstanceId(INSTANCE_ID_2).withLifecycleState(LifecycleState.InService)))));
when(amazonEC2Client.describeVolumes(any())).thenReturn(new DescribeVolumesResult().withVolumes(new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_1).withState(VolumeState.Available), new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_2).withState(VolumeState.Available), new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_3).withState(VolumeState.InUse)));
when(amazonEC2Client.describeInstances(any())).thenReturn(new DescribeInstancesResult().withReservations(new Reservation().withInstances(new com.amazonaws.services.ec2.model.Instance().withInstanceId("i-instance"))));
DescribeScalingActivitiesResult result = new DescribeScalingActivitiesResult();
result.setActivities(List.of());
when(amazonAutoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(result);
AmazonEC2Waiters waiters = mock(AmazonEC2Waiters.class);
when(amazonEC2Client.waiters()).thenReturn(waiters);
Waiter<DescribeInstancesRequest> instanceWaiter = mock(Waiter.class);
when(waiters.instanceRunning()).thenReturn(instanceWaiter);
when(amazonAutoScalingClient.waiters()).thenReturn(asWaiters);
when(asWaiters.groupInService()).thenReturn(describeAutoScalingGroupsRequestWaiter);
underTest.upscale(authenticatedContext, stack, cloudResources, new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L));
verify(amazonAutoScalingClient).resumeProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && argument.getScalingProcesses().contains("Launch")));
verify(amazonAutoScalingClient).updateAutoScalingGroup(argThat(argument -> {
Group workerGroup = stack.getGroups().get(1);
return AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && workerGroup.getInstancesSize().equals(argument.getMaxSize()) && workerGroup.getInstancesSize().equals(argument.getDesiredCapacity());
}));
verify(amazonAutoScalingClient, times(stack.getGroups().size())).suspendProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && SUSPENDED_PROCESSES.equals(argument.getScalingProcesses())));
ArgumentCaptor<CloudResource> updatedCloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
verify(resourceNotifier, times(2)).notifyUpdate(updatedCloudResourceArgumentCaptor.capture(), any());
assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1);
assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2);
}
Aggregations