forked from apache/mesos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hook_module.cpp
372 lines (290 loc) · 10.3 KB
/
test_hook_module.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// 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 <mesos/hook.hpp>
#include <mesos/mesos.hpp>
#include <mesos/module.hpp>
#include <mesos/module/hook.hpp>
#include <process/future.hpp>
#include <process/id.hpp>
#include <process/process.hpp>
#include <process/protobuf.hpp>
#include <stout/foreach.hpp>
#include <stout/os.hpp>
#include <stout/try.hpp>
#include "messages/messages.hpp"
using namespace mesos;
using std::map;
using std::string;
using process::Failure;
using process::Future;
// Must be kept in sync with variables of the same name in
// tests/hook_tests.cpp.
const char* testLabelKey = "MESOS_Test_Label";
const char* testLabelValue = "ApacheMesos";
const char* testRemoveLabelKey = "MESOS_Test_Remove_Label";
const char* testErrorLabelKey = "MESOS_Test_Error_Label";
class HookProcess : public ProtobufProcess<HookProcess>
{
public:
HookProcess() : ProcessBase(process::ID::generate("example-hook")) {}
void initialize()
{
install<internal::HookExecuted>(
&HookProcess::handler,
&internal::HookExecuted::module);
}
void signal()
{
LOG(INFO) << "HookProcess emitting signal";
internal::HookExecuted message;
message.set_module("org_apache_mesos_TestHook");
send(self(), message);
}
void handler(const process::UPID& from, const string& module)
{
LOG(INFO) << "HookProcess caught signal: " << module;
promise.set(Nothing());
}
Future<Nothing> await()
{
return promise.future();
}
private:
process::Promise<Nothing> promise;
};
class TestHook : public Hook
{
public:
virtual Result<Labels> masterLaunchTaskLabelDecorator(
const TaskInfo& taskInfo,
const FrameworkInfo& frameworkInfo,
const SlaveInfo& slaveInfo)
{
LOG(INFO) << "Executing 'masterLaunchTaskLabelDecorator' hook";
Labels labels;
// Set one known label.
Label* newLabel = labels.add_labels();
newLabel->set_key(testLabelKey);
newLabel->set_value(testLabelValue);
// Remove the 'testRemoveLabelKey' label which was set by the test.
foreach (const Label& oldLabel, taskInfo.labels().labels()) {
if (oldLabel.key() != testRemoveLabelKey) {
labels.add_labels()->CopyFrom(oldLabel);
}
}
return labels;
}
virtual Try<Nothing> masterSlaveLostHook(const SlaveInfo& slaveInfo)
{
LOG(INFO) << "Executing 'masterSlaveLostHook' in agent '"
<< slaveInfo.id() << "'";
// TODO(nnielsen): Add argument to signal(), so we can filter messages from
// the `masterSlaveLostHook` from `slaveRemoveExecutorHook`.
// NOTE: Will not be a problem **as long as** the test doesn't start any
// tasks.
HookProcess hookProcess;
process::spawn(&hookProcess);
Future<Nothing> future =
process::dispatch(hookProcess, &HookProcess::await);
process::dispatch(hookProcess, &HookProcess::signal);
// Make sure we don't terminate the process before the message self-send has
// completed.
future.await();
process::terminate(hookProcess);
process::wait(hookProcess);
return Nothing();
}
// TODO(nnielsen): Split hook tests into multiple modules to avoid
// interference.
virtual Result<Labels> slaveRunTaskLabelDecorator(
const TaskInfo& taskInfo,
const ExecutorInfo& executorInfo,
const FrameworkInfo& frameworkInfo,
const SlaveInfo& slaveInfo)
{
LOG(INFO) << "Executing 'slaveRunTaskLabelDecorator' hook";
Labels labels;
// Set one known label.
Label* newLabel = labels.add_labels();
newLabel->set_key("baz");
newLabel->set_value("qux");
// Remove label which was set by test.
foreach (const Label& oldLabel, taskInfo.labels().labels()) {
if (oldLabel.key() != "foo") {
labels.add_labels()->CopyFrom(oldLabel);
}
}
return labels;
}
// In this hook, we create a new environment variable "FOO" and set
// it's value to "bar".
virtual Result<Environment> slaveExecutorEnvironmentDecorator(
const ExecutorInfo& executorInfo)
{
LOG(INFO) << "Executing 'slaveExecutorEnvironmentDecorator' hook";
Environment environment;
if (executorInfo.command().has_environment()) {
environment.CopyFrom(executorInfo.command().environment());
}
Environment::Variable* variable = environment.add_variables();
variable->set_name("FOO");
variable->set_value("bar");
return environment;
}
// In this hook, look for the existence of a specific label.
// If found, return a `Failure`.
// Otherwise, add an environment variable to the task.
virtual Future<Option<Environment>> slavePreLaunchDockerEnvironmentDecorator(
const Option<TaskInfo>& taskInfo,
const ExecutorInfo& executorInfo,
const string& name,
const string& sandboxDirectory,
const string& mappedDirectory,
const Option<map<string, string>>& env)
{
LOG(INFO) << "Executing 'slavePreLaunchDockerEnvironmentDecorator' hook";
if (taskInfo.isSome()) {
foreach (const Label& label, taskInfo->labels().labels()) {
if (label.key() == testErrorLabelKey) {
return Failure("Spotted error label");
}
}
}
Environment environment;
Environment::Variable* variable = environment.add_variables();
variable->set_name("FOO_DOCKER");
variable->set_value("docker_bar");
return environment;
}
virtual Try<Nothing> slavePreLaunchDockerHook(
const ContainerInfo& containerInfo,
const CommandInfo& commandInfo,
const Option<TaskInfo>& taskInfo,
const ExecutorInfo& executorInfo,
const string& name,
const string& sandboxDirectory,
const string& mappedDirectory,
const Option<Resources>& resources,
const Option<map<string, string>>& env)
{
LOG(INFO) << "Executing 'slavePreLaunchDockerHook'";
return os::touch(path::join(sandboxDirectory, "foo"));
}
virtual Try<Nothing> slavePostFetchHook(
const ContainerID& containerId,
const string& directory)
{
LOG(INFO) << "Executing 'slavePostFetchHook'";
const string path = path::join(directory, "post_fetch_hook");
if (os::exists(path)) {
return os::rm(path);
} else {
return Nothing();
}
}
// This hook locates the file created by environment decorator hook
// and deletes it.
virtual Try<Nothing> slaveRemoveExecutorHook(
const FrameworkInfo& frameworkInfo,
const ExecutorInfo& executorInfo)
{
LOG(INFO) << "Executing 'slaveRemoveExecutorHook'";
// Send a HookExecuted message to ourself. The hook test
// "VerifySlaveLaunchExecutorHook" will wait for the testing
// infrastructure to intercept this message. The intercepted message
// indicates successful execution of this hook.
HookProcess hookProcess;
process::spawn(&hookProcess);
Future<Nothing> future =
process::dispatch(hookProcess, &HookProcess::await);
process::dispatch(hookProcess, &HookProcess::signal);
// Make sure we don't terminate the process before the message self-send has
// completed.
future.await();
process::terminate(hookProcess);
process::wait(hookProcess);
return Nothing();
}
virtual Result<TaskStatus> slaveTaskStatusDecorator(
const FrameworkID& frameworkId,
const TaskStatus& status)
{
LOG(INFO) << "Executing 'slaveTaskStatusDecorator' hook";
Labels labels;
// Set one known label.
Label* newLabel = labels.add_labels();
newLabel->set_key("bar");
newLabel->set_value("qux");
// Remove label which was set by test.
foreach (const Label& oldLabel, status.labels().labels()) {
if (oldLabel.key() != "foo") {
labels.add_labels()->CopyFrom(oldLabel);
}
}
TaskStatus result;
result.mutable_labels()->CopyFrom(labels);
// Set an IP address, a network isolation group, and a known label
// in network info. This data is later validated by the
// 'HookTest.VerifySlaveTaskStatusDecorator' test.
NetworkInfo* networkInfo =
result.mutable_container_status()->add_network_infos();
NetworkInfo::IPAddress* ipAddress = networkInfo->add_ip_addresses();
ipAddress->set_ip_address("4.3.2.1");
networkInfo->add_groups("public");
Label* networkInfoLabel = networkInfo->mutable_labels()->add_labels();
networkInfoLabel->set_key("net_foo");
networkInfoLabel->set_value("net_bar");
return result;
}
virtual Result<Resources> slaveResourcesDecorator(
const SlaveInfo& slaveInfo)
{
LOG(INFO) << "Executing 'slaveResourcesDecorator' hook";
Resources resources;
// Remove the existing "cpus" resource, it will be overwritten by the
// current hook. Keep other resources unchanged.
foreach (const Resource& resource, slaveInfo.resources()) {
if (resource.name() != "cpus") {
resources += resource;
}
}
// Force the value of "cpus" to 4 and add a new custom resource named "foo"
// of type set.
resources += Resources::parse("cpus:4;foo:{bar,baz}").get();
return resources;
}
virtual Result<Attributes> slaveAttributesDecorator(
const SlaveInfo& slaveInfo)
{
LOG(INFO) << "Executing 'slaveAttributesDecorator' hook";
Attributes attributes = slaveInfo.attributes();
attributes.add(Attributes::parse("rack", "rack1"));
return attributes;
}
};
static Hook* createHook(const Parameters& parameters)
{
return new TestHook();
}
// Declares a Hook module named 'org_apache_mesos_TestHook'.
mesos::modules::Module<Hook> org_apache_mesos_TestHook(
MESOS_MODULE_API_VERSION,
MESOS_VERSION,
"Apache Mesos",
"Test Hook module.",
nullptr,
createHook);