@Test public void testCheckLastScalingActivityWhenActivitiesSuccessThenNoException() throws AmazonAutoscalingFailed { DescribeScalingActivitiesResult result = new DescribeScalingActivitiesResult(); Activity activity1 = new Activity(); activity1.setStatusMessage("Status"); activity1.setDescription("Description"); activity1.setCause("Cause"); activity1.setStatusCode("success"); result.setActivities(List.of(activity1)); Group group = createGroup("master", InstanceGroupType.GATEWAY, List.of(new CloudInstance("anId", null, null))); when(amazonAutoScalingRetryClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(result); when(customAmazonWaiterProvider.getAutoscalingActivitiesWaiter(any(), any())).thenReturn(describeScalingActivitiesRequestWaiter); Date date = new Date(); underTest.checkLastScalingActivity(amazonAutoScalingClient, amazonAutoScalingRetryClient, "asGroup", date, group); }
@VisibleForTesting void checkLastScalingActivity(AmazonAutoScalingClient asClient, AmazonAutoScalingRetryClient asRetryClient, String autoScalingGroupName, Date timeBeforeASUpdate, Group group) throws AmazonAutoscalingFailed { if (group.getInstancesSize() > 0) { LOGGER.debug("Check last activity after AS update. Wait for the first if it doesn't exist. [asGroup: {}]", autoScalingGroupName); Optional<Activity> firstActivity; try { Waiter<DescribeScalingActivitiesRequest> autoscalingActivitiesWaiter = customAmazonWaiterProvider .getAutoscalingActivitiesWaiter(asClient, timeBeforeASUpdate); autoscalingActivitiesWaiter.run(new WaiterParameters<>(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName))); DescribeScalingActivitiesResult describeScalingActivitiesResult = asRetryClient .describeScalingActivities(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName)); firstActivity = describeScalingActivitiesResult.getActivities().stream().findFirst() .filter(activity -> "failed".equals(activity.getStatusCode().toLowerCase()) && !activity.getStatusMessage().contains("InsufficientInstanceCapacity")); } catch (Exception e) { LOGGER.error("Failed to list activities: {}", e.getMessage(), e); throw new AmazonAutoscalingFailed(e.getMessage(), e); } if (firstActivity.isPresent()) { Activity activity = firstActivity.get(); LOGGER.error("Cannot execute autoscale, because last activity is failed: {}", activity); throw new AmazonAutoscalingFailed(activity.getDescription() + " " + activity.getCause()); } } }
AwsAutoScalingService { @VisibleForTesting void checkLastScalingActivity(AmazonAutoScalingClient asClient, AmazonAutoScalingRetryClient asRetryClient, String autoScalingGroupName, Date timeBeforeASUpdate, Group group) throws AmazonAutoscalingFailed { if (group.getInstancesSize() > 0) { LOGGER.debug("Check last activity after AS update. Wait for the first if it doesn't exist. [asGroup: {}]", autoScalingGroupName); Optional<Activity> firstActivity; try { Waiter<DescribeScalingActivitiesRequest> autoscalingActivitiesWaiter = customAmazonWaiterProvider .getAutoscalingActivitiesWaiter(asClient, timeBeforeASUpdate); autoscalingActivitiesWaiter.run(new WaiterParameters<>(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName))); DescribeScalingActivitiesResult describeScalingActivitiesResult = asRetryClient .describeScalingActivities(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName)); firstActivity = describeScalingActivitiesResult.getActivities().stream().findFirst() .filter(activity -> "failed".equals(activity.getStatusCode().toLowerCase()) && !activity.getStatusMessage().contains("InsufficientInstanceCapacity")); } catch (Exception e) { LOGGER.error("Failed to list activities: {}", e.getMessage(), e); throw new AmazonAutoscalingFailed(e.getMessage(), e); } if (firstActivity.isPresent()) { Activity activity = firstActivity.get(); LOGGER.error("Cannot execute autoscale, because last activity is failed: {}", activity); throw new AmazonAutoscalingFailed(activity.getDescription() + " " + activity.getCause()); } } } }
AwsAutoScalingService { @VisibleForTesting void checkLastScalingActivity(AmazonAutoScalingClient asClient, AmazonAutoScalingRetryClient asRetryClient, String autoScalingGroupName, Date timeBeforeASUpdate, Group group) throws AmazonAutoscalingFailed { if (group.getInstancesSize() > 0) { LOGGER.debug("Check last activity after AS update. Wait for the first if it doesn't exist. [asGroup: {}]", autoScalingGroupName); Optional<Activity> firstActivity; try { Waiter<DescribeScalingActivitiesRequest> autoscalingActivitiesWaiter = customAmazonWaiterProvider .getAutoscalingActivitiesWaiter(asClient, timeBeforeASUpdate); autoscalingActivitiesWaiter.run(new WaiterParameters<>(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName))); DescribeScalingActivitiesResult describeScalingActivitiesResult = asRetryClient .describeScalingActivities(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName)); firstActivity = describeScalingActivitiesResult.getActivities().stream().findFirst() .filter(activity -> "failed".equals(activity.getStatusCode().toLowerCase()) && !activity.getStatusMessage().contains("InsufficientInstanceCapacity")); } catch (Exception e) { LOGGER.error("Failed to list activities: {}", e.getMessage(), e); throw new AmazonAutoscalingFailed(e.getMessage(), e); } if (firstActivity.isPresent()) { Activity activity = firstActivity.get(); LOGGER.error("Cannot execute autoscale, because last activity is failed: {}", activity); throw new AmazonAutoscalingFailed(activity.getDescription() + " " + activity.getCause()); } } }  }
AwsAutoScalingService { @VisibleForTesting void checkLastScalingActivity(AmazonAutoScalingClient asClient, AmazonAutoScalingRetryClient asRetryClient, String autoScalingGroupName, Date timeBeforeASUpdate, Group group) throws AmazonAutoscalingFailed { if (group.getInstancesSize() > 0) { LOGGER.debug("Check last activity after AS update. Wait for the first if it doesn't exist. [asGroup: {}]", autoScalingGroupName); Optional<Activity> firstActivity; try { Waiter<DescribeScalingActivitiesRequest> autoscalingActivitiesWaiter = customAmazonWaiterProvider .getAutoscalingActivitiesWaiter(asClient, timeBeforeASUpdate); autoscalingActivitiesWaiter.run(new WaiterParameters<>(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName))); DescribeScalingActivitiesResult describeScalingActivitiesResult = asRetryClient .describeScalingActivities(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName)); firstActivity = describeScalingActivitiesResult.getActivities().stream().findFirst() .filter(activity -> "failed".equals(activity.getStatusCode().toLowerCase()) && !activity.getStatusMessage().contains("InsufficientInstanceCapacity")); } catch (Exception e) { LOGGER.error("Failed to list activities: {}", e.getMessage(), e); throw new AmazonAutoscalingFailed(e.getMessage(), e); } if (firstActivity.isPresent()) { Activity activity = firstActivity.get(); LOGGER.error("Cannot execute autoscale, because last activity is failed: {}", activity); throw new AmazonAutoscalingFailed(activity.getDescription() + " " + activity.getCause()); } } }  void suspendAutoScaling(AuthenticatedContext ac, CloudStack stack); void resumeAutoScaling(AmazonAutoScalingRetryClient amazonASClient, Collection<String> groupNames, List<String> autoScalingPolicies); void scheduleStatusChecks(List<Group> groups, AuthenticatedContext ac, AmazonCloudFormationRetryClient cloudFormationClient, Date timeBeforeASUpdate); void scheduleStatusChecks(Map<String, Integer> groupsWithSize, AuthenticatedContext ac, Date timeBeforeASUpdate); void scheduleStatusChecks(List<Group> groups, AuthenticatedContext ac, AmazonCloudFormationRetryClient cloudFormationClient); List<AutoScalingGroup> getAutoscalingGroups(AmazonAutoScalingRetryClient amazonASClient, Set<String> groupNames); void updateAutoscalingGroup(AmazonAutoScalingRetryClient amazonASClient, String groupName, Integer newSize); void terminateInstance(AmazonAutoScalingRetryClient amazonASClient, String instanceId); }
AwsAutoScalingService { @VisibleForTesting void checkLastScalingActivity(AmazonAutoScalingClient asClient, AmazonAutoScalingRetryClient asRetryClient, String autoScalingGroupName, Date timeBeforeASUpdate, Group group) throws AmazonAutoscalingFailed { if (group.getInstancesSize() > 0) { LOGGER.debug("Check last activity after AS update. Wait for the first if it doesn't exist. [asGroup: {}]", autoScalingGroupName); Optional<Activity> firstActivity; try { Waiter<DescribeScalingActivitiesRequest> autoscalingActivitiesWaiter = customAmazonWaiterProvider .getAutoscalingActivitiesWaiter(asClient, timeBeforeASUpdate); autoscalingActivitiesWaiter.run(new WaiterParameters<>(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName))); DescribeScalingActivitiesResult describeScalingActivitiesResult = asRetryClient .describeScalingActivities(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName)); firstActivity = describeScalingActivitiesResult.getActivities().stream().findFirst() .filter(activity -> "failed".equals(activity.getStatusCode().toLowerCase()) && !activity.getStatusMessage().contains("InsufficientInstanceCapacity")); } catch (Exception e) { LOGGER.error("Failed to list activities: {}", e.getMessage(), e); throw new AmazonAutoscalingFailed(e.getMessage(), e); } if (firstActivity.isPresent()) { Activity activity = firstActivity.get(); LOGGER.error("Cannot execute autoscale, because last activity is failed: {}", activity); throw new AmazonAutoscalingFailed(activity.getDescription() + " " + activity.getCause()); } } }  void suspendAutoScaling(AuthenticatedContext ac, CloudStack stack); void resumeAutoScaling(AmazonAutoScalingRetryClient amazonASClient, Collection<String> groupNames, List<String> autoScalingPolicies); void scheduleStatusChecks(List<Group> groups, AuthenticatedContext ac, AmazonCloudFormationRetryClient cloudFormationClient, Date timeBeforeASUpdate); void scheduleStatusChecks(Map<String, Integer> groupsWithSize, AuthenticatedContext ac, Date timeBeforeASUpdate); void scheduleStatusChecks(List<Group> groups, AuthenticatedContext ac, AmazonCloudFormationRetryClient cloudFormationClient); List<AutoScalingGroup> getAutoscalingGroups(AmazonAutoScalingRetryClient amazonASClient, Set<String> groupNames); void updateAutoscalingGroup(AmazonAutoScalingRetryClient amazonASClient, String groupName, Integer newSize); void terminateInstance(AmazonAutoScalingRetryClient amazonASClient, String instanceId);  }