Apache Mesos
mock_docker.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __TESTS_MOCKDOCKER_HPP__
18 #define __TESTS_MOCKDOCKER_HPP__
19 
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 #include <gmock/gmock.h>
25 
26 #include <mesos/resources.hpp>
27 
29 
30 #include <process/future.hpp>
31 #include <process/owned.hpp>
32 #include <process/shared.hpp>
33 
34 #include <stout/json.hpp>
35 #include <stout/option.hpp>
36 
38 
40 
41 using ::testing::_;
42 using ::testing::Invoke;
43 
45 
46 namespace mesos {
47 namespace internal {
48 namespace tests {
49 
50 // Definition of a mock Docker to be used in tests with gmock.
51 class MockDocker : public Docker
52 {
53 public:
54  MockDocker(
55  const std::string& path,
56  const std::string& socket,
57  const Option<JSON::Object>& config = None());
58  ~MockDocker() override;
59 
61  run,
63  const Docker::RunOptions& options,
65  const process::Subprocess::IO&));
66 
68  ps,
69  process::Future<std::vector<Docker::Container>>(
70  bool, const Option<std::string>&));
71 
73  pull,
75  const std::string&,
76  const std::string&,
77  bool));
78 
80  stop,
82  const std::string&,
83  const Duration&,
84  bool));
85 
87  inspect,
89  const std::string&,
90  const Option<Duration>&));
91 
93  const Docker::RunOptions& runOptions,
94  const process::Subprocess::IO& _stdout,
95  const process::Subprocess::IO& _stderr) const
96  {
97  return Docker::run(
98  runOptions,
99  _stdout,
100  _stderr);
101  }
102 
104  bool all,
105  const Option<std::string>& prefix) const
106  {
107  return Docker::ps(all, prefix);
108  }
109 
111  const std::string& directory,
112  const std::string& image,
113  bool force) const
114  {
115  return Docker::pull(directory, image, force);
116  }
117 
119  const std::string& containerName,
120  const Duration& timeout,
121  bool remove) const
122  {
123  return Docker::stop(containerName, timeout, remove);
124  }
125 
127  const std::string& containerName,
128  const Option<Duration>& retryInterval)
129  {
130  return Docker::inspect(containerName, retryInterval);
131  }
132 };
133 
134 
135 // Definition of a mock DockerContainerizer to be used in tests with gmock.
137 public:
139  const slave::Flags& flags,
140  slave::Fetcher* fetcher,
143  const Option<NvidiaComponents>& nvidia = None());
144 
147 
148  ~MockDockerContainerizer() override;
149 
150  void initialize()
151  {
152  // NOTE: See TestContainerizer::setup for why we use
153  // 'EXPECT_CALL' and 'WillRepeatedly' here instead of
154  // 'ON_CALL' and 'WillByDefault'.
155  EXPECT_CALL(*this, launch(_, _, _, _))
156  .WillRepeatedly(Invoke(this, &MockDockerContainerizer::_launch));
157 
158  EXPECT_CALL(*this, update(_, _, _))
159  .WillRepeatedly(Invoke(this, &MockDockerContainerizer::_update));
160  }
161 
162  MOCK_METHOD4(
163  launch,
165  const ContainerID&,
166  const mesos::slave::ContainerConfig&,
167  const std::map<std::string, std::string>&,
168  const Option<std::string>&));
169 
170  MOCK_METHOD3(
171  update,
173  const ContainerID&,
174  const Resources&,
175  const google::protobuf::Map<std::string, Value::Scalar>&));
176 
177  // Default 'launch' implementation (necessary because we can't just
178  // use &slave::DockerContainerizer::launch with 'Invoke').
180  const ContainerID& containerId,
181  const mesos::slave::ContainerConfig& containerConfig,
182  const std::map<std::string, std::string>& environment,
183  const Option<std::string>& pidCheckpointPath)
184  {
186  containerId,
187  containerConfig,
188  environment,
189  pidCheckpointPath);
190  }
191 
193  const ContainerID& containerId,
194  const Resources& resourceRequests,
195  const google::protobuf::Map<std::string, Value::Scalar>& resourceLimits)
196  {
198  containerId,
199  resourceRequests,
200  resourceLimits);
201  }
202 };
203 
204 
205 // Definition of a mock DockerContainerizerProcess to be used in tests
206 // with gmock.
208 {
209 public:
211  const slave::Flags& flags,
212  slave::Fetcher* fetcher,
215  const Option<NvidiaComponents>& nvidia = None());
216 
217  ~MockDockerContainerizerProcess() override;
218 
219  MOCK_METHOD1(
220  fetch,
221  process::Future<Nothing>(const ContainerID&));
222 
223  MOCK_METHOD1(
224  pull,
225  process::Future<Nothing>(const ContainerID&));
226 
227  process::Future<Nothing> _fetch(const ContainerID& containerId)
228  {
229  return slave::DockerContainerizerProcess::fetch(containerId);
230  }
231 
232  process::Future<Nothing> _pull(const ContainerID& containerId)
233  {
234  return slave::DockerContainerizerProcess::pull(containerId);
235  }
236 };
237 
238 } // namespace tests {
239 } // namespace internal {
240 } // namespace mesos {
241 
242 #endif // __TESTS_MOCKDOCKER_HPP__
Definition: path.hpp:29
virtual process::Future< Nothing > pull(const ContainerID &containerId)
Try< bool > update(const std::string &link, const Handle &parent, uint16_t protocol, const action::Mirror &mirror)
Definition: fetcher.hpp:49
MockDocker(const std::string &path, const std::string &socket, const Option< JSON::Object > &config=None())
process::Future< Nothing > _stop(const std::string &containerName, const Duration &timeout, bool remove) const
Definition: mock_docker.hpp:118
constexpr const char * prefix
Definition: os.hpp:96
process::Future< Nothing > update(const ContainerID &containerId, const Resources &resourceRequests, const google::protobuf::Map< std::string, Value::Scalar > &resourceLimits={}) override
virtual process::Future< std::vector< Container > > ps(bool all=false, const Option< std::string > &prefix=None()) const
Definition: resources.hpp:83
Try< T > fetch(const std::string &value)
Definition: fetch.hpp:38
virtual process::Future< Image > pull(const std::string &directory, const std::string &image, bool force=false) const
Definition: flags.hpp:39
Definition: duration.hpp:32
process::Future< Nothing > _fetch(const ContainerID &containerId)
Definition: mock_docker.hpp:227
virtual process::Future< Container > inspect(const std::string &containerName, const Option< Duration > &retryInterval=None()) const
MOCK_CONST_METHOD3(run, process::Future< Option< int >>(const Docker::RunOptions &options, const process::Subprocess::IO &, const process::Subprocess::IO &))
Environment * environment
process::Future< Containerizer::LaunchResult > launch(const ContainerID &containerId, const mesos::slave::ContainerConfig &containerConfig, const std::map< std::string, std::string > &environment, const Option< std::string > &pidCheckpointPath) override
process::Future< Option< int > > _run(const Docker::RunOptions &runOptions, const process::Subprocess::IO &_stdout, const process::Subprocess::IO &_stderr) const
Definition: mock_docker.hpp:92
process::PID< master::Master > launch(const Flags &flags, mesos::allocator::Allocator *_allocator=nullptr)
process::Future< Docker::Container > _inspect(const std::string &containerName, const Option< Duration > &retryInterval)
Definition: mock_docker.hpp:126
Definition: components.hpp:34
Describes how the I/O is redirected for stdin/stdout/stderr.
Definition: subprocess.hpp:62
Definition: agent.hpp:25
Definition: mock_docker.hpp:51
virtual process::Future< Nothing > fetch(const ContainerID &containerId)
MOCK_CONST_METHOD2(ps, process::Future< std::vector< Docker::Container >>(bool, const Option< std::string > &))
process::Future< std::vector< Docker::Container > > _ps(bool all, const Option< std::string > &prefix) const
Definition: mock_docker.hpp:103
Definition: docker.hpp:57
URI image(const std::string &repository, const std::string &reference, const std::string &registry, const Option< std::string > &scheme=None(), const Option< int > &port=None())
Definition: docker.hpp:30
process::Future< Nothing > _pull(const ContainerID &containerId)
Definition: mock_docker.hpp:232
Definition: none.hpp:27
Definition: attributes.hpp:24
Definition: executor.hpp:48
process::Future< slave::Containerizer::LaunchResult > _launch(const ContainerID &containerId, const mesos::slave::ContainerConfig &containerConfig, const std::map< std::string, std::string > &environment, const Option< std::string > &pidCheckpointPath)
Definition: mock_docker.hpp:179
process::Future< Docker::Image > _pull(const std::string &directory, const std::string &image, bool force) const
Definition: mock_docker.hpp:110
void initialize()
Definition: mock_docker.hpp:150
virtual process::Future< Option< int > > run(const RunOptions &options, const process::Subprocess::IO &_stdout=process::Subprocess::FD(STDOUT_FILENO), const process::Subprocess::IO &_stderr=process::Subprocess::FD(STDERR_FILENO)) const
Definition: spec.hpp:35
Definition: parse.hpp:33
process::Future< Nothing > _update(const ContainerID &containerId, const Resources &resourceRequests, const google::protobuf::Map< std::string, Value::Scalar > &resourceLimits)
Definition: mock_docker.hpp:192
Definition: docker.hpp:177
virtual process::Future< Nothing > stop(const std::string &containerName, const Duration &timeout=Seconds(0), bool remove=false) const
Definition: future.hpp:58