changeset 1233:4279c0994a38

Add support for mongodb v2.4. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-August/008039.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Mon, 26 Aug 2013 18:33:02 +0200
parents 7936f09da466
children ba8a0e50ddee
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunnerTest.java
diffstat 2 files changed, 54 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java	Tue Aug 27 11:50:35 2013 -0400
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java	Mon Aug 26 18:33:02 2013 +0200
@@ -74,7 +74,7 @@
     };
 
     private static final String NO_JOURNAL_ARGUMENT = "--nojournal";
-    private static final String NO_JOURNAL_FIRST_VERSION = "1.9.2";
+    static final String NO_JOURNAL_FIRST_VERSION = "1.9.2";
 
     private DBStartupConfiguration configuration;
     private boolean isQuiet;
@@ -271,11 +271,31 @@
             throw e;
         }
         InputStream out = process.getInputStream();
-        InputStreamReader reader = new InputStreamReader(out);
-        BufferedReader bufReader = new BufferedReader(reader);
-        String firstLine = bufReader.readLine();
-        int commaIdx = firstLine.indexOf(",", 12);
-        String versionString = firstLine.substring(12, commaIdx);
+        return doGetDBVersion(out);
+    }
+    
+    // package private for testing
+    String doGetDBVersion(InputStream in) throws IOException {
+        // Default to no-journal first version if we can't parse the version
+        // output for some reason.
+        String versionString = NO_JOURNAL_FIRST_VERSION;
+        String firstLine = null;
+        try(InputStreamReader reader = new InputStreamReader(in)) {
+            BufferedReader bufReader = new BufferedReader(reader);
+            firstLine = bufReader.readLine();
+            int commaIdx = firstLine.indexOf(",", 12);
+            if (commaIdx != -1) {
+                versionString = firstLine.substring(12, commaIdx);
+            } else {
+                versionString = firstLine.substring(12);
+            }
+        } catch (Exception e) {
+            // catching Exception here in order to also catch potential NPEs or
+            // IndexOutOfBoundExceptions. If those conditions happen we fall
+            // back to the no journal first version.
+            logger.log(Level.WARNING, "Failed to parse mongodb version from: '" +
+                firstLine + "'. Assuming version " + NO_JOURNAL_FIRST_VERSION, e);
+        }
         return versionString;
     }
 
--- a/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunnerTest.java	Tue Aug 27 11:50:35 2013 -0400
+++ b/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunnerTest.java	Mon Aug 26 18:33:02 2013 +0200
@@ -7,6 +7,7 @@
 import static org.mockito.Mockito.when;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.util.List;
@@ -123,6 +124,33 @@
         Integer pid = runner.doGetPid(reader);
         assertNull(pid);
     }
+    
+    @Test
+    public void canGetVersionFromVersionCmdOutputV22() throws IOException {
+        String versionOutput = "db version v2.2.4, pdfile version 4.5\n" +
+                               "Mon Aug 26 17:13:45 git version: nogitversion";
+        ByteArrayInputStream in = new ByteArrayInputStream(versionOutput.getBytes());
+        String version = runner.doGetDBVersion(in);
+        assertEquals("2.2.4", version);
+    }
+    
+    @Test
+    public void canGetVersionFromVersionCmdOutputV24() throws IOException {
+        String versionOutput = "db version v2.4.5\n" +
+                               "Mon Aug 26 18:01:28.404 git version: nogitversion";
+        ByteArrayInputStream in = new ByteArrayInputStream(versionOutput.getBytes());
+        String version = runner.doGetDBVersion(in);
+        assertEquals("2.4.5", version);
+    }
+    
+    @Test
+    public void canProceedIfGetDbVersionThrowsException() throws IOException {
+        String versionOutput = "foo\n" +
+                               "Mon Aug 26 18:01:28.404 git version: nogitversion";
+        ByteArrayInputStream in = new ByteArrayInputStream(versionOutput.getBytes());
+        String version = runner.doGetDBVersion(in);
+        assertEquals(MongoProcessRunner.NO_JOURNAL_FIRST_VERSION, version);
+    }
 
     private void verifyEquals(String[] expected, String[] actual) {
         assertEquals(expected.length, actual.length);