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 <list>
21 #include <map>
22 #include <string>
23 #include <vector>
24 
25 #include <gmock/gmock.h>
26 
27 #include <mesos/resources.hpp>
28 
30 
31 #include <process/future.hpp>
32 #include <process/owned.hpp>
33 #include <process/shared.hpp>
34 
35 #include <stout/json.hpp>
36 #include <stout/option.hpp>
37 
39 
41 
42 using ::testing::_;
43 using ::testing::Invoke;
44 
46 
47 namespace mesos {
48 namespace internal {
49 namespace tests {
50 
51 // Definition of a mock Docker to be used in tests with gmock.
52 class MockDocker : public Docker
53 {
54 public:
55  MockDocker(
56  const std::string& path,
57  const std::string& socket,
58  const Option<JSON::Object>& config = None());
59  virtual ~MockDocker();
60 
62  run,
64  const Docker::RunOptions& options,
66  const process::Subprocess::IO&));
67 
69  ps,
70  process::Future<std::list<Docker::Container>>(
71  bool, const Option<std::string>&));
72 
74  pull,
76  const std::string&,
77  const std::string&,
78  bool));
79 
81  stop,
83  const std::string&,
84  const Duration&,
85  bool));
86 
88  inspect,
90  const std::string&,
91  const Option<Duration>&));
92 
94  const Docker::RunOptions& runOptions,
95  const process::Subprocess::IO& _stdout,
96  const process::Subprocess::IO& _stderr) const
97  {
98  return Docker::run(
99  runOptions,
100  _stdout,
101  _stderr);
102  }
103 
105  bool all,
106  const Option<std::string>& prefix) const
107  {
108  return Docker::ps(all, prefix);
109  }
110 
112  const std::string& directory,
113  const std::string& image,
114  bool force) const
115  {
116  return Docker::pull(directory, image, force);
117  }
118 
120  const std::string& containerName,
121  const Duration& timeout,
122  bool remove) const
123  {
124  return Docker::stop(containerName, timeout, remove);
125  }
126 
128  const std::string& containerName,
129  const Option<Duration>& retryInterval)
130  {
131  return Docker::inspect(containerName, retryInterval);
132  }
133 };
134 
135 
136 // Definition of a mock DockerContainerizer to be used in tests with gmock.
138 public:
140  const slave::Flags& flags,
141  slave::Fetcher* fetcher,
144  const Option<NvidiaComponents>& nvidia = None());
145 
148 
149  virtual ~MockDockerContainerizer();
150 
151  void initialize()
152  {
153  // NOTE: See TestContainerizer::setup for why we use
154  // 'EXPECT_CALL' and 'WillRepeatedly' here instead of
155  // 'ON_CALL' and 'WillByDefault'.
156  EXPECT_CALL(*this, launch(_, _, _, _))
157  .WillRepeatedly(Invoke(this, &MockDockerContainerizer::_launch));
158 
159  EXPECT_CALL(*this, update(_, _))
160  .WillRepeatedly(Invoke(this, &MockDockerContainerizer::_update));
161  }
162 
163  MOCK_METHOD4(
164  launch,
166  const ContainerID&,
167  const mesos::slave::ContainerConfig&,
168  const std::map<std::string, std::string>&,
169  const Option<std::string>&));
170 
171  MOCK_METHOD2(
172  update,
174  const ContainerID&,
175  const Resources&));
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& resources)
195  {
197  containerId,
198  resources);
199  }
200 };
201 
202 
203 // Definition of a mock DockerContainerizerProcess to be used in tests
204 // with gmock.
206 {
207 public:
209  const slave::Flags& flags,
210  slave::Fetcher* fetcher,
213  const Option<NvidiaComponents>& nvidia = None());
214 
216 
217  MOCK_METHOD1(
218  fetch,
219  process::Future<Nothing>(const ContainerID&));
220 
221  MOCK_METHOD1(
222  pull,
223  process::Future<Nothing>(const ContainerID&));
224 
225  process::Future<Nothing> _fetch(const ContainerID& containerId)
226  {
227  return slave::DockerContainerizerProcess::fetch(containerId);
228  }
229 
230  process::Future<Nothing> _pull(const ContainerID& containerId)
231  {
232  return slave::DockerContainerizerProcess::pull(containerId);
233  }
234 };
235 
236 } // namespace tests {
237 } // namespace internal {
238 } // namespace mesos {
239 
240 #endif // __TESTS_MOCKDOCKER_HPP__
Definition: path.hpp:26
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)
process::Future< std::list< Docker::Container > > _ps(bool all, const Option< std::string > &prefix) const
Definition: mock_docker.hpp:104
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:119
virtual 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)
constexpr const char * prefix
Definition: os.hpp:94
Definition: resources.hpp:79
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:225
virtual process::Future< Container > inspect(const std::string &containerName, const Option< Duration > &retryInterval=None()) const
virtual process::Future< Nothing > update(const ContainerID &containerId, const Resources &resources)
MOCK_CONST_METHOD3(run, process::Future< Option< int >>(const Docker::RunOptions &options, const process::Subprocess::IO &, const process::Subprocess::IO &))
Environment * environment
process::Future< Option< int > > _run(const Docker::RunOptions &runOptions, const process::Subprocess::IO &_stdout, const process::Subprocess::IO &_stderr) const
Definition: mock_docker.hpp:93
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:127
Definition: components.hpp:34
MOCK_CONST_METHOD2(ps, process::Future< std::list< Docker::Container >>(bool, const Option< std::string > &))
Describes how the I/O is redirected for stdin/stdout/stderr.
Definition: subprocess.hpp:61
process::Future< Nothing > _update(const ContainerID &containerId, const Resources &resources)
Definition: mock_docker.hpp:192
Definition: spec.hpp:30
Definition: mock_docker.hpp:52
virtual process::Future< Nothing > fetch(const ContainerID &containerId)
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:230
Definition: none.hpp:27
Definition: attributes.hpp:24
Definition: executor.hpp:47
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:111
virtual process::Future< std::list< Container > > ps(bool all=false, const Option< std::string > &prefix=None()) const
void initialize()
Definition: mock_docker.hpp:151
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:34
Definition: parse.hpp:33
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:57