|
1 | 1 | #include "common/ceph_argparse.h" |
2 | 2 | #include "common/config.h" |
3 | 3 | #include "common/config_proxy.h" |
| 4 | +#include "common/admin_socket.h" |
| 5 | +#include "common/admin_socket_client.h" |
4 | 6 | #include <gmock/gmock.h> |
5 | 7 | #include "gtest/gtest.h" |
6 | 8 | #include "common/ceph_context.h" |
7 | 9 | #include "global/global_context.h" |
8 | 10 | #include "global/global_init.h" |
9 | 11 | #include "exporter/util.h" |
10 | 12 | #include "exporter/DaemonMetricCollector.h" |
| 13 | +#include <filesystem> |
11 | 14 |
|
12 | 15 | #include <regex> |
13 | 16 | #include <string> |
@@ -674,6 +677,27 @@ static std::vector<std::pair<std::string, std::string>> promethize_data = { |
674 | 677 | {"rocksdb.submit_sync_latency_sum", "ceph_rocksdb_submit_sync_latency_sum"} |
675 | 678 | }; |
676 | 679 |
|
| 680 | + |
| 681 | +class AdminSocketTest |
| 682 | +{ |
| 683 | +public: |
| 684 | + explicit AdminSocketTest(AdminSocket *asokc) |
| 685 | + : m_asokc(asokc) |
| 686 | + { |
| 687 | + } |
| 688 | + bool init(const std::string &uri) { |
| 689 | + return m_asokc->init(uri); |
| 690 | + } |
| 691 | + std::string bind_and_listen(const std::string &sock_path, int *fd) { |
| 692 | + return m_asokc->bind_and_listen(sock_path, fd); |
| 693 | + } |
| 694 | + bool shutdown() { |
| 695 | + m_asokc->shutdown(); |
| 696 | + return true; |
| 697 | + } |
| 698 | + AdminSocket *m_asokc; |
| 699 | +}; |
| 700 | + |
677 | 701 | int main(int argc, char **argv) |
678 | 702 | { |
679 | 703 | ::testing::InitGoogleTest(&argc, argv); |
@@ -1289,8 +1313,11 @@ ceph_mon_session_rm{ceph_daemon="mon.a"} 577 |
1289 | 1313 | # TYPE ceph_mon_session_trim counter |
1290 | 1314 | ceph_mon_session_trim{ceph_daemon="mon.a"} 9 |
1291 | 1315 | )"; |
1292 | | - |
1293 | | - ASSERT_TRUE(collector.metrics.find(expectedMetrics) != std::string::npos); |
| 1316 | + |
| 1317 | + std::string actualMetrics = collector.metrics; |
| 1318 | + std::cout << "Actual MON Metrics: " << actualMetrics << std::endl; |
| 1319 | + ASSERT_TRUE(actualMetrics.find(expectedMetrics) != std::string::npos); |
| 1320 | + //ASSERT_TRUE(collector.metrics.find(expectedMetrics) != std::string::npos); |
1294 | 1321 |
|
1295 | 1322 | // Test for labeled metrics - RGW |
1296 | 1323 | daemon = "ceph-client.rgw.foo.ceph-node-00.aayrrj.2.93993527376064"; |
@@ -1452,3 +1479,82 @@ TEST(Exporter, add_fixed_name_metrics) { |
1452 | 1479 | EXPECT_EQ(new_metric.first, expected_labels); |
1453 | 1480 | ASSERT_TRUE(new_metric.second == expected_metric_name); |
1454 | 1481 | } |
| 1482 | + |
| 1483 | +TEST(Exporter, UpdateSockets) { |
| 1484 | + const std::string mock_dir = "/tmp/fake_sock_dir"; |
| 1485 | + |
| 1486 | + // Create the mock directory |
| 1487 | + std::filesystem::create_directories(mock_dir); |
| 1488 | + |
| 1489 | + // Create a mix of vstart and real cluster mock .asok files |
| 1490 | + std::ofstream(mock_dir + "/ceph-osd.0.asok").close(); |
| 1491 | + std::ofstream(mock_dir + "/ceph-mds.a.asok").close(); |
| 1492 | + std::ofstream(mock_dir + "/ceph-mgr.chatest-node-00.ijzynn.asok").close(); |
| 1493 | + std::ofstream(mock_dir + "/ceph-client.rgw.rgwfoo.chatest-node-00.yqaoen.2.94354846193952.asok").close(); |
| 1494 | + std::ofstream(mock_dir + "/ceph-client.ceph-exporter.chatest-node-00.asok").close(); |
| 1495 | + std::ofstream(mock_dir + "/ceph-mon.chatest-node-00.asok").close(); |
| 1496 | + |
| 1497 | + g_conf().set_val("exporter_sock_dir", mock_dir); |
| 1498 | + |
| 1499 | + DaemonMetricCollector collector; |
| 1500 | + |
| 1501 | + // Run the function that interacts with the mock directory |
| 1502 | + collector.update_sockets(); |
| 1503 | + |
| 1504 | + // Verify the expected results |
| 1505 | + ASSERT_EQ(collector.clients.size(), 4); |
| 1506 | + ASSERT_TRUE(collector.clients.find("ceph-osd.0") != collector.clients.end()); |
| 1507 | + ASSERT_TRUE(collector.clients.find("ceph-mds.a") != collector.clients.end()); |
| 1508 | + ASSERT_TRUE(collector.clients.find("ceph-mon.chatest-node-00") != collector.clients.end()); |
| 1509 | + ASSERT_TRUE(collector.clients.find("ceph-client.rgw.rgwfoo.chatest-node-00.yqaoen.2.94354846193952") != collector.clients.end()); |
| 1510 | + |
| 1511 | + |
| 1512 | + // Remove the mock directory and files |
| 1513 | + std::filesystem::remove_all(mock_dir); |
| 1514 | +} |
| 1515 | + |
| 1516 | + |
| 1517 | +TEST(Exporter, HealthMetrics) { |
| 1518 | + std::map<std::string, AdminSocketClient> clients; |
| 1519 | + DaemonMetricCollector &collector = collector_instance(); |
| 1520 | + std::string daemon = "test_daemon"; |
| 1521 | + std::string expectedCounterDump = ""; |
| 1522 | + std::string expectedCounterSchema = ""; |
| 1523 | + std::string metricName = "ceph_daemon_socket_up"; |
| 1524 | + |
| 1525 | + // Fake admin socket |
| 1526 | + std::string asok_path = "/tmp/" + daemon + ".asok"; |
| 1527 | + std::unique_ptr<AdminSocket> asokc = std::make_unique<AdminSocket>(g_ceph_context); |
| 1528 | + AdminSocketClient client(asok_path); |
| 1529 | + |
| 1530 | + // Add the daemon clients to the collector |
| 1531 | + clients.insert({daemon, std::move(client)}); |
| 1532 | + collector.clients = clients; |
| 1533 | + |
| 1534 | + auto verifyMetricValue = [&](const std::string &metricValue, bool shouldInitializeSocket) { |
| 1535 | + collector.metrics = ""; |
| 1536 | + |
| 1537 | + if (shouldInitializeSocket) { |
| 1538 | + AdminSocketTest asoct(asokc.get()); |
| 1539 | + ASSERT_TRUE(asoct.init(asok_path)); |
| 1540 | + } |
| 1541 | + |
| 1542 | + collector.dump_asok_metrics(true, 5, true, expectedCounterDump, expectedCounterSchema, false); |
| 1543 | + |
| 1544 | + if (shouldInitializeSocket) { |
| 1545 | + AdminSocketTest asoct(asokc.get()); |
| 1546 | + ASSERT_TRUE(asoct.shutdown()); |
| 1547 | + } |
| 1548 | + |
| 1549 | + std::string retrievedMetrics = collector.metrics; |
| 1550 | + std::string pattern = metricName + R"(\{[^}]*ceph_daemon=\")" + daemon + R"(\"[^}]*\}\s+)" + metricValue + R"(\b)"; |
| 1551 | + std::regex regexPattern(pattern); |
| 1552 | + ASSERT_TRUE(std::regex_search(retrievedMetrics, regexPattern)); |
| 1553 | + }; |
| 1554 | + |
| 1555 | + // Test an admin socket not answering: metric value should be "0" |
| 1556 | + verifyMetricValue("0", false); |
| 1557 | + |
| 1558 | + // Test an admin socket answering: metric value should be "1" |
| 1559 | + verifyMetricValue("1", true); |
| 1560 | +} |
0 commit comments