-
Notifications
You must be signed in to change notification settings - Fork 1
/
testswitchlog.C
135 lines (109 loc) · 3.18 KB
/
testswitchlog.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
/*
** Copyright 2024 Double Precision, Inc.
** See COPYING for distribution information.
*/
#include "config.h"
#include "switchlog.H"
#include <unistd.h>
#include <exception>
#include <filesystem>
#include <set>
#include <sys/stat.h>
void switchlog_start()
{
}
struct timespec ts;
void update_current_time()
{
++ts.tv_sec;
}
const struct timespec &log_current_timespec()
{
return ts;
}
std::ofstream current_switchlog;
std::ostream *get_current_switchlog()
{
return ¤t_switchlog;
}
void testswitchlog()
{
if (!std::filesystem::create_directory("testswitchlog.dir")
|| !std::filesystem::create_directory("testswitchlog.dir"
"/2023-08-03")
|| !std::filesystem::create_directory("testswitchlog.dir"
"/2022-08-03")
|| !std::filesystem::create_directory("testswitchlog.dir"
"/2021-08-03")
|| !std::filesystem::create_directory("testswitchlog.dir"
"/2021-08-03/x")
|| !std::ofstream{"testswitchlog.dir/2021-08-03/y"}.is_open()
|| !std::ofstream{"testswitchlog.dir/z"}.is_open()
)
throw std::runtime_error("Cannot create directories");
switchlog_purge("testswitchlog.dir", 2, [](const std::string &){});
std::set<std::string> contents;
for (auto b=std::filesystem::recursive_directory_iterator{
"testswitchlog.dir"
}; b != std::filesystem::recursive_directory_iterator{}; ++b)
{
contents.insert(b->path());
}
if (contents.size() != 2 ||
*contents.begin() != "testswitchlog.dir/2022-08-03" ||
*--contents.end() != "testswitchlog.dir/2023-08-03")
throw std::runtime_error("Unexpected switchlog_purge results");
switchlog_create("testswitchlog.dir", current_switchlog);
update_current_time();
switchlog_start("system/graphical runlevel");
current_switchlog.close();
bool error=false;
switchlog_save("testswitchlog.dir",
[&](std::string s)
{
error=true;
std::cerr << s << std::endl;
});
if (error)
throw std::runtime_error("switchlog_save() failed");
contents.clear();
for (auto b=std::filesystem::recursive_directory_iterator{
"testswitchlog.dir"
}; b != std::filesystem::recursive_directory_iterator{}; ++b)
{
if (b->path() == "testswitchlog.dir/2022-08-03" ||
b->path() == "testswitchlog.dir/2023-08-03")
continue;
contents.insert(b->path());
}
// Should be YYYY-MM-DD and YYYY-MM-DD/something
if (contents.size() != 2 ||
*(--contents.end())->substr(contents.begin()->size()).c_str()
!= '/')
throw std::runtime_error(
"Unexpected results from switchlog_save"
);
// switchlog_enumerate should ignore the current log file, if any.
switchlog_create("testswitchlog.dir", current_switchlog);
current_switchlog.close();
auto logs=enumerate_switchlogs("testswitchlog.dir");
if (logs.size() != 1 ||
logs[0].switchname != "system/graphical runlevel")
throw std::runtime_error("Unexpected switchlog enumeration");
}
int main(int argc, char **argv)
{
umask(022);
alarm(15);
try {
std::error_code ec;
std::filesystem::remove_all("testswitchlog.dir", ec);
testswitchlog();
std::filesystem::remove_all("testswitchlog.dir", ec);
} catch (const std::exception &e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
return 0;
}