blob: 53f83c3401e1ce6c1367edbf8b167b78239f53e7 [file] [log] [blame]
Tomáš Peckabe797072022-07-15 20:45:52 +02001#include "trompeloeil_doctest.h"
2#include <sysrepo-cpp/Enum.hpp>
3#include <trompeloeil.hpp>
4#include "dbus-helpers/dbus_rauc_server.h"
5#include "health/State.h"
Tomáš Pecka1d2b95c2023-05-11 16:01:46 +02006#include "health/SystemdUnits.h"
Tomáš Peckabe797072022-07-15 20:45:52 +02007#include "health/outputs/AlarmsOutputs.h"
8#include "pretty_printers.h"
9#include "test_log_setup.h"
10#include "test_sysrepo_helpers.h"
11#include "tests/dbus-helpers/dbus_systemd_server.h"
12#include "utils/io.h"
13
14using namespace std::literals;
15
16struct FakeLedCallback {
17 MAKE_CONST_MOCK1(call, void(velia::health::State));
18};
19
20#define EXPECT_COLOUR(STATE) REQUIRE_CALL(fakeLeds, call(STATE)).IN_SEQUENCE(seq1)
21
22TEST_CASE("Test raising alarms lighting LEDs with real sysrepo-ietf-alarmsd server")
23{
24 trompeloeil::sequence seq1;
25
26 TEST_SYSREPO_INIT_LOGS;
27 TEST_SYSREPO_INIT_CLIENT;
28
29 {
30 TEST_SYSREPO_INIT;
31 auto srSessLED = srConn.sessionStart();
32
33 // Create and setup separate connections for both client and server to simulate real-world (systemd server and our client)
34 auto clientConnection = sdbus::createSessionBusConnection();
35 auto serverConnection = sdbus::createSessionBusConnection();
36 clientConnection->enterEventLoopAsync();
37 serverConnection->enterEventLoopAsync();
38 DbusSystemdServer systemdServer(*serverConnection);
39
40 client.switchDatastore(sysrepo::Datastore::Operational);
41 srSess.switchDatastore(sysrepo::Datastore::Operational);
42 srSessLED.switchDatastore(sysrepo::Datastore::Operational);
43
44 systemdServer.createUnit(*serverConnection, "unit1.service", "/org/freedesktop/systemd1/unit/unit1", "active", "running");
45 systemdServer.createUnit(*serverConnection, "unit2.service", "/org/freedesktop/systemd1/unit/unit2", "activating", "auto-restart");
46 systemdServer.createUnit(*serverConnection, "unit3.service", "/org/freedesktop/systemd1/unit/unit3", "failed", "failed");
47
48 FakeLedCallback fakeLeds;
49 velia::health::SystemdUnits systemdAlarms(srSess, *clientConnection, serverConnection->getUniqueName(), "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "org.freedesktop.systemd1.Unit");
50
51 std::this_thread::sleep_for(100ms); // let's wait for some time so the alarm-inventory is populated
52
53 REQUIRE(dataFromSysrepo(client, "/ietf-alarms:alarms/alarm-inventory", sysrepo::Datastore::Operational) == std::map<std::string, std::string>{
54 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']", ""},
55 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/alarm-type-id", "velia-alarms:systemd-unit-failure"},
56 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/alarm-type-qualifier", ""},
57 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/description", "The systemd service is considered in failed state."},
58 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/resource[1]", "unit1.service"},
59 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/resource[2]", "unit2.service"},
60 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/resource[3]", "unit3.service"},
61 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/severity-level[1]", "critical"},
62 {"/alarm-type[alarm-type-id='velia-alarms:systemd-unit-failure'][alarm-type-qualifier='']/will-clear", "true"},
63 });
64
65 EXPECT_COLOUR(velia::health::State::ERROR);
66 velia::health::AlarmsOutputs alarms(srSessLED, {[&](velia::health::State state) { fakeLeds.call(state); }});
67
68 EXPECT_COLOUR(velia::health::State::ERROR);
69 systemdServer.changeUnitState("/org/freedesktop/systemd1/unit/unit2", "active", "running");
70
71 EXPECT_COLOUR(velia::health::State::ERROR);
72 systemdServer.changeUnitState("/org/freedesktop/systemd1/unit/unit1", "activating", "auto-restart");
73
74 EXPECT_COLOUR(velia::health::State::ERROR);
75 systemdServer.changeUnitState("/org/freedesktop/systemd1/unit/unit3", "active", "running");
76
77 EXPECT_COLOUR(velia::health::State::OK);
78 systemdServer.changeUnitState("/org/freedesktop/systemd1/unit/unit1", "active", "running");
79
80 EXPECT_COLOUR(velia::health::State::ERROR);
81 systemdServer.changeUnitState("/org/freedesktop/systemd1/unit/unit3", "failed", "failed");
82
83 waitForCompletionAndBitMore(seq1);
84 }
85
86 REQUIRE(!client.getData("/ietf-alarms:alarms/alarm-inventory/*"));
87}