/** @throws DSLException . */ @Test public void testBasicExtendParsing() throws DSLException { final File testParsingBaseDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_parsing_base-service.groovy"); final File testParsingBaseWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service baseService = ServiceReader.getServiceFromFile(testParsingBaseDslFile, testParsingBaseWorkDir) .getService(); Assert.assertFalse(baseService.getName().equals("test parsing extend")); final ServiceLifecycle baseLifecycle = baseService.getLifecycle(); Assert.assertFalse(baseLifecycle.getInit().equals("test_parsing_extend_install.groovy")); Assert.assertNull(baseLifecycle.getStop()); final File testParsingExtendDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_parsing_extend-service.groovy"); final File testParsingExtendWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service service = ServiceReader.getServiceFromFile(testParsingExtendDslFile, testParsingExtendWorkDir) .getService(); Assert.assertEquals("test parsing extend", service.getName()); final ServiceLifecycle lifecycle = service.getLifecycle(); Assert.assertEquals(ExecutableDSLEntryType.STRING, lifecycle.getInit().getEntryType()); Assert.assertEquals( "test_parsing_extend_install.groovy", ((StringExecutableEntry) lifecycle.getInit()).getCommand()); Assert.assertNotNull(lifecycle.getStart()); Assert.assertNotNull(lifecycle.getPostStart()); Assert.assertNotNull(lifecycle.getPreStop()); Assert.assertNotNull(lifecycle.getStop()); Assert.assertEquals(1, service.getExtendedServicesPaths().size()); Assert.assertEquals( new File(TEST_PARSING_RESOURCE_PATH, "test_parsing_base-service.groovy").getAbsolutePath(), service.getExtendedServicesPaths().getFirst()); }
@Test public void testStorageDetailsParsing() throws Exception { final File serviceFile = new File(TEST_PARSING_RESOURCE_PATH3 + "simpleStorage-service.groovy"); final Service service = ServiceReader.getServiceFromFile(serviceFile); Assert.assertNotNull(service.getStorage()); Assert.assertNotNull(service.getStorage().getTemplate()); Assert.assertNotNull(service.getStorage().getTemplate().equals("SMALL_BLOCK")); }
@Test public void minConfigedGroovy() throws Exception { final File cassandraDslFile = new File(CORRUPTED_RESOURCES_PATH + "cassandra_bare_essentials-service.groovy"); final File cassandraWorkDir = new File(CORRUPTED_RESOURCES_PATH); final Service service = ServiceReader.getServiceFromFile(cassandraDslFile, cassandraWorkDir).getService(); assertNotNull(service); ServiceTestUtil.validateName(service, nameInGroovy); }
@Test public void testServiceWithMapMerge() throws Exception { final File serviceFile = new File(TEST_PARSING_AMP_MERGE_RESOURCE_PATH3 + "/mapMerge/b/b-service.groovy"); final Service service = ServiceReader.getServiceFromFile(serviceFile); Assert.assertNotNull(service); Assert.assertNotNull(service.getLifecycle()); Assert.assertNotNull(service.getCustomCommands()); Assert.assertEquals(2, service.getCustomCommands().size()); }
@Test public void uiOmmitedConfigGroovy() throws Exception { final File cassandraDslFile = new File(CORRUPTED_RESOURCES_PATH + "cassandra_UI_ommited-service.groovy"); final File cassandraWorkDir = new File(CORRUPTED_RESOURCES_PATH); final Service service = ServiceReader.getServiceFromFile(cassandraDslFile, cassandraWorkDir).getService(); assertNotNull(service); assertNull(service.getUserInterface()); ServiceTestUtil.validateName(service, nameInGroovy); ServiceTestUtil.validateIcon(service, CORRUPTED_RESOURCES_PATH); }
@Test public void testServiceWithGrab() throws Exception { final File serviceFile = new File(TEST_PARSING_RESOURCE_PATH3 + "serviceWithGrab-service.groovy"); final File serviceDir = serviceFile.getParentFile(); final Service service = ServiceReader.getServiceFromFile(serviceFile); Assert.assertNotNull(service); Assert.assertNotNull(service.getLifecycle()); // check that the @grab annotation worked. Assert.assertEquals("Tomcat", service.getName()); }
@Test public void testBasicExtendIllegalNestedPropertyLocation() { final File testParsingExtendDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_parsing_extend_illegal_nested-service.groovy"); final File testParsingExtendWorkDir = new File(TEST_PARSING_RESOURCE_PATH); try { ServiceReader.getServiceFromFile(testParsingExtendDslFile, testParsingExtendWorkDir) .getService(); Assert.fail("No exception thrown while extend resides in illegal place"); } catch (final Exception e) { // ignore } }
@Test public void testServiceWithMapOverride() throws Exception { final File serviceFile = new File(TEST_PARSING_AMP_MERGE_RESOURCE_PATH3 + "/mapOverride/b/b-service.groovy"); final Service service = ServiceReader.getServiceFromFile(serviceFile); Assert.assertNotNull(service); Assert.assertNotNull(service.getLifecycle()); Assert.assertNotNull(service.getCustomCommands()); Assert.assertEquals(1, service.getCustomCommands().size()); Assert.assertTrue(service.getCustomCommands().containsKey("cmdA")); Assert.assertTrue(service.getCustomCommands().get("cmdA") == null); }
/** @throws DSLException . */ @Test public void testDuplicateServicePropertyParsing() throws DSLException { final File testParsingBaseDslFile = new File( TEST_PARSING_RESOURCE_PATH + "test_parsing_on_duplicate_property_in_service_class-service.groovy"); final File testParsingBaseWorkDir = new File(TEST_PARSING_RESOURCE_PATH); try { ServiceReader.getServiceFromFile(testParsingBaseDslFile, testParsingBaseWorkDir).getService(); } catch (final IllegalArgumentException e) { System.out.println("Test passed. found duplication: " + e.getMessage()); } }
/** @throws Exception . */ @Test public void testLocationAwareParsing() throws Exception { final File testParsingBaseDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_location-aware-service.groovy"); final File testParsingBaseWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service service = ServiceReader.getServiceFromFile(testParsingBaseDslFile, testParsingBaseWorkDir) .getService(); Assert.assertTrue( "service.isLocationAware() returned false even though locationAware property is set to true", service.isLocationAware()); }
@Test public void nameFieldCorruptedGroovy() { final File cassandraDslFile = new File(CORRUPTED_RESOURCES_PATH + "cassandra_name_field_corrupted-service.groovy"); final File cassandraWorkDir = new File(CORRUPTED_RESOURCES_PATH); try { final Service service = ServiceReader.getServiceFromFile(cassandraDslFile, cassandraWorkDir).getService(); assertTrue("No exception was thrown due to Name field corruption", false); } catch (final Throwable t) { // getServiceFromFile should throw something informative due to corruption assertTrue( "Throwable isn't informative enought", t.getMessage().toLowerCase().contains("nae")); } }
@Test public void testStopDetectionWithScript() { final File serviceFile = new File(TEST_PARSING_RESOURCE_BASE + "misc/stopDetectionScript-service.groovy"); try { ServiceReader.getServiceFromFile(serviceFile); Assert.fail("Expected parsing to fail"); } catch (PackagingException e) { Assert.assertTrue( "Unexpected error message: " + e.getMessage(), e.getMessage().contains("The stop detection field only supports execution of closures")); } }
/** @throws DSLException . */ @Test public void testPropertyInCustomCommand() throws DSLException { final File testParsingExtendDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_property_in_custom_command-service.groovy"); final File testParsingExtendWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service service = ServiceReader.getServiceFromFile(testParsingExtendDslFile, testParsingExtendWorkDir) .getService(); final Closure<?> customCommand = ((ClosureExecutableEntry) service.getCustomCommands().get("custom_command")).getCommand(); final String[] params = new String[2]; params[0] = "name"; params[1] = "port"; customCommand.call((Object[]) params); customCommand.call((Object[]) params); }
/** * @throws DSLException . * @throws UnknownHostException . */ @Test public void testFeaturesParsing() throws DSLException, UnknownHostException { final File testParsingBaseDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_features-service.groovy"); final File testParsingBaseWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service service = ServiceReader.getServiceFromFile(testParsingBaseDslFile, testParsingBaseWorkDir) .getService(); Assert.assertEquals("test features", service.getName()); Assert.assertEquals( "http://" + InetAddress.getLocalHost().getHostName() + ":8080", service.getUrl()); final ServiceLifecycle lifecycle = service.getLifecycle(); Assert.assertNotNull(lifecycle.getStart()); Assert.assertNotNull(lifecycle.getPostStart()); Assert.assertNotNull(lifecycle.getPreStop()); }
/** @throws Exception . */ @Test public void testNoLocatorsParsing() throws Exception { final File testParsingBaseDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_no_locators_statement-service.groovy"); final File testParsingBaseWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service service = ServiceReader.getServiceFromFile(testParsingBaseDslFile, testParsingBaseWorkDir) .getService(); Assert.assertNotNull("Lifecycle should not be null", service.getLifecycle()); Assert.assertNotNull("Lifecycle should not be null", service.getLifecycle().getLocator()); final Object result = ((ClosureExecutableEntry) service.getLifecycle().getLocator()).getCommand().call(); Assert.assertNotNull("locators result should not be null", result); final List<Long> list = (List<Long>) result; Assert.assertEquals("Expected empty list", 0, list.size()); }
/** @throws DSLException . */ @Test public void testBasicParsing() throws DSLException { final File testParsingBaseDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_parsing_base-service.groovy"); final File testParsingBaseWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service service = ServiceReader.getServiceFromFile(testParsingBaseDslFile, testParsingBaseWorkDir) .getService(); Assert.assertEquals("test parsing base", service.getName()); final ServiceLifecycle lifecycle = service.getLifecycle(); Assert.assertEquals(ExecutableDSLEntryType.STRING, lifecycle.getInit().getEntryType()); Assert.assertEquals( "test_parsing_base_install.groovy", ((StringExecutableEntry) lifecycle.getInit()).getCommand()); Assert.assertNotNull(lifecycle.getStart()); Assert.assertNotNull(lifecycle.getPostStart()); Assert.assertNotNull(lifecycle.getPreStop()); }
/** * @throws DSLException . * @throws UnknownHostException . */ @Test public void testAutoScalingParsing() throws DSLException, UnknownHostException { final File testParsingBaseDslFile = new File(TEST_PARSING_RESOURCE_PATH + "test_parsing_autoscaling-service.groovy"); final File testParsingBaseWorkDir = new File(TEST_PARSING_RESOURCE_PATH); final Service service = ServiceReader.getServiceFromFile(testParsingBaseDslFile, testParsingBaseWorkDir) .getService(); Assert.assertTrue(service.getMinAllowedInstances() > 1); Assert.assertTrue(service.getNumInstances() >= service.getMinAllowedInstances()); Assert.assertTrue(service.getMaxAllowedInstances() >= service.getNumInstances()); Assert.assertEquals(1, service.getScaleInCooldownInSeconds()); Assert.assertEquals(1, service.getScaleOutCooldownInSeconds()); Assert.assertEquals(1, service.getScaleCooldownInSeconds()); Assert.assertNotNull(service.getSamplingPeriodInSeconds()); Assert.assertEquals("scalingRules", service.getName()); final List<ServiceStatisticsDetails> serviceStatistics = service.getServiceStatistics(); Assert.assertEquals(1, serviceStatistics.size()); Assert.assertNotNull(serviceStatistics.get(0)); final String servicestatisticsName = serviceStatistics.get(0).getName(); Assert.assertNotNull(serviceStatistics.get(0).getMetric()); Assert.assertNotNull(serviceStatistics.get(0).getInstancesStatistics()); Assert.assertNotNull(serviceStatistics.get(0).getTimeStatistics()); Assert.assertNotNull(serviceStatistics.get(0).getMovingTimeRangeInSeconds()); final List<PerInstanceStatisticsDetails> perInstanceStatistics = service.getPerInstanceStatistics(); Assert.assertEquals(1, perInstanceStatistics.size()); Assert.assertNotNull(perInstanceStatistics.get(0)); Assert.assertNotNull(perInstanceStatistics.get(0).getMetric()); Assert.assertNotNull(perInstanceStatistics.get(0).getInstancesStatistics()); Assert.assertNotNull(perInstanceStatistics.get(0).getTimeStatistics()); Assert.assertNotNull(perInstanceStatistics.get(0).getMovingTimeRangeInSeconds()); final List<ScalingRuleDetails> scalingRules = service.getScalingRules(); Assert.assertNotNull(scalingRules); Assert.assertEquals(2, scalingRules.size()); Assert.assertNotNull(scalingRules.get(0).getHighThreshold()); Assert.assertNotNull(scalingRules.get(0).getLowThreshold()); Assert.assertEquals(servicestatisticsName, scalingRules.get(0).getServiceStatistics()); final HighThresholdDetails highThreshold = scalingRules.get(0).getHighThreshold(); Assert.assertNotNull(highThreshold.getValue()); Assert.assertNotNull(highThreshold.getInstancesIncrease()); final LowThresholdDetails lowThreshold = scalingRules.get(0).getLowThreshold(); Assert.assertNotNull(lowThreshold.getValue()); Assert.assertNotNull(lowThreshold.getInstancesDecrease()); Assert.assertEquals( ((ServiceStatisticsDetails) scalingRules.get(1).getServiceStatistics()).getMetric(), serviceStatistics.get(0).getMetric()); Assert.assertEquals( ((ServiceStatisticsDetails) scalingRules.get(1).getServiceStatistics()) .getInstancesStatistics() .createInstancesStatistics(), serviceStatistics.get(0).getInstancesStatistics().createInstancesStatistics()); Assert.assertEquals( ((ServiceStatisticsDetails) scalingRules.get(1).getServiceStatistics()) .getTimeStatistics() .createTimeWindowStatistics(1, TimeUnit.MINUTES), serviceStatistics .get(0) .getTimeStatistics() .createTimeWindowStatistics(1, TimeUnit.MINUTES)); Assert.assertEquals( ((ServiceStatisticsDetails) scalingRules.get(1).getServiceStatistics()) .getMovingTimeRangeInSeconds(), serviceStatistics.get(0).getMovingTimeRangeInSeconds()); Assert.assertNotNull(scalingRules.get(1).getHighThreshold()); Assert.assertNotNull(scalingRules.get(1).getLowThreshold()); }