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 2017/11/30 00:04:23 UTC
[12/13] mesos git commit: Added a test for ContainerDaemon.
Added a test for ContainerDaemon.
Review: https://reviews.apache.org/r/64176
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/e861c0e4
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/e861c0e4
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/e861c0e4
Branch: refs/heads/master
Commit: e861c0e497bac65a6a7be8270f020f9b3e436d2b
Parents: 5c97963
Author: Jie Yu <yu...@gmail.com>
Authored: Wed Nov 29 13:35:21 2017 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Wed Nov 29 16:03:25 2017 -0800
----------------------------------------------------------------------
src/Makefile.am | 1 +
src/tests/CMakeLists.txt | 1 +
src/tests/container_daemon_tests.cpp | 184 ++++++++++++++++++++++++++++++
src/tests/mesos.cpp | 24 ++++
src/tests/mesos.hpp | 7 ++
5 files changed, 217 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/e861c0e4/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 8dcc367..4a3b728 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2412,6 +2412,7 @@ mesos_tests_SOURCES = \
tests/common_validation_tests.cpp \
tests/container_logger_tests.cpp \
tests/containerizer.cpp \
+ tests/container_daemon_tests.cpp \
tests/cram_md5_authentication_tests.cpp \
tests/credentials_tests.cpp \
tests/default_executor_tests.cpp \
http://git-wip-us.apache.org/repos/asf/mesos/blob/e861c0e4/src/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 8997cc0..65fca82 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -84,6 +84,7 @@ set(MESOS_TESTS_SRC
check_tests.cpp
command_executor_tests.cpp
common_validation_tests.cpp
+ container_daemon_tests.cpp
cram_md5_authentication_tests.cpp
credentials_tests.cpp
default_executor_tests.cpp
http://git-wip-us.apache.org/repos/asf/mesos/blob/e861c0e4/src/tests/container_daemon_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/container_daemon_tests.cpp b/src/tests/container_daemon_tests.cpp
new file mode 100644
index 0000000..3d88390
--- /dev/null
+++ b/src/tests/container_daemon_tests.cpp
@@ -0,0 +1,184 @@
+// 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 <process/future.hpp>
+#include <process/gmock.hpp>
+#include <process/http.hpp>
+
+#include <process/ssl/flags.hpp>
+
+#include <stout/option.hpp>
+#include <stout/try.hpp>
+#include <stout/uuid.hpp>
+
+#include <mesos/mesos.hpp>
+
+#include <mesos/authentication/secret_generator.hpp>
+
+#ifdef USE_SSL_SOCKET
+#include "authentication/executor/jwt_secret_generator.hpp"
+#endif // USE_SSL_SOCKET
+
+#include "common/validation.hpp"
+
+#include "slave/container_daemon.hpp"
+
+#include "tests/cluster.hpp"
+#include "tests/mesos.hpp"
+
+namespace http = process::http;
+
+#ifdef USE_SSL_SOCKET
+namespace openssl = process::network::openssl;
+#endif // USE_SSL_SOCKET
+
+using std::string;
+
+using process::Future;
+using process::Owned;
+using process::PID;
+using process::Promise;
+
+using process::http::authentication::Principal;
+
+#ifdef USE_SSL_SOCKET
+using mesos::authentication::executor::JWTSecretGenerator;
+#endif // USE_SSL_SOCKET
+
+using mesos::internal::common::validation::validateSecret;
+
+using mesos::internal::slave::ContainerDaemon;
+using mesos::internal::slave::Slave;
+
+using mesos::master::detector::MasterDetector;
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+class ContainerDaemonTest : public MesosTest {};
+
+
+TEST_F(ContainerDaemonTest, RestartOnTermination)
+{
+ Try<Owned<cluster::Master>> master = StartMaster();
+ ASSERT_SOME(master);
+
+ Owned<MasterDetector> detector = master.get()->createDetector();
+ Owned<SecretGenerator> secretGenerator;
+
+ slave::Flags slaveFlags = CreateSlaveFlags();
+
+#ifdef USE_SSL_SOCKET
+ ASSERT_SOME(slaveFlags.jwt_secret_key);
+
+ Try<string> jwtSecretKey = os::read(slaveFlags.jwt_secret_key.get());
+ ASSERT_SOME(jwtSecretKey);
+
+ secretGenerator.reset(new JWTSecretGenerator(jwtSecretKey.get()));
+#endif // USE_SSL_SOCKET
+
+ Future<Nothing> recover = FUTURE_DISPATCH(_, &Slave::__recover);
+
+ Try<Owned<cluster::Slave>> slave = StartSlave(
+ detector.get(),
+ secretGenerator.get(),
+ slaveFlags);
+
+ ASSERT_SOME(slave);
+
+ PID<Slave> slavePid = slave.get()->pid;
+
+ // Ensure slave has finished recovery.
+ AWAIT_READY(recover);
+
+ string scheme = "http";
+
+#ifdef USE_SSL_SOCKET
+ if (openssl::flags().enabled) {
+ scheme = "https";
+ }
+#endif // USE_SSL_SOCKET
+
+ http::URL url(
+ scheme,
+ slavePid.address.ip,
+ slavePid.address.port,
+ strings::join("/", slavePid.id, "api/v1"));
+
+ // NOTE: The current implicit authorization for creating standalone
+ // containers is to check if the container ID prefix in the claims
+ // of the principal is indeed a prefix of the container ID that is
+ // specified in the API call.
+ string containerIdPrefix = UUID::random().toString();
+
+ ContainerID containerId;
+ containerId.set_value(strings::join(
+ "-",
+ containerIdPrefix,
+ UUID::random().toString()));
+
+ Principal principal(
+ None(),
+ {{"cid_prefix", containerIdPrefix}});
+
+ Option<string> authToken;
+ if (secretGenerator.get() != nullptr) {
+ Future<Secret> secret = secretGenerator->generate(principal);
+ AWAIT_READY(secret);
+
+ ASSERT_NONE(validateSecret(secret.get()));
+ ASSERT_EQ(Secret::VALUE, secret->type());
+
+ authToken = secret->value().data();
+ }
+
+ int runs = 0;
+ Promise<Nothing> done;
+
+ auto postStartHook = [&]() -> Future<Nothing> {
+ runs++;
+ return Nothing();
+ };
+
+ auto postStopHook = [&]() -> Future<Nothing> {
+ if (runs >= 5) {
+ done.set(Nothing());
+ }
+ return Nothing();
+ };
+
+ Try<Owned<ContainerDaemon>> daemon = ContainerDaemon::create(
+ url,
+ authToken,
+ containerId,
+ createCommandInfo("exit 0"),
+ None(),
+ None(),
+ postStartHook,
+ postStopHook);
+
+ ASSERT_SOME(daemon);
+
+ AWAIT_READY(done.future());
+
+ Future<Nothing> wait = daemon.get()->wait();
+ EXPECT_TRUE(wait.isPending());
+}
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/e861c0e4/src/tests/mesos.cpp
----------------------------------------------------------------------
diff --git a/src/tests/mesos.cpp b/src/tests/mesos.cpp
index b6daf85..b76eb4d 100644
--- a/src/tests/mesos.cpp
+++ b/src/tests/mesos.cpp
@@ -596,6 +596,30 @@ Try<Owned<cluster::Slave>> MesosTest::StartSlave(
}
+Try<Owned<cluster::Slave>> MesosTest::StartSlave(
+ mesos::master::detector::MasterDetector* detector,
+ mesos::SecretGenerator* secretGenerator,
+ const Option<slave::Flags>& flags)
+{
+ Try<Owned<cluster::Slave>> slave = cluster::Slave::create(
+ detector,
+ flags.isNone() ? CreateSlaveFlags() : flags.get(),
+ None(),
+ None(),
+ None(),
+ None(),
+ None(),
+ None(),
+ secretGenerator);
+
+ if (slave.isSome()) {
+ slave.get()->start();
+ }
+
+ return slave;
+}
+
+
// Although the constructors and destructors for mock classes are
// often trivial, defining them out-of-line (in a separate compilation
// unit) improves compilation time: see MESOS-3827.
http://git-wip-us.apache.org/repos/asf/mesos/blob/e861c0e4/src/tests/mesos.hpp
----------------------------------------------------------------------
diff --git a/src/tests/mesos.hpp b/src/tests/mesos.hpp
index 68f78d0..5fe5356 100644
--- a/src/tests/mesos.hpp
+++ b/src/tests/mesos.hpp
@@ -257,6 +257,13 @@ protected:
const Option<slave::Flags>& flags = None(),
bool mock = false);
+ // Starts a slave with the specified detector, secretGenerator,
+ // and flags.
+ virtual Try<process::Owned<cluster::Slave>> StartSlave(
+ mesos::master::detector::MasterDetector* detector,
+ mesos::SecretGenerator* secretGenerator,
+ const Option<slave::Flags>& flags = None());
+
Option<zookeeper::URL> zookeeperUrl;
const std::string defaultAgentResourcesString{