YARN-9679. Regular code cleanup in TestResourcePluginManager (#1122)

This commit is contained in:
Adam Antal 2019-08-15 17:32:05 +02:00 committed by Szilard Nemeth
parent 1845a83cec
commit 22c4f38c4b
1 changed files with 43 additions and 43 deletions

View File

@ -48,7 +48,6 @@ import org.apache.hadoop.yarn.server.security.ApplicationACLsManager;
import org.apache.hadoop.yarn.util.resource.ResourceUtils; import org.apache.hadoop.yarn.util.resource.ResourceUtils;
import org.apache.hadoop.yarn.util.resource.TestResourceUtils; import org.apache.hadoop.yarn.util.resource.TestResourceUtils;
import org.junit.After; import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -57,6 +56,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.io.File; import java.io.File;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -67,14 +68,10 @@ import static org.mockito.Mockito.spy;
public class TestResourcePluginManager extends NodeManagerTestBase { public class TestResourcePluginManager extends NodeManagerTestBase {
private NodeManager nm; private NodeManager nm;
private YarnConfiguration conf;
private String tempResourceTypesFile; private String tempResourceTypesFile;
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
this.conf = createNMConfig();
// setup resource-types.xml // setup resource-types.xml
ResourceUtils.resetResourceTypes(); ResourceUtils.resetResourceTypes();
String resourceTypesFile = "resource-types-pluggable-devices.xml"; String resourceTypesFile = "resource-types-pluggable-devices.xml";
@ -82,7 +79,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
TestResourceUtils.setupResourceTypes(this.conf, resourceTypesFile); TestResourceUtils.setupResourceTypes(this.conf, resourceTypesFile);
} }
ResourcePluginManager stubResourcePluginmanager() { private ResourcePluginManager stubResourcePluginmanager() {
// Stub ResourcePluginManager // Stub ResourcePluginManager
final ResourcePluginManager rpm = mock(ResourcePluginManager.class); final ResourcePluginManager rpm = mock(ResourcePluginManager.class);
Map<String, ResourcePlugin> plugins = new HashMap<>(); Map<String, ResourcePlugin> plugins = new HashMap<>();
@ -117,7 +114,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
// cleanup resource-types.xml // cleanup resource-types.xml
File dest = new File(this.tempResourceTypesFile); File dest = new File(this.tempResourceTypesFile);
if (dest.exists()) { if (dest.exists()) {
dest.delete(); assertThat(dest.delete()).isTrue();
} }
} }
@ -155,10 +152,10 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
} }
} }
private class MyMockNM extends NodeManager { private class ResourcePluginMockNM extends NodeManager {
private final ResourcePluginManager rpm; private final ResourcePluginManager rpm;
public MyMockNM(ResourcePluginManager rpm) { ResourcePluginMockNM(ResourcePluginManager rpm) {
this.rpm = rpm; this.rpm = rpm;
} }
@ -196,28 +193,28 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
} }
} }
/* /**
* Make sure ResourcePluginManager is initialized during NM start up. * Make sure {@link ResourcePluginManager} is initialized during NM start up.
*/ */
@Test(timeout = 30000) @Test(timeout = 30000)
public void testResourcePluginManagerInitialization() throws Exception { public void testResourcePluginManagerInitialization() throws Exception {
final ResourcePluginManager rpm = stubResourcePluginmanager(); final ResourcePluginManager rpm = stubResourcePluginmanager();
nm = new MyMockNM(rpm); nm = new ResourcePluginMockNM(rpm);
nm.init(conf); nm.init(conf);
verify(rpm).initialize( verify(rpm).initialize(
any(Context.class)); any(Context.class));
} }
/* /**
* Make sure ResourcePluginManager is invoked during NM update. * Make sure {@link ResourcePluginManager} is invoked during NM update.
*/ */
@Test(timeout = 30000) @Test(timeout = 30000)
public void testNodeStatusUpdaterWithResourcePluginsEnabled() public void testNodeStatusUpdaterWithResourcePluginsEnabled()
throws Exception { throws Exception {
final ResourcePluginManager rpm = stubResourcePluginmanager(); final ResourcePluginManager rpm = stubResourcePluginmanager();
nm = new MyMockNM(rpm); nm = new ResourcePluginMockNM(rpm);
nm.init(conf); nm.init(conf);
nm.start(); nm.start();
@ -230,8 +227,8 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
.updateConfiguredResource(any(Resource.class)); .updateConfiguredResource(any(Resource.class));
} }
/* /**
* Make sure ResourcePluginManager is used to initialize ResourceHandlerChain * Make sure ResourcePluginManager is used to initialize ResourceHandlerChain.
*/ */
@Test(timeout = 30000) @Test(timeout = 30000)
public void testLinuxContainerExecutorWithResourcePluginsEnabled() { public void testLinuxContainerExecutorWithResourcePluginsEnabled() {
@ -270,33 +267,36 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
nm.start(); nm.start();
ResourceHandler handler = lce.getResourceHandler(); ResourceHandler handler = lce.getResourceHandler();
Assert.assertNotNull(handler); assertThat(handler).isNotNull();
Assert.assertTrue(handler instanceof ResourceHandlerChain); assertThat(handler instanceof ResourceHandlerChain).isTrue();
boolean newHandlerAdded = false; boolean newHandlerAdded = false;
for (ResourceHandler h : ((ResourceHandlerChain) handler) for (ResourceHandler h : ((ResourceHandlerChain) handler)
.getResourceHandlerList()) { .getResourceHandlerList()) {
if (h instanceof DevicePluginAdapter) { if (h instanceof DevicePluginAdapter) {
Assert.assertTrue(false); fail("ResourceHandler is a DevicePluginAdapter.");
} }
if (h instanceof CustomizedResourceHandler) { if (h instanceof CustomizedResourceHandler) {
newHandlerAdded = true; newHandlerAdded = true;
break; break;
} }
} }
Assert.assertTrue("New ResourceHandler should be added", newHandlerAdded); assertThat(newHandlerAdded).withFailMessage(
"New ResourceHandler should be added").isTrue();
} }
// Disabled pluggable framework in configuration. /**
// We use spy object of real rpm to verify "initializePluggableDevicePlugins" * Disabled pluggable framework in configuration.
// because use mock rpm will not working * We use spy object of real rpm to verify "initializePluggableDevicePlugins"
* because use mock rpm will not working
*/
@Test(timeout = 30000) @Test(timeout = 30000)
public void testInitializationWithPluggableDeviceFrameworkDisabled() public void testInitializationWithPluggableDeviceFrameworkDisabled()
throws Exception { throws Exception {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
false); false);
@ -315,7 +315,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
nm.init(conf); nm.init(conf);
nm.start(); nm.start();
@ -332,7 +332,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
true); true);
@ -355,7 +355,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
boolean fail = false; boolean fail = false;
try { try {
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
@ -366,11 +366,11 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
} catch (YarnRuntimeException e) { } catch (YarnRuntimeException e) {
fail = true; fail = true;
} catch (Exception ignored) { } catch (Exception ignored) {
// ignore
} }
verify(rpmSpy).initializePluggableDevicePlugins( verify(rpmSpy).initializePluggableDevicePlugins(
any(Context.class), any(Configuration.class), anyMap()); any(Context.class), any(Configuration.class), anyMap());
Assert.assertTrue(fail); assertThat(fail).isTrue();
} }
@Test(timeout = 30000) @Test(timeout = 30000)
@ -378,7 +378,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
true); true);
@ -388,10 +388,10 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
nm.init(conf); nm.init(conf);
nm.start(); nm.start();
Map<String, ResourcePlugin> pluginMap = rpmSpy.getNameToPlugins(); Map<String, ResourcePlugin> pluginMap = rpmSpy.getNameToPlugins();
Assert.assertEquals(1, pluginMap.size()); assertThat(pluginMap.size()).isOne();
ResourcePlugin rp = pluginMap.get("cmpA.com/hdwA"); ResourcePlugin rp = pluginMap.get("cmpA.com/hdwA");
if (!(rp instanceof DevicePluginAdapter)) { if (!(rp instanceof DevicePluginAdapter)) {
Assert.fail(); fail("ResourcePlugin is not DevicePluginAdapter.");
} }
verify(rpmSpy).checkInterfaceCompatibility( verify(rpmSpy).checkInterfaceCompatibility(
DevicePlugin.class, FakeTestDevicePlugin1.class); DevicePlugin.class, FakeTestDevicePlugin1.class);
@ -403,7 +403,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
true); true);
@ -421,7 +421,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
} catch (YarnRuntimeException e) { } catch (YarnRuntimeException e) {
actualMessage = e.getMessage(); actualMessage = e.getMessage();
} }
Assert.assertEquals(expectedMessage, actualMessage); assertThat(actualMessage).isEqualTo(expectedMessage);
} }
// Fail to register duplicated resource name. // Fail to register duplicated resource name.
@ -430,7 +430,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
true); true);
@ -451,7 +451,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
} catch (YarnRuntimeException e) { } catch (YarnRuntimeException e) {
actualMessage = e.getMessage(); actualMessage = e.getMessage();
} }
Assert.assertEquals(expectedMessage, actualMessage); assertThat(actualMessage).isEqualTo(expectedMessage);
} }
/** /**
@ -463,7 +463,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
true); true);
@ -481,7 +481,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
} catch (YarnRuntimeException e) { } catch (YarnRuntimeException e) {
actualMessage = e.getMessage(); actualMessage = e.getMessage();
} }
Assert.assertEquals(expectedMessage, actualMessage); assertThat(actualMessage).isEqualTo(expectedMessage);
} }
@Test @Test
@ -493,7 +493,7 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
ResourcePluginManager rpmSpy = spy(rpm); ResourcePluginManager rpmSpy = spy(rpm);
rpmSpy.setDeviceMappingManager(dmmSpy); rpmSpy.setDeviceMappingManager(dmmSpy);
nm = new MyMockNM(rpmSpy); nm = new ResourcePluginMockNM(rpmSpy);
conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED,
true); true);
@ -508,16 +508,16 @@ public class TestResourcePluginManager extends NodeManagerTestBase {
DevicePlugin.class, FakeTestDevicePlugin1.class); DevicePlugin.class, FakeTestDevicePlugin1.class);
verify(dmmSpy).addDevicePluginScheduler( verify(dmmSpy).addDevicePluginScheduler(
any(String.class), any(DevicePluginScheduler.class)); any(String.class), any(DevicePluginScheduler.class));
Assert.assertEquals(1, dmm.getDevicePluginSchedulers().size()); assertThat(dmm.getDevicePluginSchedulers().size()).isOne();
} }
@Test(timeout = 30000) @Test(timeout = 30000)
public void testRequestedResourceNameIsConfigured() { public void testRequestedResourceNameIsConfigured() {
ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpm = new ResourcePluginManager();
String resourceName = "a.com/a"; String resourceName = "a.com/a";
Assert.assertFalse(rpm.isConfiguredResourceName(resourceName)); assertThat(rpm.isConfiguredResourceName(resourceName)).isFalse();
resourceName = "cmp.com/cmp"; resourceName = "cmp.com/cmp";
Assert.assertTrue(rpm.isConfiguredResourceName(resourceName)); assertThat(rpm.isConfiguredResourceName(resourceName)).isTrue();
} }
} }