HIGUCHI Yuta
Committed by Gerrit Code Review

Clean up tempDir after each test runs.

- Fix for issue with /tmp disk full issue on Jenkins.
- Using JUnit Rule TemporaryFolder where possible.

Change-Id: Ie91eba37581ba5bf6c32be7f614220e2098ce2f8
......@@ -17,9 +17,10 @@
package org.onosproject.cfg.impl;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.onosproject.cfg.ComponentConfigAdapter;
import org.slf4j.Logger;
......@@ -38,7 +39,8 @@ import static org.slf4j.LoggerFactory.getLogger;
*/
public class ComponentConfigLoaderTest {
static final File TEST_DIR = Files.createTempDir();
@ClassRule
public static TemporaryFolder testFolder = new TemporaryFolder();
private static final String FOO_COMPONENT = "fooComponent";
......@@ -53,8 +55,8 @@ public class ComponentConfigLoaderTest {
* and assign it to the loader.configService for the test.
*/
@Before
public void setUp() {
ComponentConfigLoader.cfgFile = new File(TEST_DIR, "test.json");
public void setUp() throws IOException {
ComponentConfigLoader.cfgFile = new File(testFolder.newFolder(), "test.json");
loader = new ComponentConfigLoader();
service = new TestConfigService();
loader.configService = service;
......
......@@ -23,7 +23,9 @@ import java.util.Map;
import java.util.stream.IntStream;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.onlab.junit.TestTools;
import org.onlab.util.ItemNotFoundException;
import org.onosproject.net.DeviceId;
......@@ -36,8 +38,6 @@ import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.io.Files;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Files.write;
import static org.hamcrest.MatcherAssert.assertThat;
......@@ -53,16 +53,17 @@ import static org.hamcrest.Matchers.nullValue;
*/
public class ControllerTest {
@ClassRule
public static TemporaryFolder testFolder = new TemporaryFolder();
Controller controller;
protected static final Logger log = LoggerFactory.getLogger(ControllerTest.class);
static final File TEST_DIR = Files.createTempDir();
/*
* Writes the necessary file for the tests in the temporary directory
*/
static File stageTestResource(String name) throws IOException {
File file = new File(TEST_DIR, name);
private static File stageTestResource(String name) throws IOException {
File file = new File(testFolder.newFolder(), name);
byte[] bytes = toByteArray(ControllerTest.class.getResourceAsStream(name));
write(bytes, file);
return file;
......
......@@ -15,9 +15,12 @@
*/
package org.onlab.stc;
import com.google.common.io.Files;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onlab.util.Tools;
import com.google.common.io.Files;
import java.io.File;
import java.io.FileInputStream;
......@@ -35,10 +38,12 @@ import static org.onlab.stc.Scenario.loadScenario;
*/
public class CompilerTest {
static final File TEST_DIR = Files.createTempDir();
private static File testDir;
@BeforeClass
public static void setUpClass() throws IOException {
testDir = Files.createTempDir();
stageTestResource("scenario.xml");
stageTestResource("simple-scenario.xml");
stageTestResource("one-scenario.xml");
......@@ -49,16 +54,21 @@ public class CompilerTest {
System.setProperty("TOC1", "1.2.3.1");
System.setProperty("TOC2", "1.2.3.2");
System.setProperty("TOC3", "1.2.3.3");
System.setProperty("test.dir", TEST_DIR.getAbsolutePath());
System.setProperty("test.dir", testDir.getAbsolutePath());
}
@AfterClass
public static void tearDownClass() throws IOException {
Tools.removeDirectory(testDir.getPath());
}
static FileInputStream getStream(String name) throws FileNotFoundException {
return new FileInputStream(new File(TEST_DIR, name));
return new FileInputStream(new File(testDir, name));
}
static void stageTestResource(String name) throws IOException {
byte[] bytes = toByteArray(CompilerTest.class.getResourceAsStream(name));
write(bytes, new File(TEST_DIR, name));
write(bytes, new File(testDir, name));
}
@Test
......@@ -72,7 +82,7 @@ public class CompilerTest {
assertEquals("incorrect step count", 33, flow.getVertexes().size());
assertEquals("incorrect dependency count", 26, flow.getEdges().size());
assertEquals("incorrect logDir",
new File(TEST_DIR.getAbsolutePath(), "foo"), compiler.logDir());
new File(testDir.getAbsolutePath(), "foo"), compiler.logDir());
Step step = compiler.getStep("there");
assertEquals("incorrect edge count", 2, flow.getEdgesFrom(step).size());
......
......@@ -15,6 +15,7 @@
*/
package org.onlab.stc;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onlab.util.Tools;
......@@ -36,11 +37,15 @@ public class CoordinatorTest {
@BeforeClass
public static void setUpClass() throws IOException {
CompilerTest.setUpClass();
Tools.removeDirectory(StepProcessorTest.DIR);
StepProcessor.launcher = "true ";
}
@AfterClass
public static void tearDownClass() throws IOException {
CompilerTest.tearDownClass();
}
@Test
public void simple() throws IOException, InterruptedException {
executeTest("simple-scenario.xml");
......
......@@ -29,6 +29,7 @@ public class DependencyTest extends StepTest {
protected Step step1, step2;
@Override
@Before
public void setUp() throws ConfigurationException {
super.setUp();
......@@ -52,6 +53,7 @@ public class DependencyTest extends StepTest {
assertTrue("incorrect isSoft", soft.isSoft());
}
@Override
@Test
public void equality() {
Dependency d1 = new Dependency(step1, step2, false);
......
......@@ -15,6 +15,8 @@
*/
package org.onlab.stc;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onlab.stc.MonitorLayout.Box;
......@@ -33,6 +35,16 @@ public class MonitorLayoutTest {
private MonitorLayout layout;
@BeforeClass
public static void setUpClass() throws IOException {
CompilerTest.setUpClass();
}
@AfterClass
public static void tearDownClass() throws IOException {
CompilerTest.tearDownClass();
}
private Compiler getCompiler(String name) throws IOException {
stageTestResource(name);
Scenario scenario = loadScenario(getStream(name));
......
......@@ -15,15 +15,11 @@
*/
package org.onlab.stc;
import com.google.common.io.Files;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.onlab.util.Tools;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import static com.google.common.base.Preconditions.checkState;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
......@@ -34,24 +30,23 @@ import static org.onlab.stc.Coordinator.Status.SUCCEEDED;
*/
public class StepProcessorTest {
static final File DIR = Files.createTempDir();
@ClassRule
public static TemporaryFolder testFolder = new TemporaryFolder();
private static File dir;
private final Listener delegate = new Listener();
@BeforeClass
public static void setUpClass() {
dir = testFolder.getRoot();
StepProcessor.launcher = "echo";
checkState(DIR.exists() || DIR.mkdirs(), "Unable to create directory");
}
@AfterClass
public static void tearDownClass() throws IOException {
Tools.removeDirectory(DIR.getPath());
checkState(dir.exists() || dir.mkdirs(), "Unable to create directory");
}
@Test
public void basics() {
Step step = new Step("foo", "ls " + DIR.getAbsolutePath(), null, null, null, 0);
StepProcessor processor = new StepProcessor(step, DIR, delegate, step.command());
Step step = new Step("foo", "ls " + dir.getAbsolutePath(), null, null, null, 0);
StepProcessor processor = new StepProcessor(step, dir, delegate, step.command());
processor.run();
assertTrue("should be started", delegate.started);
assertTrue("should be stopped", delegate.stopped);
......