Search in sources :

Example 6 with Cluster

use of com.amazonaws.services.elasticmapreduce.model.Cluster in project herd by FINRAOS.

the class EmrHelperTest method testGetActiveEmrClusterIdAssertErrorWhenClusterIdSpecifiedAndNameMismatch.

@Test
public void testGetActiveEmrClusterIdAssertErrorWhenClusterIdSpecifiedAndNameMismatch() {
    EmrDao originalEmrDao = emrHelper.getEmrDao();
    EmrDao mockEmrDao = mock(EmrDao.class);
    emrHelper.setEmrDao(mockEmrDao);
    try {
        String emrClusterId = "emrClusterId";
        String emrClusterName = "emrClusterName";
        String expectedEmrClusterId = "expectedEmrClusterId";
        String actualEmrClusterName = "actualEmrClusterName";
        when(mockEmrDao.getEmrClusterById(any(), any())).thenReturn(new Cluster().withId(expectedEmrClusterId).withName(actualEmrClusterName).withStatus(new ClusterStatus().withState(ClusterState.RUNNING)));
        try {
            emrHelper.getActiveEmrClusterId(emrClusterId, emrClusterName, null);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals(String.format("The cluster with ID \"%s\" does not match the expected name \"%s\". The actual name is \"%s\".", expectedEmrClusterId, emrClusterName, actualEmrClusterName), e.getMessage());
        }
        verify(mockEmrDao).getEmrClusterById(eq(emrClusterId.trim()), any());
        verifyNoMoreInteractions(mockEmrDao);
    } finally {
        emrHelper.setEmrDao(originalEmrDao);
    }
}
Also used : Cluster(com.amazonaws.services.elasticmapreduce.model.Cluster) EmrDao(org.finra.herd.dao.EmrDao) ClusterStatus(com.amazonaws.services.elasticmapreduce.model.ClusterStatus) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 7 with Cluster

use of com.amazonaws.services.elasticmapreduce.model.Cluster in project herd by FINRAOS.

the class EmrHelperTest method testGetActiveEmrClusterIdAssertParametersTrimmed.

@Test
public void testGetActiveEmrClusterIdAssertParametersTrimmed() {
    EmrDao originalEmrDao = emrHelper.getEmrDao();
    EmrDao mockEmrDao = mock(EmrDao.class);
    emrHelper.setEmrDao(mockEmrDao);
    try {
        String emrClusterId = "emrClusterId";
        String emrClusterName = "emrClusterName";
        String expectedEmrClusterId = "expectedEmrClusterId";
        when(mockEmrDao.getEmrClusterById(any(), any())).thenReturn(new Cluster().withId(expectedEmrClusterId).withName(emrClusterName).withStatus(new ClusterStatus().withState(ClusterState.RUNNING)));
        assertEquals(expectedEmrClusterId, emrHelper.getActiveEmrClusterId(StringUtils.wrap(emrClusterId, BLANK_TEXT), StringUtils.wrap(emrClusterName, BLANK_TEXT), null));
        verify(mockEmrDao).getEmrClusterById(eq(emrClusterId.trim()), any());
        verifyNoMoreInteractions(mockEmrDao);
    } finally {
        emrHelper.setEmrDao(originalEmrDao);
    }
}
Also used : Cluster(com.amazonaws.services.elasticmapreduce.model.Cluster) EmrDao(org.finra.herd.dao.EmrDao) ClusterStatus(com.amazonaws.services.elasticmapreduce.model.ClusterStatus) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 8 with Cluster

use of com.amazonaws.services.elasticmapreduce.model.Cluster in project herd by FINRAOS.

the class EmrHelperTest method testGetActiveEmrClusterIdAssertErrorWhenClusterIdSpecifiedAndClusterStateNotActive.

@Test
public void testGetActiveEmrClusterIdAssertErrorWhenClusterIdSpecifiedAndClusterStateNotActive() {
    EmrDao originalEmrDao = emrHelper.getEmrDao();
    EmrDao mockEmrDao = mock(EmrDao.class);
    emrHelper.setEmrDao(mockEmrDao);
    try {
        String emrClusterId = "emrClusterId";
        String emrClusterName = "emrClusterName";
        String expectedEmrClusterId = "expectedEmrClusterId";
        ClusterState actualClusterState = ClusterState.TERMINATED;
        when(mockEmrDao.getEmrClusterById(any(), any())).thenReturn(new Cluster().withId(expectedEmrClusterId).withName(emrClusterName).withStatus(new ClusterStatus().withState(actualClusterState)));
        try {
            emrHelper.getActiveEmrClusterId(emrClusterId, emrClusterName, null);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals(String.format("The cluster with ID \"%s\" is not active. The cluster state must be in one of [STARTING, BOOTSTRAPPING, RUNNING, " + "WAITING]. Current state is \"%s\"", emrClusterId, actualClusterState), e.getMessage());
        }
        verify(mockEmrDao).getEmrClusterById(eq(emrClusterId), any());
        verifyNoMoreInteractions(mockEmrDao);
    } finally {
        emrHelper.setEmrDao(originalEmrDao);
    }
}
Also used : ClusterState(com.amazonaws.services.elasticmapreduce.model.ClusterState) Cluster(com.amazonaws.services.elasticmapreduce.model.Cluster) EmrDao(org.finra.herd.dao.EmrDao) ClusterStatus(com.amazonaws.services.elasticmapreduce.model.ClusterStatus) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 9 with Cluster

use of com.amazonaws.services.elasticmapreduce.model.Cluster in project herd by FINRAOS.

the class EmrDaoTest method getEmrClusterStatusByIdAssertReturnClusterState.

@Test
public void getEmrClusterStatusByIdAssertReturnClusterState() throws Exception {
    String clusterId = "clusterId";
    ClusterState expectedState = ClusterState.BOOTSTRAPPING;
    when(mockEmrOperations.describeClusterRequest(any(), any())).then(new Answer<DescribeClusterResult>() {

        @Override
        public DescribeClusterResult answer(InvocationOnMock invocation) throws Throwable {
            DescribeClusterRequest describeClusterRequest = invocation.getArgument(1);
            assertEquals(clusterId, describeClusterRequest.getClusterId());
            DescribeClusterResult describeClusterResult = new DescribeClusterResult();
            Cluster cluster = new Cluster();
            ClusterStatus status = new ClusterStatus();
            status.setState(expectedState);
            cluster.setStatus(status);
            describeClusterResult.setCluster(cluster);
            return describeClusterResult;
        }
    });
    assertEquals(expectedState.toString(), emrDao.getEmrClusterStatusById(clusterId, new AwsParamsDto()));
}
Also used : ClusterState(com.amazonaws.services.elasticmapreduce.model.ClusterState) AwsParamsDto(org.finra.herd.model.dto.AwsParamsDto) DescribeClusterRequest(com.amazonaws.services.elasticmapreduce.model.DescribeClusterRequest) DescribeClusterResult(com.amazonaws.services.elasticmapreduce.model.DescribeClusterResult) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Cluster(com.amazonaws.services.elasticmapreduce.model.Cluster) ClusterStatus(com.amazonaws.services.elasticmapreduce.model.ClusterStatus) Test(org.junit.Test)

Example 10 with Cluster

use of com.amazonaws.services.elasticmapreduce.model.Cluster in project herd by FINRAOS.

the class EmrHelper method getActiveEmrClusterId.

/**
 * Gets the ID of an active EMR cluster which matches the given criteria. If both cluster ID and cluster name is specified, the name of the actual cluster
 * with the given ID must match the specified name. For cases where the cluster is not found (does not exists or not active), the method fails. All
 * parameters are case-insensitive and whitespace trimmed. Blank parameters are equal to null.
 *
 * @param emrClusterId EMR cluster ID
 * @param emrClusterName EMR cluster name
 * @param accountId the account Id that EMR cluster is running under
 *
 * @return The cluster ID
 */
public String getActiveEmrClusterId(String emrClusterId, String emrClusterName, String accountId) {
    boolean emrClusterIdSpecified = StringUtils.isNotBlank(emrClusterId);
    boolean emrClusterNameSpecified = StringUtils.isNotBlank(emrClusterName);
    Assert.isTrue(emrClusterIdSpecified || emrClusterNameSpecified, "One of EMR cluster ID or EMR cluster name must be specified.");
    AwsParamsDto awsParamsDto = getAwsParamsDtoByAcccountId(accountId);
    // Get cluster by ID first
    if (emrClusterIdSpecified) {
        String emrClusterIdTrimmed = emrClusterId.trim();
        // Assert cluster exists
        Cluster cluster = emrDao.getEmrClusterById(emrClusterIdTrimmed, awsParamsDto);
        Assert.notNull(cluster, String.format("The cluster with ID \"%s\" does not exist.", emrClusterIdTrimmed));
        // Assert the cluster's state is active
        String emrClusterState = cluster.getStatus().getState();
        Assert.isTrue(isActiveEmrState(emrClusterState), String.format("The cluster with ID \"%s\" is not active. The cluster state must be in one of %s. Current state is \"%s\"", emrClusterIdTrimmed, Arrays.toString(getActiveEmrClusterStates()), emrClusterState));
        // Assert cluster name equals if cluster name was specified
        if (emrClusterNameSpecified) {
            String emrClusterNameTrimmed = emrClusterName.trim();
            Assert.isTrue(cluster.getName().equalsIgnoreCase(emrClusterNameTrimmed), String.format("The cluster with ID \"%s\" does not match the expected name \"%s\". The actual name is \"%s\".", cluster.getId(), emrClusterNameTrimmed, cluster.getName()));
        }
        return cluster.getId();
    } else {
        String emrClusterNameTrimmed = emrClusterName.trim();
        ClusterSummary clusterSummary = emrDao.getActiveEmrClusterByName(emrClusterNameTrimmed, awsParamsDto);
        Assert.notNull(clusterSummary, String.format("The cluster with name \"%s\" does not exist.", emrClusterNameTrimmed));
        return clusterSummary.getId();
    }
}
Also used : AwsParamsDto(org.finra.herd.model.dto.AwsParamsDto) ClusterSummary(com.amazonaws.services.elasticmapreduce.model.ClusterSummary) Cluster(com.amazonaws.services.elasticmapreduce.model.Cluster)

Aggregations

Cluster (com.amazonaws.services.elasticmapreduce.model.Cluster)11 Test (org.junit.Test)9 ClusterStatus (com.amazonaws.services.elasticmapreduce.model.ClusterStatus)7 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)6 EmrDao (org.finra.herd.dao.EmrDao)6 AwsParamsDto (org.finra.herd.model.dto.AwsParamsDto)4 ClusterState (com.amazonaws.services.elasticmapreduce.model.ClusterState)2 ClusterSummary (com.amazonaws.services.elasticmapreduce.model.ClusterSummary)2 DescribeClusterRequest (com.amazonaws.services.elasticmapreduce.model.DescribeClusterRequest)2 DescribeClusterResult (com.amazonaws.services.elasticmapreduce.model.DescribeClusterResult)2 EmrCluster (org.finra.herd.model.api.xml.EmrCluster)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ListInstanceFleetsResult (com.amazonaws.services.elasticmapreduce.model.ListInstanceFleetsResult)1 Step (com.amazonaws.services.elasticmapreduce.model.Step)1 StepSummary (com.amazonaws.services.elasticmapreduce.model.StepSummary)1 EmrStep (org.finra.herd.model.api.xml.EmrStep)1 EmrClusterDefinitionEntity (org.finra.herd.model.jpa.EmrClusterDefinitionEntity)1 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)1 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)1