FSConfigToCSConfigArgumentHandler { int parseAndConvert(String[] args) throws Exception { Options opts = createOptions(); int retVal = 0; try { if (args.length == 0) { LOG.info("Missing command line arguments"); printHelp(opts); return 0; } CommandLine cliParser = new GnuParser().parse(opts, args); if (cliParser.hasOption(CliOption.HELP.shortSwitch)) { printHelp(opts); return 0; } FSConfigToCSConfigConverter converter = prepareAndGetConverter(cliParser); converter.convert(converterParams); String outputDir = converterParams.getOutputDirectory(); boolean skipVerification = cliParser.hasOption(CliOption.SKIP_VERIFICATION.shortSwitch); if (outputDir != null && !skipVerification) { validator.validateConvertedConfig( converterParams.getOutputDirectory()); } } catch (ParseException e) { String msg = "Options parsing failed: " + e.getMessage(); logAndStdErr(e, msg); printHelp(opts); retVal = -1; } catch (PreconditionException e) { String msg = "Cannot start FS config conversion due to the following" + " precondition error: " + e.getMessage(); handleException(e, msg); retVal = -1; } catch (UnsupportedPropertyException e) { String msg = "Unsupported property/setting encountered during FS config " + "conversion: " + e.getMessage(); handleException(e, msg); retVal = -1; } catch (ConversionException | IllegalArgumentException e) { String msg = "Fatal error during FS config conversion: " + e.getMessage(); handleException(e, msg); retVal = -1; } catch (VerificationException e) { Throwable cause = e.getCause(); String msg = "Verification failed: " + e.getCause().getMessage(); conversionOptions.handleVerificationFailure(cause, msg); retVal = -1; } conversionOptions.handleParsingFinished(); return retVal; } FSConfigToCSConfigArgumentHandler(); @VisibleForTesting  FSConfigToCSConfigArgumentHandler(ConversionOptions conversionOptions,
      ConvertedConfigValidator validator);   }
@Test public void testMissingYarnSiteXmlArgument() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = new String[] {"-o", FSConfigConverterTestCommons.OUTPUT_DIR}; int retVal = argumentHandler.parseAndConvert(args); assertEquals("Return value", -1, retVal); assertTrue("Error content missing", fsTestCommons.getErrContent() .toString().contains("Missing yarn-site.xml parameter")); }
@Test public void testMissingFairSchedulerXmlArgument() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); argumentHandler.parseAndConvert(getDefaultArgumentsAsArray()); }
@Test public void testMissingOutputDirArgument() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = new String[] {"-y", FSConfigConverterTestCommons.YARN_SITE_XML}; int retVal = argumentHandler.parseAndConvert(args); assertEquals("Return value", -1, retVal); assertTrue("Error content missing", fsTestCommons.getErrContent() .toString() .contains("Output directory or console mode was not defined")); }
@Test public void testMissingRulesConfiguration() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); argumentHandler.parseAndConvert(getDefaultArgumentsAsArray()); }
@Test public void testInvalidRulesConfigFile() throws Exception { FSConfigConverterTestCommons.configureYarnSiteXmlWithFsAllocFileDefined(); FSConfigConverterTestCommons.configureFairSchedulerXml(); FSConfigConverterTestCommons.configureInvalidConversionRulesFile(); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArrayWithDefaults(); argumentHandler.parseAndConvert(args); }
@Test public void testInvalidOutputDir() throws Exception { FSConfigConverterTestCommons.configureYarnSiteXmlWithFsAllocFileDefined(); FSConfigConverterTestCommons.configureFairSchedulerXml(); FSConfigConverterTestCommons.configureDummyConversionRulesFile(); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArray("-y", FSConfigConverterTestCommons.YARN_SITE_XML, "-o", FSConfigConverterTestCommons.YARN_SITE_XML); int retVal = argumentHandler.parseAndConvert(args); assertEquals("Return value", -1, retVal); assertTrue("Error content missing", fsTestCommons.getErrContent() .toString() .contains("Cannot start FS config conversion due to the following " + "precondition error")); }
@Test public void testFairSchedulerXmlIsNotDefinedIfItsDefinedInYarnSiteXml() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); argumentHandler.parseAndConvert(getDefaultArgumentsAsArray()); }
@Test public void testEmptyYarnSiteXmlSpecified() throws Exception { FSConfigConverterTestCommons.configureFairSchedulerXml(); FSConfigConverterTestCommons.configureEmptyYarnSiteXml(); FSConfigConverterTestCommons.configureDummyConversionRulesFile(); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE); argumentHandler.parseAndConvert(args); }
@Test public void testEmptyFairSchedulerXmlSpecified() throws Exception { FSConfigConverterTestCommons.configureEmptyFairSchedulerXml(); FSConfigConverterTestCommons.configureEmptyYarnSiteXml(); FSConfigConverterTestCommons.configureDummyConversionRulesFile(); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE); argumentHandler.parseAndConvert(args); }
@Test public void testEmptyRulesConfigurationSpecified() throws Exception { FSConfigConverterTestCommons.configureEmptyFairSchedulerXml(); FSConfigConverterTestCommons.configureEmptyYarnSiteXml(); FSConfigConverterTestCommons.configureEmptyConversionRulesFile(); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-r", FSConfigConverterTestCommons.CONVERSION_RULES_FILE); argumentHandler.parseAndConvert(args); }
@Test public void testConvertFSConfigurationDefaults() throws Exception { setupFSConfigConversionFiles(true); ArgumentCaptor<FSConfigToCSConfigConverterParams> conversionParams = ArgumentCaptor.forClass(FSConfigToCSConfigConverterParams.class); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-r", FSConfigConverterTestCommons.CONVERSION_RULES_FILE); argumentHandler.parseAndConvert(args); verify(mockConverter).convert(conversionParams.capture()); FSConfigToCSConfigConverterParams params = conversionParams.getValue(); LOG.info("FS config converter parameters: " + params); assertEquals("Yarn site config", FSConfigConverterTestCommons.YARN_SITE_XML, params.getYarnSiteXmlConfig()); assertEquals("FS xml", FSConfigConverterTestCommons.FS_ALLOC_FILE, params.getFairSchedulerXmlConfig()); assertEquals("Conversion rules config", FSConfigConverterTestCommons.CONVERSION_RULES_FILE, params.getConversionRulesConfig()); assertFalse("Console mode", params.isConsole()); }
@Test public void testConvertFSConfigurationWithConsoleParam() throws Exception { setupFSConfigConversionFiles(true); ArgumentCaptor<FSConfigToCSConfigConverterParams> conversionParams = ArgumentCaptor.forClass(FSConfigToCSConfigConverterParams.class); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-r", FSConfigConverterTestCommons.CONVERSION_RULES_FILE, "-p"); argumentHandler.parseAndConvert(args); verify(mockConverter).convert(conversionParams.capture()); FSConfigToCSConfigConverterParams params = conversionParams.getValue(); LOG.info("FS config converter parameters: " + params); assertEquals("Yarn site config", FSConfigConverterTestCommons.YARN_SITE_XML, params.getYarnSiteXmlConfig()); assertEquals("FS xml", FSConfigConverterTestCommons.FS_ALLOC_FILE, params.getFairSchedulerXmlConfig()); assertEquals("Conversion rules config", FSConfigConverterTestCommons.CONVERSION_RULES_FILE, params.getConversionRulesConfig()); assertTrue("Console mode", params.isConsole()); }
@Test public void testConvertFSConfigurationClusterResource() throws Exception { setupFSConfigConversionFiles(true); ArgumentCaptor<FSConfigToCSConfigConverterParams> conversionParams = ArgumentCaptor.forClass(FSConfigToCSConfigConverterParams.class); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-r", FSConfigConverterTestCommons.CONVERSION_RULES_FILE, "-p", "-c", "vcores=20, memory-mb=240"); argumentHandler.parseAndConvert(args); verify(mockConverter).convert(conversionParams.capture()); FSConfigToCSConfigConverterParams params = conversionParams.getValue(); LOG.info("FS config converter parameters: " + params); assertEquals("Yarn site config", FSConfigConverterTestCommons.YARN_SITE_XML, params.getYarnSiteXmlConfig()); assertEquals("FS xml", FSConfigConverterTestCommons.FS_ALLOC_FILE, params.getFairSchedulerXmlConfig()); assertEquals("Conversion rules config", FSConfigConverterTestCommons.CONVERSION_RULES_FILE, params.getConversionRulesConfig()); assertEquals("Cluster resource", "vcores=20, memory-mb=240", params.getClusterResource()); assertTrue("Console mode", params.isConsole()); }
@Test public void testConvertFSConfigurationErrorHandling() throws Exception { setupFSConfigConversionFiles(true); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-r", FSConfigConverterTestCommons.CONVERSION_RULES_FILE, "-p"); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); Mockito.doThrow(UnsupportedPropertyException.class) .when(mockConverter) .convert(ArgumentMatchers.any(FSConfigToCSConfigConverterParams.class)); int retVal = argumentHandler.parseAndConvert(args); assertEquals("Return value", -1, retVal); assertTrue("Error content missing", fsTestCommons.getErrContent() .toString().contains("Unsupported property/setting encountered")); }
@Test public void testConvertFSConfigurationErrorHandling2() throws Exception { setupFSConfigConversionFiles(true); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-r", FSConfigConverterTestCommons.CONVERSION_RULES_FILE, "-p"); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); Mockito.doThrow(ConversionException.class).when(mockConverter) .convert(ArgumentMatchers.any(FSConfigToCSConfigConverterParams.class)); int retVal = argumentHandler.parseAndConvert(args); assertEquals("Return value", -1, retVal); assertTrue("Error content missing", fsTestCommons.getErrContent() .toString().contains("Fatal error during FS config conversion")); }
@Test public void testYarnSiteOptionInOutputFolder() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = createArgumentHandler(); String[] args = new String[] { "-y", FSConfigConverterTestCommons.YARN_SITE_XML, "-o", FSConfigConverterTestCommons.TEST_DIR}; int retVal = argumentHandler.parseAndConvert(args); assertEquals("Return value", -1, retVal); assertTrue(fsTestCommons.getErrContent() .toString().contains("contains the yarn-site.xml")); }
@Test public void testValidationSkippedWhenCmdLineSwitchIsDefined() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = new FSConfigToCSConfigArgumentHandler(conversionOptions, mockValidator); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-s"); argumentHandler.parseAndConvert(args); verifyZeroInteractions(mockValidator); }
@Test public void testValidationSkippedWhenOutputIsConsole() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = new FSConfigToCSConfigArgumentHandler(conversionOptions, mockValidator); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-s", "-p"); argumentHandler.parseAndConvert(args); verifyZeroInteractions(mockValidator); }
@Test public void testEnabledAsyncScheduling() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = new FSConfigToCSConfigArgumentHandler(conversionOptions, mockValidator); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-p", "-a"); argumentHandler.parseAndConvert(args); assertTrue("-a switch had no effect", conversionOptions.isEnableAsyncScheduler()); }
@Test public void testDisabledAsyncScheduling() throws Exception { setupFSConfigConversionFiles(true); FSConfigToCSConfigArgumentHandler argumentHandler = new FSConfigToCSConfigArgumentHandler(conversionOptions, mockValidator); String[] args = getArgumentsAsArrayWithDefaults("-f", FSConfigConverterTestCommons.FS_ALLOC_FILE, "-p"); argumentHandler.parseAndConvert(args); assertFalse("-a switch wasn't provided but async scheduling option is true", conversionOptions.isEnableAsyncScheduler()); }