You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2016/08/25 21:57:57 UTC

[04/11] mesos git commit: Refactored the test helper to use a single binary.

Refactored the test helper to use a single binary.

Review: https://reviews.apache.org/r/51195/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f5f91990
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f5f91990
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f5f91990

Branch: refs/heads/master
Commit: f5f919907a0b89ad69ef351fe3958760286c266b
Parents: 3ec6080
Author: Jie Yu <yu...@gmail.com>
Authored: Mon Aug 22 13:07:41 2016 +0200
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Aug 25 14:57:50 2016 -0700

----------------------------------------------------------------------
 src/Makefile.am                           | 12 ++++---
 src/tests/CMakeLists.txt                  |  9 ++---
 src/tests/active_user_test_helper.cpp     | 41 +++++++++++++++-------
 src/tests/active_user_test_helper.hpp     | 48 ++++++++++++++++++++++++++
 src/tests/cmake/MesosTestsConfigure.cmake |  4 +--
 src/tests/slave_tests.cpp                 | 14 +++++---
 src/tests/test_helper_main.cpp            | 31 +++++++++++++++++
 7 files changed, 131 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/f5f91990/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 69e5655..11bd72a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -980,6 +980,7 @@ libmesos_no_3rdparty_la_SOURCES +=					\
   slave/containerizer/mesos/provisioner/docker/store.hpp		\
   slave/qos_controllers/noop.hpp					\
   slave/resource_estimators/noop.hpp					\
+  tests/active_user_test_helper.hpp					\
   tests/allocator.hpp							\
   tests/cluster.hpp							\
   tests/containerizer.hpp						\
@@ -1923,10 +1924,12 @@ memory_test_helper_SOURCES =					\
 memory_test_helper_CPPFLAGS = $(mesos_tests_CPPFLAGS)
 memory_test_helper_LDADD = $(mesos_tests_LDADD)
 
-check_PROGRAMS += active-user-test-helper
-active_user_test_helper_SOURCES = tests/active_user_test_helper.cpp
-active_user_test_helper_CPPFLAGS = $(MESOS_CPPFLAGS)
-active_user_test_helper_LDADD = libmesos.la $(LDADD)
+check_PROGRAMS += test-helper
+test_helper_SOURCES =						\
+  tests/active_user_test_helper.cpp				\
+  tests/test_helper_main.cpp
+test_helper_CPPFLAGS = $(MESOS_CPPFLAGS)
+test_helper_LDADD = libmesos.la $(LDADD)
 
 check_PROGRAMS += mesos-tests
 
@@ -2059,6 +2062,7 @@ endif
 
 mesos_tests_SOURCES =						\
   slave/qos_controllers/load.cpp				\
+  tests/active_user_test_helper.cpp				\
   tests/anonymous_tests.cpp					\
   tests/api_tests.cpp						\
   tests/attributes_tests.cpp					\

http://git-wip-us.apache.org/repos/asf/mesos/blob/f5f91990/src/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 1ea8b21..9df2804 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -17,9 +17,10 @@
 include(MesosTestsConfigure)
 
 ######################
-set(ACTIVE_USER_TEST_HELPER_SRC
-  ${ACTIVE_USER_TEST_HELPER_SRC}
+set(TEST_HELPER_SRC
+  ${TEST_HELPER_SRC}
   active_user_test_helper.cpp
+  test_helper_main.cpp
   )
 
 add_subdirectory(containerizer/)
@@ -28,8 +29,8 @@ add_subdirectory(containerizer/)
 
 # THE TEST AND HELPER EXECUTABLEs (generates, e.g., stout_tests, etc., on Linux).
 #################################$###############################################
-add_executable(${ACTIVE_USER_TEST_HELPER_TARGET} ${ACTIVE_USER_TEST_HELPER_SRC})
+add_executable(${TEST_HELPER_TARGET} ${TEST_HELPER_SRC})
 
 # ADD BINARY DEPENDENCIES (tells CMake what to compile/build first).
 ####################################################################
-add_dependencies(${ACTIVE_USER_TEST_HELPER_TARGET} ${STOUT_DEPENDENCIES})
+add_dependencies(${TEST_HELPER_TARGET} ${STOUT_DEPENDENCIES})

http://git-wip-us.apache.org/repos/asf/mesos/blob/f5f91990/src/tests/active_user_test_helper.cpp
----------------------------------------------------------------------
diff --git a/src/tests/active_user_test_helper.cpp b/src/tests/active_user_test_helper.cpp
index 54e8668..80bd0ac 100644
--- a/src/tests/active_user_test_helper.cpp
+++ b/src/tests/active_user_test_helper.cpp
@@ -14,26 +14,41 @@
 // See the License for the specific language governing permissions and
 // limitations under the License
 
-#include <iostream>
 #include <string>
 
 #include <stout/os.hpp>
 
-// This helper program takes an expected user.
-// Returns 0 if the current username equals the expected username.
-// Returns 1 otherwise.
-int main(int argc, char **argv) {
-  if (argc < 2) {
-    std::cerr << "Usage: " << argv[0] << " <expected username>" << std::endl;
-    return 1;
-  }
+#include "tests/active_user_test_helper.hpp"
+
+using std::string;
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+const char ActiveUserTestHelper::NAME[] = "ActiveUser";
 
-  const std::string expected(argv[1]);
 
-  Result<std::string> user = os::user();
-  if (user.isSome() && user.get() == expected) {
-      return 0;
+ActiveUserTestHelper::Flags::Flags()
+{
+  add(&user,
+      "user",
+      "The expected user name.");
+}
+
+
+// This test helper returns 0 if the current username equals the
+// expected username. Returns 1 otherwise.
+int ActiveUserTestHelper::execute()
+{
+  Result<string> user = os::user();
+  if (user.isSome() && user.get() == flags.user) {
+    return 0;
   }
 
   return 1;
 }
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/f5f91990/src/tests/active_user_test_helper.hpp
----------------------------------------------------------------------
diff --git a/src/tests/active_user_test_helper.hpp b/src/tests/active_user_test_helper.hpp
new file mode 100644
index 0000000..b99d1e4
--- /dev/null
+++ b/src/tests/active_user_test_helper.hpp
@@ -0,0 +1,48 @@
+// 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 <string>
+
+#include <stout/subcommand.hpp>
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+class ActiveUserTestHelper : public Subcommand
+{
+public:
+  static const char NAME[];
+
+  struct Flags : public flags::FlagsBase
+  {
+    Flags();
+
+    std::string user;
+  };
+
+  ActiveUserTestHelper() : Subcommand(NAME) {}
+
+  Flags flags;
+
+protected:
+  virtual int execute();
+  virtual flags::FlagsBase* getFlags() { return &flags; }
+};
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/f5f91990/src/tests/cmake/MesosTestsConfigure.cmake
----------------------------------------------------------------------
diff --git a/src/tests/cmake/MesosTestsConfigure.cmake b/src/tests/cmake/MesosTestsConfigure.cmake
index 3610320..8ab4ca3 100644
--- a/src/tests/cmake/MesosTestsConfigure.cmake
+++ b/src/tests/cmake/MesosTestsConfigure.cmake
@@ -20,8 +20,8 @@ set(
   )
 
 set(
-  ACTIVE_USER_TEST_HELPER_TARGET active-user-test-helper
-  CACHE STRING "Test helper target required to run tests with a user."
+  TEST_HELPER_TARGET test-helper
+  CACHE STRING "Test helper target to run tests that require a subprocess."
   )
 
 if (LINUX)

http://git-wip-us.apache.org/repos/asf/mesos/blob/f5f91990/src/tests/slave_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/slave_tests.cpp b/src/tests/slave_tests.cpp
index 84ee37c..701fab2 100644
--- a/src/tests/slave_tests.cpp
+++ b/src/tests/slave_tests.cpp
@@ -60,6 +60,7 @@
 
 #include "slave/containerizer/mesos/containerizer.hpp"
 
+#include "tests/active_user_test_helper.hpp"
 #include "tests/containerizer.hpp"
 #include "tests/environment.hpp"
 #include "tests/flags.hpp"
@@ -991,14 +992,15 @@ TEST_F(SlaveTest, ROOT_RunTaskWithCommandInfoWithoutUser)
   CHECK_SOME(user) << "Failed to get current user name"
                    << (user.isError() ? ": " + user.error() : "");
 
-  const string helper = getTestHelperPath("active-user-test-helper");
+  const string helper = getTestHelperPath("test-helper");
 
   // Command executor will run as user running test.
   CommandInfo command;
   command.set_shell(false);
   command.set_value(helper);
   command.add_arguments(helper);
-  command.add_arguments(user.get());
+  command.add_arguments(ActiveUserTestHelper::NAME);
+  command.add_arguments("--user=" + user.get());
 
   task.mutable_command()->MergeFrom(command);
 
@@ -1071,7 +1073,7 @@ TEST_F(SlaveTest, DISABLED_ROOT_RunTaskWithCommandInfoWithUser)
 
   Future<TaskStatus> statusRunning;
   Future<TaskStatus> statusFinished;
-  const string helper = getTestHelperPath("active-user-test-helper");
+  const string helper = getTestHelperPath("test-helper");
 
   Future<vector<Offer>> offers;
   EXPECT_CALL(sched, resourceOffers(&driver, _))
@@ -1107,7 +1109,8 @@ TEST_F(SlaveTest, DISABLED_ROOT_RunTaskWithCommandInfoWithUser)
   prepareCommand.set_shell(false);
   prepareCommand.set_value(helper);
   prepareCommand.add_arguments(helper);
-  prepareCommand.add_arguments(user.get());
+  prepareCommand.add_arguments(ActiveUserTestHelper::NAME);
+  prepareCommand.add_arguments("--user=" + user.get());
   prepareTask.mutable_command()->CopyFrom(prepareCommand);
 
   EXPECT_CALL(sched, statusUpdate(&driver, _))
@@ -1146,7 +1149,8 @@ TEST_F(SlaveTest, DISABLED_ROOT_RunTaskWithCommandInfoWithUser)
   command.set_shell(false);
   command.set_value(helper);
   command.add_arguments(helper);
-  command.add_arguments(testUser);
+  command.add_arguments(ActiveUserTestHelper::NAME);
+  command.add_arguments("--user=" + testUser);
 
   task.mutable_command()->CopyFrom(command);
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/f5f91990/src/tests/test_helper_main.cpp
----------------------------------------------------------------------
diff --git a/src/tests/test_helper_main.cpp b/src/tests/test_helper_main.cpp
new file mode 100644
index 0000000..120e881
--- /dev/null
+++ b/src/tests/test_helper_main.cpp
@@ -0,0 +1,31 @@
+// 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 <stout/subcommand.hpp>
+
+#include "tests/active_user_test_helper.hpp"
+
+using mesos::internal::tests::ActiveUserTestHelper;
+
+
+int main(int argc, char** argv)
+{
+  return Subcommand::dispatch(
+      None(),
+      argc,
+      argv,
+      new ActiveUserTestHelper());
+}