|
26 | 26 | import static org.mockito.ArgumentMatchers.any; |
27 | 27 | import static org.mockito.ArgumentMatchers.anyBoolean; |
28 | 28 | import static org.mockito.Mockito.doAnswer; |
| 29 | +import static org.mockito.Mockito.doNothing; |
29 | 30 | import static org.mockito.Mockito.doThrow; |
30 | 31 | import static org.mockito.Mockito.mock; |
31 | 32 | import static org.mockito.Mockito.spy; |
|
37 | 38 | import java.io.FileReader; |
38 | 39 | import java.io.IOException; |
39 | 40 | import java.io.LineNumberReader; |
| 41 | +import java.lang.reflect.Field; |
40 | 42 | import java.net.InetSocketAddress; |
41 | 43 | import java.net.URI; |
42 | 44 | import java.net.URISyntaxException; |
@@ -345,7 +347,8 @@ public void testStartLocalizer() throws IOException { |
345 | 347 |
|
346 | 348 | @Test |
347 | 349 | public void testContainerLaunchError() |
348 | | - throws IOException, ContainerExecutionException, URISyntaxException { |
| 350 | + throws IOException, ContainerExecutionException, URISyntaxException, IllegalAccessException, |
| 351 | + NoSuchFieldException { |
349 | 352 |
|
350 | 353 | final String[] expecetedMessage = {"badcommand", "Exit code: 24"}; |
351 | 354 | final String[] executor = { |
@@ -387,6 +390,14 @@ public Object answer(InvocationOnMock invocationOnMock) |
387 | 390 | dirsHandler.init(conf); |
388 | 391 | mockExec.setConf(conf); |
389 | 392 |
|
| 393 | + //set the private nmContext field without initing the LinuxContainerExecutor |
| 394 | + NodeManager nodeManager = new NodeManager(); |
| 395 | + NodeManager.NMContext nmContext = |
| 396 | + nodeManager.createNMContext(null, null, null, false, conf); |
| 397 | + Field lceNmContext = LinuxContainerExecutor.class.getDeclaredField("nmContext"); |
| 398 | + lceNmContext.setAccessible(true); |
| 399 | + lceNmContext.set(mockExec, nmContext); |
| 400 | + |
390 | 401 | String appSubmitter = "nobody"; |
391 | 402 | String cmd = String |
392 | 403 | .valueOf(PrivilegedOperation.RunAsUserCommand.LAUNCH_CONTAINER. |
@@ -601,15 +612,30 @@ public void testNoExitCodeFromPrivilegedOperation() throws Exception { |
601 | 612 | LinuxContainerRuntime runtime = new DefaultLinuxContainerRuntime( |
602 | 613 | spyPrivilegedExecutor); |
603 | 614 | runtime.initialize(conf, null); |
604 | | - mockExec = new LinuxContainerExecutor(runtime); |
605 | | - mockExec.setConf(conf); |
606 | 615 | LinuxContainerExecutor lce = new LinuxContainerExecutor(runtime) { |
607 | 616 | @Override |
608 | 617 | protected PrivilegedOperationExecutor getPrivilegedOperationExecutor() { |
609 | 618 | return spyPrivilegedExecutor; |
610 | 619 | } |
611 | 620 | }; |
612 | 621 | lce.setConf(conf); |
| 622 | + |
| 623 | + //set the private nmContext field without initing the LinuxContainerExecutor |
| 624 | + NodeManager nodeManager = new NodeManager(); |
| 625 | + NodeManager.NMContext nmContext = |
| 626 | + nodeManager.createNMContext(null, null, null, false, conf); |
| 627 | + NodeManager.NMContext spyNmContext = spy(nmContext); |
| 628 | + |
| 629 | + //initialize a mock NodeStatusUpdater |
| 630 | + NodeStatusUpdaterImpl nodeStatusUpdater = mock(NodeStatusUpdaterImpl.class); |
| 631 | + nmContext.setNodeStatusUpdater(nodeStatusUpdater); |
| 632 | + //imitate a void method call on the NodeStatusUpdater when setting NM unhealthy. |
| 633 | + doNothing().when(nodeStatusUpdater).reportException(any()); |
| 634 | + |
| 635 | + Field lceNmContext = LinuxContainerExecutor.class.getDeclaredField("nmContext"); |
| 636 | + lceNmContext.setAccessible(true); |
| 637 | + lceNmContext.set(lce, nmContext); |
| 638 | + |
613 | 639 | InetSocketAddress address = InetSocketAddress.createUnresolved( |
614 | 640 | "localhost", 8040); |
615 | 641 | Path nmPrivateCTokensPath= new Path("file:///bin/nmPrivateCTokensPath"); |
@@ -672,6 +698,9 @@ protected PrivilegedOperationExecutor getPrivilegedOperationExecutor() { |
672 | 698 | assertTrue("Unexpected exception " + e, |
673 | 699 | e.getMessage().contains("exit code")); |
674 | 700 | } |
| 701 | + |
| 702 | + //verify that the NM was set unhealthy on PrivilegedOperationException |
| 703 | + verify(nodeStatusUpdater, times(1)).reportException(any()); |
675 | 704 | } |
676 | 705 |
|
677 | 706 | @Test |
|
0 commit comments