changeset 1188:deec46f46f05

Factor out startStorage()/stopStorage() and add diagnostics to integration-tests. Reviewed-by: vanaltj, omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007685.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Wed, 31 Jul 2013 14:58:11 +0200
parents ee1d0f51d9c0
children e9d7209e8c38
files integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/MongoQueriesTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/PluginTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/StorageConnectionTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/StorageTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java
diffstat 8 files changed, 45 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -45,7 +45,6 @@
 import org.junit.Test;
 
 import expectj.Spawn;
-import expectj.TimeoutException;
 
 /**
  * Integration tests to exercise the basics of the thermostat command line.
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -132,6 +132,33 @@
     public static Spawn spawnThermostat(String... args) throws IOException {
         return spawnThermostat(false, args);
     }
+    
+    public static Spawn startStorage() throws Exception {
+        clearStorageDataDirectory();
+
+        Spawn storage = spawnThermostat("storage", "--start");
+        try {
+            storage.expect("pid:");
+        } catch (IOException e) {
+            // this may happen if storage is already running.
+            e.printStackTrace();
+            String stderrContents = storage.getCurrentStandardOutContents();
+            System.err.println(stderrContents);
+            assertFalse(stderrContents.contains("Storage is already running with pid"));
+        }
+        storage.expectClose();
+
+        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+        return storage;
+    }
+    
+    public static Spawn stopStorage() throws Exception {
+        Spawn storage = spawnThermostat("storage", "--stop");
+        storage.expect("server shutdown complete");
+        storage.expectClose();
+        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+        return storage;
+    }
 
     public static Spawn spawnThermostat(boolean localeDependent, String... args) throws IOException {
         ExpectJ expect = new ExpectJ(TIMEOUT_IN_SECONDS);
@@ -213,6 +240,7 @@
     }
 
     private static void killProcess(int processId) throws Exception {
+        System.err.println("Killing process with pid: " + processId);
         Runtime.getRuntime().exec("kill " + processId).waitFor();
     }
 
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/MongoQueriesTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/MongoQueriesTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -72,8 +72,6 @@
 import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO;
 import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat;
 
-import expectj.Spawn;
-
 /*
  * This test class starts up a mongod instance and tests if thermostat
  * queries with expressions return what they supposed to return. 
@@ -117,13 +115,7 @@
 
     @BeforeClass
     public static void setUpOnce() throws Exception {
-        clearStorageDataDirectory();
-
-        Spawn storage = spawnThermostat("storage", "--start");
-        storage.expect("pid:");
-        storage.expectClose();
-
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+        startStorage();
 
         addCpuData(4);
     }
@@ -132,11 +124,7 @@
     public static void tearDownOnce() throws Exception {
         deleteCpuData();
 
-        Spawn storage = spawnThermostat("storage", "--stop");
-        storage.expect("server shutdown complete");
-        storage.expectClose();
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
-
+        stopStorage();
     }
 
     /*
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/PluginTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/PluginTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -74,7 +74,6 @@
         shell.expectClose();
 
         String stdOut = shell.getCurrentStandardOutContents();
-        String stdErr = shell.getCurrentStandardErrContents();
 
         assertTrue(stdOut.contains("list of commands"));
         assertTrue(stdOut.contains("help"));
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/StorageConnectionTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/StorageConnectionTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -38,8 +38,6 @@
 
 import java.io.IOException;
 
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 
@@ -49,22 +47,14 @@
 
 public class StorageConnectionTest extends IntegrationTest {
 
-    @BeforeClass
-    public static void setUpOnce() throws IOException, TimeoutException, ExpectJException {
-        Spawn storage = spawnThermostat("storage", "--start");
-        storage.expect("pid:");
-        storage.expectClose();
-
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+    // @BeforeClass // reinstate once we actually need storage running (see ignored tests)
+    public static void setUpOnce() throws Exception {
+        startStorage();
     }
 
-    @AfterClass
-    public static void tearDownOnce() throws IOException, TimeoutException, ExpectJException {
-        Spawn storage = spawnThermostat("storage", "--stop");
-        storage.expect("server shutdown complete");
-        storage.expectClose();
-
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+    // @AfterClass // reinstate once we actually need storage running
+    public static void tearDownOnce() throws Exception {
+        stopStorage();
     }
 
     @Ignore //FIXME when keyring/preferences improvements have been made, un-Ignore
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/StorageTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/StorageTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -46,11 +46,7 @@
     public void startAndStopStorage() throws Exception {
         Spawn storage;
 
-        storage = spawnThermostat("storage", "--start");
-        storage.expect("pid:");
-        storage.expectClose();
-
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+        storage = startStorage();
 
         storage = spawnThermostat("storage", "--status");
         storage.expect("Storage is running");
@@ -58,11 +54,7 @@
         
         assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
         
-        storage = spawnThermostat("storage", "--stop");
-        storage.expect("server shutdown complete");
-        storage.expectClose();
-        
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+        storage = stopStorage();
         
         storage = spawnThermostat("storage", "--status");
         storage.expect("Storage is not running");
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -52,19 +52,15 @@
 public class VmCommandsTest extends IntegrationTest {
 
     @BeforeClass
-    public static void setUpOnce() throws IOException, InterruptedException {
-        clearStorageDataDirectory();
-
-        Process startStorage = runThermostat("storage", "--start");
-        startStorage.waitFor();
+    public static void setUpOnce() throws Exception {
+        startStorage();
 
         // TODO insert actual data into the database and test that
     }
 
     @AfterClass
-    public static void tearDownOnce() throws InterruptedException, IOException {
-        Process stopStorage = runThermostat("storage", "--stop");
-        stopStorage.waitFor();
+    public static void tearDownOnce() throws Exception {
+        stopStorage();
     }
 
     @Ignore //FIXME when keyring/preferences improvements have been made, un-Ignore
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java	Mon Jul 22 13:58:07 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java	Wed Jul 31 14:58:11 2013 +0200
@@ -93,8 +93,6 @@
 import com.redhat.thermostat.web.client.internal.WebStorage;
 import com.redhat.thermostat.web.server.auth.Roles;
 
-import expectj.Spawn;
-
 /**
  * This test class starts up a mongod instance and a web storage instance
  * (in jetty container) in front of that.  Tests should make their own
@@ -230,13 +228,8 @@
 
     @BeforeClass
     public static void setUpOnce() throws Exception {
-        clearStorageDataDirectory();
+        startStorage();
 
-        Spawn storage = spawnThermostat("storage", "--start");
-        storage.expect("pid:");
-        storage.expectClose();
-
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
         backupUsers = Files.createTempFile("itest-backup-thermostat-users", "");
         backupRoles = Files.createTempFile("itest-backup-thermostat-roles", "");
         backupRoles.toFile().deleteOnExit();
@@ -261,13 +254,10 @@
     public static void tearDownOnce() throws Exception {
         deleteCpuData();
 
-        Spawn storage = spawnThermostat("storage", "--stop");
-        storage.expect("server shutdown complete");
-        storage.expectClose();
-        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
-
         server.stop();
         server.join();
+        
+        stopStorage();
 
         Files.copy(backupUsers, new File(THERMOSTAT_USERS_FILE).toPath(), StandardCopyOption.REPLACE_EXISTING);
         Files.copy(backupRoles, new File(THERMOSTAT_ROLES_FILE).toPath(), StandardCopyOption.REPLACE_EXISTING);