diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java index dbd980b8501..b6219e81fe2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java @@ -18,6 +18,7 @@ package org.apache.hadoop.yarn.server.nodemanager.webapp; +import com.google.common.collect.ImmutableMap; import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; import com.sun.jersey.api.client.ClientResponse; @@ -93,7 +94,6 @@ import java.net.URI; import java.net.URL; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -189,6 +189,58 @@ public boolean isPmemCheckEnabled() { Guice.createInjector(new WebServletModule())); } + private void setupMockPluginsWithNmResourceInfo() throws YarnException { + ResourcePlugin mockPlugin1 = mock(ResourcePlugin.class); + NMResourceInfo nmResourceInfo1 = new NMResourceInfo() { + public long a = 1000L; + }; + when(mockPlugin1.getNMResourceInfo()).thenReturn(nmResourceInfo1); + + ResourcePluginManager pluginManager = createResourceManagerWithPlugins( + ImmutableMap.builder() + .put("resource-1", mockPlugin1) + .put("yarn.io/resource-1", mockPlugin1) + .put("resource-2", mock(ResourcePlugin.class)) + .build() + ); + + nmContext.setResourcePluginManager(pluginManager); + } + + private void setupMockPluginsWithGpuResourceInfo() throws YarnException { + GpuDeviceInformation gpuDeviceInformation = new GpuDeviceInformation(); + gpuDeviceInformation.setDriverVersion("1.2.3"); + gpuDeviceInformation.setGpus(Arrays.asList(new PerGpuDeviceInformation())); + + ResourcePlugin mockPlugin1 = mock(ResourcePlugin.class); + List totalGpuDevices = Arrays.asList( + new GpuDevice(1, 1), new GpuDevice(2, 2), new GpuDevice(3, 3)); + List assignedGpuDevices = Arrays.asList( + new AssignedGpuDevice(2, 2, createContainerId(1)), + new AssignedGpuDevice(3, 3, createContainerId(2))); + NMResourceInfo nmResourceInfo1 = new NMGpuResourceInfo(gpuDeviceInformation, + totalGpuDevices, + assignedGpuDevices); + when(mockPlugin1.getNMResourceInfo()).thenReturn(nmResourceInfo1); + + ResourcePluginManager pluginManager = createResourceManagerWithPlugins( + ImmutableMap.builder() + .put("resource-1", mockPlugin1) + .put("yarn.io/resource-1", mockPlugin1) + .put("resource-2", mock(ResourcePlugin.class)) + .build() + ); + + nmContext.setResourcePluginManager(pluginManager); + } + + private ResourcePluginManager createResourceManagerWithPlugins( + Map plugins) { + ResourcePluginManager pluginManager = mock(ResourcePluginManager.class); + when(pluginManager.getNameToPlugins()).thenReturn(plugins); + return pluginManager; + } + @Before @Override public void setUp() throws Exception { @@ -431,60 +483,74 @@ public void testNMRedirect() { } @Test - public void testGetNMResourceInfo() - throws YarnException, InterruptedException, JSONException { - ResourcePluginManager rpm = mock(ResourcePluginManager.class); - Map namesToPlugins = new HashMap<>(); - ResourcePlugin mockPlugin1 = mock(ResourcePlugin.class); - NMResourceInfo nmResourceInfo1 = new NMResourceInfo() { - public long a = 1000L; - }; - when(mockPlugin1.getNMResourceInfo()).thenReturn(nmResourceInfo1); - namesToPlugins.put("resource-1", mockPlugin1); - namesToPlugins.put("yarn.io/resource-1", mockPlugin1); - ResourcePlugin mockPlugin2 = mock(ResourcePlugin.class); - namesToPlugins.put("resource-2", mockPlugin2); - when(rpm.getNameToPlugins()).thenReturn(namesToPlugins); - - nmContext.setResourcePluginManager(rpm); + public void testGetNMResourceInfoSuccessful() + throws YarnException, JSONException { + setupMockPluginsWithNmResourceInfo(); WebResource r = resource(); ClientResponse response = r.path("ws").path("v1").path("node").path( - "resources").path("resource-2").accept(MediaType.APPLICATION_JSON).get( + "resources").path("resource-1").accept(MediaType.APPLICATION_JSON).get( ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, + assertEquals("MediaType of the response is not the expected!", + MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, response.getType().toString()); - - // Access resource-2 should fail (empty NMResourceInfo returned). JSONObject json = response.getEntity(JSONObject.class); - Assert.assertEquals(0, json.length()); + Assert.assertEquals("Unexpected value in the json response!", + 1000, json.get("a")); + } - // Access resource-3 should fail (unknown plugin) - response = r.path("ws").path("v1").path("node").path( - "resources").path("resource-3").accept(MediaType.APPLICATION_JSON).get( - ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, + @Test + public void testGetNMResourceInfoEncodedIsSuccessful() + throws YarnException, JSONException { + setupMockPluginsWithNmResourceInfo(); + + //test encoded yarn.io/resource-1 path + WebResource r = resource(); + ClientResponse response = r.path("ws").path("v1").path("node").path( + "resources").path("yarn.io%2Fresource-1") + .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + assertEquals("MediaType of the response is not the expected!", + MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, response.getType().toString()); - json = response.getEntity(JSONObject.class); - Assert.assertEquals(0, json.length()); + JSONObject json = response.getEntity(JSONObject.class); + Assert.assertEquals("Unexpected value in the json response!", + 1000, json.get("a")); + } - // Access resource-1 should success - response = r.path("ws").path("v1").path("node").path( - "resources").path("resource-1").accept(MediaType.APPLICATION_JSON).get( + @Test + public void testGetNMResourceInfoFailBecauseOfEmptyResourceInfo() + throws YarnException { + setupMockPluginsWithNmResourceInfo(); + + WebResource r = resource(); + ClientResponse response = r.path("ws").path("v1").path("node").path( + "resources").path("resource-2").accept(MediaType.APPLICATION_JSON).get( ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, + assertEquals("MediaType of the response is not the expected!", + MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, response.getType().toString()); - json = response.getEntity(JSONObject.class); - Assert.assertEquals(1000, json.get("a")); - // Access resource-1 should success (encoded yarn.io/Fresource-1). - response = r.path("ws").path("v1").path("node").path("resources").path( - "yarn.io%2Fresource-1").accept(MediaType.APPLICATION_JSON).get( + JSONObject json = response.getEntity(JSONObject.class); + Assert.assertEquals("Unexpected value in the json response!", + 0, json.length()); + } + + @Test + public void testGetNMResourceInfoFailBecauseOfUnknownPlugin() + throws YarnException { + setupMockPluginsWithNmResourceInfo(); + + WebResource r = resource(); + ClientResponse response = r.path("ws").path("v1").path("node").path( + "resources").path("resource-3").accept(MediaType.APPLICATION_JSON).get( ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, + assertEquals("MediaType of the response is not the expected!", + MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, response.getType().toString()); - json = response.getEntity(JSONObject.class); - Assert.assertEquals(1000, json.get("a")); + + JSONObject json = response.getEntity(JSONObject.class); + Assert.assertEquals("Unexpected value in the json response!", + 0, json.length()); } private ContainerId createContainerId(int id) { @@ -497,43 +563,24 @@ private ContainerId createContainerId(int id) { @Test public void testGetYarnGpuResourceInfo() - throws YarnException, InterruptedException, JSONException { - ResourcePluginManager rpm = mock(ResourcePluginManager.class); - Map namesToPlugins = new HashMap<>(); - ResourcePlugin mockPlugin1 = mock(ResourcePlugin.class); - GpuDeviceInformation gpuDeviceInformation = new GpuDeviceInformation(); - gpuDeviceInformation.setDriverVersion("1.2.3"); - gpuDeviceInformation.setGpus(Arrays.asList(new PerGpuDeviceInformation())); - NMResourceInfo nmResourceInfo1 = new NMGpuResourceInfo(gpuDeviceInformation, - Arrays.asList(new GpuDevice(1, 1), new GpuDevice(2, 2), - new GpuDevice(3, 3)), Arrays - .asList(new AssignedGpuDevice(2, 2, createContainerId(1)), - new AssignedGpuDevice(3, 3, createContainerId(2)))); - when(mockPlugin1.getNMResourceInfo()).thenReturn(nmResourceInfo1); - namesToPlugins.put("resource-1", mockPlugin1); - namesToPlugins.put("yarn.io/resource-1", mockPlugin1); - ResourcePlugin mockPlugin2 = mock(ResourcePlugin.class); - namesToPlugins.put("resource-2", mockPlugin2); - when(rpm.getNameToPlugins()).thenReturn(namesToPlugins); - - nmContext.setResourcePluginManager(rpm); + throws YarnException, JSONException { + setupMockPluginsWithGpuResourceInfo(); WebResource r = resource(); - ClientResponse response; - JSONObject json; - - // Access resource-1 should success - response = r.path("ws").path("v1").path("node").path( + ClientResponse response = r.path("ws").path("v1").path("node").path( "resources").path("resource-1").accept(MediaType.APPLICATION_JSON).get( ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, + assertEquals("MediaType of the response is not the expected!", + MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, response.getType().toString()); - json = response.getEntity(JSONObject.class); - Assert.assertEquals("1.2.3", + JSONObject json = response.getEntity(JSONObject.class); + Assert.assertEquals("Unexpected driverVersion in the json response!", + "1.2.3", json.getJSONObject("gpuDeviceInformation").get("driverVersion")); - Assert.assertEquals(3, json.getJSONArray("totalGpuDevices").length()); - Assert.assertEquals(2, json.getJSONArray("assignedGpuDevices").length()); - Assert.assertEquals(2, json.getJSONArray("assignedGpuDevices").length()); + Assert.assertEquals("Unexpected totalGpuDevices in the json response!", + 3, json.getJSONArray("totalGpuDevices").length()); + Assert.assertEquals("Unexpected assignedGpuDevices in the json response!", + 2, json.getJSONArray("assignedGpuDevices").length()); } private void testContainerLogs(WebResource r, ContainerId containerId)