Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_alert_types.cpp File Reference
#include <gtest/gtest.h>
#include <kcenon/monitoring/alert/alert_types.h>
#include <chrono>
#include <thread>
Include dependency graph for test_alert_types.cpp:

Go to the source code of this file.

Classes

class  AlertLabelsTest
 
class  AlertTest
 
class  AlertTransitionTest
 
class  AlertGroupTest
 
class  AlertSilenceTest
 

Functions

 TEST (AlertSeverityTest, ToStringConversions)
 
 TEST (AlertSeverityTest, OrderingByValue)
 
 TEST (AlertStateTest, ToStringConversions)
 
 TEST_F (AlertLabelsTest, DefaultConstructionIsEmpty)
 
 TEST_F (AlertLabelsTest, ConstructFromMap)
 
 TEST_F (AlertLabelsTest, SetAndGet)
 
 TEST_F (AlertLabelsTest, GetNonexistentReturnsEmpty)
 
 TEST_F (AlertLabelsTest, HasExistingKey)
 
 TEST_F (AlertLabelsTest, SetOverwritesExisting)
 
 TEST_F (AlertLabelsTest, FingerprintIsDeterministic)
 
 TEST_F (AlertLabelsTest, FingerprintDiffersForDifferentValues)
 
 TEST_F (AlertLabelsTest, EqualityOperator)
 
 TEST_F (AlertLabelsTest, InequalityWhenDifferent)
 
 TEST (AlertAnnotationsTest, DefaultConstruction)
 
 TEST (AlertAnnotationsTest, ConstructWithSummaryAndDescription)
 
 TEST (AlertAnnotationsTest, RunbookUrl)
 
 TEST (AlertAnnotationsTest, CustomAnnotations)
 
 TEST_F (AlertTest, DefaultConstruction)
 
 TEST_F (AlertTest, ConstructWithNameAndLabels)
 
 TEST_F (AlertTest, UniqueIds)
 
 TEST_F (AlertTest, FingerprintIncludesNameAndLabels)
 
 TEST_F (AlertTest, FingerprintConsistency)
 
 TEST_F (AlertTest, IsActiveForPendingAndFiring)
 
 TEST_F (AlertTest, StateDurationIsPositive)
 
 TEST_F (AlertTest, FiringDurationZeroWhenNotFiring)
 
 TEST_F (AlertTransitionTest, InactiveToLending)
 
 TEST_F (AlertTransitionTest, InactiveToFiringInvalid)
 
 TEST_F (AlertTransitionTest, InactiveToResolvedInvalid)
 
 TEST_F (AlertTransitionTest, PendingToFiring)
 
 TEST_F (AlertTransitionTest, PendingToInactive)
 
 TEST_F (AlertTransitionTest, FiringToResolved)
 
 TEST_F (AlertTransitionTest, FiringToPendingInvalid)
 
 TEST_F (AlertTransitionTest, ResolvedToPending)
 
 TEST_F (AlertTransitionTest, ResolvedToInactive)
 
 TEST_F (AlertTransitionTest, AnyStateToSuppressed)
 
 TEST_F (AlertTransitionTest, SuppressedToAnyState)
 
 TEST_F (AlertTransitionTest, FullLifecycle)
 
 TEST_F (AlertTransitionTest, FiringStartedAtSetOnlyOnce)
 
 TEST_F (AlertTransitionTest, UpdatedAtChangesOnTransition)
 
 TEST_F (AlertGroupTest, DefaultConstruction)
 
 TEST_F (AlertGroupTest, ConstructWithKey)
 
 TEST_F (AlertGroupTest, AddAlert)
 
 TEST_F (AlertGroupTest, AddMultipleAlerts)
 
 TEST_F (AlertGroupTest, MaxSeverityEmptyGroup)
 
 TEST_F (AlertGroupTest, MaxSeveritySingleAlert)
 
 TEST_F (AlertGroupTest, MaxSeverityMultipleAlerts)
 
 TEST_F (AlertGroupTest, UpdatedAtChangesOnAdd)
 
 TEST_F (AlertSilenceTest, DefaultConstructionIsActive)
 
 TEST_F (AlertSilenceTest, UniqueIds)
 
 TEST_F (AlertSilenceTest, ExpiredSilenceNotActive)
 
 TEST_F (AlertSilenceTest, FutureSilenceNotActive)
 
 TEST_F (AlertSilenceTest, MatchesAlertWithMatchingLabels)
 
 TEST_F (AlertSilenceTest, DoesNotMatchAlertWithDifferentLabels)
 
 TEST_F (AlertSilenceTest, DoesNotMatchAlertMissingLabel)
 
 TEST_F (AlertSilenceTest, EmptyMatchersMatchesAll)
 
 TEST_F (AlertSilenceTest, ExpiredSilenceDoesNotMatch)
 
 TEST_F (AlertSilenceTest, MultipleMatchersMustAllMatch)
 
 TEST_F (AlertSilenceTest, CommentAndCreatedBy)
 

Function Documentation

◆ TEST() [1/7]

TEST ( AlertAnnotationsTest ,
ConstructWithSummaryAndDescription  )

Definition at line 157 of file test_alert_types.cpp.

157 {
158 alert_annotations ann("High CPU", "CPU usage exceeded 80%");
159 EXPECT_EQ(ann.summary, "High CPU");
160 EXPECT_EQ(ann.description, "CPU usage exceeded 80%");
161}
Additional metadata for alert context.

References kcenon::monitoring::alert_annotations::description, and kcenon::monitoring::alert_annotations::summary.

◆ TEST() [2/7]

TEST ( AlertAnnotationsTest ,
CustomAnnotations  )

Definition at line 170 of file test_alert_types.cpp.

170 {
172 ann.custom["dashboard"] = "grafana/cpu";
173 EXPECT_EQ(ann.custom.at("dashboard"), "grafana/cpu");
174}
std::unordered_map< std::string, std::string > custom
Custom annotations.

References kcenon::monitoring::alert_annotations::custom.

◆ TEST() [3/7]

TEST ( AlertAnnotationsTest ,
DefaultConstruction  )

Definition at line 149 of file test_alert_types.cpp.

149 {
151 EXPECT_TRUE(ann.summary.empty());
152 EXPECT_TRUE(ann.description.empty());
153 EXPECT_FALSE(ann.runbook_url.has_value());
154 EXPECT_TRUE(ann.custom.empty());
155}
std::string description
Detailed description.
std::string summary
Brief description.
std::optional< std::string > runbook_url
Link to runbook.

References kcenon::monitoring::alert_annotations::custom, kcenon::monitoring::alert_annotations::description, kcenon::monitoring::alert_annotations::runbook_url, and kcenon::monitoring::alert_annotations::summary.

◆ TEST() [4/7]

TEST ( AlertAnnotationsTest ,
RunbookUrl  )

Definition at line 163 of file test_alert_types.cpp.

163 {
165 ann.runbook_url = "https://runbooks.example.com/cpu";
166 EXPECT_TRUE(ann.runbook_url.has_value());
167 EXPECT_EQ(*ann.runbook_url, "https://runbooks.example.com/cpu");
168}

References kcenon::monitoring::alert_annotations::runbook_url.

◆ TEST() [5/7]

TEST ( AlertSeverityTest ,
OrderingByValue  )

Definition at line 37 of file test_alert_types.cpp.

37 {
38 EXPECT_LT(static_cast<uint8_t>(alert_severity::info),
39 static_cast<uint8_t>(alert_severity::warning));
40 EXPECT_LT(static_cast<uint8_t>(alert_severity::warning),
41 static_cast<uint8_t>(alert_severity::critical));
42 EXPECT_LT(static_cast<uint8_t>(alert_severity::critical),
43 static_cast<uint8_t>(alert_severity::emergency));
44}

◆ TEST() [6/7]

TEST ( AlertSeverityTest ,
ToStringConversions  )

Definition at line 30 of file test_alert_types.cpp.

30 {
31 EXPECT_STREQ(alert_severity_to_string(alert_severity::info), "info");
32 EXPECT_STREQ(alert_severity_to_string(alert_severity::warning), "warning");
33 EXPECT_STREQ(alert_severity_to_string(alert_severity::critical), "critical");
34 EXPECT_STREQ(alert_severity_to_string(alert_severity::emergency), "emergency");
35}
constexpr const char * alert_severity_to_string(alert_severity severity) noexcept
Convert alert severity to string.
Definition alert_types.h:47

References kcenon::monitoring::alert_severity_to_string().

Here is the call graph for this function:

◆ TEST() [7/7]

TEST ( AlertStateTest ,
ToStringConversions  )

Definition at line 50 of file test_alert_types.cpp.

50 {
51 EXPECT_STREQ(alert_state_to_string(alert_state::inactive), "inactive");
52 EXPECT_STREQ(alert_state_to_string(alert_state::pending), "pending");
53 EXPECT_STREQ(alert_state_to_string(alert_state::firing), "firing");
54 EXPECT_STREQ(alert_state_to_string(alert_state::resolved), "resolved");
55 EXPECT_STREQ(alert_state_to_string(alert_state::suppressed), "suppressed");
56}
constexpr const char * alert_state_to_string(alert_state state) noexcept
Convert alert state to string.
Definition alert_types.h:82

References kcenon::monitoring::alert_state_to_string().

Here is the call graph for this function:

◆ TEST_F() [1/51]

TEST_F ( AlertGroupTest ,
AddAlert  )

Definition at line 392 of file test_alert_types.cpp.

392 {
393 alert a("test", alert_labels{});
394 group_.add_alert(a);
395 EXPECT_EQ(group_.size(), 1u);
396 EXPECT_FALSE(group_.empty());
397}
Key-value labels for alert identification and routing.
Core alert data structure.

◆ TEST_F() [2/51]

TEST_F ( AlertGroupTest ,
AddMultipleAlerts  )

Definition at line 399 of file test_alert_types.cpp.

399 {
400 for (int i = 0; i < 5; ++i) {
401 alert a("alert_" + std::to_string(i), alert_labels{});
402 group_.add_alert(a);
403 }
404 EXPECT_EQ(group_.size(), 5u);
405}

◆ TEST_F() [3/51]

TEST_F ( AlertGroupTest ,
ConstructWithKey  )

Definition at line 387 of file test_alert_types.cpp.

387 {
388 EXPECT_EQ(group_.group_key, "test_group");
389 EXPECT_TRUE(group_.empty());
390}

◆ TEST_F() [4/51]

TEST_F ( AlertGroupTest ,
DefaultConstruction  )

Definition at line 380 of file test_alert_types.cpp.

380 {
381 alert_group g;
382 EXPECT_TRUE(g.group_key.empty());
383 EXPECT_TRUE(g.empty());
384 EXPECT_EQ(g.size(), 0u);
385}
Group of related alerts for batch notification.
std::string group_key
Common grouping key.
bool empty() const
Check if group is empty.
size_t size() const
Get count of alerts in the group.

References kcenon::monitoring::alert_group::empty(), kcenon::monitoring::alert_group::group_key, and kcenon::monitoring::alert_group::size().

Here is the call graph for this function:

◆ TEST_F() [5/51]

TEST_F ( AlertGroupTest ,
MaxSeverityEmptyGroup  )

Definition at line 407 of file test_alert_types.cpp.

407 {
408 EXPECT_EQ(group_.max_severity(), alert_severity::info);
409}

◆ TEST_F() [6/51]

TEST_F ( AlertGroupTest ,
MaxSeverityMultipleAlerts  )

Definition at line 418 of file test_alert_types.cpp.

418 {
419 alert a1;
420 a1.severity = alert_severity::info;
421 group_.add_alert(a1);
422
423 alert a2;
424 a2.severity = alert_severity::emergency;
425 group_.add_alert(a2);
426
427 alert a3;
428 a3.severity = alert_severity::warning;
429 group_.add_alert(a3);
430
431 EXPECT_EQ(group_.max_severity(), alert_severity::emergency);
432}
alert_severity severity
Alert severity level.

References kcenon::monitoring::alert::severity.

◆ TEST_F() [7/51]

TEST_F ( AlertGroupTest ,
MaxSeveritySingleAlert  )

Definition at line 411 of file test_alert_types.cpp.

411 {
412 alert a;
413 a.severity = alert_severity::critical;
414 group_.add_alert(a);
415 EXPECT_EQ(group_.max_severity(), alert_severity::critical);
416}

References kcenon::monitoring::alert::severity.

◆ TEST_F() [8/51]

TEST_F ( AlertGroupTest ,
UpdatedAtChangesOnAdd  )

Definition at line 434 of file test_alert_types.cpp.

434 {
435 auto initial = group_.updated_at;
436 std::this_thread::sleep_for(1ms);
437 alert a;
438 group_.add_alert(a);
439 EXPECT_GT(group_.updated_at, initial);
440}

◆ TEST_F() [9/51]

TEST_F ( AlertLabelsTest ,
ConstructFromMap  )

Definition at line 71 of file test_alert_types.cpp.

71 {
72 std::unordered_map<std::string, std::string> map{
73 {"env", "production"}, {"service", "api"}};
74 alert_labels lbl(map);
75 EXPECT_EQ(lbl.labels.size(), 2u);
76 EXPECT_EQ(lbl.get("env"), "production");
77}

References kcenon::monitoring::alert_labels::get(), and kcenon::monitoring::alert_labels::labels.

Here is the call graph for this function:

◆ TEST_F() [10/51]

TEST_F ( AlertLabelsTest ,
DefaultConstructionIsEmpty  )

Definition at line 67 of file test_alert_types.cpp.

67 {
68 EXPECT_TRUE(labels_.labels.empty());
69}

◆ TEST_F() [11/51]

TEST_F ( AlertLabelsTest ,
EqualityOperator  )

Definition at line 125 of file test_alert_types.cpp.

125 {
126 labels_.set("a", "1");
127 labels_.set("b", "2");
128
130 other.set("a", "1");
131 other.set("b", "2");
132
133 EXPECT_EQ(labels_, other);
134}
void set(const std::string &key, const std::string &value)
Add or update a label.

References kcenon::monitoring::other, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [12/51]

TEST_F ( AlertLabelsTest ,
FingerprintDiffersForDifferentValues  )

Definition at line 114 of file test_alert_types.cpp.

114 {
115 labels_.set("key", "value1");
116 auto fp1 = labels_.fingerprint();
117
119 other.set("key", "value2");
120 auto fp2 = other.fingerprint();
121
122 EXPECT_NE(fp1, fp2);
123}

References kcenon::monitoring::other, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [13/51]

TEST_F ( AlertLabelsTest ,
FingerprintIsDeterministic  )

Definition at line 100 of file test_alert_types.cpp.

100 {
101 labels_.set("b", "2");
102 labels_.set("a", "1");
103 auto fp1 = labels_.fingerprint();
104
106 other.set("a", "1");
107 other.set("b", "2");
108 auto fp2 = other.fingerprint();
109
110 // Sorted order: a=1,b=2, regardless of insertion order
111 EXPECT_EQ(fp1, fp2);
112}

References kcenon::monitoring::other, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [14/51]

TEST_F ( AlertLabelsTest ,
GetNonexistentReturnsEmpty  )

Definition at line 84 of file test_alert_types.cpp.

84 {
85 EXPECT_EQ(labels_.get("missing"), "");
86}

◆ TEST_F() [15/51]

TEST_F ( AlertLabelsTest ,
HasExistingKey  )

Definition at line 88 of file test_alert_types.cpp.

88 {
89 labels_.set("region", "us-east");
90 EXPECT_TRUE(labels_.has("region"));
91 EXPECT_FALSE(labels_.has("zone"));
92}

◆ TEST_F() [16/51]

TEST_F ( AlertLabelsTest ,
InequalityWhenDifferent  )

Definition at line 136 of file test_alert_types.cpp.

136 {
137 labels_.set("a", "1");
138
140 other.set("a", "2");
141
142 EXPECT_FALSE(labels_ == other);
143}

References kcenon::monitoring::other, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [17/51]

TEST_F ( AlertLabelsTest ,
SetAndGet  )

Definition at line 79 of file test_alert_types.cpp.

79 {
80 labels_.set("team", "infra");
81 EXPECT_EQ(labels_.get("team"), "infra");
82}

◆ TEST_F() [18/51]

TEST_F ( AlertLabelsTest ,
SetOverwritesExisting  )

Definition at line 94 of file test_alert_types.cpp.

94 {
95 labels_.set("env", "staging");
96 labels_.set("env", "production");
97 EXPECT_EQ(labels_.get("env"), "production");
98}

◆ TEST_F() [19/51]

TEST_F ( AlertSilenceTest ,
CommentAndCreatedBy  )

Definition at line 535 of file test_alert_types.cpp.

535 {
536 silence_.comment = "Maintenance window";
537 silence_.created_by = "admin@example.com";
538 EXPECT_EQ(silence_.comment, "Maintenance window");
539 EXPECT_EQ(silence_.created_by, "admin@example.com");
540}

◆ TEST_F() [20/51]

TEST_F ( AlertSilenceTest ,
DefaultConstructionIsActive  )

Definition at line 451 of file test_alert_types.cpp.

451 {
452 // Default: starts_at = now, ends_at = now + 1 hour
453 EXPECT_TRUE(silence_.is_active());
454}

◆ TEST_F() [21/51]

TEST_F ( AlertSilenceTest ,
DoesNotMatchAlertMissingLabel  )

Definition at line 493 of file test_alert_types.cpp.

493 {
494 silence_.matchers.set("service", "api");
495
496 alert a;
497 a.labels.set("env", "prod");
498
499 EXPECT_FALSE(silence_.matches(a));
500}
alert_labels labels
Identifying labels.

References kcenon::monitoring::alert::labels, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [22/51]

TEST_F ( AlertSilenceTest ,
DoesNotMatchAlertWithDifferentLabels  )

Definition at line 484 of file test_alert_types.cpp.

484 {
485 silence_.matchers.set("service", "api");
486
487 alert a;
488 a.labels.set("service", "web");
489
490 EXPECT_FALSE(silence_.matches(a));
491}

References kcenon::monitoring::alert::labels, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [23/51]

TEST_F ( AlertSilenceTest ,
EmptyMatchersMatchesAll  )

Definition at line 502 of file test_alert_types.cpp.

502 {
503 // No matcher labels means all alerts match
504 alert a;
505 a.labels.set("anything", "value");
506 EXPECT_TRUE(silence_.matches(a));
507}

References kcenon::monitoring::alert::labels, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [24/51]

TEST_F ( AlertSilenceTest ,
ExpiredSilenceDoesNotMatch  )

Definition at line 509 of file test_alert_types.cpp.

509 {
510 silence_.matchers.set("service", "api");
511 silence_.starts_at = std::chrono::steady_clock::now() - 2h;
512 silence_.ends_at = std::chrono::steady_clock::now() - 1h;
513
514 alert a;
515 a.labels.set("service", "api");
516
517 EXPECT_FALSE(silence_.matches(a));
518}

References kcenon::monitoring::alert::labels, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [25/51]

TEST_F ( AlertSilenceTest ,
ExpiredSilenceNotActive  )

Definition at line 462 of file test_alert_types.cpp.

462 {
463 silence_.starts_at = std::chrono::steady_clock::now() - 2h;
464 silence_.ends_at = std::chrono::steady_clock::now() - 1h;
465 EXPECT_FALSE(silence_.is_active());
466}

◆ TEST_F() [26/51]

TEST_F ( AlertSilenceTest ,
FutureSilenceNotActive  )

Definition at line 468 of file test_alert_types.cpp.

468 {
469 silence_.starts_at = std::chrono::steady_clock::now() + 1h;
470 silence_.ends_at = std::chrono::steady_clock::now() + 2h;
471 EXPECT_FALSE(silence_.is_active());
472}

◆ TEST_F() [27/51]

TEST_F ( AlertSilenceTest ,
MatchesAlertWithMatchingLabels  )

Definition at line 474 of file test_alert_types.cpp.

474 {
475 silence_.matchers.set("service", "api");
476
477 alert a;
478 a.labels.set("service", "api");
479 a.labels.set("env", "prod");
480
481 EXPECT_TRUE(silence_.matches(a));
482}

References kcenon::monitoring::alert::labels, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [28/51]

TEST_F ( AlertSilenceTest ,
MultipleMatchersMustAllMatch  )

Definition at line 520 of file test_alert_types.cpp.

520 {
521 silence_.matchers.set("service", "api");
522 silence_.matchers.set("env", "prod");
523
524 alert a1;
525 a1.labels.set("service", "api");
526 a1.labels.set("env", "prod");
527 EXPECT_TRUE(silence_.matches(a1));
528
529 alert a2;
530 a2.labels.set("service", "api");
531 a2.labels.set("env", "staging");
532 EXPECT_FALSE(silence_.matches(a2));
533}

References kcenon::monitoring::alert::labels, and kcenon::monitoring::alert_labels::set().

Here is the call graph for this function:

◆ TEST_F() [29/51]

TEST_F ( AlertSilenceTest ,
UniqueIds  )

Definition at line 456 of file test_alert_types.cpp.

456 {
457 alert_silence s1;
458 alert_silence s2;
459 EXPECT_NE(s1.id, s2.id);
460}
Silence configuration to suppress alerts.

References kcenon::monitoring::alert_silence::id.

◆ TEST_F() [30/51]

TEST_F ( AlertTest ,
ConstructWithNameAndLabels  )

Definition at line 203 of file test_alert_types.cpp.

203 {
204 auto a = create_test_alert();
205 EXPECT_EQ(a.name, "high_cpu");
206 EXPECT_EQ(a.labels.get("service"), "api");
207 EXPECT_EQ(a.severity, alert_severity::critical);
208}

◆ TEST_F() [31/51]

TEST_F ( AlertTest ,
DefaultConstruction  )

Definition at line 193 of file test_alert_types.cpp.

193 {
194 alert a;
195 EXPECT_TRUE(a.name.empty());
196 EXPECT_EQ(a.state, alert_state::inactive);
197 EXPECT_EQ(a.severity, alert_severity::warning);
198 EXPECT_EQ(a.value, 0.0);
199 EXPECT_FALSE(a.started_at.has_value());
200 EXPECT_FALSE(a.resolved_at.has_value());
201}
alert_state state
Current state.
std::optional< std::chrono::steady_clock::time_point > started_at
When firing started.
double value
Current metric value.
std::optional< std::chrono::steady_clock::time_point > resolved_at
When resolved.
std::string name
Alert name/identifier.

References kcenon::monitoring::alert::name, kcenon::monitoring::alert::resolved_at, kcenon::monitoring::alert::severity, kcenon::monitoring::alert::started_at, kcenon::monitoring::alert::state, and kcenon::monitoring::alert::value.

◆ TEST_F() [32/51]

TEST_F ( AlertTest ,
FingerprintConsistency  )

Definition at line 223 of file test_alert_types.cpp.

223 {
224 auto a1 = create_test_alert();
225 auto a2 = create_test_alert();
226 // Same name + same labels = same fingerprint (for dedup)
227 EXPECT_EQ(a1.fingerprint(), a2.fingerprint());
228}

◆ TEST_F() [33/51]

TEST_F ( AlertTest ,
FingerprintIncludesNameAndLabels  )

Definition at line 216 of file test_alert_types.cpp.

216 {
217 auto a = create_test_alert();
218 auto fp = a.fingerprint();
219 EXPECT_FALSE(fp.empty());
220 EXPECT_NE(fp.find("high_cpu"), std::string::npos);
221}

◆ TEST_F() [34/51]

TEST_F ( AlertTest ,
FiringDurationZeroWhenNotFiring  )

Definition at line 255 of file test_alert_types.cpp.

255 {
256 alert a;
257 EXPECT_EQ(a.firing_duration().count(), 0);
258}
std::chrono::steady_clock::duration firing_duration() const
Get firing duration (if currently firing)

References kcenon::monitoring::alert::firing_duration().

Here is the call graph for this function:

◆ TEST_F() [35/51]

TEST_F ( AlertTest ,
IsActiveForPendingAndFiring  )

Definition at line 230 of file test_alert_types.cpp.

230 {
231 alert a;
232 EXPECT_FALSE(a.is_active()); // inactive
233
234 a.state = alert_state::pending;
235 EXPECT_TRUE(a.is_active());
236
237 a.state = alert_state::firing;
238 EXPECT_TRUE(a.is_active());
239
240 a.state = alert_state::resolved;
241 EXPECT_FALSE(a.is_active());
242
243 a.state = alert_state::suppressed;
244 EXPECT_FALSE(a.is_active());
245}
bool is_active() const
Check if alert is currently active (pending or firing)

References kcenon::monitoring::alert::is_active(), and kcenon::monitoring::alert::state.

Here is the call graph for this function:

◆ TEST_F() [36/51]

TEST_F ( AlertTest ,
StateDurationIsPositive  )

Definition at line 247 of file test_alert_types.cpp.

247 {
248 alert a;
249 // Sleep briefly to ensure non-zero duration
250 std::this_thread::sleep_for(1ms);
251 auto dur = a.state_duration();
252 EXPECT_GT(dur.count(), 0);
253}
std::chrono::steady_clock::duration state_duration() const
Get duration in current state.

References kcenon::monitoring::alert::state_duration().

Here is the call graph for this function:

◆ TEST_F() [37/51]

TEST_F ( AlertTest ,
UniqueIds  )

Definition at line 210 of file test_alert_types.cpp.

210 {
211 alert a1;
212 alert a2;
213 EXPECT_NE(a1.id, a2.id);
214}
uint64_t id
Unique alert ID.

References kcenon::monitoring::alert::id.

◆ TEST_F() [38/51]

TEST_F ( AlertTransitionTest ,
AnyStateToSuppressed  )

Definition at line 327 of file test_alert_types.cpp.

327 {
328 EXPECT_TRUE(a_.transition_to(alert_state::suppressed));
329 EXPECT_EQ(a_.state, alert_state::suppressed);
330}

◆ TEST_F() [39/51]

TEST_F ( AlertTransitionTest ,
FiringStartedAtSetOnlyOnce  )

Definition at line 350 of file test_alert_types.cpp.

350 {
351 a_.transition_to(alert_state::pending);
352 a_.transition_to(alert_state::firing);
353 auto first_started = a_.started_at;
354
355 // Resolve and re-fire
356 a_.transition_to(alert_state::resolved);
357 a_.transition_to(alert_state::pending);
358 a_.transition_to(alert_state::firing);
359
360 // started_at should remain the same (first firing)
361 EXPECT_EQ(a_.started_at, first_started);
362}

◆ TEST_F() [40/51]

TEST_F ( AlertTransitionTest ,
FiringToPendingInvalid  )

Definition at line 305 of file test_alert_types.cpp.

305 {
306 a_.transition_to(alert_state::pending);
307 a_.transition_to(alert_state::firing);
308 EXPECT_FALSE(a_.transition_to(alert_state::pending));
309 EXPECT_EQ(a_.state, alert_state::firing);
310}

◆ TEST_F() [41/51]

TEST_F ( AlertTransitionTest ,
FiringToResolved  )

Definition at line 297 of file test_alert_types.cpp.

297 {
298 a_.transition_to(alert_state::pending);
299 a_.transition_to(alert_state::firing);
300 EXPECT_TRUE(a_.transition_to(alert_state::resolved));
301 EXPECT_EQ(a_.state, alert_state::resolved);
302 EXPECT_TRUE(a_.resolved_at.has_value());
303}

◆ TEST_F() [42/51]

TEST_F ( AlertTransitionTest ,
FullLifecycle  )

Definition at line 340 of file test_alert_types.cpp.

340 {
341 // inactive -> pending -> firing -> resolved -> pending -> firing -> resolved
342 EXPECT_TRUE(a_.transition_to(alert_state::pending));
343 EXPECT_TRUE(a_.transition_to(alert_state::firing));
344 EXPECT_TRUE(a_.transition_to(alert_state::resolved));
345 EXPECT_TRUE(a_.transition_to(alert_state::pending));
346 EXPECT_TRUE(a_.transition_to(alert_state::firing));
347 EXPECT_TRUE(a_.transition_to(alert_state::resolved));
348}

◆ TEST_F() [43/51]

TEST_F ( AlertTransitionTest ,
InactiveToFiringInvalid  )

Definition at line 274 of file test_alert_types.cpp.

274 {
275 EXPECT_FALSE(a_.transition_to(alert_state::firing));
276 EXPECT_EQ(a_.state, alert_state::inactive);
277}

◆ TEST_F() [44/51]

TEST_F ( AlertTransitionTest ,
InactiveToLending  )

Definition at line 269 of file test_alert_types.cpp.

269 {
270 EXPECT_TRUE(a_.transition_to(alert_state::pending));
271 EXPECT_EQ(a_.state, alert_state::pending);
272}

◆ TEST_F() [45/51]

TEST_F ( AlertTransitionTest ,
InactiveToResolvedInvalid  )

Definition at line 279 of file test_alert_types.cpp.

279 {
280 EXPECT_FALSE(a_.transition_to(alert_state::resolved));
281 EXPECT_EQ(a_.state, alert_state::inactive);
282}

◆ TEST_F() [46/51]

TEST_F ( AlertTransitionTest ,
PendingToFiring  )

Definition at line 284 of file test_alert_types.cpp.

284 {
285 a_.transition_to(alert_state::pending);
286 EXPECT_TRUE(a_.transition_to(alert_state::firing));
287 EXPECT_EQ(a_.state, alert_state::firing);
288 EXPECT_TRUE(a_.started_at.has_value());
289}

◆ TEST_F() [47/51]

TEST_F ( AlertTransitionTest ,
PendingToInactive  )

Definition at line 291 of file test_alert_types.cpp.

291 {
292 a_.transition_to(alert_state::pending);
293 EXPECT_TRUE(a_.transition_to(alert_state::inactive));
294 EXPECT_EQ(a_.state, alert_state::inactive);
295}

◆ TEST_F() [48/51]

TEST_F ( AlertTransitionTest ,
ResolvedToInactive  )

Definition at line 320 of file test_alert_types.cpp.

320 {
321 a_.transition_to(alert_state::pending);
322 a_.transition_to(alert_state::firing);
323 a_.transition_to(alert_state::resolved);
324 EXPECT_TRUE(a_.transition_to(alert_state::inactive));
325}

◆ TEST_F() [49/51]

TEST_F ( AlertTransitionTest ,
ResolvedToPending  )

Definition at line 312 of file test_alert_types.cpp.

312 {
313 a_.transition_to(alert_state::pending);
314 a_.transition_to(alert_state::firing);
315 a_.transition_to(alert_state::resolved);
316 EXPECT_TRUE(a_.transition_to(alert_state::pending));
317 EXPECT_EQ(a_.state, alert_state::pending);
318}

◆ TEST_F() [50/51]

TEST_F ( AlertTransitionTest ,
SuppressedToAnyState  )

Definition at line 332 of file test_alert_types.cpp.

332 {
333 a_.transition_to(alert_state::suppressed);
334
335 // From suppressed, all transitions should be valid
336 EXPECT_TRUE(a_.transition_to(alert_state::firing));
337 EXPECT_EQ(a_.state, alert_state::firing);
338}

◆ TEST_F() [51/51]

TEST_F ( AlertTransitionTest ,
UpdatedAtChangesOnTransition  )

Definition at line 364 of file test_alert_types.cpp.

364 {
365 auto initial_updated = a_.updated_at;
366 std::this_thread::sleep_for(1ms);
367 a_.transition_to(alert_state::pending);
368 EXPECT_GT(a_.updated_at, initial_updated);
369}