You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2011/06/05 07:45:29 UTC

svn commit: r1131891 - in /incubator/mesos/trunk/src: configurator.cpp tests/test_configurator.cpp

Author: benh
Date: Sun Jun  5 05:45:29 2011
New Revision: 1131891

URL: http://svn.apache.org/viewvc?rev=1131891&view=rev
Log:
Support spaces around equal sign in config file, and generally trim
options in a more expected way. Fixes #68.

Modified:
    incubator/mesos/trunk/src/configurator.cpp
    incubator/mesos/trunk/src/tests/test_configurator.cpp

Modified: incubator/mesos/trunk/src/configurator.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/configurator.cpp?rev=1131891&r1=1131890&r2=1131891&view=diff
==============================================================================
--- incubator/mesos/trunk/src/configurator.cpp (original)
+++ incubator/mesos/trunk/src/configurator.cpp Sun Jun  5 05:45:29 2011
@@ -8,6 +8,7 @@
 #include "configurator.hpp"
 #include "foreach.hpp"
 #include "params.hpp"
+#include "string_utils.hpp"
 
 extern char** environ;   // libc's environment variable list; for some reason,
                          // this is not in headers on all platforms
@@ -151,23 +152,36 @@ void Configurator::loadConfigFile(const 
     throw ConfigurationException(message.c_str());
   }
 
-  Params fileParams;
-  string buf, line;
+  string line, originalLine;
 
   while (!cfg.eof()) {
     getline(cfg, line);
-    size_t beg = line.find_first_of("#"); // strip comments
-    beg = line.find_last_not_of("#\t \n\r", beg) + 1; // strip trailing ws
-    buf += line.substr(0, beg) + "\n";
-  }
-  cfg.close();
-  fileParams.loadString(buf); // Parse key=value pairs using Params's code
-
-  foreachpair (const string& key, const string& value, fileParams.getMap()) {
+    originalLine = line;
+    // Strip any comment at end of line
+    size_t hash = line.find_first_of("#"); // strip comments
+    if (hash != string::npos) {
+      line = originalLine.substr(0, hash);
+    }
+    // Check for empty line
+    line = StringUtils::trim(line);
+    if (line == "") {
+      continue;
+    }
+    // Split line by = and trim to get key and value
+    vector<string> tokens;
+    StringUtils::split(line, "=", &tokens);
+    if (tokens.size() != 2) {
+      string message = "Malformed line in config file: '" + 
+                       StringUtils::trim(originalLine) + "'";
+      throw ConfigurationException(message.c_str());
+    }
+    string key = StringUtils::trim(tokens[0]);
+    string value = StringUtils::trim(tokens[1]);
     if (overwrite || !params.contains(key)) {
       params[key] = value;
     }
   }
+  cfg.close();
 }
 
 

Modified: incubator/mesos/trunk/src/tests/test_configurator.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/test_configurator.cpp?rev=1131891&r1=1131890&r2=1131891&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/test_configurator.cpp (original)
+++ incubator/mesos/trunk/src/tests/test_configurator.cpp Sun Jun  5 05:45:29 2011
@@ -230,3 +230,51 @@ TEST_WITH_WORKDIR(ConfiguratorTest, Load
   EXPECT_EQ("fromCmdLine", conf.getParams()["c"]);
   EXPECT_EQ("fromFile",    conf.getParams()["d"]);
 }
+
+
+// Check that spaces before and after the = signs in config files are ignored
+TEST_WITH_WORKDIR(ConfiguratorTest, ConfigFileSpacesIgnored)
+{
+  if (mkdir("conf", 0755) != 0)
+    FAIL() << "Failed to create directory conf";
+  ofstream file("conf/mesos.conf");
+  file << "test1=coffee # beans are tasty\n";
+  file << "# just a comment\n";
+  file << "  \t # comment with spaces in front\n";
+  file << "test2 =tea\n";
+  file << "test3=  water\n";
+  file << "   test4 =  milk\n";
+  file << "  test5 =  hot  chocolate\t\n";
+  file << "\ttest6 =  juice# #\n";
+  file.close();
+
+  setenv("MESOS_HOME", ".", 1);
+  Configurator conf;
+  EXPECT_NO_THROW(conf.load());
+  unsetenv("MESOS_HOME");
+
+  EXPECT_EQ("coffee",         conf.getParams()["test1"]);
+  EXPECT_EQ("tea",            conf.getParams()["test2"]);
+  EXPECT_EQ("water",          conf.getParams()["test3"]);
+  EXPECT_EQ("milk",           conf.getParams()["test4"]);
+  EXPECT_EQ("hot  chocolate", conf.getParams()["test5"]);
+  EXPECT_EQ("juice",          conf.getParams()["test6"]);
+}
+
+
+// Check that exceptions are thrown on invalid config file
+TEST_WITH_WORKDIR(ConfiguratorTest, MalformedConfigFile)
+{
+  if (mkdir("conf", 0755) != 0)
+    FAIL() << "Failed to create directory conf";
+  ofstream file("conf/mesos.conf");
+  file << "test1=coffee\n";
+  file << "JUNK\n";
+  file << "test2=tea\n";
+  file.close();
+
+  setenv("MESOS_HOME", ".", 1);
+  Configurator conf;
+  EXPECT_THROW(conf.load(), ConfigurationException);
+  unsetenv("MESOS_HOME");
+}