-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLOUDTRUST-5196] Add health check for AuditEventsReporterModule (#126)
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package healthcheck | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cloudtrust/common-service/v2/events" | ||
log "github.com/cloudtrust/common-service/v2/log" | ||
) | ||
|
||
type auditEventsReporterChecker struct { | ||
alias string | ||
reporter events.AuditEventsReporterModule | ||
timeout time.Duration | ||
response HealthStatus | ||
logger log.Logger | ||
} | ||
|
||
func newAuditEventsReporterChecker(alias string, reporter events.AuditEventsReporterModule, timeout time.Duration, cacheDuration time.Duration, logger log.Logger) BasicChecker { | ||
healthStatusType := "auditEventreporter" | ||
response := HealthStatus{Name: &alias, Type: &healthStatusType, CacheDuration: cacheDuration} | ||
response.connection("init") | ||
response.stateUp() | ||
return &auditEventsReporterChecker{ | ||
alias: alias, | ||
reporter: reporter, | ||
timeout: timeout, | ||
response: response, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (a *auditEventsReporterChecker) CheckStatus() HealthStatus { | ||
if a.response.hasExpired() { | ||
go a.updateStatus() | ||
} | ||
|
||
return a.response | ||
} | ||
|
||
func (a *auditEventsReporterChecker) updateStatus() { | ||
finished := make(chan bool) | ||
go func() { | ||
event := events.NewEvent("healthcheck", "", "master", "health_checker", "health_checker", "master", map[string]string{}) | ||
a.reporter.ReportEvent(context.Background(), event) | ||
finished <- true | ||
}() | ||
|
||
select { | ||
case <-finished: | ||
a.response.connection("established") | ||
a.response.stateUp() | ||
case <-time.After(a.timeout): | ||
a.response.stateDown("Events reporter timeout") | ||
a.logger.Error(context.Background(), "msg", "Audit Events Reporter timeout to produce events", "timeout", a.timeout) | ||
} | ||
|
||
a.response.touch() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package healthcheck | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cloudtrust/common-service/v2/healthcheck/mock" | ||
log "github.com/cloudtrust/common-service/v2/log" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAuditEventsReporterChecker(t *testing.T) { | ||
var mockCtrl = gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
var mockAuditEventReporter = mock.NewAuditEventsReporterModule(mockCtrl) | ||
|
||
t.Run("Success ", func(t *testing.T) { | ||
var auditEventReporterChecker = newAuditEventsReporterChecker("alias", mockAuditEventReporter, 10*time.Second, 10*time.Second, log.NewNopLogger()) | ||
internalChecker := auditEventReporterChecker.(*auditEventsReporterChecker) | ||
mockAuditEventReporter.EXPECT().ReportEvent(gomock.Any(), gomock.Any()).Times(1) | ||
|
||
internalChecker.updateStatus() | ||
var res = internalChecker.response | ||
assert.NotNil(t, res.Connection) | ||
assert.Equal(t, "established", *res.Connection) | ||
}) | ||
|
||
t.Run("Failure ", func(t *testing.T) { | ||
var auditEventReporterChecker = newAuditEventsReporterChecker("alias", mockAuditEventReporter, 1*time.Second, 10*time.Second, log.NewNopLogger()) | ||
internalChecker := auditEventReporterChecker.(*auditEventsReporterChecker) | ||
mockAuditEventReporter.EXPECT().ReportEvent(gomock.Any(), gomock.Any()).Do(func(arg0 interface{}, arg1 interface{}) { | ||
time.Sleep(2 * time.Second) | ||
}) | ||
|
||
internalChecker.updateStatus() | ||
res := internalChecker.response | ||
assert.NotNil(t, res.Message) | ||
assert.Equal(t, "Events reporter timeout", *res.Message) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
package healthcheck | ||
|
||
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/healthcheck.go -package=mock -mock_names=HealthDatabase=HealthDatabase github.com/cloudtrust/common-service/v2/healthcheck HealthDatabase | ||
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/eventsreportermodule.go -package=mock -mock_names=AuditEventsReporterModule=AuditEventsReporterModule github.com/cloudtrust/common-service/v2/events AuditEventsReporterModule |