use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsForSystemFailuresDoesNotBatchDeletionWhenDescribeAlarmPermissionIsMissing.
@Test
void testDeleteCloudWatchAlarmsForSystemFailuresDoesNotBatchDeletionWhenDescribeAlarmPermissionIsMissing() {
List<CloudInstance> cloudInstances = new LinkedList<>();
List<List<String>> deleteAlarmNames = new LinkedList<>();
List<String> alarmNames1 = new LinkedList<>();
List<String> alarmNames2 = new LinkedList<>();
List<String> alarmNames3 = new LinkedList<>();
for (int i = 1; i <= 100; i++) {
String alarmName = "i-" + i + "-Status-Check-Failed-System";
alarmNames1.add(alarmName);
deleteAlarmNames.add(List.of(alarmName));
}
for (int i = 101; i <= 200; i++) {
String alarmName = "i-" + i + "-Status-Check-Failed-System";
alarmNames2.add(alarmName);
deleteAlarmNames.add(List.of(alarmName));
}
for (int i = 201; i <= 210; i++) {
String alarmName = "i-" + i + "-Status-Check-Failed-System";
alarmNames3.add(alarmName);
deleteAlarmNames.add(List.of(alarmName));
}
for (int i = 1; i <= 210; i++) {
String instanceId = "i-" + i;
CloudInstance cloudInstance = mock(CloudInstance.class);
when(cloudInstance.getInstanceId()).thenReturn(instanceId);
cloudInstances.add(cloudInstance);
}
AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
AwsCredentialView credentialView = mock(AwsCredentialView.class);
CloudStack stack = mock(CloudStack.class);
Group group = mock(Group.class);
List<Group> groups = List.of(group);
when(stack.getGroups()).thenReturn(groups);
when(group.getInstances()).thenReturn(cloudInstances);
when(awsClient.createCloudWatchClient(credentialView, REGION)).thenReturn(cloudWatchClient);
AmazonCloudWatchException exception = new AmazonCloudWatchException("No permissions to describe cloudwatch alarms");
when(cloudWatchClient.describeAlarms(any())).thenThrow(exception);
underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
verify(cloudWatchClient, times(3)).describeAlarms(captorDescribe.capture());
verify(cloudWatchClient, times(210)).deleteAlarms(captorDelete.capture());
assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), captorDescribe.getAllValues().stream().map(DescribeAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
assertEquals(deleteAlarmNames, captorDelete.getAllValues().stream().map(DeleteAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsCloudWatchServiceTest method testAddCloudWatchAlarmsForSystemFailuresWithRecoveryNotSupportedType.
@Test
void testAddCloudWatchAlarmsForSystemFailuresWithRecoveryNotSupportedType() {
CloudResource firstInst = CloudResource.builder().instanceId("i-1").name("i-1").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
CloudResource secondInst = CloudResource.builder().instanceId("i-2").name("i-2").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
secondInst.putParameter(INSTANCE_TYPE, "m5d.2xlarge");
AwsCredentialView credentialView = mock(AwsCredentialView.class);
AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
underTest.addCloudWatchAlarmsForSystemFailures(List.of(firstInst, secondInst), REGION, credentialView);
ArgumentCaptor<PutMetricAlarmRequest> captorPut = ArgumentCaptor.forClass(PutMetricAlarmRequest.class);
verify(cloudWatchClient, times(1)).putMetricAlarm(captorPut.capture());
assertEquals(1, captorPut.getAllValues().size());
assertEquals("i-1" + ALARM_SUFFIX, captorPut.getAllValues().get(0).getAlarmName());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsCloudWatchServiceTest method testAddCloudWatchAlarmsForSystemFailures.
@Test
void testAddCloudWatchAlarmsForSystemFailures() {
CloudResource firstInst = CloudResource.builder().instanceId("i-1").name("i-1").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
CloudResource secondInst = CloudResource.builder().instanceId("i-2").name("i-2").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
AwsCredentialView credentialView = mock(AwsCredentialView.class);
AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
underTest.addCloudWatchAlarmsForSystemFailures(List.of(firstInst, secondInst), REGION, credentialView);
ArgumentCaptor<PutMetricAlarmRequest> captorPut = ArgumentCaptor.forClass(PutMetricAlarmRequest.class);
verify(cloudWatchClient, times(2)).putMetricAlarm(captorPut.capture());
assertEquals(2, captorPut.getAllValues().size());
assertEquals("i-1" + ALARM_SUFFIX, captorPut.getAllValues().get(0).getAlarmName());
assertEquals("i-2" + ALARM_SUFFIX, captorPut.getAllValues().get(1).getAlarmName());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsForSystemFailuresWhenOneAlarmHasAlreadyBeenDeleted.
@Test
void testDeleteCloudWatchAlarmsForSystemFailuresWhenOneAlarmHasAlreadyBeenDeleted() {
String alarm1Name = "i-1-Status-Check-Failed-System";
String alarm2Name = "i-2-Status-Check-Failed-System";
String alarm3Name = "i-3-Status-Check-Failed-System";
String instanceId1 = "i-1";
String instanceId2 = "i-2";
String instanceId3 = "i-3";
MetricAlarm alarm1 = mock(MetricAlarm.class);
MetricAlarm alarm2 = mock(MetricAlarm.class);
DescribeAlarmsResult describeAlarmsResult = mock(DescribeAlarmsResult.class);
AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
AwsCredentialView credentialView = mock(AwsCredentialView.class);
CloudStack stack = mock(CloudStack.class);
Group group = mock(Group.class);
CloudInstance instance1 = mock(CloudInstance.class);
CloudInstance instance2 = mock(CloudInstance.class);
CloudInstance instance3 = mock(CloudInstance.class);
List<Group> groups = List.of(group);
when(stack.getGroups()).thenReturn(groups);
when(group.getInstances()).thenReturn(List.of(instance1, instance2, instance3));
when(instance1.getInstanceId()).thenReturn(instanceId1);
when(instance2.getInstanceId()).thenReturn(instanceId2);
when(instance3.getInstanceId()).thenReturn(instanceId3);
when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult);
// alarm 3 was already deleted
when(describeAlarmsResult.getMetricAlarms()).thenReturn(List.of(alarm1, alarm2));
when(alarm1.getAlarmName()).thenReturn(alarm1Name);
when(alarm2.getAlarmName()).thenReturn(alarm2Name);
underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
verify(cloudWatchClient, times(1)).describeAlarms(captorDescribe.capture());
verify(cloudWatchClient, times(1)).deleteAlarms(captorDelete.capture());
assertEquals(List.of(alarm1Name, alarm2Name, alarm3Name), captorDescribe.getValue().getAlarmNames());
// only delete alarms that were not already deleted
assertEquals(List.of(alarm1Name, alarm2Name), captorDelete.getValue().getAlarmNames());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsWhenInstanceDeletedOnProvider.
@Test
void testDeleteCloudWatchAlarmsWhenInstanceDeletedOnProvider() {
String alarm1Name = "i-1-Status-Check-Failed-System";
String alarm2Name = "i-2-Status-Check-Failed-System";
String deletedAlarmName = "i-deleted-Status-Check-Failed-System";
String instanceId1 = "i-1";
String instanceId2 = "i-2";
String deletedInstanceId = "i-deleted";
MetricAlarm alarm1 = mock(MetricAlarm.class);
MetricAlarm alarm2 = mock(MetricAlarm.class);
MetricAlarm deletedInstanceAlarm = mock(MetricAlarm.class);
DescribeAlarmsResult describeAlarmsResult = mock(DescribeAlarmsResult.class);
AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
AwsCredentialView credentialView = mock(AwsCredentialView.class);
CloudStack stack = mock(CloudStack.class);
Group group = mock(Group.class);
Group deletedGroup = mock(Group.class);
CloudInstance instance1 = mock(CloudInstance.class);
CloudInstance instance2 = mock(CloudInstance.class);
CloudInstance deletedInstance = mock(CloudInstance.class);
List<Group> groups = List.of(group, deletedGroup);
when(stack.getGroups()).thenReturn(groups);
when(group.getInstances()).thenReturn(List.of(instance1, instance2));
when(deletedGroup.getInstances()).thenReturn(List.of(deletedInstance));
when(instance1.getInstanceId()).thenReturn(instanceId1);
when(instance2.getInstanceId()).thenReturn(instanceId2);
when(deletedInstance.getInstanceId()).thenReturn(deletedInstanceId);
when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult);
when(describeAlarmsResult.getMetricAlarms()).thenReturn(List.of(alarm1, alarm2, deletedInstanceAlarm));
when(alarm1.getAlarmName()).thenReturn(alarm1Name);
when(alarm2.getAlarmName()).thenReturn(alarm2Name);
when(deletedInstanceAlarm.getAlarmName()).thenReturn(deletedAlarmName);
underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
verify(cloudWatchClient, times(1)).describeAlarms(captorDescribe.capture());
verify(cloudWatchClient, times(1)).deleteAlarms(captorDelete.capture());
assertEquals(List.of(alarm1Name, alarm2Name, deletedAlarmName), captorDescribe.getValue().getAlarmNames());
assertEquals(List.of(alarm1Name, alarm2Name, deletedAlarmName), captorDelete.getValue().getAlarmNames());
}
Aggregations