changeset 1746:9fea95e1edd4

Add support for mongodb 3.0 Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/015751.html PR2607
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 03 Sep 2015 16:13:53 +0200
parents 0e4c60a50547
children 2c681fdcfd33
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/ServiceCommand.java agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/ServiceCommandTest.java distribution/lib/create-user.js distribution/pom.xml distribution/scripts/thermostat-setup integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/DevWebStorageTest.java integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/MongoQueriesTest.java integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/StorageTest.java integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/WebAppTest.java
diffstat 11 files changed, 116 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/ServiceCommand.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/ServiceCommand.java	Thu Sep 03 16:13:53 2015 +0200
@@ -86,7 +86,7 @@
         ServiceReference launcherRef = context.getServiceReference(Launcher.class);
         requireNonNull(launcherRef, translator.localize(LocaleResources.LAUNCHER_UNAVAILABLE));
         launcher = (Launcher) context.getService(launcherRef);
-        String[] storageStartArgs = new String[] { "storage", "--start", "--permitLocalhostException"};
+        String[] storageStartArgs = new String[] { "storage", "--start" };
         launcher.run(storageStartArgs, listeners, false);
         agentBarrier.acquireUninterruptibly();
         
--- a/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/ServiceCommandTest.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/ServiceCommandTest.java	Thu Sep 03 16:13:53 2015 +0200
@@ -78,7 +78,7 @@
     private static ActionEvent<ApplicationState> mockActionEvent;
     private static Collection<ActionListener<ApplicationState>> listeners;
 
-    private static final String[] STORAGE_START_ARGS = { "storage", "--start", "--permitLocalhostException" };
+    private static final String[] STORAGE_START_ARGS = { "storage", "--start" };
     private static final String[] STORAGE_STOP_ARGS = { "storage", "--stop" };
     private static final String[] AGENT_ARGS = {"agent", "-d", "Test String"};
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/distribution/lib/create-user.js	Thu Sep 03 16:13:53 2015 +0200
@@ -0,0 +1,22 @@
+db = db.getSiblingDB("thermostat")
+var v = db.version()
+var majorVersion = v.substring(0, v.indexOf('.'))
+var minorMicro = v.substr(v.indexOf('.') + 1)
+var minorVersion = minorMicro.substr(0, minorMicro.indexOf('.'))
+if ( majorVersion < 2 || ( majorVersion == 2 && minorVersion <= 2 ) ) {
+    // mongodb version 2.2 and below don't have the third argument.
+    // this should create the user as read + write.
+    db.addUser("$USERNAME","$PASSWORD")
+} else if ( majorVersion == 2 && minorVersion <= 4 ) {
+    db.addUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] })
+} else if ( majorVersion == 2 ) {
+    db.createUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] })
+} else if ( majorVersion == 3 ) {
+    db = db.getSiblingDB("admin")
+    db.createUser({ user: "thermostat-admin", pwd: "$PASSWORD", roles: [ "dbOwner", "userAdminAnyDatabase" ] })
+    db.auth("thermostat-admin", "$PASSWORD")
+    db = db.getSiblingDB("thermostat")
+    db.createUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] })
+} else {
+    throw "Unknown mongo version: " + v
+}
--- a/distribution/pom.xml	Thu Sep 03 12:12:17 2015 -0400
+++ b/distribution/pom.xml	Thu Sep 03 16:13:53 2015 +0200
@@ -161,6 +161,14 @@
                   <filtering>true</filtering>
                 </resource>
                 <resource>
+                  <directory>lib</directory>
+                  <targetPath>image/lib</targetPath>
+                  <filtering>true</filtering>
+                  <includes>
+                    <include>create-user.js</include>
+                  </includes>
+                </resource>
+                <resource>
                   <directory>config</directory>
                   <targetPath>image/etc</targetPath>
                   <filtering>true</filtering>
--- a/distribution/scripts/thermostat-setup	Thu Sep 03 12:12:17 2015 -0400
+++ b/distribution/scripts/thermostat-setup	Thu Sep 03 16:13:53 2015 +0200
@@ -277,25 +277,13 @@
     exitFail
   fi
   sleep 3
-  mongo 127.0.0.1:27518 << EOF
-use thermostat
-var v = db.version();
-var minorMicro = v.substr(v.indexOf('.') + 1)
-var minorVersion = minorMicro.substr(0, minorMicro.indexOf('.'))
-if ( minorVersion <= 2 ) {
-    // mongodb version 2.2 and below don't have the third argument.
-    // this should create the user as read + write.
-    db.addUser("$USERNAME","$PASSWORD")
-} else {
-  if ( minorVersion <= 4 ) {
-    db.addUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] })
-  } else {
-    db.createUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] })
-  }
-}
-quit()
-EOF
+  mkdir -p $USER_THERMOSTAT_HOME
+  touch $USER_THERMOSTAT_HOME/creds.js
+  chmod u=rw $USER_THERMOSTAT_HOME/creds.js
+  sed -e s/'\$USERNAME'/"$USERNAME"/g -e s/'\$PASSWORD'/"$PASSWORD"/g $THERMOSTAT_HOME/lib/create-user.js > $USER_THERMOSTAT_HOME/creds.js
+  mongo 127.0.0.1:27518/thermostat $USER_THERMOSTAT_HOME/creds.js
   MONGO_SETUP_RETVAL="$?"
+  rm $USER_THERMOSTAT_HOME/creds.js
   if [ "$MONGO_SETUP_RETVAL" -ne 0 ] ; then
     echo -e "\nMongodb user setup failed." 1>&2
     exitFail
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/DevWebStorageTest.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/DevWebStorageTest.java	Thu Sep 03 16:13:53 2015 +0200
@@ -71,6 +71,7 @@
         restoreBackedUpCredentialsFiles();
         removeSetupCompleteStampFiles();
         removeAgentAuthFile();
+        clearStorageDataDirectory();
     }
     
     private static void removeAgentAuthFile() throws IOException {
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java	Thu Sep 03 16:13:53 2015 +0200
@@ -42,12 +42,16 @@
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
+import java.nio.file.StandardOpenOption;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 
 import com.redhat.thermostat.common.utils.StreamUtils;
@@ -115,11 +119,12 @@
 
     private static final String THERMOSTAT_SCRIPT = "thermostat";
     
-    private static void createUserThermostatHomeAndEtc() {
+    private static File createUserThermostatHomeAndEtc() {
         File userThHome = new File(getUserThermostatHome());
         userThHome.mkdir();
         File etcThHome = new File(userThHome, "etc");
         etcThHome.mkdir();
+        return etcThHome;
     }
     
     /**
@@ -198,6 +203,20 @@
         return testProperties;
     }
 
+    static protected void createAgentAuthFile(String userName, String password) throws IOException {
+        File etcHome = createUserThermostatHomeAndEtc();
+
+        List<String> lines = new ArrayList<>();
+        lines.add("username=" + userName);
+        lines.add("password=" + password);
+        Files.write(new File(etcHome, "agent.auth").toPath(), lines, StandardCharsets.UTF_8,
+                StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
+    }
+
+    protected void deleteAgentAuthFile() throws IOException {
+        Files.delete(new File(new File(getUserThermostatHome(), "etc"), "agent.auth").toPath());
+    }
+
     /* This is a mirror of paths from c.r.t.shared.Configuration */
 
     public static String getThermostatHome() {
@@ -255,12 +274,44 @@
         }
     }
 
+    /** pre-conditions:
+     *    storage must *NOT* be running.
+     *    No users set up
+     */
+    public static void addUserToStorage(String username, String password) throws Exception {
+        startStorage("--permitLocalhostException");
+
+        TimeUnit.SECONDS.sleep(3);
+
+        try {
+            ExpectJ mongo = new ExpectJ(TIMEOUT_IN_SECONDS);
+            Spawn mongoSpawn = mongo.spawn("mongo 127.0.0.1:27518/thermostat");
+            File createUser = new File(new File(getThermostatHome(), "lib"), "create-user.js");
+            String contents = new String(Files.readAllBytes(createUser.toPath()), StandardCharsets.UTF_8);
+            contents = contents.replaceAll("\\$USERNAME", username);
+            contents = contents.replaceAll("\\$PASSWORD", password);
+            mongoSpawn.send(contents);
+            mongoSpawn.send("quit();\n");
+            mongoSpawn.expectClose();
+
+            TimeUnit.SECONDS.sleep(3);
+
+        } finally {
+            stopStorage();
+        }
+    }
+
     public static Spawn spawnThermostat(String... args) throws IOException {
         return spawnThermostat(false, args);
     }
     
-    public static Spawn startStorage() throws Exception {
-        Spawn storage = spawnThermostat("storage", "--start", "--permitLocalhostException");
+    public static Spawn startStorage(String... extraArgs) throws Exception {
+        List<String> args = new ArrayList<>(Arrays.asList(new String[] { "storage", "--start", }));
+        if (extraArgs != null) {
+            args.addAll(Arrays.asList(extraArgs));
+        }
+
+        Spawn storage = spawnThermostat(args.toArray(new String[0]));
         try {
             storage.expect("pid:");
         } catch (IOException e) {
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/MongoQueriesTest.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/MongoQueriesTest.java	Thu Sep 03 16:13:53 2015 +0200
@@ -96,6 +96,9 @@
  */
 public class MongoQueriesTest extends IntegrationTest {
 
+    static final String USERNAME = "foobar";
+    static final String PASSWORD = "baz";
+
     private static class CountdownConnectionListener implements ConnectionListener {
 
         private final ConnectionStatus target;
@@ -122,8 +125,10 @@
 
     @BeforeClass
     public static void setUpOnce() throws Exception {
+        clearStorageDataDirectory();
         createFakeSetupCompleteFile();
-        clearStorageDataDirectory();
+        addUserToStorage(USERNAME, PASSWORD);
+
         startStorage();
 
         addCpuData(4);
@@ -135,6 +140,7 @@
 
         stopStorage();
         removeSetupCompleteStampFiles();
+        clearStorageDataDirectory();
     }
 
     /*
@@ -144,17 +150,15 @@
     private static BackingStorage getAndConnectStorage(ConnectionListener listener) {
         final String url = "mongodb://127.0.0.1:27518";
         StorageCredentials creds = new StorageCredentials() {
-
             @Override
             public String getUsername() {
-                return null;
+                return USERNAME;
             }
 
             @Override
             public char[] getPassword() {
-                return null;
+                return PASSWORD.toCharArray();
             }
-            
         };
         CommonPaths paths = mock(CommonPaths.class);
         File tmpFile = new File("/tmp");
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/StorageTest.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/StorageTest.java	Thu Sep 03 16:13:53 2015 +0200
@@ -81,6 +81,10 @@
 
     @Test
     public void testServiceStartAndKilling() throws Exception {
+        clearStorageDataDirectory();
+        addUserToStorage("foo", "bar");
+        createAgentAuthFile("foo", "bar");
+
         Map<String, String> testProperties = getVerboseModeProperties();
         SpawnResult spawnResult = spawnThermostatWithPropertiesSetAndGetProcess(testProperties, "service");
         Spawn service = spawnResult.spawn;
@@ -101,6 +105,7 @@
         service.stop();
         service.expectClose();
 
+        deleteAgentAuthFile();
     }
 
 }
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java	Thu Sep 03 16:13:53 2015 +0200
@@ -51,10 +51,16 @@
 /** Integration tests for the various vm commands */
 public class VmCommandsTest extends IntegrationTest {
 
+    private static final String USERNAME = "foo";
+    private static final String PASSWORD = "bar";
+
     @BeforeClass
     public static void setUpOnce() throws Exception {
         createFakeSetupCompleteFile();
         clearStorageDataDirectory();
+        addUserToStorage(USERNAME, PASSWORD);
+        createAgentAuthFile(USERNAME, PASSWORD);
+
         startStorage();
 
         // TODO insert actual data into the database and test that
@@ -152,7 +158,7 @@
 
         shell.expect(SHELL_DISCONNECT_PROMPT);
         shell.send("list-vms -d " + storageURL + "\n");
-        handleAuthPrompt(shell, storageURL, "", "");
+        handleAuthPrompt(shell, storageURL, USERNAME, PASSWORD);
 
         shell.expect(SHELL_CONNECT_PROMPT);
 
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/WebAppTest.java	Thu Sep 03 12:12:17 2015 -0400
+++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/WebAppTest.java	Thu Sep 03 16:13:53 2015 +0200
@@ -259,7 +259,7 @@
         createFakeSetupCompleteFile();
         createFakeUserSetupDoneFile();
 
-        setupMongodbUser();
+        addUserToStorage(getMongodbUsername(), getMongodbPassword());
 
         startStorage();
 
@@ -272,7 +272,6 @@
         mongoSpawn.send("db[\"fake\"].insert({foo:\"bar\", baz: 1})\n");
         mongoSpawn.send("db[\"fake\"].findOne()\n");
         mongoSpawn.send("show collections\n");
-        mongoSpawn.send("show users\n");
 
         createWebAuthFile();
 
@@ -315,6 +314,7 @@
                     removeSetupCompleteStampFiles();
                     restoreBackedUpCredentialsFiles();
                     System.out.println("RESTORED backed-up files!");
+                    clearStorageDataDirectory();
                 }
             }
         }
@@ -335,65 +335,6 @@
         }
     }
 
-    // PRE: storage started with --permitLocalhostException
-    private static void setupMongodbUser() throws Exception {
-        String mongodbUsername = getMongodbUsername();
-        String mongodbPassword = getMongodbPassword();
-
-        final String HOST = "127.0.0.1";
-        final String PORT = "27518";
-
-        try {
-            System.out.println("THERMOSTAT_HOME: " + getThermostatHome());
-            System.out.println("USER_THERMOSTAT_HOME: " + getUserThermostatHome());
-
-            // start mongod
-            startStorage();
-
-            System.out.println("Started mongod");
-            TimeUnit.SECONDS.sleep(3);
-
-            ExpectJ mongo = new ExpectJ(TIMEOUT_IN_SECONDS);
-            Spawn mongoSpawn = mongo.spawn("mongo " + HOST + ":" + PORT);
-            mongoSpawn.send("use thermostat\n");
-            mongoSpawn.send("var v = db.version()\n");
-            mongoSpawn.send("var minorMicro = v.substr(v.indexOf('.') + 1)\n");
-            mongoSpawn.send("var minorVersion = minorMicro.substr(0, minorMicro.indexOf('.'))\n");
-            mongoSpawn.send("if ( minorVersion <= 2 ) {");
-            mongoSpawn.send(String.format("db.addUser(\"%s\", \"%s\")", mongodbUsername, mongodbPassword));
-            mongoSpawn.send("} else {");
-            mongoSpawn.send("if ( minorVersion <= 4 ) {");
-            mongoSpawn.send(String.format("db.addUser({ user: \"%s\", pwd: \"%s\", roles: [ \"readWrite\" ] })",
-                    mongodbUsername, mongodbPassword));
-            mongoSpawn.send("} else {");
-            mongoSpawn.send(String.format("db.createUser({ user: \"%s\", pwd: \"%s\", roles: [ \"readWrite\" ] })",
-                    mongodbUsername, mongodbPassword));
-            mongoSpawn.send("}\n");
-            mongoSpawn.send("}\n");
-            mongoSpawn.send("quit()\n");
-            mongoSpawn.expectClose();
-
-            mongo = new ExpectJ(TIMEOUT_IN_SECONDS);
-            mongoSpawn = mongo.spawn("mongo " + HOST + ":" + PORT);
-            mongoSpawn.send("use thermostat\n");
-            mongoSpawn.expect("switched to db thermostat");
-            mongoSpawn.send(String.format("db.auth(\"%s\", \"%s\")\n", mongodbUsername, mongodbPassword));
-            mongoSpawn.expect("1");
-
-            // now insert some fake data and display some information that
-            // might be useful for post-mortem analysis if this test fails
-            mongoSpawn.send("db[\"fake\"].insert({foo:\"bar\", baz: 1})\n");
-            mongoSpawn.send("db[\"fake\"].findOne()\n");
-            mongoSpawn.send("show collections\n");
-            mongoSpawn.send("show users\n");
-            
-        } catch (TimeoutException | IOException e) {
-            throw e;
-        } finally {
-            stopStorage();
-        }
-    }
-
     private static void createWebAuthFile() throws IOException {
         System.out.println("WRITING auth file: " + getMongodbUsername() + "/" + getMongodbPassword());
         List<String> lines = new ArrayList<String>();