You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by an...@apache.org on 2018/02/26 07:24:21 UTC

[05/12] mesos git commit: Added Docker constants and common functions file.

Added Docker constants and common functions file.

This patch is in preparation of porting Docker tests to Windows. There
were a few hard-coded values that were used throughout the codebase,
such as the `alpine` image, that are defined as a constant in a single
file.

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


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

Branch: refs/heads/master
Commit: 94d4dcce3711ac64ed050d1c1e67181939aaeb6e
Parents: 97dadbe
Author: Akash Gupta <ak...@hotmail.com>
Authored: Sun Feb 25 13:34:55 2018 -0800
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Sun Feb 25 22:13:32 2018 -0800

----------------------------------------------------------------------
 src/Makefile.am                                 |   1 +
 src/tests/containerizer/docker_common.hpp       | 171 +++++++++++++++++++
 .../docker_containerizer_tests.cpp              |   2 +
 src/tests/health_check_tests.cpp                |   2 +
 src/tests/mesos.hpp                             |  65 -------
 5 files changed, 176 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/94d4dcce/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 976af3c..8475379 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1273,6 +1273,7 @@ libmesos_no_3rdparty_la_SOURCES +=					\
   tests/zookeeper_test_server.hpp					\
   tests/containerizer/capabilities_test_helper.hpp			\
   tests/containerizer/docker_archive.hpp				\
+  tests/containerizer/docker_common.hpp					\
   tests/containerizer/isolator.hpp					\
   tests/containerizer/launcher.hpp					\
   tests/containerizer/memory_test_helper.hpp				\

http://git-wip-us.apache.org/repos/asf/mesos/blob/94d4dcce/src/tests/containerizer/docker_common.hpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/docker_common.hpp b/src/tests/containerizer/docker_common.hpp
new file mode 100644
index 0000000..544f604
--- /dev/null
+++ b/src/tests/containerizer/docker_common.hpp
@@ -0,0 +1,171 @@
+// 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.
+
+#ifndef __TEST_DOCKER_COMMON_HPP__
+#define __TEST_DOCKER_COMMON_HPP__
+
+#include <string>
+
+#include <process/future.hpp>
+#include <process/gtest.hpp>
+#include <process/io.hpp>
+#include <process/owned.hpp>
+#include <process/subprocess.hpp>
+
+#include <stout/format.hpp>
+#include <stout/gtest.hpp>
+#include <stout/lambda.hpp>
+#include <stout/nothing.hpp>
+#include <stout/try.hpp>
+
+#include <stout/os/mkdtemp.hpp>
+
+#include "docker/docker.hpp"
+
+#include "tests/flags.hpp"
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+#ifdef __WINDOWS__
+// The following image is the microsoft/powershell:nanoserver image with
+// ContainerAdministrator as the default user. There are some permission bugs
+// with accessing volume mounts in process (but not Hyper-V) isolation as
+// the regular ContainerUser user, but accesing them as ContainerAdministrator
+// works fine. Note that this image runs the 1709 version, so the host needs
+// to be 1709 if running on Windows Server. Running on a 1709 host is
+// recommended anyway since it fixes bugs that made these tests run slowly.
+static constexpr char DOCKER_TEST_IMAGE[] = "akagup/pwsh-nano-admin";
+
+// The powershell image uses Powershell Core, which calls the executable as
+// `pwsh` instead of `powershell`. So the regular sleep macro doesn't work.
+#define DOCKER_SLEEP_CMD(x) "pwsh -Command Start-Sleep -Seconds " #x
+#else
+static constexpr char DOCKER_TEST_IMAGE[] = "alpine";
+#define DOCKER_SLEEP_CMD(x) SLEEP_COMMAND(x)
+#endif // __WINDOWS__
+
+constexpr char DOCKER_IPv6_NETWORK[] = "mesos-docker-ip6-test";
+
+
+inline process::Future<Nothing> pullDockerImage(const std::string& imageName)
+{
+  Try<process::Owned<Docker>> docker =
+    Docker::create(tests::flags.docker, tests::flags.docker_socket, false);
+
+  if (docker.isError()) {
+    return process::Failure(docker.error());
+  }
+
+  const Try<std::string> directory = os::mkdtemp();
+  if (directory.isError()) {
+    return process::Failure(docker.error());
+  }
+
+  return docker.get()->pull(directory.get(), imageName)
+    .then([]() {
+      // `Docker::pull` returns a `Future<Docker::Image`>, but we only really
+      // if the pull was successful, so we just return `Nothing` to match the
+      // return type of `pullDockerImage`.
+      return Nothing();
+    })
+    .onAny([directory]() -> process::Future<Nothing> {
+      Try<Nothing> rmdir = os::rmdir(directory.get());
+      if (rmdir.isError()) {
+        return process::Failure(rmdir.error());
+      }
+      return Nothing();
+    });
+}
+
+
+inline void createDockerIPv6UserNetwork()
+{
+  // Docker IPv6 is not supported on Windows, so no-op on that platform.
+  // TODO(akagup): Remove the #ifdef when Windows supports IPv6 networks
+  // in docker containers. See MESOS-8566.
+#ifndef __WINDOWS__
+  // Create a Docker user network with IPv6 enabled.
+  Try<std::string> dockerCommand = strings::format(
+      "docker network create --driver=bridge --ipv6 "
+      "--subnet=fd01::/64 %s",
+      DOCKER_IPv6_NETWORK);
+
+  Try<process::Subprocess> s = process::subprocess(
+      dockerCommand.get(),
+      process::Subprocess::PATH("/dev/null"),
+      process::Subprocess::PATH("/dev/null"),
+      process::Subprocess::PIPE());
+
+  ASSERT_SOME(s) << "Unable to create the Docker IPv6 network: "
+                 << DOCKER_IPv6_NETWORK;
+
+  process::Future<std::string> err = process::io::read(s->err().get());
+
+  // Wait for the network to be created.
+  AWAIT_READY(s->status());
+  AWAIT_READY(err);
+
+  ASSERT_SOME(s->status().get());
+  ASSERT_EQ(s->status()->get(), 0)
+    << "Unable to create the Docker IPv6 network "
+    << DOCKER_IPv6_NETWORK
+    << " : " << err.get();
+#endif // __WINDOWS__
+}
+
+
+inline void removeDockerIPv6UserNetwork()
+{
+  // Docker IPv6 is not supported on Windows, so no-op on that platform.
+  // TODO(akagup): Remove the #ifdef when Windows supports IPv6 networks
+  // in docker containers. See MESOS-8566.
+#ifndef __WINDOWS__
+  // Delete the Docker user network.
+  Try<std::string> dockerCommand =
+    strings::format("docker network rm %s", DOCKER_IPv6_NETWORK);
+
+  Try<process::Subprocess> s = subprocess(
+      dockerCommand.get(),
+      process::Subprocess::PATH("/dev/null"),
+      process::Subprocess::PATH("/dev/null"),
+      process::Subprocess::PIPE());
+
+  // This is best effort cleanup. In case of an error just a log an
+  // error.
+  ASSERT_SOME(s) << "Unable to delete the Docker IPv6 network: "
+                 << DOCKER_IPv6_NETWORK;
+
+  process::Future<std::string> err = process::io::read(s->err().get());
+
+  // Wait for the network to be deleted.
+  AWAIT_READY(s->status());
+  AWAIT_READY(err);
+
+  ASSERT_SOME(s->status().get());
+  ASSERT_EQ(s->status()->get(), 0)
+    << "Unable to delete the Docker IPv6 network "
+    << DOCKER_IPv6_NETWORK
+    << " : " << err.get();
+#endif // __WINDOWS__
+}
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __TEST_DOCKER_COMMON_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/94d4dcce/src/tests/containerizer/docker_containerizer_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/docker_containerizer_tests.cpp b/src/tests/containerizer/docker_containerizer_tests.cpp
index a7cfd06..9930580 100644
--- a/src/tests/containerizer/docker_containerizer_tests.cpp
+++ b/src/tests/containerizer/docker_containerizer_tests.cpp
@@ -49,6 +49,8 @@
 #include "tests/mesos.hpp"
 #include "tests/mock_docker.hpp"
 
+#include "tests/containerizer/docker_common.hpp"
+
 using namespace mesos::internal::slave::paths;
 using namespace mesos::internal::slave::state;
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/94d4dcce/src/tests/health_check_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/health_check_tests.cpp b/src/tests/health_check_tests.cpp
index 948bf17..29d3df1 100644
--- a/src/tests/health_check_tests.cpp
+++ b/src/tests/health_check_tests.cpp
@@ -38,6 +38,8 @@
 #include "tests/resources_utils.hpp"
 #include "tests/utils.hpp"
 
+#include "tests/containerizer/docker_common.hpp"
+
 #ifdef __linux__
 #include "tests/containerizer/docker_archive.hpp"
 #endif // __linux__

http://git-wip-us.apache.org/repos/asf/mesos/blob/94d4dcce/src/tests/mesos.hpp
----------------------------------------------------------------------
diff --git a/src/tests/mesos.hpp b/src/tests/mesos.hpp
index 1416682..2c3d0c9 100644
--- a/src/tests/mesos.hpp
+++ b/src/tests/mesos.hpp
@@ -111,8 +111,6 @@ constexpr char DEFAULT_JWT_SECRET_KEY[] =
   "PcWRQlrJAt871oWgSH+n52vMZ3aVI+AFMzXSo8+sUfMk83IGp0WJefhzeQsjDlGH"
   "GYQgCAuGim0BE2X5U+lEue8s697uQpAO8L/FFRuDH2s";
 
-constexpr char DOCKER_IPv6_NETWORK[] = "mesos-docker-ip6-test";
-
 
 // Forward declarations.
 class MockExecutor;
@@ -2169,69 +2167,6 @@ inline mesos::Environment createEnvironment(
 }
 
 
-inline void createDockerIPv6UserNetwork()
-{
-  // Create a Docker user network with IPv6 enabled.
-  Try<std::string> dockerCommand = strings::format(
-      "docker network create --driver=bridge --ipv6 "
-      "--subnet=fd01::/64 %s",
-      DOCKER_IPv6_NETWORK);
-
-  Try<process::Subprocess> s = subprocess(
-      dockerCommand.get(),
-      process::Subprocess::PATH("/dev/null"),
-      process::Subprocess::PATH("/dev/null"),
-      process::Subprocess::PIPE());
-
-  ASSERT_SOME(s) << "Unable to create the Docker IPv6 network: "
-                 << DOCKER_IPv6_NETWORK;
-
-  process::Future<std::string> err = process::io::read(s->err().get());
-
-  // Wait for the network to be created.
-  AWAIT_READY(s->status());
-  AWAIT_READY(err);
-
-  ASSERT_SOME(s->status().get());
-  ASSERT_EQ(s->status()->get(), 0)
-    << "Unable to create the Docker IPv6 network "
-    << DOCKER_IPv6_NETWORK
-    << " : " << err.get();
-}
-
-
-inline void removeDockerIPv6UserNetwork()
-{
-  // Delete the Docker user network.
-  Try<std::string> dockerCommand = strings::format(
-      "docker network rm %s",
-      DOCKER_IPv6_NETWORK);
-
-  Try<process::Subprocess> s = subprocess(
-      dockerCommand.get(),
-      process::Subprocess::PATH("/dev/null"),
-      process::Subprocess::PATH("/dev/null"),
-      process::Subprocess::PIPE());
-
-  // This is best effort cleanup. In case of an error just a log an
-  // error.
-  ASSERT_SOME(s) << "Unable to delete the Docker IPv6 network: "
-                 << DOCKER_IPv6_NETWORK;
-
-  process::Future<std::string> err = process::io::read(s->err().get());
-
-  // Wait for the network to be deleted.
-  AWAIT_READY(s->status());
-  AWAIT_READY(err);
-
-  ASSERT_SOME(s->status().get());
-  ASSERT_EQ(s->status()->get(), 0)
-    << "Unable to delete the Docker IPv6 network "
-    << DOCKER_IPv6_NETWORK
-    << " : " << err.get();
-}
-
-
 // Macros to get/create (default) ExecutorInfos and FrameworkInfos.
 #define DEFAULT_EXECUTOR_INFO createExecutorInfo("default", "exit 1")