EmrClusterDefinitionHelper { public void validateEmrClusterDefinitionConfiguration(EmrClusterDefinition emrClusterDefinition) throws IllegalArgumentException { Assert.notNull(emrClusterDefinition, "An EMR cluster definition configuration must be specified."); Assert.isTrue(StringUtils.isNotBlank(emrClusterDefinition.getSubnetId()), "Subnet ID must be specified"); for (String token : emrClusterDefinition.getSubnetId().split(",")) { Assert.isTrue(StringUtils.isNotBlank(token), "No blank is allowed in the list of subnet IDs"); } Assert.isTrue(!emrHelper.isInstanceDefinitionsEmpty(emrClusterDefinition.getInstanceDefinitions()) || CollectionUtils.isNotEmpty(emrClusterDefinition.getInstanceFleets()), "Instance group definitions or instance fleets must be specified."); if (!emrHelper.isInstanceDefinitionsEmpty(emrClusterDefinition.getInstanceDefinitions())) { Assert.notNull(emrClusterDefinition.getInstanceDefinitions().getMasterInstances(), "Master instances must be specified."); validateMasterInstanceDefinition(emrClusterDefinition.getInstanceDefinitions().getMasterInstances()); if (emrClusterDefinition.getInstanceDefinitions().getCoreInstances() != null) { validateInstanceDefinition("core", emrClusterDefinition.getInstanceDefinitions().getCoreInstances(), 0); if (emrClusterDefinition.getInstanceDefinitions().getCoreInstances().getInstanceCount() <= 0) { emrClusterDefinition.getInstanceDefinitions().setCoreInstances(null); } } if (emrClusterDefinition.getInstanceDefinitions().getTaskInstances() != null) { validateInstanceDefinition("task", emrClusterDefinition.getInstanceDefinitions().getTaskInstances(), 1); } int maxEmrInstanceCount = configurationHelper.getProperty(ConfigurationValue.MAX_EMR_INSTANCES_COUNT, Integer.class); if (maxEmrInstanceCount > 0) { int instancesRequested = emrClusterDefinition.getInstanceDefinitions().getMasterInstances().getInstanceCount(); if (emrClusterDefinition.getInstanceDefinitions().getCoreInstances() != null) { instancesRequested += emrClusterDefinition.getInstanceDefinitions().getCoreInstances().getInstanceCount(); } if (emrClusterDefinition.getInstanceDefinitions().getTaskInstances() != null) { instancesRequested += emrClusterDefinition.getInstanceDefinitions().getTaskInstances().getInstanceCount(); } Assert.isTrue((maxEmrInstanceCount >= instancesRequested), "Total number of instances requested can not exceed : " + maxEmrInstanceCount); } } if (emrClusterDefinition.getInstanceFleetMinimumIpAvailableFilter() != null) { Assert.isTrue(emrClusterDefinition.getInstanceFleetMinimumIpAvailableFilter() >= 0, "Minimum number of available IPs required for the instance fleet must be non-negative"); } Assert.notEmpty(emrClusterDefinition.getNodeTags(), "Node tags must be specified."); HashSet<String> nodeTagNameValidationSet = new HashSet<>(); for (NodeTag nodeTag : emrClusterDefinition.getNodeTags()) { Assert.hasText(nodeTag.getTagName(), "A node tag name must be specified."); Assert.hasText(nodeTag.getTagValue(), "A node tag value must be specified."); Assert.isTrue(!nodeTagNameValidationSet.contains(nodeTag.getTagName()), String.format("Duplicate node tag \"%s\" is found.", nodeTag.getTagName())); nodeTagNameValidationSet.add(nodeTag.getTagName()); } for (String mandatoryTag : herdStringHelper.splitStringWithDefaultDelimiter(configurationHelper.getProperty(ConfigurationValue.MANDATORY_AWS_TAGS))) { Assert.isTrue(nodeTagNameValidationSet.contains(mandatoryTag), String.format("Mandatory AWS tag not specified: \"%s\"", mandatoryTag)); } emrClusterDefinition.setAdditionalMasterSecurityGroups( assertNotBlankAndTrim(emrClusterDefinition.getAdditionalMasterSecurityGroups(), "additionalMasterSecurityGroup")); emrClusterDefinition .setAdditionalSlaveSecurityGroups(assertNotBlankAndTrim(emrClusterDefinition.getAdditionalSlaveSecurityGroups(), "additionalSlaveSecurityGroup")); if (StringUtils.isNotBlank(emrClusterDefinition.getSecurityConfiguration())) { final DefaultArtifactVersion securityConfigurationMinEmrVersion = new DefaultArtifactVersion("4.8.0"); Assert.isTrue(StringUtils.isNotBlank(emrClusterDefinition.getReleaseLabel()) && securityConfigurationMinEmrVersion.compareTo(new DefaultArtifactVersion(emrClusterDefinition.getReleaseLabel().replaceFirst("^(emr-)", ""))) <= 0, "EMR security configuration is not supported prior to EMR release 4.8.0."); } }  void validateEmrClusterDefinitionConfiguration(EmrClusterDefinition emrClusterDefinition); void validateEmrClusterDefinitionKey(EmrClusterDefinitionKey key);  }
@Test public void testValidateEmrClusterDefinitionConfigurationNullSubnet() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.setSubnetId(null); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception", IllegalArgumentException.class, e.getClass()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationValid() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); } catch (Exception e) { fail("expected no exception, but " + e.getClass() + " was thrown. " + e); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterSpotPriceSpecified() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceSpotPrice(BigDecimal.ONE); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); } catch (Exception e) { fail("expected no exception, but " + e.getClass() + " was thrown. " + e); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterMaxSearchPriceSpecified() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceMaxSearchPrice(BigDecimal.ONE); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); } catch (Exception e) { fail("expected no exception, but " + e.getClass() + " was thrown. " + e); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterMaxSearchPriceAndOnDemandThresholdSpecified() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceMaxSearchPrice(BigDecimal.ONE); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceOnDemandThreshold(BigDecimal.ONE); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); } catch (Exception e) { fail("expected no exception, but " + e.getClass() + " was thrown. " + e); } }
@Test public void testValidateEmrClusterDefinitionConfigurationTaskInstanceDefinitionNotSpecified() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().setTaskInstances(null); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); } catch (Exception e) { fail("expected no exception, but " + e.getClass() + " was thrown. " + e); } }
@Test public void testValidateEmrClusterDefinitionConfigurationBlankSubnet() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.setSubnetId(" \r\t\n"); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception", IllegalArgumentException.class, e.getClass()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationSubnetListBlankElement() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.setSubnetId(MockEc2OperationsImpl.SUBNET_1 + ", \r\t\n"); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception", IllegalArgumentException.class, e.getClass()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterSpotPriceNegative() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceSpotPrice(BigDecimal.ONE.negate()); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception", IllegalArgumentException.class, e.getClass()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterSpotPriceAndMaxSearchPriceSpecified() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceSpotPrice(BigDecimal.ONE); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceMaxSearchPrice(BigDecimal.ONE); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception", IllegalArgumentException.class, e.getClass()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterMaxSearchPriceNegative() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceMaxSearchPrice(BigDecimal.ONE.negate()); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception", IllegalArgumentException.class, e.getClass()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterOnDemandThresholdSpecified() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceOnDemandThreshold(BigDecimal.ONE); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { assertEquals("thrown exception", IllegalArgumentException.class, e.getClass()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationMasterMaxSearchPriceSpecifiedAndOnDemandThresholdNegative() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceMaxSearchPrice(BigDecimal.ONE); emrClusterDefinition.getInstanceDefinitions().getMasterInstances().setInstanceOnDemandThreshold(BigDecimal.ONE.negate()); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail("expected IllegalArgumentException, but no exception was thrown"); } catch (IllegalArgumentException e) { assertEquals("Master instance on-demand threshold must be greater than 0", e.getMessage()); } }
@Test public void testValidateEmrClusterDefinitionConfigurationCoreInstancesNotSpecified() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().setCoreInstances(null); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); } catch (Exception e) { fail("expected no exception, but " + e.getClass() + " was thrown. " + e); } }
@Test public void testValidateEmrClusterDefinitionConfigurationCoreInstanceSpecifiedInstanceCountNegative() { EmrClusterDefinition emrClusterDefinition = createValidEmrClusterDefinition(); emrClusterDefinition.getInstanceDefinitions().getCoreInstances().setInstanceCount(-1); try { emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition); fail(); } catch (IllegalArgumentException e) { assertEquals("At least 0 core instance must be specified.", e.getMessage()); } }