YARN-8759. Copy of resource-types.xml is not deleted if test fails, causes other test failures. Contributed by Antal Bálint Steinbach.

This commit is contained in:
Sunil G 2018-10-17 16:05:08 +05:30
parent 41b3603b5b
commit 5085e5fa9e
2 changed files with 32 additions and 36 deletions

View File

@ -39,6 +39,9 @@ import java.util.Map;
*/
public class TestResourceUtils {
private File nodeResourcesFile;
private File resourceTypesFile;
static class ResourceFileInformation {
String filename;
int resourceCount;
@ -75,12 +78,11 @@ public class TestResourceUtils {
@After
public void teardown() {
Configuration conf = new YarnConfiguration();
File source = new File(
conf.getClassLoader().getResource("resource-types-1.xml").getFile());
File dest = new File(source.getParent(), "resource-types.xml");
if (dest.exists()) {
dest.delete();
if(nodeResourcesFile != null && nodeResourcesFile.exists()) {
nodeResourcesFile.delete();
}
if(resourceTypesFile != null && resourceTypesFile.exists()) {
resourceTypesFile.delete();
}
}
@ -136,8 +138,8 @@ public class TestResourceUtils {
File source = new File(
conf.getClassLoader().getResource(testInformation.filename)
.getFile());
File dest = new File(source.getParent(), "resource-types.xml");
FileUtils.copyFile(source, dest);
resourceTypesFile = new File(source.getParent(), "resource-types.xml");
FileUtils.copyFile(source, resourceTypesFile);
res = ResourceUtils.getResourceTypes();
testMemoryAndVcores(res);
Assert.assertEquals(testInformation.resourceCount, res.size());
@ -148,7 +150,6 @@ public class TestResourceUtils {
res.containsKey(resourceName));
Assert.assertEquals(entry.getValue(), res.get(resourceName).getUnits());
}
dest.delete();
}
}
@ -161,20 +162,17 @@ public class TestResourceUtils {
"resource-types-error-4.xml"};
for (String resourceFile : resourceFiles) {
ResourceUtils.resetResourceTypes();
File dest = null;
try {
File source =
new File(conf.getClassLoader().getResource(resourceFile).getFile());
dest = new File(source.getParent(), "resource-types.xml");
FileUtils.copyFile(source, dest);
resourceTypesFile = new File(source.getParent(), "resource-types.xml");
FileUtils.copyFile(source, resourceTypesFile);
ResourceUtils.getResourceTypes();
Assert.fail("Expected error with file " + resourceFile);
} catch (NullPointerException ne) {
throw ne;
} catch (Exception e) {
if (dest != null) {
dest.delete();
}
//Test passed
}
}
}
@ -275,7 +273,7 @@ public class TestResourceUtils {
ResourceUtils.initializeResourcesMap(conf);
Assert.fail("resource map initialization should fail");
} catch (Exception e) {
// do nothing
//Test passed
}
}
}
@ -299,11 +297,10 @@ public class TestResourceUtils {
for (Map.Entry<String, Resource> entry : testRun.entrySet()) {
String resourceFile = entry.getKey();
ResourceUtils.resetNodeResources();
File dest;
File source = new File(
conf.getClassLoader().getResource(resourceFile).getFile());
dest = new File(source.getParent(), "node-resources.xml");
FileUtils.copyFile(source, dest);
nodeResourcesFile = new File(source.getParent(), "node-resources.xml");
FileUtils.copyFile(source, nodeResourcesFile);
Map<String, ResourceInformation> actual = ResourceUtils
.getNodeResourceInformation(conf);
Assert.assertEquals(actual.size(),
@ -311,7 +308,6 @@ public class TestResourceUtils {
for (ResourceInformation resInfo : entry.getValue().getResources()) {
Assert.assertEquals(resInfo, actual.get(resInfo.getName()));
}
dest.delete();
}
}
@ -324,19 +320,16 @@ public class TestResourceUtils {
for (String resourceFile : invalidNodeResFiles) {
ResourceUtils.resetNodeResources();
File dest = null;
try {
File source = new File(conf.getClassLoader().getResource(resourceFile).getFile());
dest = new File(source.getParent(), "node-resources.xml");
FileUtils.copyFile(source, dest);
nodeResourcesFile = new File(source.getParent(), "node-resources.xml");
FileUtils.copyFile(source, nodeResourcesFile);
Map<String, ResourceInformation> actual = ResourceUtils.getNodeResourceInformation(conf);
Assert.fail("Expected error with file " + resourceFile);
} catch (NullPointerException ne) {
throw ne;
} catch (Exception e) {
if (dest != null) {
dest.delete();
}
//Test passed
}
}
}
@ -401,11 +394,10 @@ public class TestResourceUtils {
for (Map.Entry<String, Resource> entry : testRun.entrySet()) {
String resourceFile = entry.getKey();
ResourceUtils.resetNodeResources();
File dest;
File source = new File(
conf.getClassLoader().getResource(resourceFile).getFile());
dest = new File(source.getParent(), "node-resources.xml");
FileUtils.copyFile(source, dest);
nodeResourcesFile = new File(source.getParent(), "node-resources.xml");
FileUtils.copyFile(source, nodeResourcesFile);
Map<String, ResourceInformation> actual = ResourceUtils
.getNodeResourceInformation(conf);
Assert.assertEquals(actual.size(),
@ -413,7 +405,6 @@ public class TestResourceUtils {
for (ResourceInformation resInfo : entry.getValue().getResources()) {
Assert.assertEquals(resInfo, actual.get(resInfo.getName()));
}
dest.delete();
}
}

View File

@ -174,6 +174,7 @@ import org.apache.hadoop.yarn.util.resource.DominantResourceCalculator;
import org.apache.hadoop.yarn.util.resource.ResourceCalculator;
import org.apache.hadoop.yarn.util.resource.ResourceUtils;
import org.apache.hadoop.yarn.util.resource.Resources;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
@ -194,6 +195,7 @@ public class TestClientRMService {
private final static String QUEUE_1 = "Q-1";
private final static String QUEUE_2 = "Q-2";
private File resourceTypesFile = null;
@Test
public void testGetDecommissioningClusterNodes() throws Exception {
@ -2492,12 +2494,12 @@ public class TestClientRMService {
public void testRegisterNMWithDiffUnits() throws Exception {
ResourceUtils.resetResourceTypes();
Configuration yarnConf = new YarnConfiguration();
String resourceTypesFile = "resource-types-4.xml";
String resourceTypesFileName = "resource-types-4.xml";
InputStream source =
yarnConf.getClassLoader().getResourceAsStream(resourceTypesFile);
File dest = new File(yarnConf.getClassLoader().
yarnConf.getClassLoader().getResourceAsStream(resourceTypesFileName);
resourceTypesFile = new File(yarnConf.getClassLoader().
getResource(".").getPath(), "resource-types.xml");
FileUtils.copyInputStreamToFile(source, dest);
FileUtils.copyInputStreamToFile(source, resourceTypesFile);
ResourceUtils.getResourceTypes();
yarnConf.setClass(
@ -2566,9 +2568,12 @@ public class TestClientRMService {
rpc.stopProxy(client, conf);
rm.close();
}
if (dest.exists()) {
dest.delete();
@After
public void tearDown(){
if (resourceTypesFile != null && resourceTypesFile.exists()) {
resourceTypesFile.delete();
}
}
}