|
11 | 11 | #include <platform/dirutils.h> |
12 | 12 |
|
13 | 13 | #include "auditfile.h" |
| 14 | +#include <fmt/format.h> |
14 | 15 | #include <folly/portability/GTest.h> |
| 16 | +#include <memcached/isotime.h> |
15 | 17 | #include <nlohmann/json.hpp> |
16 | 18 | #include <platform/platform_time.h> |
| 19 | +#include <platform/strerror.h> |
17 | 20 | #include <time.h> |
18 | 21 | #include <atomic> |
19 | 22 | #include <cstring> |
| 23 | +#include <deque> |
20 | 24 | #include <iostream> |
21 | 25 | #include <map> |
22 | 26 |
|
@@ -323,3 +327,87 @@ TEST_F(AuditFileTest, MB53282) { |
323 | 327 | auditfile.reconfigure(config); |
324 | 328 | auditfile.test_mb53282(); |
325 | 329 | } |
| 330 | + |
| 331 | +TEST_F(AuditFileTest, PruneFiles) { |
| 332 | + class MockAuditFile : public AuditFile { |
| 333 | + public: |
| 334 | + explicit MockAuditFile(const std::filesystem::path& logdir) |
| 335 | + : AuditFile("PruneFiles") { |
| 336 | + set_log_directory(logdir.generic_string()); |
| 337 | + for (int ii = 0; ii < 10; ++ii) { |
| 338 | + createAuditLogFile( |
| 339 | + logdir, "PruneFiles", std::chrono::hours(ii)); |
| 340 | + } |
| 341 | + }; |
| 342 | + |
| 343 | + static void createAuditLogFile(const std::filesystem::path& logdir, |
| 344 | + std::string_view hostname, |
| 345 | + std::chrono::seconds seconds) { |
| 346 | + using namespace std::filesystem; |
| 347 | + auto ts = ISOTime::generatetimestamp( |
| 348 | + time(nullptr) - seconds.count(), 0) |
| 349 | + .substr(0, 19); |
| 350 | + std::replace(ts.begin(), ts.end(), ':', '-'); |
| 351 | + auto filename = fmt::format("{}-{}-audit.log", hostname, ts); |
| 352 | + auto path = logdir / fmt::format("{}-{}-audit.log", hostname, ts); |
| 353 | + FILE* fp = fopen(path.generic_string().c_str(), "w"); |
| 354 | + if (!fp) { |
| 355 | + throw std::runtime_error(fmt::format( |
| 356 | + "createAuditLogFile: Failed to create {}: {}", |
| 357 | + path.generic_string(), |
| 358 | + cb_strerror())); |
| 359 | + } |
| 360 | + fclose(fp); |
| 361 | + auto ftime = file_time_type::clock::now() - seconds; |
| 362 | + last_write_time(path, ftime); |
| 363 | + } |
| 364 | + |
| 365 | + void set_prune_age(std::chrono::seconds age) { |
| 366 | + using namespace std::chrono; |
| 367 | + prune_age = age; |
| 368 | + next_prune = steady_clock::now() - seconds(1); |
| 369 | + } |
| 370 | + |
| 371 | + std::deque<std::filesystem::path> get_log_files() { |
| 372 | + std::deque<std::filesystem::path> ret; |
| 373 | + for (const auto& p : |
| 374 | + std::filesystem::directory_iterator(log_directory)) { |
| 375 | + if (is_regular_file(p.path())) { |
| 376 | + ret.push_back(p.path()); |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + std::sort(ret.begin(), ret.end()); |
| 381 | + return ret; |
| 382 | + } |
| 383 | + }; |
| 384 | + |
| 385 | + MockAuditFile auditfile(testdir); |
| 386 | + auto blueprint = auditfile.get_log_files(); |
| 387 | + EXPECT_EQ(10, blueprint.size()); |
| 388 | + |
| 389 | + // We don't have a prune time, so all files should be there |
| 390 | + auditfile.prune_old_audit_files(); |
| 391 | + EXPECT_EQ(blueprint, auditfile.get_log_files()); |
| 392 | + |
| 393 | + // If we set the prune time longer than all the files they should still |
| 394 | + // be there |
| 395 | + auditfile.set_prune_age(std::chrono::hours(9) + std::chrono::minutes{30}); |
| 396 | + auditfile.prune_old_audit_files(); |
| 397 | + EXPECT_EQ(blueprint, auditfile.get_log_files()); |
| 398 | + |
| 399 | + // set the prune time between the two last files |
| 400 | + auditfile.set_prune_age(std::chrono::hours(8) + std::chrono::minutes{30}); |
| 401 | + auditfile.prune_old_audit_files(); |
| 402 | + blueprint.pop_front(); |
| 403 | + EXPECT_EQ(blueprint, auditfile.get_log_files()); |
| 404 | + |
| 405 | + // Verify that we nuke multiple old files by specifying a time |
| 406 | + // only leaving one file |
| 407 | + auditfile.set_prune_age(std::chrono::minutes{30}); |
| 408 | + auditfile.prune_old_audit_files(); |
| 409 | + while (blueprint.size() > 1) { |
| 410 | + blueprint.pop_front(); |
| 411 | + } |
| 412 | + EXPECT_EQ(blueprint, auditfile.get_log_files()); |
| 413 | +} |
0 commit comments