Skip to content

Commit

Permalink
fix(siren): provider plugin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh committed Sep 24, 2023
1 parent 48505b2 commit 1428d7d
Show file tree
Hide file tree
Showing 60 changed files with 863 additions and 1,877 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Lint
on: [ push, pull_request, workflow_dispatch ]
on: [push]

jobs:
golangci-lint:
Expand All @@ -14,4 +14,4 @@ jobs:
with:
skip-go-installation: true
version: v1.50.1
args: --timeout=10m
args: --timeout=10m
3 changes: 1 addition & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

name: Test
on: [push, pull_request]
on: [push]

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME="github.com/goto/siren"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
APP_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "4fc96a5406f640044ebe0572385336af7bd221ae"
PROTON_COMMIT := "534a703026bd70fa464c3bedc6a9aef3f8fcdd19"

.PHONY: all build test clean dist vet proto install

Expand Down
10 changes: 7 additions & 3 deletions cli/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ func InitDeps(
templateRepository := postgres.NewTemplateRepository(pgClient)
templateService := template.NewService(templateRepository)

providerRepository := postgres.NewProviderRepository(pgClient)
providerService := provider.NewService(providerRepository)

logRepository := postgres.NewLogRepository(pgClient)
logService := log.NewService(logRepository)

Expand Down Expand Up @@ -103,6 +100,13 @@ func InitDeps(
ruleUploaders[k] = pc.(rule.RuleUploader)
}

var supportedProviderTypes = []string{}
for typ := range providerPlugins {
supportedProviderTypes = append(supportedProviderTypes, typ)
}
providerRepository := postgres.NewProviderRepository(pgClient)
providerService := provider.NewService(providerRepository, supportedProviderTypes)

alertRepository := postgres.NewAlertRepository(pgClient)
alertService := alert.NewService(
alertRepository,
Expand Down
20 changes: 10 additions & 10 deletions core/alert/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestService_List(t *testing.T) {
{ID: 2, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "baz", MetricValue: "0",
Rule: "bar", TriggeredAt: timenow},
}
repositoryMock.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), alert.Filter{
repositoryMock.EXPECT().List(mock.AnythingOfType("context.todoCtx"), alert.Filter{
ProviderID: 1,
ResourceName: "foo",
StartTime: 0,
Expand All @@ -53,7 +53,7 @@ func TestService_List(t *testing.T) {
{ID: 2, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "baz", MetricValue: "0",
Rule: "bar", TriggeredAt: timenow},
}
repositoryMock.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), mock.Anything).Return(dummyAlerts, nil)
repositoryMock.EXPECT().List(mock.AnythingOfType("context.todoCtx"), mock.Anything).Return(dummyAlerts, nil)
actualAlerts, err := dummyService.List(ctx, alert.Filter{
ProviderID: 1,
ResourceName: "foo",
Expand All @@ -68,7 +68,7 @@ func TestService_List(t *testing.T) {
t.Run("should call repository List method and handle errors", func(t *testing.T) {
repositoryMock := &mocks.AlertRepository{}
dummyService := alert.NewService(repositoryMock, nil, nil)
repositoryMock.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), mock.Anything).
repositoryMock.EXPECT().List(mock.AnythingOfType("context.todoCtx"), mock.Anything).
Return(nil, errors.New("random error"))
actualAlerts, err := dummyService.List(ctx, alert.Filter{
ProviderID: 1,
Expand Down Expand Up @@ -116,19 +116,19 @@ func TestService_CreateAlerts(t *testing.T) {
{
name: "should return error if TransformToAlerts return error",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return(nil, 0, errors.New("some error"))
at.EXPECT().TransformToAlerts(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return(nil, 0, errors.New("some error"))
},
alertsToBeCreated: alertsToBeCreated,
wantErr: true,
},
{
name: "should call repository Create method with proper arguments",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]alert.Alert{
at.EXPECT().TransformToAlerts(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]alert.Alert{
{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
Rule: "lagHigh", TriggeredAt: timenow},
}, 1, nil)
ar.EXPECT().Create(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("alert.Alert")).Return(alert.Alert{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
ar.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("alert.Alert")).Return(alert.Alert{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
Rule: "lagHigh", TriggeredAt: timenow}, nil)
},
alertsToBeCreated: alertsToBeCreated,
Expand All @@ -141,23 +141,23 @@ func TestService_CreateAlerts(t *testing.T) {
{
name: "should return error not found if repository return err relation",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]alert.Alert{
at.EXPECT().TransformToAlerts(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]alert.Alert{
{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
Rule: "lagHigh", TriggeredAt: timenow},
}, 1, nil)
ar.EXPECT().Create(mock.AnythingOfType("*context.emptyCtx"), mock.Anything).Return(alert.Alert{}, alert.ErrRelation)
ar.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), mock.Anything).Return(alert.Alert{}, alert.ErrRelation)
},
alertsToBeCreated: alertsToBeCreated,
wantErr: true,
},
{
name: "should handle errors from repository",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]alert.Alert{
at.EXPECT().TransformToAlerts(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]alert.Alert{
{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
Rule: "lagHigh", TriggeredAt: timenow},
}, 1, nil)
ar.EXPECT().Create(mock.AnythingOfType("*context.emptyCtx"), mock.Anything).Return(alert.Alert{}, errors.New("random error"))
ar.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), mock.Anything).Return(alert.Alert{}, errors.New("random error"))
},
alertsToBeCreated: alertsToBeCreated,
wantErr: true,
Expand Down
Loading

0 comments on commit 1428d7d

Please sign in to comment.