Added a `TestDiskProfileServer` helper. This helper class will be used to test functionalities related to profile changes.
Review: https://reviews.apache.org/r/67666 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/0e90d7e2 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/0e90d7e2 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/0e90d7e2 Branch: refs/heads/master Commit: 0e90d7e29baf7df848c77346951a30583804d391 Parents: 45e3c16 Author: Chun-Hung Hsiao <chhs...@mesosphere.io> Authored: Thu Jun 14 15:50:41 2018 -0700 Committer: Chun-Hung Hsiao <chhs...@mesosphere.io> Committed: Fri Jul 13 14:30:07 2018 -0700 ---------------------------------------------------------------------- src/Makefile.am | 1 + src/tests/disk_profile_adaptor_tests.cpp | 50 ++------------ src/tests/disk_profile_server.hpp | 98 +++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/0e90d7e2/src/Makefile.am ---------------------------------------------------------------------- diff --git a/src/Makefile.am b/src/Makefile.am index 06b6a34..9c13eaa 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2508,6 +2508,7 @@ mesos_tests_SOURCES = \ tests/credentials_tests.cpp \ tests/default_executor_tests.cpp \ tests/disk_profile_adaptor_tests.cpp \ + tests/disk_profile_server.hpp \ tests/disk_quota_tests.cpp \ tests/dynamic_weights_tests.cpp \ tests/environment.cpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/0e90d7e2/src/tests/disk_profile_adaptor_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/disk_profile_adaptor_tests.cpp b/src/tests/disk_profile_adaptor_tests.cpp index efff798..fe8d1d3 100644 --- a/src/tests/disk_profile_adaptor_tests.cpp +++ b/src/tests/disk_profile_adaptor_tests.cpp @@ -45,11 +45,13 @@ #include "tests/flags.hpp" #include "tests/mesos.hpp" +#include "tests/disk_profile_server.hpp" #include "tests/utils.hpp" using namespace process; using std::map; +using std::shared_ptr; using std::string; using std::tuple; using std::vector; @@ -488,39 +490,6 @@ TEST_F(UriDiskProfileAdaptorTest, FetchFromFile) } -// Basic helper for UriDiskProfileAdaptor modules configured to fetch -// from HTTP URIs. -class MockProfileServer : public Process<MockProfileServer> -{ -public: - MOCK_METHOD1(profiles, Future<http::Response>(const http::Request&)); - -protected: - virtual void initialize() - { - route("/profiles", None(), &MockProfileServer::profiles); - } -}; - - -class ServerWrapper -{ -public: - ServerWrapper() : process(new MockProfileServer()) - { - spawn(process.get()); - } - - ~ServerWrapper() - { - terminate(process.get()); - wait(process.get()); - } - - Owned<MockProfileServer> process; -}; - - // This creates a UriDiskProfileAdaptor module configured to read from // an HTTP URI. The HTTP server will return a different profile mapping // between each of the calls. We expect the module to ignore the third @@ -603,16 +572,15 @@ TEST_F(UriDiskProfileAdaptorTest, FetchFromHTTP) resourceProviderInfo.set_type("resource_provider_type"); resourceProviderInfo.set_name("resource_provider_name"); - ServerWrapper server; - - // Wait for the server to finish initializing so that the routes are ready. - AWAIT_READY(dispatch(server.process->self(), []() { return Nothing(); })); + Future<shared_ptr<TestDiskProfileServer>> server = + TestDiskProfileServer::create(); + AWAIT_READY(server); // We need to intercept this call since the module is expected to // ignore the result of the third call. Future<Nothing> thirdCall; - EXPECT_CALL(*server.process, profiles(_)) + EXPECT_CALL(*server.get()->process, profiles(_)) .WillOnce(Return(http::OK(contents1))) .WillOnce(Return(http::OK(contents2))) .WillOnce(DoAll(FutureSatisfy(&thirdCall), Return(http::OK(contents3)))) @@ -626,11 +594,7 @@ TEST_F(UriDiskProfileAdaptorTest, FetchFromHTTP) Parameter* uriFlag = params.add_parameter(); uriFlag->set_key("uri"); - uriFlag->set_value(stringify(process::http::URL( - "http", - process::address().ip, - process::address().port, - server.process->self().id + "/profiles"))); + uriFlag->set_value(stringify(server.get()->process->url())); Try<DiskProfileAdaptor*> _module = modules::ModuleManager::create<DiskProfileAdaptor>( http://git-wip-us.apache.org/repos/asf/mesos/blob/0e90d7e2/src/tests/disk_profile_server.hpp ---------------------------------------------------------------------- diff --git a/src/tests/disk_profile_server.hpp b/src/tests/disk_profile_server.hpp new file mode 100644 index 0000000..1a8d291 --- /dev/null +++ b/src/tests/disk_profile_server.hpp @@ -0,0 +1,98 @@ +// 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 __TESTS_DISK_PROFILE_SERVER_HPP__ +#define __TESTS_DISK_PROFILE_SERVER_HPP__ + +#include <memory> + +#include <gmock/gmock.h> + +#include <process/dispatch.hpp> +#include <process/future.hpp> +#include <process/http.hpp> +#include <process/owned.hpp> +#include <process/process.hpp> + +#include <stout/none.hpp> + +namespace mesos { +namespace internal { +namespace tests { + +// A simple helper to provide a mock HTTP endpoint for `UriDiskProfileAdaptor` +// to fetch disk profile mappings from. +class TestDiskProfileServerProcess + : public process::Process<TestDiskProfileServerProcess> +{ +public: + TestDiskProfileServerProcess() + : process::ProcessBase("test-disk-profile-server") {} + + inline process::http::URL url() + { + return process::http::URL( + "http", + process::address().ip, + process::address().port, + self().id + "/profiles"); + } + + MOCK_METHOD1( + profiles, + process::Future<process::http::Response>(const process::http::Request&)); + +private: + void initialize() override + { + route("/profiles", None(), &Self::profiles); + } +}; + + +class TestDiskProfileServer +{ +public: + static inline process::Future<std::shared_ptr<TestDiskProfileServer>> create() + { + // TODO(chhsiao): Make `server` a `unique_ptr` and move it into the + // following lambda once we get C++14. + std::shared_ptr<TestDiskProfileServer> server(new TestDiskProfileServer()); + + // Wait for the process to finish initializing so that the routes are ready. + return process::dispatch(server->process->self(), [=] { return server; }); + } + + ~TestDiskProfileServer() + { + process::terminate(process.get()); + process::wait(process.get()); + } + + std::unique_ptr<TestDiskProfileServerProcess> process; + +private: + TestDiskProfileServer() : process(new TestDiskProfileServerProcess()) + { + process::spawn(process.get()); + } +}; + +} // namespace tests { +} // namespace internal { +} // namespace mesos { + +#endif // __TESTS_DISK_PROFILE_SERVER_HPP__