diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java index 201b6d77d3..4df20da68c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java @@ -66,6 +66,7 @@ import javax.servlet.http.HttpServletResponse; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -74,17 +75,10 @@ */ public class TestSchedConfCLI extends JerseyTestBase { - private ByteArrayOutputStream sysOutStream; - private PrintStream sysOut; - - private ByteArrayOutputStream sysErrStream; - private PrintStream sysErr; - private SchedConfCLI cli; private static MockRM rm; private static String userName; - private static CapacitySchedulerConfiguration csConf; private static final File CONF_FILE = new File(new File("target", "test-classes"), YarnConfiguration.CS_CONFIGURATION_FILE); @@ -101,14 +95,6 @@ public TestSchedConfCLI() { @Before public void setUp() { - sysOutStream = new ByteArrayOutputStream(); - sysOut = new PrintStream(sysOutStream); - System.setOut(sysOut); - - sysErrStream = new ByteArrayOutputStream(); - sysErr = new PrintStream(sysErrStream); - System.setErr(sysErr); - cli = new SchedConfCLI(); } @@ -131,8 +117,8 @@ protected void configureServlets() { + ioe.getMessage(), ioe); } - csConf = new CapacitySchedulerConfiguration(new Configuration(false), - false); + CapacitySchedulerConfiguration csConf = new + CapacitySchedulerConfiguration(new Configuration(false), false); setupQueueConfiguration(csConf); try { @@ -220,6 +206,9 @@ private void cleanUp() throws Exception { @Test(timeout = 10000) public void testGetSchedulerConf() throws Exception { + ByteArrayOutputStream sysOutStream = new ByteArrayOutputStream(); + PrintStream sysOut = new PrintStream(sysOutStream); + System.setOut(sysOut); try { super.setUp(); GuiceServletConfig.setInjector( @@ -268,51 +257,46 @@ public void testFormatSchedulerConf() throws Exception { @Test(timeout = 10000) public void testInvalidConf() throws Exception { - // conf pair with no key should be invalid - int exitCode = cli.run(new String[] {"-add", "root.a:=confVal"}); - assertTrue("Should return an error code", exitCode != 0); - assertTrue(sysErrStream.toString().contains("Specify configuration key " + - "value as confKey=confVal.")); - exitCode = cli.run(new String[] {"-update", "root.a:=confVal"}); - assertTrue("Should return an error code", exitCode != 0); - assertTrue(sysErrStream.toString().contains("Specify configuration key " + - "value as confKey=confVal.")); + ByteArrayOutputStream sysErrStream = new ByteArrayOutputStream(); + PrintStream sysErr = new PrintStream(sysErrStream); + System.setErr(sysErr); - exitCode = cli.run(new String[] {"-add", "root.a:confKey=confVal=conf"}); - assertTrue("Should return an error code", exitCode != 0); - assertTrue(sysErrStream.toString().contains("Specify configuration key " + - "value as confKey=confVal.")); - exitCode = cli.run(new String[] {"-update", "root.a:confKey=confVal=c"}); - assertTrue("Should return an error code", exitCode != 0); - assertTrue(sysErrStream.toString().contains("Specify configuration key " + - "value as confKey=confVal.")); + // conf pair with no key should be invalid + executeCommand(sysErrStream, "-add", "root.a:=confVal"); + executeCommand(sysErrStream, "-update", "root.a:=confVal"); + executeCommand(sysErrStream, "-add", "root.a:confKey=confVal=conf"); + executeCommand(sysErrStream, "-update", "root.a:confKey=confVal=c"); + } + + private void executeCommand(ByteArrayOutputStream sysErrStream, String op, + String queueConf) throws Exception { + int exitCode = cli.run(new String[] {op, queueConf}); + assertNotEquals("Should return an error code", 0, exitCode); + assertTrue(sysErrStream.toString() + .contains("Specify configuration key " + "value as confKey=confVal.")); } @Test(timeout = 10000) public void testAddQueues() { SchedConfUpdateInfo schedUpdateInfo = new SchedConfUpdateInfo(); cli.addQueues("root.a:a1=aVal1,a2=aVal2,a3=", schedUpdateInfo); - QueueConfigInfo addInfo = schedUpdateInfo.getAddQueueInfo().get(0); - assertEquals("root.a", addInfo.getQueue()); - Map params = addInfo.getParams(); - assertEquals(3, params.size()); - assertEquals("aVal1", params.get("a1")); - assertEquals("aVal2", params.get("a2")); - assertNull(params.get("a3")); + Map paramValues = new HashMap<>(); + List addQueueInfo = schedUpdateInfo.getAddQueueInfo(); + paramValues.put("a1", "aVal1"); + paramValues.put("a2", "aVal2"); + paramValues.put("a3", null); + validateQueueConfigInfo(addQueueInfo, 0, "root.a", paramValues); schedUpdateInfo = new SchedConfUpdateInfo(); cli.addQueues("root.b:b1=bVal1;root.c:c1=cVal1", schedUpdateInfo); - assertEquals(2, schedUpdateInfo.getAddQueueInfo().size()); - QueueConfigInfo bAddInfo = schedUpdateInfo.getAddQueueInfo().get(0); - assertEquals("root.b", bAddInfo.getQueue()); - Map bParams = bAddInfo.getParams(); - assertEquals(1, bParams.size()); - assertEquals("bVal1", bParams.get("b1")); - QueueConfigInfo cAddInfo = schedUpdateInfo.getAddQueueInfo().get(1); - assertEquals("root.c", cAddInfo.getQueue()); - Map cParams = cAddInfo.getParams(); - assertEquals(1, cParams.size()); - assertEquals("cVal1", cParams.get("c1")); + addQueueInfo = schedUpdateInfo.getAddQueueInfo(); + assertEquals(2, addQueueInfo.size()); + paramValues.clear(); + paramValues.put("b1", "bVal1"); + validateQueueConfigInfo(addQueueInfo, 0, "root.b", paramValues); + paramValues.clear(); + paramValues.put("c1", "cVal1"); + validateQueueConfigInfo(addQueueInfo, 1, "root.c", paramValues); } @Test(timeout = 10000) @@ -320,12 +304,11 @@ public void testAddQueuesWithCommaInValue() { SchedConfUpdateInfo schedUpdateInfo = new SchedConfUpdateInfo(); cli.addQueues("root.a:a1=a1Val1\\,a1Val2 a1Val3,a2=a2Val1\\,a2Val2", schedUpdateInfo); - QueueConfigInfo addInfo = schedUpdateInfo.getAddQueueInfo().get(0); - assertEquals("root.a", addInfo.getQueue()); - Map params = addInfo.getParams(); - assertEquals(2, params.size()); - assertEquals("a1Val1,a1Val2 a1Val3", params.get("a1")); - assertEquals("a2Val1,a2Val2", params.get("a2")); + List addQueueInfo = schedUpdateInfo.getAddQueueInfo(); + Map params = new HashMap<>(); + params.put("a1", "a1Val1,a1Val2 a1Val3"); + params.put("a2", "a2Val1,a2Val2"); + validateQueueConfigInfo(addQueueInfo, 0, "root.a", params); } @Test(timeout = 10000) @@ -342,28 +325,35 @@ public void testRemoveQueues() { @Test(timeout = 10000) public void testUpdateQueues() { SchedConfUpdateInfo schedUpdateInfo = new SchedConfUpdateInfo(); + Map paramValues = new HashMap<>(); cli.updateQueues("root.a:a1=aVal1,a2=aVal2,a3=", schedUpdateInfo); - QueueConfigInfo updateInfo = schedUpdateInfo.getUpdateQueueInfo().get(0); - assertEquals("root.a", updateInfo.getQueue()); - Map params = updateInfo.getParams(); - assertEquals(3, params.size()); - assertEquals("aVal1", params.get("a1")); - assertEquals("aVal2", params.get("a2")); - assertNull(params.get("a3")); + List updateQueueInfo = schedUpdateInfo + .getUpdateQueueInfo(); + paramValues.put("a1", "aVal1"); + paramValues.put("a2", "aVal2"); + paramValues.put("a3", null); + validateQueueConfigInfo(updateQueueInfo, 0, "root.a", paramValues); schedUpdateInfo = new SchedConfUpdateInfo(); cli.updateQueues("root.b:b1=bVal1;root.c:c1=cVal1", schedUpdateInfo); - assertEquals(2, schedUpdateInfo.getUpdateQueueInfo().size()); - QueueConfigInfo bUpdateInfo = schedUpdateInfo.getUpdateQueueInfo().get(0); - assertEquals("root.b", bUpdateInfo.getQueue()); - Map bParams = bUpdateInfo.getParams(); - assertEquals(1, bParams.size()); - assertEquals("bVal1", bParams.get("b1")); - QueueConfigInfo cUpdateInfo = schedUpdateInfo.getUpdateQueueInfo().get(1); - assertEquals("root.c", cUpdateInfo.getQueue()); - Map cParams = cUpdateInfo.getParams(); - assertEquals(1, cParams.size()); - assertEquals("cVal1", cParams.get("c1")); + updateQueueInfo = schedUpdateInfo.getUpdateQueueInfo(); + assertEquals(2, updateQueueInfo.size()); + paramValues.clear(); + paramValues.put("b1", "bVal1"); + validateQueueConfigInfo(updateQueueInfo, 0, "root.b", paramValues); + paramValues.clear(); + paramValues.put("c1", "cVal1"); + validateQueueConfigInfo(updateQueueInfo, 1, "root.c", paramValues); + } + + private void validateQueueConfigInfo( + List updateQueueInfo, int index, String queuename, + Map paramValues) { + QueueConfigInfo updateInfo = updateQueueInfo.get(index); + assertEquals(queuename, updateInfo.getQueue()); + Map params = updateInfo.getParams(); + assertEquals(paramValues.size(), params.size()); + paramValues.forEach((k, v) -> assertEquals(v, params.get(k))); } @Test(timeout = 10000) @@ -371,12 +361,12 @@ public void testUpdateQueuesWithCommaInValue() { SchedConfUpdateInfo schedUpdateInfo = new SchedConfUpdateInfo(); cli.updateQueues("root.a:a1=a1Val1\\,a1Val2 a1Val3,a2=a2Val1\\,a2Val2", schedUpdateInfo); - QueueConfigInfo updateInfo = schedUpdateInfo.getUpdateQueueInfo().get(0); - assertEquals("root.a", updateInfo.getQueue()); - Map params = updateInfo.getParams(); - assertEquals(2, params.size()); - assertEquals("a1Val1,a1Val2 a1Val3", params.get("a1")); - assertEquals("a2Val1,a2Val2", params.get("a2")); + List updateQueueInfo = schedUpdateInfo + .getUpdateQueueInfo(); + Map paramValues = new HashMap<>(); + paramValues.put("a1", "a1Val1,a1Val2 a1Val3"); + paramValues.put("a2", "a2Val1,a2Val2"); + validateQueueConfigInfo(updateQueueInfo, 0, "root.a", paramValues); } @Test(timeout = 10000) @@ -384,10 +374,10 @@ public void testGlobalUpdate() { SchedConfUpdateInfo schedUpdateInfo = new SchedConfUpdateInfo(); cli.globalUpdates("schedKey1=schedVal1,schedKey2=schedVal2", schedUpdateInfo); - Map globalInfo = schedUpdateInfo.getGlobalParams(); - assertEquals(2, globalInfo.size()); - assertEquals("schedVal1", globalInfo.get("schedKey1")); - assertEquals("schedVal2", globalInfo.get("schedKey2")); + Map paramValues = new HashMap<>(); + paramValues.put("schedKey1", "schedVal1"); + paramValues.put("schedKey2", "schedVal2"); + validateGlobalParams(schedUpdateInfo, paramValues); } @Test(timeout = 10000) @@ -396,10 +386,16 @@ public void testGlobalUpdateWithCommaInValue() { cli.globalUpdates( "schedKey1=schedVal1.1\\,schedVal1.2 schedVal1.3,schedKey2=schedVal2", schedUpdateInfo); + Map paramValues = new HashMap<>(); + paramValues.put("schedKey1", "schedVal1.1,schedVal1.2 schedVal1.3"); + paramValues.put("schedKey2", "schedVal2"); + validateGlobalParams(schedUpdateInfo, paramValues); + } + + private void validateGlobalParams(SchedConfUpdateInfo schedUpdateInfo, + Map paramValues) { Map globalInfo = schedUpdateInfo.getGlobalParams(); - assertEquals(2, globalInfo.size()); - assertEquals("schedVal1.1,schedVal1.2 schedVal1.3", - globalInfo.get("schedKey1")); - assertEquals("schedVal2", globalInfo.get("schedKey2")); + assertEquals(paramValues.size(), globalInfo.size()); + paramValues.forEach((k, v) -> assertEquals(v, globalInfo.get(k))); } }