Skip to content

Commit

Permalink
Add Downtime#WasCancelled() & adjust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and julianbrost committed May 13, 2024
1 parent 0ebcc4b commit 454502e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 59 deletions.
5 changes: 5 additions & 0 deletions internal/icinga2/api_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ type Downtime struct {
IsFixed bool `json:"fixed"`
}

// WasCancelled returns true when the current downtime was cancelled prematurely by a user.
func (d *Downtime) WasCancelled() bool {
return d.RemoveTime.Time().After(time.UnixMilli(0))
}

// HostServiceRuntimeAttributes are common attributes of both Host and Service objects.
//
// When catching up potentially missed changes, the following fields are holding relevant changes which, fortunately,
Expand Down
128 changes: 70 additions & 58 deletions internal/icinga2/api_responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestUnixFloat_UnmarshalJSON(t *testing.T) {
{
name: "epoch-time",
jsonData: "0.0",
expected: UnixFloat(time.Time{}),
expected: UnixFloat(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)),
},
{
name: "example-time",
Expand Down Expand Up @@ -128,10 +128,11 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Name: "dummy-11!af73f9d9-2ed8-45f8-b541-cce3f3fe0f6c",
Type: "Downtime",
Attrs: Downtime{
Host: "dummy-11",
Author: "icingaadmin",
Comment: "turn down for what",
IsFixed: true,
Host: "dummy-11",
Author: "icingaadmin",
Comment: "turn down for what",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -144,10 +145,11 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Name: "dummy-7!691d508b-c93f-4565-819c-3e46ffef1555",
Type: "Downtime",
Attrs: Downtime{
Host: "dummy-7",
Author: "icingaadmin",
Comment: "Flexible",
IsFixed: false,
Host: "dummy-7",
Author: "icingaadmin",
Comment: "Flexible",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: false,
},
},
},
Expand All @@ -160,11 +162,12 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Name: "docker-master!disk /!97078a44-8902-495a-9f2a-c1f6802bc63d",
Type: "Downtime",
Attrs: Downtime{
Host: "docker-master",
Service: "disk /",
Author: "icingaadmin",
Comment: "Flexible",
IsFixed: false,
Host: "docker-master",
Service: "disk /",
Author: "icingaadmin",
Comment: "Flexible",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: false,
},
},
},
Expand All @@ -177,11 +180,12 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Name: "docker-master!load!c27b27c2-e0ab-45ff-8b9b-e95f29851eb0",
Type: "Downtime",
Attrs: Downtime{
Host: "docker-master",
Service: "load",
Author: "icingaadmin",
Comment: "Scheduled downtime for backup",
IsFixed: true,
Host: "docker-master",
Service: "load",
Author: "icingaadmin",
Comment: "Scheduled downtime for backup",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -208,7 +212,7 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
LastStateChange: UnixFloat(time.UnixMicro(1697099900637215)),
DowntimeDepth: 0,
Acknowledgement: AcknowledgementNone,
AcknowledgementLastChange: UnixFloat(time.Time{}),
AcknowledgementLastChange: UnixFloat(time.UnixMilli(0)),
},
},
},
Expand Down Expand Up @@ -264,7 +268,7 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
LastStateChange: UnixFloat(time.UnixMicro(1697704135756310)),
DowntimeDepth: 0,
Acknowledgement: AcknowledgementNone,
AcknowledgementLastChange: UnixFloat(time.Time{}),
AcknowledgementLastChange: UnixFloat(time.UnixMilli(0)),
},
},
},
Expand Down Expand Up @@ -451,10 +455,11 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeAdded{
Timestamp: UnixFloat(time.UnixMicro(1697207050511293)),
Downtime: Downtime{
Host: "dummy-157",
Author: "icingaadmin",
Comment: "updates",
IsFixed: true,
Host: "dummy-157",
Author: "icingaadmin",
Comment: "updates",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -464,11 +469,12 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeAdded{
Timestamp: UnixFloat(time.UnixMicro(1697207141217425)),
Downtime: Downtime{
Host: "docker-master",
Service: "http",
Author: "icingaadmin",
Comment: "broken until Monday",
IsFixed: true,
Host: "docker-master",
Service: "http",
Author: "icingaadmin",
Comment: "broken until Monday",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -478,10 +484,11 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeStarted{
Timestamp: UnixFloat(time.UnixMicro(1697207050511378)),
Downtime: Downtime{
Host: "dummy-157",
Author: "icingaadmin",
Comment: "updates",
IsFixed: true,
Host: "dummy-157",
Author: "icingaadmin",
Comment: "updates",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -491,11 +498,12 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeStarted{
Timestamp: UnixFloat(time.UnixMicro(1697207141217507)),
Downtime: Downtime{
Host: "docker-master",
Service: "http",
Author: "icingaadmin",
Comment: "broken until Monday",
IsFixed: true,
Host: "docker-master",
Service: "http",
Author: "icingaadmin",
Comment: "broken until Monday",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -505,10 +513,11 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeTriggered{
Timestamp: UnixFloat(time.UnixMicro(1697207050511608)),
Downtime: Downtime{
Host: "dummy-157",
Author: "icingaadmin",
Comment: "updates",
IsFixed: true,
Host: "dummy-157",
Author: "icingaadmin",
Comment: "updates",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -518,10 +527,11 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeTriggered{
Timestamp: UnixFloat(time.UnixMicro(1714040073242575)),
Downtime: Downtime{
Host: "dummy-7",
Author: "icingaadmin",
Comment: "Flexible",
IsFixed: false,
Host: "dummy-7",
Author: "icingaadmin",
Comment: "Flexible",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: false,
},
},
},
Expand All @@ -531,11 +541,12 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeTriggered{
Timestamp: UnixFloat(time.UnixMicro(1697207141217726)),
Downtime: Downtime{
Host: "docker-master",
Service: "http",
Author: "icingaadmin",
Comment: "broken until Monday",
IsFixed: true,
Host: "docker-master",
Service: "http",
Author: "icingaadmin",
Comment: "broken until Monday",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
},
Expand All @@ -545,11 +556,12 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &DowntimeTriggered{
Timestamp: UnixFloat(time.UnixMicro(1714039143460918)),
Downtime: Downtime{
Host: "docker-master",
Service: "disk /",
Author: "icingaadmin",
Comment: "Flexible",
IsFixed: false,
Host: "docker-master",
Service: "disk /",
Author: "icingaadmin",
Comment: "Flexible",
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: false,
},
},
},
Expand All @@ -562,7 +574,7 @@ func TestApiResponseUnmarshal(t *testing.T) {
Host: "dummy-157",
Author: "icingaadmin",
Comment: "updates",
RemoveTime: UnixFloat(time.Time{}),
RemoveTime: UnixFloat(time.UnixMilli(0)),
IsFixed: true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/icinga2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (client *Client) buildDowntimeEvent(ctx context.Context, d Downtime, startE

if startEvent {
ev.Type = event.TypeDowntimeStart
} else if d.RemoveTime.Time().IsZero() {
} else if !d.WasCancelled() {
ev.Type = event.TypeDowntimeEnd
} else {
ev.Type = event.TypeDowntimeRemoved
Expand Down

0 comments on commit 454502e

Please sign in to comment.