Search in sources :

Example 1 with DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE

use of com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE in project cloudbreak by hortonworks.

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenTargetGroupAlreadyExistsWithNameAndLoadBalancerListenerCouldNotBeCreated.

@Test
void testLaunchLoadBalancerResourcesWhenTargetGroupAlreadyExistsWithNameAndLoadBalancerListenerCouldNotBeCreated() {
    CloudStack stack = getCloudStack();
    when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
    CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
    when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    AmazonServiceException amazonServiceException = new AmazonServiceException(DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE);
    amazonServiceException.setErrorCode(DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE);
    when(loadBalancingClient.createTargetGroup(any())).thenThrow(amazonServiceException);
    TargetGroup targetGroup = new TargetGroup().withTargetGroupArn("aTargetGroupArn");
    DescribeTargetGroupsResult describeTargetGroupsResult = new DescribeTargetGroupsResult().withTargetGroups(targetGroup);
    when(loadBalancingClient.describeTargetGroup(any())).thenReturn(describeTargetGroupsResult);
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    when(loadBalancingClient.registerListener(any())).thenThrow(new AmazonElasticLoadBalancingException("something went wrong"));
    Assertions.assertThrows(CloudConnectorException.class, () -> undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true));
    ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(persistenceNotifier, times(2)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP.equals(cloudResource.getType())));
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) DescribeListenersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collections.emptyList(java.util.Collections.emptyList) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Test(org.junit.jupiter.api.Test) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) NetworkConstants(com.sequenceiq.cloudbreak.common.network.NetworkConstants) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) Mock(org.mockito.Mock) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) PersistenceRetriever(com.sequenceiq.cloudbreak.cloud.notification.PersistenceRetriever) ArgumentCaptor(org.mockito.ArgumentCaptor) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Network(com.sequenceiq.cloudbreak.cloud.model.Network) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE) Answers(org.mockito.Answers) DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Mockito.times(org.mockito.Mockito.times) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) Mockito.when(org.mockito.Mockito.when) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) Assertions(org.junit.jupiter.api.Assertions) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) DUPLICATE_LISTENER_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LISTENER_ERROR_CODE) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) AmazonServiceException(com.amazonaws.AmazonServiceException) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.jupiter.api.Test)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonElasticLoadBalancingException (com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException)1 CreateListenerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult)1 CreateLoadBalancerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult)1 CreateTargetGroupResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult)1 DescribeListenersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult)1 DescribeLoadBalancersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult)1 DescribeTargetGroupsResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult)1 Listener (com.amazonaws.services.elasticloadbalancingv2.model.Listener)1 LoadBalancer (com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer)1 RegisterTargetsResult (com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult)1 Tag (com.amazonaws.services.elasticloadbalancingv2.model.Tag)1 TargetGroup (com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup)1 AwsTaggingService (com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService)1 AmazonElasticLoadBalancingClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient)1 AwsLoadBalancerCommonService (com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService)1 AwsLoadBalancer (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)1 AwsLoadBalancerScheme (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)1 AwsResourceNameService (com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService)1 DUPLICATE_LISTENER_ERROR_CODE (com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LISTENER_ERROR_CODE)1