Search in sources :

Example 1 with ScaleContext

use of com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext in project cloudbreak by hortonworks.

the class CloudFailureHandlerTest method dontRollbackWhenPercentageReachThreshold.

@Test
void dontRollbackWhenPercentageReachThreshold() throws Exception {
    ScaleContext scaleContext = new ScaleContext(true, AdjustmentType.PERCENTAGE, 40L);
    Group group = mock(Group.class);
    ArrayList<CloudResourceStatus> failuresList = new ArrayList<>();
    CloudResource instance2 = newResource("instance-2", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance2, ResourceStatus.FAILED, "failed instance", 2L));
    CloudResource instance3 = newResource("instance-3", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance3, ResourceStatus.FAILED, "failed instance", 3L));
    CloudResource instance4 = newResource("instance-4", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance4, ResourceStatus.FAILED, "failed instance", 4L));
    AuthenticatedContext auth = new AuthenticatedContext(mock(CloudContext.class), mock(CloudCredential.class));
    ResourceBuilderContext resourceBuilderContext = mock(ResourceBuilderContext.class);
    ArrayList<CloudResourceStatus> resourceStatuses = new ArrayList<>(failuresList);
    CloudResource volume2 = newResource("vol-2", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume2, ResourceStatus.CREATED, 2L));
    CloudResource volume4 = newResource("vol-4", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume4, ResourceStatus.CREATED, 4L));
    CloudResource instance1 = newResource("instance-1", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance1, ResourceStatus.CREATED, 1L));
    CloudResource volume1 = newResource("vol-1", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume1, ResourceStatus.CREATED, 1L));
    CloudResource instance5 = newResource("instance-5", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance5, ResourceStatus.CREATED, 5L));
    CloudResource volume5 = newResource("vol-5", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume5, ResourceStatus.CREATED, 5L));
    CloudResource instance6 = newResource("instance-6", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance6, ResourceStatus.CREATED, 6L));
    CloudResource volume6 = newResource("vol-6", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume6, ResourceStatus.CREATED, 6L));
    ResourceBuilders resourceBuilders = mock(ResourceBuilders.class);
    ArrayList<ComputeResourceBuilder<ResourceBuilderContext>> computeResourceBuilders = new ArrayList<>();
    ComputeResourceBuilder instanceResourceBuilder = mock(ComputeResourceBuilder.class);
    when(instanceResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_INSTANCE);
    computeResourceBuilders.add(instanceResourceBuilder);
    ComputeResourceBuilder volumeResourceBuilder = mock(ComputeResourceBuilder.class);
    when(volumeResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_VOLUMESET);
    computeResourceBuilders.add(volumeResourceBuilder);
    when(resourceBuilders.compute(any())).thenReturn(computeResourceBuilders);
    ArgumentCaptor<ResourceDeletionCallable> callableArgumentCaptor = ArgumentCaptor.forClass(ResourceDeletionCallable.class);
    when(resourceBuilderExecutor.submit(callableArgumentCaptor.capture())).thenAnswer(invocation -> ((Callable) invocation.getArgument(0)).call());
    cloudFailureHandler.rollbackIfNecessary(new CloudFailureContext(auth, scaleContext, resourceBuilderContext), failuresList, resourceStatuses, group, resourceBuilders, 6);
    verifyDeleteAll(volumeResourceBuilder, resourceBuilderContext, auth, volume2, volume4);
    verify(resourceBuilderExecutor, times(5)).submit(any(Callable.class));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) ScaleContext(com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Callable(java.util.concurrent.Callable) ComputeResourceBuilder(com.sequenceiq.cloudbreak.cloud.template.ComputeResourceBuilder) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceBuilders(com.sequenceiq.cloudbreak.cloud.template.init.ResourceBuilders) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 2 with ScaleContext

use of com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext in project cloudbreak by hortonworks.

the class CloudFailureHandlerTest method dontRollbackWhenExactReachThreshold.

@Test
void dontRollbackWhenExactReachThreshold() throws Exception {
    ScaleContext scaleContext = new ScaleContext(true, AdjustmentType.EXACT, 3L);
    Group group = mock(Group.class);
    ArrayList<CloudResourceStatus> failuresList = new ArrayList<>();
    CloudResource instance2 = newResource("instance-2", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance2, ResourceStatus.FAILED, "failed instance", 2L));
    CloudResource instance3 = newResource("instance-3", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance3, ResourceStatus.FAILED, "failed instance", 3L));
    CloudResource instance4 = newResource("instance-4", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance4, ResourceStatus.FAILED, "failed instance", 4L));
    AuthenticatedContext auth = new AuthenticatedContext(mock(CloudContext.class), mock(CloudCredential.class));
    ResourceBuilderContext resourceBuilderContext = mock(ResourceBuilderContext.class);
    ArrayList<CloudResourceStatus> resourceStatuses = new ArrayList<>(failuresList);
    CloudResource volume2 = newResource("vol-2", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume2, ResourceStatus.CREATED, 2L));
    CloudResource volume4 = newResource("vol-4", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume4, ResourceStatus.CREATED, 4L));
    CloudResource instance1 = newResource("instance-1", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance1, ResourceStatus.CREATED, 1L));
    CloudResource volume1 = newResource("vol-1", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume1, ResourceStatus.CREATED, 1L));
    CloudResource instance5 = newResource("instance-5", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance5, ResourceStatus.CREATED, 5L));
    CloudResource volume5 = newResource("vol-5", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume5, ResourceStatus.CREATED, 5L));
    CloudResource instance6 = newResource("instance-6", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance6, ResourceStatus.CREATED, 6L));
    CloudResource volume6 = newResource("vol-6", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume6, ResourceStatus.CREATED, 6L));
    ResourceBuilders resourceBuilders = mock(ResourceBuilders.class);
    ArrayList<ComputeResourceBuilder<ResourceBuilderContext>> computeResourceBuilders = new ArrayList<>();
    ComputeResourceBuilder instanceResourceBuilder = mock(ComputeResourceBuilder.class);
    when(instanceResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_INSTANCE);
    computeResourceBuilders.add(instanceResourceBuilder);
    ComputeResourceBuilder volumeResourceBuilder = mock(ComputeResourceBuilder.class);
    when(volumeResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_VOLUMESET);
    computeResourceBuilders.add(volumeResourceBuilder);
    when(resourceBuilders.compute(any())).thenReturn(computeResourceBuilders);
    ArgumentCaptor<ResourceDeletionCallable> callableArgumentCaptor = ArgumentCaptor.forClass(ResourceDeletionCallable.class);
    when(resourceBuilderExecutor.submit(callableArgumentCaptor.capture())).thenAnswer(invocation -> ((Callable) invocation.getArgument(0)).call());
    cloudFailureHandler.rollbackIfNecessary(new CloudFailureContext(auth, scaleContext, resourceBuilderContext), failuresList, resourceStatuses, group, resourceBuilders, 6);
    verifyDeleteAll(volumeResourceBuilder, resourceBuilderContext, auth, volume2, volume4);
    verify(resourceBuilderExecutor, times(5)).submit(any(Callable.class));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) ScaleContext(com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Callable(java.util.concurrent.Callable) ComputeResourceBuilder(com.sequenceiq.cloudbreak.cloud.template.ComputeResourceBuilder) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceBuilders(com.sequenceiq.cloudbreak.cloud.template.init.ResourceBuilders) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 3 with ScaleContext

use of com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext in project cloudbreak by hortonworks.

the class CloudFailureHandlerTest method rollbackOnExactDoesNotReachThreshold.

@Test
void rollbackOnExactDoesNotReachThreshold() throws Exception {
    ScaleContext scaleContext = new ScaleContext(true, AdjustmentType.EXACT, 4L);
    Group group = mock(Group.class);
    List<CloudResourceStatus> failuresList = new ArrayList<>();
    CloudResource instance2 = newResource("instance-2", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance2, ResourceStatus.FAILED, "failed instance", 2L));
    CloudResource instance3 = newResource("instance-3", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance3, ResourceStatus.FAILED, "failed instance", 3L));
    CloudResource instance4 = newResource("instance-4", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance4, ResourceStatus.FAILED, "failed instance", 4L));
    AuthenticatedContext auth = new AuthenticatedContext(mock(CloudContext.class), mock(CloudCredential.class));
    ResourceBuilderContext resourceBuilderContext = mock(ResourceBuilderContext.class);
    List<CloudResourceStatus> resourceStatuses = new ArrayList<>(failuresList);
    CloudResource volume2 = newResource("vol-2", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume2, ResourceStatus.CREATED, 2L));
    CloudResource volume4 = newResource("vol-4", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume4, ResourceStatus.CREATED, 4L));
    CloudResource instance1 = newResource("instance-1", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance1, ResourceStatus.CREATED, 1L));
    CloudResource volume1 = newResource("vol-1", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume1, ResourceStatus.CREATED, 1L));
    CloudResource instance5 = newResource("instance-5", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance5, ResourceStatus.CREATED, 5L));
    CloudResource volume5 = newResource("vol-5", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume5, ResourceStatus.CREATED, 5L));
    CloudResource instance6 = newResource("instance-6", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance6, ResourceStatus.CREATED, 6L));
    CloudResource volume6 = newResource("vol-6", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume6, ResourceStatus.CREATED, 6L));
    ResourceBuilders resourceBuilders = mock(ResourceBuilders.class);
    ArrayList<ComputeResourceBuilder<ResourceBuilderContext>> computeResourceBuilders = new ArrayList<>();
    ComputeResourceBuilder instanceResourceBuilder = mock(ComputeResourceBuilder.class);
    when(instanceResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_INSTANCE);
    computeResourceBuilders.add(instanceResourceBuilder);
    ComputeResourceBuilder volumeResourceBuilder = mock(ComputeResourceBuilder.class);
    when(volumeResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_VOLUMESET);
    computeResourceBuilders.add(volumeResourceBuilder);
    when(resourceBuilders.compute(any())).thenReturn(computeResourceBuilders);
    ArgumentCaptor<ResourceDeletionCallable> callableArgumentCaptor = ArgumentCaptor.forClass(ResourceDeletionCallable.class);
    when(resourceBuilderExecutor.submit(callableArgumentCaptor.capture())).thenAnswer(invocation -> ((Callable) invocation.getArgument(0)).call());
    RolledbackResourcesException rolledbackResourcesException = assertThrows(RolledbackResourcesException.class, () -> cloudFailureHandler.rollbackIfNecessary(new CloudFailureContext(auth, scaleContext, resourceBuilderContext), failuresList, resourceStatuses, group, resourceBuilders, 6));
    assertEquals("Resources are rolled back because successful node count was lower than threshold. 3 nodes are failed. Error reason: failed instance", rolledbackResourcesException.getMessage());
    verifyDeleteAll(instanceResourceBuilder, resourceBuilderContext, auth, instance1, instance5, instance6);
    verifyDeleteAll(volumeResourceBuilder, resourceBuilderContext, auth, volume1, volume2, volume4, volume5, volume6);
    verify(resourceBuilderExecutor, times(11)).submit(any(Callable.class));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) ScaleContext(com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) RolledbackResourcesException(com.sequenceiq.cloudbreak.cloud.exception.RolledbackResourcesException) Callable(java.util.concurrent.Callable) ComputeResourceBuilder(com.sequenceiq.cloudbreak.cloud.template.ComputeResourceBuilder) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceBuilders(com.sequenceiq.cloudbreak.cloud.template.init.ResourceBuilders) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 4 with ScaleContext

use of com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext in project cloudbreak by hortonworks.

the class CloudFailureHandlerTest method dontRollbackOnBestEffort.

@Test
void dontRollbackOnBestEffort() throws Exception {
    ScaleContext scaleContext = new ScaleContext(true, AdjustmentType.BEST_EFFORT, 0L);
    Group group = mock(Group.class);
    ArrayList<CloudResourceStatus> failuresList = new ArrayList<>();
    CloudResource instance2 = newResource("instance-2", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance2, ResourceStatus.FAILED, "failed instance", 2L));
    CloudResource instance3 = newResource("instance-3", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance3, ResourceStatus.FAILED, "failed instance", 3L));
    CloudResource instance4 = newResource("instance-4", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance4, ResourceStatus.FAILED, "failed instance", 4L));
    AuthenticatedContext auth = new AuthenticatedContext(mock(CloudContext.class), mock(CloudCredential.class));
    ResourceBuilderContext resourceBuilderContext = mock(ResourceBuilderContext.class);
    ArrayList<CloudResourceStatus> resourceStatuses = new ArrayList<>(failuresList);
    CloudResource volume2 = newResource("vol-2", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume2, ResourceStatus.CREATED, 2L));
    CloudResource volume4 = newResource("vol-4", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume4, ResourceStatus.CREATED, 4L));
    CloudResource instance1 = newResource("instance-1", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance1, ResourceStatus.CREATED, 1L));
    CloudResource volume1 = newResource("vol-1", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume1, ResourceStatus.CREATED, 1L));
    CloudResource instance5 = newResource("instance-5", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance5, ResourceStatus.CREATED, 5L));
    CloudResource volume5 = newResource("vol-5", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume5, ResourceStatus.CREATED, 5L));
    CloudResource instance6 = newResource("instance-6", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance6, ResourceStatus.CREATED, 6L));
    CloudResource volume6 = newResource("vol-6", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume6, ResourceStatus.CREATED, 6L));
    ResourceBuilders resourceBuilders = mock(ResourceBuilders.class);
    ArrayList<ComputeResourceBuilder<ResourceBuilderContext>> computeResourceBuilders = new ArrayList<>();
    ComputeResourceBuilder instanceResourceBuilder = mock(ComputeResourceBuilder.class);
    when(instanceResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_INSTANCE);
    computeResourceBuilders.add(instanceResourceBuilder);
    ComputeResourceBuilder volumeResourceBuilder = mock(ComputeResourceBuilder.class);
    when(volumeResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_VOLUMESET);
    computeResourceBuilders.add(volumeResourceBuilder);
    when(resourceBuilders.compute(any())).thenReturn(computeResourceBuilders);
    ArgumentCaptor<ResourceDeletionCallable> callableArgumentCaptor = ArgumentCaptor.forClass(ResourceDeletionCallable.class);
    when(resourceBuilderExecutor.submit(callableArgumentCaptor.capture())).thenAnswer(invocation -> ((Callable) invocation.getArgument(0)).call());
    cloudFailureHandler.rollbackIfNecessary(new CloudFailureContext(auth, scaleContext, resourceBuilderContext), failuresList, resourceStatuses, group, resourceBuilders, 6);
    verifyDeleteAll(volumeResourceBuilder, resourceBuilderContext, auth, volume2, volume4);
    verify(resourceBuilderExecutor, times(5)).submit(any(Callable.class));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) ScaleContext(com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Callable(java.util.concurrent.Callable) ComputeResourceBuilder(com.sequenceiq.cloudbreak.cloud.template.ComputeResourceBuilder) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceBuilders(com.sequenceiq.cloudbreak.cloud.template.init.ResourceBuilders) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 5 with ScaleContext

use of com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext in project cloudbreak by hortonworks.

the class CloudFailureHandlerTest method rollbackOnPercentageDoesNotReachThreshold.

@Test
void rollbackOnPercentageDoesNotReachThreshold() throws Exception {
    ScaleContext scaleContext = new ScaleContext(true, AdjustmentType.PERCENTAGE, 60L);
    Group group = mock(Group.class);
    ArrayList<CloudResourceStatus> failuresList = new ArrayList<>();
    CloudResource instance2 = newResource("instance-2", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance2, ResourceStatus.FAILED, "failed instance", 2L));
    CloudResource instance3 = newResource("instance-3", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance3, ResourceStatus.FAILED, "failed instance", 3L));
    CloudResource instance4 = newResource("instance-4", ResourceType.AWS_INSTANCE, CommonStatus.FAILED);
    failuresList.add(new CloudResourceStatus(instance4, ResourceStatus.FAILED, "failed instance", 4L));
    AuthenticatedContext auth = new AuthenticatedContext(mock(CloudContext.class), mock(CloudCredential.class));
    ResourceBuilderContext resourceBuilderContext = mock(ResourceBuilderContext.class);
    ArrayList<CloudResourceStatus> resourceStatuses = new ArrayList<>(failuresList);
    CloudResource volume2 = newResource("vol-2", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume2, ResourceStatus.CREATED, 2L));
    CloudResource volume4 = newResource("vol-4", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume4, ResourceStatus.CREATED, 4L));
    CloudResource instance1 = newResource("instance-1", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance1, ResourceStatus.CREATED, 1L));
    CloudResource volume1 = newResource("vol-1", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume1, ResourceStatus.CREATED, 1L));
    CloudResource instance5 = newResource("instance-5", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance5, ResourceStatus.CREATED, 5L));
    CloudResource volume5 = newResource("vol-5", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume5, ResourceStatus.CREATED, 5L));
    CloudResource instance6 = newResource("instance-6", ResourceType.AWS_INSTANCE, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(instance6, ResourceStatus.CREATED, 6L));
    CloudResource volume6 = newResource("vol-6", ResourceType.AWS_VOLUMESET, CommonStatus.CREATED);
    resourceStatuses.add(new CloudResourceStatus(volume6, ResourceStatus.CREATED, 6L));
    ResourceBuilders resourceBuilders = mock(ResourceBuilders.class);
    ArrayList<ComputeResourceBuilder<ResourceBuilderContext>> computeResourceBuilders = new ArrayList<>();
    ComputeResourceBuilder instanceResourceBuilder = mock(ComputeResourceBuilder.class);
    when(instanceResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_INSTANCE);
    computeResourceBuilders.add(instanceResourceBuilder);
    ComputeResourceBuilder volumeResourceBuilder = mock(ComputeResourceBuilder.class);
    when(volumeResourceBuilder.resourceType()).thenReturn(ResourceType.AWS_VOLUMESET);
    computeResourceBuilders.add(volumeResourceBuilder);
    when(resourceBuilders.compute(any())).thenReturn(computeResourceBuilders);
    ArgumentCaptor<ResourceDeletionCallable> callableArgumentCaptor = ArgumentCaptor.forClass(ResourceDeletionCallable.class);
    when(resourceBuilderExecutor.submit(callableArgumentCaptor.capture())).thenAnswer(invocation -> ((Callable) invocation.getArgument(0)).call());
    RolledbackResourcesException rolledbackResourcesException = assertThrows(RolledbackResourcesException.class, () -> cloudFailureHandler.rollbackIfNecessary(new CloudFailureContext(auth, scaleContext, resourceBuilderContext), failuresList, resourceStatuses, group, resourceBuilders, 6));
    assertEquals("Resources are rolled back because successful node count was lower than threshold. 3 nodes are failed. Error reason: failed instance", rolledbackResourcesException.getMessage());
    verifyDeleteAll(instanceResourceBuilder, resourceBuilderContext, auth, instance1, instance5, instance6);
    verifyDeleteAll(volumeResourceBuilder, resourceBuilderContext, auth, volume1, volume2, volume4, volume5, volume6);
    verify(resourceBuilderExecutor, times(11)).submit(any(Callable.class));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) ScaleContext(com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) RolledbackResourcesException(com.sequenceiq.cloudbreak.cloud.exception.RolledbackResourcesException) Callable(java.util.concurrent.Callable) ComputeResourceBuilder(com.sequenceiq.cloudbreak.cloud.template.ComputeResourceBuilder) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceBuilders(com.sequenceiq.cloudbreak.cloud.template.init.ResourceBuilders) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Aggregations

AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)5 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)5 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)5 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)5 Group (com.sequenceiq.cloudbreak.cloud.model.Group)5 ComputeResourceBuilder (com.sequenceiq.cloudbreak.cloud.template.ComputeResourceBuilder)5 ScaleContext (com.sequenceiq.cloudbreak.cloud.template.compute.CloudFailureHandler.ScaleContext)5 ResourceBuilderContext (com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext)5 ResourceBuilders (com.sequenceiq.cloudbreak.cloud.template.init.ResourceBuilders)5 ArrayList (java.util.ArrayList)5 Callable (java.util.concurrent.Callable)5 Test (org.junit.jupiter.api.Test)5 RolledbackResourcesException (com.sequenceiq.cloudbreak.cloud.exception.RolledbackResourcesException)2