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 2013/05/29 19:40:47 UTC
[09/35] Replaced flags and configurator in Mesos with flags in stout.
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/29183c1b/src/tests/configurator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/configurator_tests.cpp b/src/tests/configurator_tests.cpp
deleted file mode 100644
index b82f5c2..0000000
--- a/src/tests/configurator_tests.cpp
+++ /dev/null
@@ -1,261 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <gtest/gtest.h>
-
-#include <stout/gtest.hpp>
-#include <stout/os.hpp>
-
-#include "configurator/configurator.hpp"
-
-#include "tests/utils.hpp"
-
-using std::string;
-
-using namespace mesos;
-using namespace mesos::internal;
-using namespace mesos::internal::tests;
-
-
-class ConfiguratorTest : public TemporaryDirectoryTest {};
-
-
-TEST_F(ConfiguratorTest, Environment)
-{
- setenv("MESOS_TEST", "working", true);
- Configurator conf;
- conf.load();
- unsetenv("MESOS_TEST");
-
- EXPECT_EQ("working", conf.getConfiguration()["test"]);
-}
-
-
-TEST_F(ConfiguratorTest, DefaultOptions)
-{
- const int ARGC = 5;
- char* argv[ARGC];
- argv[0] = (char*) "bin/filename";
- argv[1] = (char*) "--test1=501";
- argv[2] = (char*) "--test2";
- argv[3] = (char*) "--excp=txt";
- argv[4] = (char*) "--test8=foo";
-
- Configurator conf;
-
- EXPECT_NO_THROW(conf.addOption<int>("test1", "Testing option", 500));
- EXPECT_NO_THROW(conf.addOption<bool>("test2", "Another tester", 0));
- EXPECT_NO_THROW(conf.addOption<long>("test3", "Tests the default", 2010));
- EXPECT_NO_THROW(conf.addOption<string>("test4", "Option without default"));
- EXPECT_NO_THROW(conf.addOption<string>("test5", "Option with a default",
- "default"));
- EXPECT_NO_THROW(conf.addOption<bool>("test6", "Toggler...", false));
- EXPECT_NO_THROW(conf.addOption<bool>("test7", "Toggler...", true));
- EXPECT_NO_THROW(conf.addOption<string>("test8", "Overridden default",
- "default"));
- EXPECT_NO_THROW(conf.load(ARGC, argv));
-
- EXPECT_NO_THROW(conf.addOption<int>("excp", "Exception tester.", 50));
- EXPECT_THROW(conf.validate(), ConfigurationException);
- conf.getConfiguration()["excp"] = "27";
- EXPECT_NO_THROW(conf.validate());
-
- EXPECT_EQ("501", conf.getConfiguration()["test1"]);
- EXPECT_EQ("1", conf.getConfiguration()["test2"]);
- EXPECT_EQ("2010", conf.getConfiguration()["test3"]);
- EXPECT_EQ("", conf.getConfiguration()["test4"]);
- EXPECT_EQ("default", conf.getConfiguration()["test5"]);
- EXPECT_EQ("27", conf.getConfiguration()["excp"]);
- EXPECT_EQ("0", conf.getConfiguration()["test6"]);
- EXPECT_EQ("1", conf.getConfiguration()["test7"]);
- EXPECT_EQ("foo", conf.getConfiguration()["test8"]);
-}
-
-
-TEST_F(ConfiguratorTest, CommandLine)
-{
- const int ARGC = 12;
- char* argv[ARGC];
- argv[0] = (char*) "bin/filename";
- argv[1] = (char*) "--test1=text1";
- argv[2] = (char*) "--test2";
- argv[3] = (char*) "text2";
- argv[4] = (char*) "-N";
- argv[5] = (char*) "-25";
- argv[6] = (char*) "--cAsE=4";
- argv[7] = (char*) "--space=Long String";
- argv[8] = (char*) "--bool1";
- argv[9] = (char*) "--no-bool2";
- argv[10] = (char*) "-a";
- argv[11] = (char*) "-no-b";
-
- Configurator conf;
- EXPECT_NO_THROW(conf.addOption<int>("negative", 'N', "some val", -30));
- EXPECT_NO_THROW(conf.addOption<string>("test1", "textual value", "text2"));
- EXPECT_NO_THROW(conf.addOption<bool>("bool1", "toggler", false));
- EXPECT_NO_THROW(conf.addOption<bool>("bool2", 'z', "toggler", true));
- EXPECT_NO_THROW(conf.addOption<bool>("bool3", 'a', "toggler", false));
- EXPECT_NO_THROW(conf.addOption<bool>("bool4", 'b', "toggler", true));
-
- EXPECT_NO_THROW( conf.load(ARGC, argv) );
-
- EXPECT_EQ("text1", conf.getConfiguration()["test1"]);
- EXPECT_EQ("1", conf.getConfiguration()["test2"]);
- EXPECT_EQ("-25", conf.getConfiguration()["negative"]);
- EXPECT_EQ("4", conf.getConfiguration()["case"]);
- EXPECT_EQ("Long String", conf.getConfiguration()["space"]);
- EXPECT_EQ("1", conf.getConfiguration()["bool1"]);
- EXPECT_EQ("0", conf.getConfiguration()["bool2"]);
- EXPECT_EQ("1", conf.getConfiguration()["bool3"]);
- EXPECT_EQ("0", conf.getConfiguration()["bool4"]);
-}
-
-
-// Check whether specifying just MESOS_CONF allows a config file to be loaded
-TEST_F(ConfiguratorTest, ConfigFileWithConfDir)
-{
- ASSERT_SOME(os::mkdir("conf2"));
- ASSERT_SOME(os::write("conf2/mesos.conf",
- "test3=shake # sugar bomb\n"
- "# just a comment\n"
- "test4=milk\n"));
-
- setenv("MESOS_CONF", "conf2", 1);
- Configurator conf;
- EXPECT_NO_THROW( conf.load() );
- unsetenv("MESOS_CONF");
-
- EXPECT_EQ("shake", conf.getConfiguration()["test3"]);
- EXPECT_EQ("milk", conf.getConfiguration()["test4"]);
-}
-
-
-// Check that when we specify a conf directory on the command line,
-// we load values from the config file first and then the command line
-TEST_F(ConfiguratorTest, CommandLineConfFlag)
-{
- ASSERT_SOME(os::mkdir("bin"));
- ASSERT_SOME(os::mkdir("conf2"));
- ASSERT_SOME(os::write("conf2/mesos.conf",
- "a=1\n"
- "b=2\n"
- "c=3"));
-
- const int ARGC = 4;
- char* argv[ARGC];
- argv[0] = (char*) "bin/filename";
- argv[1] = (char*) "--conf=conf2";
- argv[2] = (char*) "--b=overridden";
- argv[3] = (char*) "--d=fromCmdLine";
-
- Configurator conf;
- EXPECT_NO_THROW( conf.load(ARGC, argv) );
-
- EXPECT_EQ("1", conf.getConfiguration()["a"]);
- EXPECT_EQ("overridden", conf.getConfiguration()["b"]);
- EXPECT_EQ("3", conf.getConfiguration()["c"]);
- EXPECT_EQ("fromCmdLine", conf.getConfiguration()["d"]);
-}
-
-
-// Check that variables are loaded with the correct priority when an
-// environment variable, a config file element , and a config flag
-// are all present. Command line flags should have the highest priority,
-// second should be environment variables, and last should be the file.
-TEST_F(ConfiguratorTest, LoadingPriorities)
-{
- ASSERT_SOME(os::mkdir("bin"));
- ASSERT_SOME(os::mkdir("conf"));
- ASSERT_SOME(os::write("conf/mesos.conf",
- "a=fromFile\n"
- "b=fromFile\n"
- "c=fromFile\n"
- "d=fromFile\n"));
-
- // Set environment to contain parameters a and b
- setenv("MESOS_A", "fromEnv", 1);
- setenv("MESOS_B", "fromEnv", 1);
- setenv("MESOS_CONF", "conf", 1);
-
- // Pass parameters a and c from the command line
- const int ARGC = 3;
- char* argv[ARGC];
- argv[0] = (char*) "bin/filename";
- argv[1] = (char*) "--a=fromCmdLine";
- argv[2] = (char*) "--c=fromCmdLine";
-
- Configurator conf;
- EXPECT_NO_THROW(conf.load(ARGC, argv));
-
- // Clear the environment vars set above
- unsetenv("MESOS_A");
- unsetenv("MESOS_B");
- unsetenv("MESOS_CONF");
-
- // Check that every variable is obtained from the highest-priority location
- // (command line > env > file)
- EXPECT_EQ("fromCmdLine", conf.getConfiguration()["a"]);
- EXPECT_EQ("fromEnv", conf.getConfiguration()["b"]);
- EXPECT_EQ("fromCmdLine", conf.getConfiguration()["c"]);
- EXPECT_EQ("fromFile", conf.getConfiguration()["d"]);
-}
-
-
-// Check that spaces before and after the = signs in config files are ignored
-TEST_F(ConfiguratorTest, ConfigFileSpacesIgnored)
-{
- ASSERT_SOME(os::mkdir("conf"));
- ASSERT_SOME(os::write("conf/mesos.conf",
- "test1=coffee # beans are tasty\n"
- "# just a comment\n"
- " \t # comment with spaces in front\n"
- "\n"
- "test2 =tea\n"
- "test3= water\n"
- " test4 = milk\n"
- " test5 = hot chocolate\t\n"
- "\ttest6 = juice# #\n"));
-
- Configurator conf;
- setenv("MESOS_CONF", "conf", 1);
- EXPECT_NO_THROW(conf.load());
- unsetenv("MESOS_CONF");
-
- EXPECT_EQ("coffee", conf.getConfiguration()["test1"]);
- EXPECT_EQ("tea", conf.getConfiguration()["test2"]);
- EXPECT_EQ("water", conf.getConfiguration()["test3"]);
- EXPECT_EQ("milk", conf.getConfiguration()["test4"]);
- EXPECT_EQ("hot chocolate", conf.getConfiguration()["test5"]);
- EXPECT_EQ("juice", conf.getConfiguration()["test6"]);
-}
-
-
-// Check that exceptions are thrown on invalid config file
-TEST_F(ConfiguratorTest, MalformedConfigFile)
-{
- ASSERT_SOME(os::mkdir("conf"));
- ASSERT_SOME(os::write("conf/mesos.conf",
- "test1=coffee\n"
- "JUNK\n"
- "test2=tea\n"));
-
- setenv("MESOS_CONF", "conf", 1);
- Configurator conf;
- EXPECT_THROW(conf.load(), ConfigurationException);
- unsetenv("MESOS_CONF");
-}
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/29183c1b/src/tests/environment.cpp
----------------------------------------------------------------------
diff --git a/src/tests/environment.cpp b/src/tests/environment.cpp
index d8bb5fd..125c306 100644
--- a/src/tests/environment.cpp
+++ b/src/tests/environment.cpp
@@ -29,8 +29,6 @@
#include <stout/os.hpp>
#include <stout/strings.hpp>
-#include "configurator/configurator.hpp"
-
#ifdef __linux__
#include "linux/cgroups.hpp"
#endif
@@ -191,7 +189,18 @@ Environment::~Environment()
void Environment::SetUp()
{
// Clear any MESOS_ environment variables so they don't affect our tests.
- Configurator::clearMesosEnvironmentVars();
+ char** environ = os::environ();
+ for (int i = 0; environ[i] != NULL; i++) {
+ std::string variable = environ[i];
+ if (variable.find("MESOS_") == 0) {
+ string key;
+ size_t eq = variable.find_first_of("=");
+ if (eq == string::npos) {
+ continue; // Not expecting a missing '=', but ignore anyway.
+ }
+ os::unsetenv(variable.substr(strlen("MESOS_"), eq - strlen("MESOS_")));
+ }
+ }
if (!GTEST_IS_THREADSAFE) {
EXIT(1) << "Testing environment is not thread safe, bailing!";
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/29183c1b/src/tests/flags.hpp
----------------------------------------------------------------------
diff --git a/src/tests/flags.hpp b/src/tests/flags.hpp
index 6ba5eca..b229e1f 100644
--- a/src/tests/flags.hpp
+++ b/src/tests/flags.hpp
@@ -21,10 +21,9 @@
#include <string>
+#include <stout/flags.hpp>
#include <stout/os.hpp>
-#include "flags/flags.hpp"
-
#include "logging/logging.hpp"
namespace mesos {
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/29183c1b/src/tests/flags_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/flags_tests.cpp b/src/tests/flags_tests.cpp
deleted file mode 100644
index d52d4db..0000000
--- a/src/tests/flags_tests.cpp
+++ /dev/null
@@ -1,223 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-#include <gmock/gmock.h>
-
-#include <map>
-#include <string>
-
-#include <stout/duration.hpp>
-#include <stout/gtest.hpp>
-#include <stout/none.hpp>
-#include <stout/option.hpp>
-
-#include "configurator/configuration.hpp"
-#include "configurator/configurator.hpp"
-
-#include "flags/flags.hpp"
-
-using namespace flags;
-
-class TestFlags : public virtual FlagsBase
-{
-public:
- TestFlags()
- {
- add(&TestFlags::name1,
- "name1",
- "Set name1",
- "ben folds");
-
- add(&TestFlags::name2,
- "name2",
- "Set name2",
- 42);
-
- add(&TestFlags::name3,
- "name3",
- "Set name3",
- false);
-
- add(&TestFlags::name4,
- "name4",
- "Set name4");
-
- add(&TestFlags::name5,
- "name5",
- "Set name5");
- }
-
- std::string name1;
- int name2;
- bool name3;
- Option<bool> name4;
- Option<bool> name5;
-};
-
-
-TEST(FlagsTest, Load)
-{
- TestFlags flags;
-
- std::map<std::string, Option<std::string> > values;
-
- values["name1"] = Option<std::string>::some("billy joel");
- values["name2"] = Option<std::string>::some("43");
- values["name3"] = Option<std::string>::some("false");
- values["no-name4"] = None();
- values["name5"] = None();
-
- flags.load(values);
-
- EXPECT_EQ("billy joel", flags.name1);
- EXPECT_EQ(43, flags.name2);
- EXPECT_FALSE(flags.name3);
- ASSERT_SOME(flags.name4);
- EXPECT_FALSE(flags.name4.get());
- ASSERT_SOME(flags.name5);
- EXPECT_TRUE(flags.name5.get());
-}
-
-
-TEST(FlagsTest, Add)
-{
- Flags<TestFlags> flags;
-
- Option<std::string> name6;
-
- flags.add(&name6,
- "name6",
- "Also set name6");
-
- bool name7;
-
- flags.add(&name7,
- "name7",
- "Also set name7",
- true);
-
- Option<std::string> name8;
-
- flags.add(&name8,
- "name8",
- "Also set name8");
-
- std::map<std::string, Option<std::string> > values;
-
- values["name6"] = Option<std::string>::some("ben folds");
- values["no-name7"] = None();
-
- flags.load(values);
-
- ASSERT_SOME(name6);
- EXPECT_EQ("ben folds", name6.get());
-
- EXPECT_FALSE(name7);
-
- ASSERT_TRUE(name8.isNone());
-}
-
-
-TEST(FlagsTest, Flags)
-{
- Flags<TestFlags> flags;
-
- std::map<std::string, Option<std::string> > values;
-
- values["name1"] = Option<std::string>::some("billy joel");
- values["name2"] = Option<std::string>::some("43");
- values["name3"] = Option<std::string>::some("false");
- values["no-name4"] = None();
- values["name5"] = None();
-
- flags.load(values);
-
- EXPECT_EQ("billy joel", flags.name1);
- EXPECT_EQ(43, flags.name2);
- EXPECT_FALSE(flags.name3);
- ASSERT_SOME(flags.name4);
- EXPECT_FALSE(flags.name4.get());
- ASSERT_SOME(flags.name5);
- EXPECT_TRUE(flags.name5.get());
-}
-
-
-TEST(FlagsTest, Configurator)
-{
- Flags<TestFlags> flags;
-
- int argc = 6;
- char* argv[argc];
-
- argv[0] = (char*) "/path/to/program";
- argv[1] = (char*) "--name1=billy joel";
- argv[2] = (char*) "--name2=43";
- argv[3] = (char*) "--no-name3";
- argv[4] = (char*) "--no-name4";
- argv[5] = (char*) "--name5";
-
- mesos::internal::Configurator configurator(flags);
- mesos::internal::Configuration configuration;
- try {
- configuration = configurator.load(argc, argv);
- } catch (mesos::internal::ConfigurationException& e) {
- FAIL() << "Configuration error: " << e.what();
- }
-
- flags.load(configuration.getMap());
-
- EXPECT_EQ("billy joel", flags.name1);
- EXPECT_EQ(43, flags.name2);
- EXPECT_FALSE(flags.name3);
- ASSERT_SOME(flags.name4);
- EXPECT_FALSE(flags.name4.get());
- ASSERT_SOME(flags.name5);
- EXPECT_TRUE(flags.name5.get());
-}
-
-
-TEST(FlagsTest, Duration)
-{
- Flags<TestFlags> flags;
-
- Duration name6;
-
- flags.add(&name6,
- "name6",
- "Amount of time",
- Milliseconds(100));
-
- Option<Duration> name7;
-
- flags.add(&name7,
- "name7",
- "Also some amount of time");
-
- std::map<std::string, Option<std::string> > values;
-
- values["name6"] = Option<std::string>::some("2mins");
- values["name7"] = Option<std::string>::some("3hrs");
-
- flags.load(values);
-
- EXPECT_EQ(Minutes(2), name6);
-
- ASSERT_SOME(name7);
- EXPECT_EQ(Hours(3), name7.get());
-}
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/29183c1b/src/tests/main.cpp
----------------------------------------------------------------------
diff --git a/src/tests/main.cpp b/src/tests/main.cpp
index 868bdd5..ba9bf64 100644
--- a/src/tests/main.cpp
+++ b/src/tests/main.cpp
@@ -22,10 +22,10 @@
#include <process/process.hpp>
+#include <stout/flags.hpp>
+#include <stout/nothing.hpp>
#include <stout/os.hpp>
-
-#include "configurator/configuration.hpp"
-#include "configurator/configurator.hpp"
+#include <stout/try.hpp>
#include "logging/logging.hpp"
@@ -42,12 +42,12 @@ using std::endl;
using std::string;
-void usage(const char* argv0, const Configurator& configurator)
+void usage(const char* argv0, const flags::FlagsBase& flags)
{
cerr << "Usage: " << os::basename(argv0).get() << " [...]" << endl
<< endl
<< "Supported options:" << endl
- << configurator.getUsage();
+ << flags.usage();
}
@@ -63,20 +63,18 @@ int main(int argc, char** argv)
"Prints this help message",
false);
- Configurator configurator(flags);
- Configuration configuration;
- try {
- configuration = configurator.load(argc, argv);
- } catch (ConfigurationException& e) {
- cerr << "Configuration error: " << e.what() << endl;
- usage(argv[0], configurator);
+ // Load flags from environment and command line but allow unknown
+ // flags (since we might have gtest/gmock flags as well).
+ Try<Nothing> load = flags.load("MESOS_", argc, argv, true);
+
+ if (load.isError()) {
+ cerr << load.error() << endl;
+ usage(argv[0], flags);
exit(1);
}
- flags.load(configuration.getMap());
-
if (help) {
- usage(argv[0], configurator);
+ usage(argv[0], flags);
cerr << endl;
testing::InitGoogleTest(&argc, argv); // Get usage from gtest too.
exit(1);