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:36:39 UTC

svn commit: r1131814 - in /incubator/mesos/trunk: ./ src/ src/swig/java/ src/swig/python/ src/tests/ src/tests/external/ src/tests/external/SampleFrameworks/

Author: benh
Date: Sun Jun  5 05:36:38 2011
New Revision: 1131814

URL: http://svn.apache.org/viewvc?rev=1131814&view=rev
Log:
Added infrastructure for running external tests (shell scripts located
in tests/external). Also created four such tests to run the sample
frameworks (Java, Python, C and C++) and make sure they all exit with
status zero.

Added:
    incubator/mesos/trunk/src/swig/python/test_framework   (with props)
    incubator/mesos/trunk/src/tests/external/
    incubator/mesos/trunk/src/tests/external/SampleFrameworks/
    incubator/mesos/trunk/src/tests/external/SampleFrameworks/CFramework.sh   (with props)
    incubator/mesos/trunk/src/tests/external/SampleFrameworks/CppFramework.sh   (with props)
    incubator/mesos/trunk/src/tests/external/SampleFrameworks/JavaFramework.sh   (with props)
    incubator/mesos/trunk/src/tests/external/SampleFrameworks/PythonFramework.sh   (with props)
    incubator/mesos/trunk/src/tests/external_test.cpp
    incubator/mesos/trunk/src/tests/external_test.hpp
    incubator/mesos/trunk/src/tests/test_sample_frameworks.cpp
    incubator/mesos/trunk/src/tests/test_utils.hpp
Modified:
    incubator/mesos/trunk/.gitignore
    incubator/mesos/trunk/src/Makefile.in
    incubator/mesos/trunk/src/cpp_test_framework.cpp
    incubator/mesos/trunk/src/swig/java/TestFramework.java
    incubator/mesos/trunk/src/test_framework.cpp
    incubator/mesos/trunk/src/tests/main.cpp

Modified: incubator/mesos/trunk/.gitignore
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/.gitignore?rev=1131814&r1=1131813&r2=1131814&view=diff
==============================================================================
--- incubator/mesos/trunk/.gitignore (original)
+++ incubator/mesos/trunk/.gitignore Sun Jun  5 05:36:38 2011
@@ -76,3 +76,4 @@ src/test-framework
 src/test-executor
 src/swig/java/nexus
 src/swig/python/nexus.py
+src/test_output

Modified: incubator/mesos/trunk/src/Makefile.in
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/Makefile.in?rev=1131814&r1=1131813&r2=1131814&view=diff
==============================================================================
--- incubator/mesos/trunk/src/Makefile.in (original)
+++ incubator/mesos/trunk/src/Makefile.in Sun Jun  5 05:36:38 2011
@@ -75,13 +75,23 @@ CXXFLAGS += -DBUILD_DATE="\"$$(date '+%Y
 CFLAGS += -DBUILD_USER="\"$$USER\""
 CXXFLAGS += -DBUILD_USER="\"$$USER\""
 
+# Expose whether we have Java and Python support through defines
+ifdef JAVA_HOME
+  CFLAGS += -DMESOS_HAS_JAVA
+  CXXFLAGS += -DMESOS_HAS_JAVA
+endif
+ifdef PYTHON_HEADERS
+  CFLAGS += -DMESOS_HAS_PYTHON
+  CXXFLAGS += -DMESOS_HAS_PYTHON
+endif
+
 # Add libev to LDFLAGS.
 LDFLAGS += -L$(LIBEV)/.libs
 
 # Add libev, libprocess, pthread, and dl to LIBS.
 LIBS += -lglog -lgtest -lprocess -lev -lpthread -ldl -lzookeeper_st
 
-NEXUS_EXES = nexus-master nexus-slave nexus-local nexus-launcher alltests \
+NEXUS_EXES = nexus-master nexus-slave nexus-local nexus-launcher \
 	     test-framework test-executor cpp-test-framework cpp-test-executor \
 	     memhog memhog-executor
 
@@ -103,7 +113,8 @@ SLAVE_OBJ = slave.o launcher.o isolation
 COMMON_OBJ = fatal.o hash_pid.o messages.o lock.o master_detector.o url_processor.o zookeeper.o
 EXEC_LIB_OBJ = nexus_exec.o
 SCHED_LIB_OBJ = nexus_sched.o nexus_local.o params.o
-TEST_OBJ = tests/main.o tests/test_master.o tests/test_resources.o
+TEST_OBJ = tests/main.o tests/test_master.o tests/test_resources.o \
+	   tests/external_test.o tests/test_sample_frameworks.o
 
 ifeq ($(OS_NAME),solaris)
   SLAVE_OBJ += solaris_project_isolation_module.o
@@ -190,8 +201,10 @@ nexus-projd: projd.cpp $(COMMON_OBJ)
 nexus-local: local.cpp nexus_local.o $(MASTER_OBJ) $(SLAVE_OBJ) $(COMMON_OBJ)
 	$(CXX) $(CXXFLAGS) -o $@ $< nexus_local.o $(MASTER_OBJ) $(SLAVE_OBJ) $(COMMON_OBJ) $(LDFLAGS) $(LIBS)
 
-alltests: $(TEST_OBJ) $(MASTER_OBJ) $(SLAVE_OBJ) $(SCHED_LIB_OBJ) $(EXEC_LIB_OBJ) $(COMMON_OBJ)
-	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
+ALLTESTS_OBJ = $(TEST_OBJ) $(MASTER_OBJ) $(SLAVE_OBJ) $(SCHED_LIB_OBJ) $(EXEC_LIB_OBJ) $(COMMON_OBJ)
+
+alltests: $(ALLTESTS_OBJ) $(NEXUS_EXES) java python
+	$(CXX) $(CXXFLAGS) -o $@ $(ALLTESTS_OBJ) $(LDFLAGS) $(LIBS)
 
 test-framework: test_framework.cpp $(SCHED_LIB) third_party/libprocess/libprocess.a
 	$(CXX) $(CXXFLAGS) -o $@ $< $(LDFLAGS) -L. -lnexus_sched $(LIBS)
@@ -276,7 +289,7 @@ ifeq ($(LOCAL_ZOOKEEPER),yes)
 	$(MAKE) -C $(ZOOKEEPER)
 endif
 
-all: third_party $(NEXUS_LIBS) $(NEXUS_EXES) java python ruby
+all: third_party $(NEXUS_LIBS) $(NEXUS_EXES) alltests java python ruby
 
 clean:
 	$(MAKE) -C third_party/libprocess clean

Modified: incubator/mesos/trunk/src/cpp_test_framework.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/cpp_test_framework.cpp?rev=1131814&r1=1131813&r2=1131814&view=diff
==============================================================================
--- incubator/mesos/trunk/src/cpp_test_framework.cpp (original)
+++ incubator/mesos/trunk/src/cpp_test_framework.cpp Sun Jun  5 05:36:38 2011
@@ -1,5 +1,7 @@
 #include <nexus_sched.hpp>
 
+#include <libgen.h>
+
 #include <cstdlib>
 #include <iostream>
 #include <sstream>
@@ -79,9 +81,11 @@ int main(int argc, char ** argv) {
     cerr << "Usage: " << argv[0] << " <masterPid>" << endl;
     return -1;
   }
-  char cwd[512];
-  getcwd(cwd, sizeof(cwd));
-  string executor = string(cwd) + "/cpp-test-executor";
+  // Find this executable's directory to locate executor
+  char buf[4096];
+  realpath(dirname(argv[0]), buf);
+  string executor = string(buf) + "/test-executor";
+  // Run a Mesos scheduler
   MyScheduler sched(executor);
   NexusSchedulerDriver driver(&sched, argv[1]);
   driver.run();

Modified: incubator/mesos/trunk/src/swig/java/TestFramework.java
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/swig/java/TestFramework.java?rev=1131814&r1=1131813&r2=1131814&view=diff
==============================================================================
--- incubator/mesos/trunk/src/swig/java/TestFramework.java (original)
+++ incubator/mesos/trunk/src/swig/java/TestFramework.java Sun Jun  5 05:36:38 2011
@@ -78,9 +78,5 @@ public class TestFramework {
 
   public static void main(String[] args) throws Exception {
     new NexusSchedulerDriver(new MyScheduler(), args[0]).run();
-    // TODO: Java should just exit here, and it does so on Linux, but
-    // it doesn't on OS X. We should figure out why. It may have to do
-    // with the libprocess threads being around and not being marked
-    // as daemon threads by the JVM.
   }
 }

Added: incubator/mesos/trunk/src/swig/python/test_framework
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/swig/python/test_framework?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/swig/python/test_framework (added)
+++ incubator/mesos/trunk/src/swig/python/test_framework Sun Jun  5 05:36:38 2011
@@ -0,0 +1,4 @@
+#!/bin/sh
+FWDIR=`dirname $0`
+cd $FWDIR
+exec python ./test_framework.py $@

Propchange: incubator/mesos/trunk/src/swig/python/test_framework
------------------------------------------------------------------------------
    svn:executable = *

Modified: incubator/mesos/trunk/src/test_framework.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/test_framework.cpp?rev=1131814&r1=1131813&r2=1131814&view=diff
==============================================================================
--- incubator/mesos/trunk/src/test_framework.cpp (original)
+++ incubator/mesos/trunk/src/test_framework.cpp Sun Jun  5 05:36:38 2011
@@ -1,9 +1,16 @@
 #include <iostream>
+#include <libgen.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <nexus_sched.h>
 
 using namespace std;
 
+const int TOTAL_TASKS = 5;
+int tasksStarted = 0;
+int tasksFinished = 0;
+
+
 void registered(nexus_sched *sched, framework_id fid)
 {
   cout << "Registered with Nexus, framework ID = " << fid << endl;
@@ -14,9 +21,8 @@ void slot_offer(nexus_sched *sched, offe
                 nexus_slot *slots, int num_slots)
 {
   // TODO: Make this loop over offers rather than looking only at first offer!
-  static int tasksStarted = 0;
   cout << "Got slot offer " << oid << endl;
-  if (tasksStarted > 4) {
+  if (tasksStarted >= TOTAL_TASKS) {
     cout << "Refusing it" << endl;
     nexus_sched_reply_to_offer(sched, oid, 0, 0, "timeout=-1");
   } else {
@@ -40,6 +46,11 @@ void slot_offer_rescinded(nexus_sched *s
 void status_update(nexus_sched *sched, nexus_task_status *status)
 {
   cout << "Task " << status->tid << " entered state " << status->state << endl;
+  if (status->state == TASK_FINISHED) {
+    tasksFinished++;
+    if (tasksFinished == TOTAL_TASKS)
+      exit(0);
+  }
 }
 
 
@@ -80,10 +91,10 @@ nexus_sched sched = {
 
 int main(int argc, char **argv)
 {
-  // Get current directory to set executor
-  char cwd[512];
-  getcwd(cwd, sizeof(cwd));
-  string executor = string(cwd) + "/test-executor";
+  // Find this executable's directory to locate executor
+  char buf[4096];
+  realpath(dirname(argv[0]), buf);
+  string executor = string(buf) + "/test-executor";
   sched.executor_name = executor.c_str();
 
   if (nexus_sched_init(&sched) < 0) {

Added: incubator/mesos/trunk/src/tests/external/SampleFrameworks/CFramework.sh
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/external/SampleFrameworks/CFramework.sh?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/external/SampleFrameworks/CFramework.sh (added)
+++ incubator/mesos/trunk/src/tests/external/SampleFrameworks/CFramework.sh Sun Jun  5 05:36:38 2011
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Check that the C test framework executes without crashing (returns 0).
+exec $MESOS_HOME/test-framework local

Propchange: incubator/mesos/trunk/src/tests/external/SampleFrameworks/CFramework.sh
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/mesos/trunk/src/tests/external/SampleFrameworks/CppFramework.sh
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/external/SampleFrameworks/CppFramework.sh?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/external/SampleFrameworks/CppFramework.sh (added)
+++ incubator/mesos/trunk/src/tests/external/SampleFrameworks/CppFramework.sh Sun Jun  5 05:36:38 2011
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Check that the C++ test framework executes without crashing (returns 0).
+exec $MESOS_HOME/cpp-test-framework local

Propchange: incubator/mesos/trunk/src/tests/external/SampleFrameworks/CppFramework.sh
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/mesos/trunk/src/tests/external/SampleFrameworks/JavaFramework.sh
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/external/SampleFrameworks/JavaFramework.sh?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/external/SampleFrameworks/JavaFramework.sh (added)
+++ incubator/mesos/trunk/src/tests/external/SampleFrameworks/JavaFramework.sh Sun Jun  5 05:36:38 2011
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Check that the Java test framework executes without crashing (returns 0).
+exec $MESOS_HOME/swig/java/test_framework local

Propchange: incubator/mesos/trunk/src/tests/external/SampleFrameworks/JavaFramework.sh
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/mesos/trunk/src/tests/external/SampleFrameworks/PythonFramework.sh
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/external/SampleFrameworks/PythonFramework.sh?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/external/SampleFrameworks/PythonFramework.sh (added)
+++ incubator/mesos/trunk/src/tests/external/SampleFrameworks/PythonFramework.sh Sun Jun  5 05:36:38 2011
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Check that the Java test framework executes without crashing (returns 0).
+exec $MESOS_HOME/swig/python/test_framework local

Propchange: incubator/mesos/trunk/src/tests/external/SampleFrameworks/PythonFramework.sh
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/mesos/trunk/src/tests/external_test.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/external_test.cpp?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/external_test.cpp (added)
+++ incubator/mesos/trunk/src/tests/external_test.cpp Sun Jun  5 05:36:38 2011
@@ -0,0 +1,55 @@
+#include <gtest/gtest.h>
+
+#include <stdlib.h>
+
+#include <string>
+
+#include <boost/lexical_cast.hpp>
+
+#include "external_test.hpp"
+#include "fatal.hpp"
+#include "test_utils.hpp"
+
+using std::string;
+
+/**
+ * Run an external test with the given name. The test is expected to be
+ * located in src/tests/external/<testCase>/<testName>.sh.
+ * We execute this script in directory test_output/<testCase>/<testName>,
+ * piping its output to files called stdout and stderr, and the test
+ * passes if the script returns 0.
+ */
+void nexus::test::runExternalTest(const char* testCase, const char* testName)
+{
+  // Figure out the absolute path to the test script
+  string script = MESOS_HOME + "/tests/external/" + testCase
+                             + "/" + testName + ".sh";
+  // Make the work directory for this test
+  string workDir = MESOS_HOME + "/test_output/" + testCase + "/" + testName;
+  string command = "rm -fr '" + workDir + "'";
+  ASSERT_EQ(0, system(command.c_str())) << "Command failed: " << command;
+  command = "mkdir -p '" + workDir + "'";
+  ASSERT_EQ(0, system(command.c_str())) << "Command failed: " << command;
+  // Fork a process to change directory and run the test
+  pid_t pid;
+  if ((pid = fork()) == -1)
+    FAIL() << "Failed to fork to launch external test";
+  if (pid) {
+    // In parent process
+    int exitCode;
+    wait(&exitCode);
+    ASSERT_EQ(0, exitCode) << "External test " << testName << " failed";
+  } else {
+    // In child process. Go into to the work directory, redirect IO to files,
+    // set MESOS_HOME environment variable, and exec the test script.
+    chdir(workDir.c_str());
+    if (freopen("stdout", "w", stdout) == NULL)
+      fatalerror("freopen failed");
+    if (freopen("stderr", "w", stderr) == NULL)
+      fatalerror("freopen failed");
+    setenv("MESOS_HOME", MESOS_HOME.c_str(), 1);
+    execl(script.c_str(), script.c_str(), (char*) NULL);
+    // If we get here, execl failed; report the error
+    fatalerror("Could not execute %s", script.c_str());
+  }
+}

Added: incubator/mesos/trunk/src/tests/external_test.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/external_test.hpp?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/external_test.hpp (added)
+++ incubator/mesos/trunk/src/tests/external_test.hpp Sun Jun  5 05:36:38 2011
@@ -0,0 +1,28 @@
+#ifndef __EXTERNAL_TEST_HPP__
+#define __EXTERNAL_TEST_HPP__
+
+#include <gtest/gtest.h>
+
+/**
+ * Run an external test with the given name. The test is expected to be
+ * located in src/tests/external/<testCase>/<testName>.sh.
+ * We execute this script in directory test_output/<testCase>/<testName>,
+ * piping its output to files called stdout and stderr, and the test
+ * passes if the script returns 0.
+ */
+#define TEST_EXTERNAL(testCase, testName) \
+  TEST(testCase, testName) { \
+    nexus::test::runExternalTest(#testCase, #testName); \
+  }
+
+namespace nexus { namespace test {
+
+/**
+ * Function called by TEST_EXTERNAL to execute external tests. See
+ * explanation of parameters at definition of TEST_EXTERNAL.
+ */
+void runExternalTest(const char* testCase, const char* testName);
+
+}} /* namespace nexus::test */
+
+#endif /* __EXTERNAL_TEST_HPP__ */

Modified: incubator/mesos/trunk/src/tests/main.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/main.cpp?rev=1131814&r1=1131813&r2=1131814&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/main.cpp (original)
+++ incubator/mesos/trunk/src/tests/main.cpp Sun Jun  5 05:36:38 2011
@@ -1,8 +1,24 @@
 #include <glog/logging.h>
 #include <gtest/gtest.h>
 
+#include <libgen.h>
+#include <stdlib.h>
+
+#include <iostream>
+#include <string>
+
+using std::string;
+
+string MESOS_HOME;
+
 
 int main(int argc, char **argv) {
+  // Get absolute path to Mesos home direcotry (really src right now)
+  char buf[4096];
+  realpath(dirname(argv[0]), buf);
+  MESOS_HOME = buf;
+  std::cout << "Mesos home is " << MESOS_HOME << std::endl;
+
   google::InitGoogleLogging("alltests");
   testing::InitGoogleTest(&argc, argv);
   testing::FLAGS_gtest_death_test_style = "threadsafe";

Added: incubator/mesos/trunk/src/tests/test_sample_frameworks.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/test_sample_frameworks.cpp?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/test_sample_frameworks.cpp (added)
+++ incubator/mesos/trunk/src/tests/test_sample_frameworks.cpp Sun Jun  5 05:36:38 2011
@@ -0,0 +1,13 @@
+#include <gtest/gtest.h>
+
+#include "external_test.hpp"
+
+// Run each of the sample frameworks in local mode
+TEST_EXTERNAL(SampleFrameworks, CFramework)
+TEST_EXTERNAL(SampleFrameworks, CppFramework)
+#ifdef MESOS_HAS_JAVA
+  TEST_EXTERNAL(SampleFrameworks, JavaFramework)
+#endif 
+#ifdef MESOS_HAS_PYTHON
+  TEST_EXTERNAL(SampleFrameworks, PythonFramework)
+#endif

Added: incubator/mesos/trunk/src/tests/test_utils.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/test_utils.hpp?rev=1131814&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/test_utils.hpp (added)
+++ incubator/mesos/trunk/src/tests/test_utils.hpp Sun Jun  5 05:36:38 2011
@@ -0,0 +1,8 @@
+#ifndef __TEST_UTILS_HPP__
+#define __TEST_UTILS_HPP__
+
+#include <string>
+
+extern std::string MESOS_HOME; // Defined and initialized in main.cpp
+
+#endif /* __TEST_UTILS_HPP__ */