Search in sources :

Example 11 with CloudContext

use of com.sequenceiq.cloudbreak.cloud.context.CloudContext in project cloudbreak by hortonworks.

the class AwsResourceConnectorTest method testFindNonOverLappingCIDRWithNon24Subnets2.

@Test
public void testFindNonOverLappingCIDRWithNon24Subnets2() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[] { 76 }));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(subnet2.getCidrBlock()).thenReturn("10.0.16.0/20");
    when(subnet3.getCidrBlock()).thenReturn("10.0.32.0/20");
    when(subnet4.getCidrBlock()).thenReturn("10.0.48.0/20");
    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
    Assert.assertEquals("10.0.76.0/24", cidr);
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Vpc(com.amazonaws.services.ec2.model.Vpc) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Test(org.junit.Test)

Example 12 with CloudContext

use of com.sequenceiq.cloudbreak.cloud.context.CloudContext in project cloudbreak by hortonworks.

the class ASGroupStatusCheckerTaskTest method successTest.

@Test
public void successTest() throws Exception {
    int requiredInstances = 160;
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    String regionName = "eu-west-1";
    when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName)));
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    String asGroupName = "as-group";
    AwsClient awsClient = mock(AwsClient.class);
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client);
    DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 100);
    firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses);
    DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160);
    secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses);
    when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))).thenReturn(firstDescribeInstanceStatusResult).thenReturn(secondDescribeInstanceStatusResult);
    CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class);
    List<String> instancIds = new ArrayList<>();
    for (int i = 0; i < requiredInstances; i++) {
        instancIds.add(Integer.toString(i));
    }
    AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient);
    when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult());
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds);
    ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil);
    Boolean taskResult = asGroupStatusCheckerTask.call();
    ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class);
    verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture());
    List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues();
    assertEquals(100, allValues.get(0).getInstanceIds().size());
    assertEquals(60, allValues.get(1).getInstanceIds().size());
    assertTrue(taskResult);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DescribeInstanceStatusRequest(com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Matchers.anyString(org.mockito.Matchers.anyString) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) DescribeInstanceStatusResult(com.amazonaws.services.ec2.model.DescribeInstanceStatusResult) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) AwsClient(com.sequenceiq.cloudbreak.cloud.aws.AwsClient) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) Test(org.junit.Test)

Example 13 with CloudContext

use of com.sequenceiq.cloudbreak.cloud.context.CloudContext in project cloudbreak by hortonworks.

the class ASGroupStatusCheckerTaskTest method failTest.

@Test
public void failTest() throws Exception {
    int requiredInstances = 160;
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    String regionName = "eu-west-1";
    when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName)));
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    String asGroupName = "as-group";
    AwsClient awsClient = mock(AwsClient.class);
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client);
    DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 99);
    firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses);
    DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
    List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160);
    secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses);
    when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))).thenReturn(firstDescribeInstanceStatusResult).thenReturn(secondDescribeInstanceStatusResult);
    CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class);
    List<String> instancIds = new ArrayList<>();
    for (int i = 0; i < requiredInstances; i++) {
        instancIds.add(Integer.toString(i));
    }
    AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient);
    when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult());
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds);
    ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil);
    Boolean taskResult = asGroupStatusCheckerTask.call();
    ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class);
    verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture());
    List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues();
    assertEquals(100, allValues.get(0).getInstanceIds().size());
    assertEquals(60, allValues.get(1).getInstanceIds().size());
    assertFalse(taskResult);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DescribeInstanceStatusRequest(com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Matchers.anyString(org.mockito.Matchers.anyString) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) DescribeInstanceStatusResult(com.amazonaws.services.ec2.model.DescribeInstanceStatusResult) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) InstanceStatus(com.amazonaws.services.ec2.model.InstanceStatus) AwsClient(com.sequenceiq.cloudbreak.cloud.aws.AwsClient) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) Test(org.junit.Test)

Example 14 with CloudContext

use of com.sequenceiq.cloudbreak.cloud.context.CloudContext in project cloudbreak by hortonworks.

the class AzureTemplateBuilderTest method buildTestVirtualNetworkNamePrefix.

@Test
public void buildTestVirtualNetworkNamePrefix() {
    // GIVEN
    Network network = new Network(new Subnet("testSubnet"));
    Map<String, String> parameters = new HashMap<>();
    parameters.put("persistentStorage", "persistentStorageTest");
    parameters.put("attachedStorageOption", "attachedStorageOptionTest");
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    groups.add(new Group(name, InstanceGroupType.GATEWAY, Collections.singletonList(instance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
    groups.add(new Group(name, InstanceGroupType.CORE, Collections.singletonList(instance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
    cloudStack = new CloudStack(groups, network, image, parameters, tags, azureTemplateBuilder.getTemplateString(), instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    azureStackView = new AzureStackView("mystack", 3, groups, azureStorageView, azureSubnetStrategy);
    // WHEN
    when(defaultCostTaggingService.prepareAllTagsForTemplate()).thenReturn(defaultTags);
    when(azureStorage.getImageStorageName(any(AzureCredentialView.class), any(CloudContext.class), any(CloudStack.class))).thenReturn("test");
    when(azureStorage.getDiskContainerName(any(CloudContext.class))).thenReturn("testStorageContainer");
    String templateString = azureTemplateBuilder.build(stackName, CUSTOM_IMAGE_NAME, azureCredentialView, azureStackView, cloudContext, cloudStack);
    // THEN
    gson.fromJson(templateString, Map.class);
    assertThat(templateString, containsString("virtualNetworkNamePrefix"));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) Network(com.sequenceiq.cloudbreak.cloud.model.Network) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) AzureStackView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureStackView) Matchers.containsString(org.hamcrest.Matchers.containsString) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.Test)

Example 15 with CloudContext

use of com.sequenceiq.cloudbreak.cloud.context.CloudContext in project cloudbreak by hortonworks.

the class AzureTemplateBuilderTest method buildWithInstanceGroupTypeCoreShouldNotContainsGatewayCustomData.

@Test
public void buildWithInstanceGroupTypeCoreShouldNotContainsGatewayCustomData() throws Exception {
    // GIVEN
    Network network = new Network(new Subnet("testSubnet"));
    Map<String, String> parameters = new HashMap<>();
    parameters.put("persistentStorage", "persistentStorageTest");
    parameters.put("attachedStorageOption", "attachedStorageOptionTest");
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    groups.add(new Group(name, InstanceGroupType.CORE, Collections.singletonList(instance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
    cloudStack = new CloudStack(groups, network, image, parameters, tags, azureTemplateBuilder.getTemplateString(), instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    azureStackView = new AzureStackView("mystack", 3, groups, azureStorageView, azureSubnetStrategy);
    // WHEN
    when(defaultCostTaggingService.prepareAllTagsForTemplate()).thenReturn(defaultTags);
    when(azureStorage.getImageStorageName(any(AzureCredentialView.class), any(CloudContext.class), any(CloudStack.class))).thenReturn("test");
    when(azureStorage.getDiskContainerName(any(CloudContext.class))).thenReturn("testStorageContainer");
    String templateString = azureTemplateBuilder.build(stackName, CUSTOM_IMAGE_NAME, azureCredentialView, azureStackView, cloudContext, cloudStack);
    // THEN
    gson.fromJson(templateString, Map.class);
    assertThat(templateString, not(containsString("\"customData\": \"" + base64EncodedUserData(GATEWAY_CUSTOM_DATA) + '"')));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) Network(com.sequenceiq.cloudbreak.cloud.model.Network) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) AzureStackView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureStackView) Matchers.containsString(org.hamcrest.Matchers.containsString) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.Test)

Aggregations

CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)100 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)43 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)34 Test (org.junit.Test)34 Group (com.sequenceiq.cloudbreak.cloud.model.Group)33 HashMap (java.util.HashMap)33 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)32 Location (com.sequenceiq.cloudbreak.cloud.model.Location)31 Network (com.sequenceiq.cloudbreak.cloud.model.Network)31 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)31 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)25 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)20 AzureCredentialView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView)19 Matchers.containsString (org.hamcrest.Matchers.containsString)18 AzureStackView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureStackView)17 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)16 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)15 ArrayList (java.util.ArrayList)15 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)14 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)14