forked from svarshavchik/vera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.C
426 lines (315 loc) · 7.51 KB
/
unit_test.C
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
** Copyright 2022 Double Precision, Inc.
** See COPYING for distribution information.
*/
#include "config.h"
#include "external_filedesc.H"
#define UNIT_TEST
#include "current_containers_info.H"
#undef UNIT_TEST
#include "proc_loader.H"
#include "proc_container_group.H"
#include "privrequest.H"
#include "log.H"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <locale>
#include <algorithm>
std::vector<std::string> logged_state_changes;
struct timespec fake_time;
std::vector<std::string> logged_runners;
std::vector<std::tuple<pid_t, int>> sent_sigs;
std::optional<std::stringstream> current_switchlog;
std::vector<std::string> completed_switchlog;
pid_t next_pid=1;
bool all_forks_fail=false;
std::string environconfig()
{
return "testenviron";
}
std::string overrideconfigdir()
{
return "testoverrides";
}
current_containers_infoObj::current_containers_infoObj()
: current_containers_infoObj{
std::tuple{default_runlevels(), false}
}
{
}
// In unit tests we use the "testcgroup" subdirectory, for those tests
// where that matters.
const char *proc_container_group_data::get_cgroupfs_base_path()
{
return "testcgroup";
}
// The "create" action consists of creating the subdirectory, pretty much
// unchanged, but creating cgroup.events with O_CREAT. Leaving it as empty
// is fine. The handler deals with it.
bool proc_container_group_data::cgroups_dir_create()
{
auto dir=cgroups_dir();
mkdir(get_cgroupfs_base_path(), 0755);
if (mkdir(dir.c_str(), 0755) < 0)
{
if (errno != EEXIST)
return false;
}
close(open(cgroup_events().c_str(), O_RDWR|O_CREAT|O_TRUNC,0644));
log_container_message(container, "cgroup created");
return true;
}
// The registration process consists of manually writing "populated 1"
bool proc_container_group_data::cgroups_register()
{
auto cgroup_eventsfd=open(cgroup_events().c_str(),
O_WRONLY|O_CREAT|O_TRUNC);
if (cgroup_eventsfd < 0)
{
log_message("Internal error: cgroup_eventsfd not set");
return false;
}
lseek(cgroup_eventsfd, 0, SEEK_SET);
write(cgroup_eventsfd, "populated 1\n", 12);
close(cgroup_eventsfd);
return true;
}
bool proc_container_group::cgroups_try_rmdir(
const proc_container &pc,
const external_filedesc &requester_stdout)
{
log_output(pc, requester_stdout);
auto dir=cgroups_dir();
unlink((dir + "/cgroup.procs").c_str());
unlink((dir + "/cgroup.kill").c_str());
unlink(cgroup_events().c_str());
if (rmdir(dir.c_str()) < 0)
{
if (errno != ENOENT)
return false;
}
log_container_message(
container, "cgroup removed"
);
return true;
}
std::string read_stdoutcc(const external_filedesc &stdoutcc)
{
std::string s;
char buffer[8192];
ssize_t l;
fcntl(stdoutcc->fd, F_SETFL, O_NONBLOCK);
for (;;)
{
l=read(stdoutcc->fd, buffer, sizeof(buffer));
if (l == 0)
break;
if (l == -1)
{
if (errno == EAGAIN)
{
do_poll(1000);
continue;
}
throw "read from stdoutcc failed unexpectedly.";
}
s.insert(s.end(), buffer, buffer+l);
}
return s;
}
void populated(const proc_container &container, bool isit)
{
proc_container_group_data dummy;
dummy.container=container;
auto dir=dummy.cgroups_dir();
dir += "/cgroup.events";
int fd=open(dir.c_str(), O_WRONLY|O_CREAT, 0666);
write(fd, (isit ? "populated 1\n":"populated 0\n"), 12);
close(fd);
}
//
// proc_container_stopped() is called by unit tests. The cgroups.events
// handler calls populated() directly.
void proc_container_stopped(const std::string &s)
{
auto ci=get_containers_info(nullptr);
auto iter=ci->containers.find(s);
if (iter != ci->containers.end())
populated(iter->first, false);
ci->populated(s, false);
}
void proc_container_group::cgroups_sendsig(pid_t p, int s)
{
sent_sigs.emplace_back(p, s);
}
void proc_container_group::prepare_to_transfer_fd(int &fd)
{
fcntl(fd, F_SETFD, 0);
fd= -1;
// Unit test will rebuild everything, prevent this fd
// from getting closed.
}
std::function<void ()> reexec_handler;
void reexec()
{
reexec_handler();
throw "unexpected return from reexec_handler";
}
std::string proc_container_start(const std::string &name)
{
auto [a, b] = create_fake_request();
send_start(a, name);
proc_do_request(b);
b=nullptr;
return get_start_status(a);
}
std::string proc_container_stop(const std::string &name)
{
auto [a, b] = create_fake_request();
send_stop(a, name);
proc_do_request(b);
b=nullptr;
return get_stop_status(a);
}
std::string proc_container_restart(const std::string &name,
const std::function<void (int)> &completed)
{
auto [a, b] = create_fake_request();
send_restart(a, name);
proc_do_request(b);
b=nullptr;
return get_restart_status(a);
}
std::string proc_container_runlevel(const std::string &new_runlevel)
{
auto [a, b] = create_fake_request();
request_runlevel(a, new_runlevel);
proc_do_request(b);
b=nullptr;
return get_runlevel_status(a);
}
std::string current_runlevel()
{
auto [a, b] = create_fake_request();
request_current_runlevel(a);
proc_do_request(b);
b=nullptr;
auto ret=get_current_runlevel(a);
std::string s, pfix;
for (auto &n:ret)
{
s += pfix;
s += n;
pfix=":";
}
return s;
}
std::string populated_sh(const proc_container &container, bool isit)
{
proc_container_group_data dummy;
dummy.container=container;
auto dir=dummy.cgroups_dir();
dir += "/cgroup.events";
return std::string{"echo \"populated "} + (isit ? "1":"0")
+ "\" >" + dir;
}
void sigusr1()
{
}
void sigusr2()
{
}
void showing_verbose_progress_off()
{
}
void update_verbose_progress_immediately()
{
}
const char slashprocslash[] = "testslashproc/";
void create_fake_cgroup(const proc_container &pc,
const std::vector<pid_t> &pids)
{
proc_container_group_data pg;
pg.container=pc;
if (!pg.cgroups_dir_create())
throw "Cannot create " + pg.cgroups_dir();
std::ofstream o{pg.cgroups_dir() + "/cgroup.procs"};
for (auto &p:pids)
o << p << "\n";
o.close();
if (!o)
throw "Cannot create " + pg.cgroups_dir() + "/cgroup.procs";
}
void create_fake_proc(
pid_t p,
pid_t ppid,
const std::string &exe,
const std::vector<std::string> &args)
{
std::error_code ec;
std::filesystem::create_directory(slashprocslash);
// Create a null file
std::ofstream dummyfile{ slashprocslash + exe };
std::ostringstream o;
o << slashprocslash << p;
std::string piddir=o.str();
ec=std::error_code{};
std::filesystem::create_directory(piddir, ec);
if (ec)
{
throw "Cannot create " + piddir;
}
std::filesystem::create_symlink( "../" + exe, piddir + "/exe", ec);
if (ec)
{
throw "Cannot create " + piddir + "/" + exe;
}
{
std::ofstream o{piddir + "/stat"};
o << p << " comm state " << ppid;
o.close();
if (!o)
throw "Cannot create " + piddir + "/stat";
}
{
std::ofstream o{piddir + "/cmdline"};
for (auto &argv:args)
o.write(argv.c_str(), argv.size()+1);
if (!o)
throw "Cannot create " + piddir + "/stat";
}
}
void switchlog_start()
{
current_switchlog.emplace();
current_switchlog->imbue(std::locale{"C"});
}
void switchlog_stop()
{
if (current_switchlog)
{
current_switchlog->seekg(0);
completed_switchlog.clear();
std::string l;
while (!std::getline(*current_switchlog, l).eof())
completed_switchlog.push_back(std::move(l));
std::sort(completed_switchlog.begin(),
completed_switchlog.end());
current_switchlog.reset();
}
}
std::ostream *get_current_switchlog()
{
if (current_switchlog)
return &*current_switchlog;
return nullptr;
}