You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2017/02/17 13:21:18 UTC

jclouds-labs git commit: Tests can be executed concurrently - fix test assumptions

Repository: jclouds-labs
Updated Branches:
  refs/heads/2.0.x e4e42454d -> 82cad92e3


Tests can be executed concurrently - fix test assumptions

Tests from the same class are executed concurrently so don't share state in the class. Additionally use temporary files/folders to allow for parallel builds.


Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/82cad92e
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/82cad92e
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/82cad92e

Branch: refs/heads/2.0.x
Commit: 82cad92e3e08bd2e4dcb9d3cbc18f5f16e2cfcfd
Parents: e4e4245
Author: Svetoslav Neykov <sv...@neykov.name>
Authored: Fri Feb 17 15:05:50 2017 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Fri Feb 17 14:19:06 2017 +0100

----------------------------------------------------------------------
 .../jclouds/vagrant/internal/BoxConfigTest.java | 14 ++----
 .../vagrant/internal/MachineConfigTest.java     | 53 +++++++++++---------
 2 files changed, 34 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/82cad92e/vagrant/src/test/java/org/jclouds/vagrant/internal/BoxConfigTest.java
----------------------------------------------------------------------
diff --git a/vagrant/src/test/java/org/jclouds/vagrant/internal/BoxConfigTest.java b/vagrant/src/test/java/org/jclouds/vagrant/internal/BoxConfigTest.java
index 631d813..9b9ee56 100644
--- a/vagrant/src/test/java/org/jclouds/vagrant/internal/BoxConfigTest.java
+++ b/vagrant/src/test/java/org/jclouds/vagrant/internal/BoxConfigTest.java
@@ -22,7 +22,7 @@ import java.io.File;
 import java.io.IOException;
 
 import org.jclouds.vagrant.reference.VagrantConstants;
-import org.testng.annotations.BeforeMethod;
+import org.jclouds.vagrant.util.VagrantUtils;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Optional;
@@ -33,20 +33,14 @@ import vagrant.api.domain.Box;
 
 public class BoxConfigTest {
 
-   private File vagrantHome;
-
-   @BeforeMethod
-   public void setUp() {
-      vagrantHome = new File(System.getProperty("java.io.tmpdir"), "jclouds/vagrant");
-   }
-
    @Test
    public void testKeys() throws IOException {
+      File vagrantHome = new File(Files.createTempDir(), "jclouds/vagrant");
       File boxFolder = new File(vagrantHome, "boxes/jclouds-VAGRANTSLASH-vagrant/0/virtualbox");
       boxFolder.mkdirs();
       File boxPath = new File(boxFolder, VagrantConstants.VAGRANTFILE);
       Resources.asByteSource(getClass().getResource("/Vagrantfile.boxconfig")).copyTo(Files.asByteSink(boxPath));
-      
+
       BoxConfig boxConfig = new BoxConfig.Factory().newInstance(vagrantHome, new Box("jclouds/vagrant", "0", "virtualbox"));
       assertEquals(boxConfig.getKey(".non.existent"), Optional.absent());
       assertEquals(boxConfig.getStringKey(".non.existent"), Optional.absent());
@@ -60,7 +54,7 @@ public class BoxConfigTest {
       assertEquals(boxConfig.getStringKey(VagrantConstants.KEY_SSH_PRIVATE_KEY_PATH), Optional.of("/path/to/private.key"));
       assertEquals(boxConfig.getStringKey(VagrantConstants.KEY_SSH_PORT), Optional.of("2222"));
 
-      boxPath.delete();
+      VagrantUtils.deleteFolder(vagrantHome);
    }
 
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/82cad92e/vagrant/src/test/java/org/jclouds/vagrant/internal/MachineConfigTest.java
----------------------------------------------------------------------
diff --git a/vagrant/src/test/java/org/jclouds/vagrant/internal/MachineConfigTest.java b/vagrant/src/test/java/org/jclouds/vagrant/internal/MachineConfigTest.java
index eb968a6..f366d81 100644
--- a/vagrant/src/test/java/org/jclouds/vagrant/internal/MachineConfigTest.java
+++ b/vagrant/src/test/java/org/jclouds/vagrant/internal/MachineConfigTest.java
@@ -25,8 +25,7 @@ import java.util.Map;
 
 import org.jclouds.JcloudsVersion;
 import org.jclouds.vagrant.reference.VagrantConstants;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
+import org.jclouds.vagrant.util.VagrantUtils;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Charsets;
@@ -44,36 +43,26 @@ public class MachineConfigTest {
          .put("cpus", "1")
          .build();
 
-   private File machineFolder;
-   private MachineConfig machineConfig;
-   private File configPath;
-
-   @BeforeMethod
-   public void setUp() throws IOException {
-      File vagrantHome = new File(System.getProperty("java.io.tmpdir"), "jclouds/vagrant");
-      machineFolder = new File(vagrantHome, "jclouds");
-      File configFolder = new File(machineFolder, VagrantConstants.MACHINES_CONFIG_SUBFOLDER);
-      configFolder.mkdirs();
-      configPath = new File(configFolder, "vagrant" + VagrantConstants.MACHINES_CONFIG_EXTENSION);
-      machineConfig = new MachineConfig.Factory().newInstance(machineFolder, "vagrant");
-   }
-
-   @AfterMethod
-   public void tearDown() {
-      configPath.delete();
-   }
-
    @Test
    public void testRead() throws IOException {
-      Resources.asByteSource(getClass().getResource("/machine-config.yaml")).copyTo(Files.asByteSink(configPath));
+      File machineFolder = Files.createTempDir();
+      File configFile = getConifgFile(machineFolder);
+      MachineConfig machineConfig = getMachineConfig(configFile);
+
+      Resources.asByteSource(getClass().getResource("/machine-config.yaml")).copyTo(Files.asByteSink(configFile));
       assertEquals(machineConfig.load(), CONFIG);
+      VagrantUtils.deleteFolder(machineFolder);
    }
 
    @Test
    public void testWrite() throws IOException {
+      File machineFolder = Files.createTempDir();
+      File configFile = getConifgFile(machineFolder);
+      MachineConfig machineConfig = getMachineConfig(configFile);
+
       machineConfig.save(CONFIG);
       ByteArrayOutputStream actualBytes = new ByteArrayOutputStream();
-      Files.asByteSource(configPath).copyTo(actualBytes);
+      Files.asByteSource(configFile).copyTo(actualBytes);
 
       ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream();
       Resources.asByteSource(getClass().getResource("/machine-config.yaml")).copyTo(expectedBytes);
@@ -85,13 +74,31 @@ public class MachineConfigTest {
             // Strip license headers
             .replaceAll("(?m)^#.*", "")
             .trim());
+      VagrantUtils.deleteFolder(machineFolder);
    }
 
    @Test
    public void testUpdatesVersion() throws IOException {
+      File machineFolder = Files.createTempDir();
+      File configFile = getConifgFile(machineFolder);
+      MachineConfig machineConfig = getMachineConfig(configFile);
+
       machineConfig.save(CONFIG);
       Map<String, Object> newConfig = machineConfig.load();
       assertEquals(newConfig.get(VagrantConstants.CONFIG_JCLOUDS_VERSION), JcloudsVersion.get().toString());
+      VagrantUtils.deleteFolder(machineFolder);
+   }
+
+   private MachineConfig getMachineConfig(File configFile) {
+      String machineName = configFile.getName().replaceAll(VagrantConstants.MACHINES_CONFIG_EXTENSION, "");
+      return new MachineConfig.Factory().newInstance(configFile.getParentFile().getParentFile(), machineName);
+   }
+
+   private File getConifgFile(File machineFolder) {
+      File configFolder = new File(machineFolder, VagrantConstants.MACHINES_CONFIG_SUBFOLDER);
+      configFolder.mkdirs();
+      File machineFile = new File(configFolder, "vagrant" + VagrantConstants.MACHINES_CONFIG_EXTENSION);
+      return machineFile;
    }
 
 }