From 5a95179870ba0d7f0e92eab7aec9a9182fa6b858 Mon Sep 17 00:00:00 2001 From: Muhammad Abduh Date: Thu, 29 Aug 2024 23:23:59 +0700 Subject: [PATCH] feat: add metric gauge notification api --- internal/api/v1beta1/alert_test.go | 25 ++++++----- internal/api/v1beta1/namespace_test.go | 52 ++++++++++++++------- internal/api/v1beta1/notification.go | 13 ++++++ internal/api/v1beta1/notification_test.go | 13 ++++-- internal/api/v1beta1/provider_test.go | 55 +++++++++++++++-------- internal/api/v1beta1/receiver_test.go | 50 ++++++++++++++------- internal/api/v1beta1/rule_test.go | 17 ++++--- internal/api/v1beta1/silence_test.go | 13 ++++-- internal/api/v1beta1/subscription_test.go | 48 +++++++++++++------- internal/api/v1beta1/template_test.go | 41 +++++++++++------ internal/api/v1beta1/v1beta1.go | 23 +++++++++- internal/server/server.go | 6 ++- 12 files changed, 247 insertions(+), 109 deletions(-) diff --git a/internal/api/v1beta1/alert_test.go b/internal/api/v1beta1/alert_test.go index 88cac008..960db2b1 100644 --- a/internal/api/v1beta1/alert_test.go +++ b/internal/api/v1beta1/alert_test.go @@ -14,6 +14,7 @@ import ( sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/structpb" ) @@ -31,8 +32,8 @@ func TestGRPCServer_ListAlerts(t *testing.T) { StartTime: 100, EndTime: 200, }).Return(dummyAlerts, nil).Once() - dummyGRPCServer := v1beta1.NewGRPCServer(nil, api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(nil, api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.ListAlertsRequest{ ResourceName: "foo", ProviderId: 1, @@ -53,8 +54,8 @@ func TestGRPCServer_ListAlerts(t *testing.T) { t.Run("should return error Internal if getting alert history failed", func(t *testing.T) { mockedAlertService := &mocks.AlertService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService}) + require.NoError(t, err) mockedAlertService.EXPECT().List(mock.AnythingOfType("context.todoCtx"), alert.Filter{ ProviderID: 1, ResourceName: "foo", @@ -173,8 +174,8 @@ func TestGRPCServer_CreateAlertHistory(t *testing.T) { Return(dummyAlerts, nil).Once() mockNotificationService.EXPECT().Dispatch(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("[]notification.Notification")).Return(nil, nil) - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService, NotificationService: mockNotificationService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService, NotificationService: mockNotificationService}) + require.NoError(t, err) res, err := dummyGRPCServer.CreateAlerts(context.TODO(), dummyReq) assert.Equal(t, 1, len(res.GetAlerts())) assert.Equal(t, uint64(1), res.GetAlerts()[0].GetId()) @@ -281,8 +282,8 @@ func TestGRPCServer_CreateAlertHistory(t *testing.T) { Return(dummyAlerts, nil).Once() mockNotificationService.EXPECT().Dispatch(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("[]notification.Notification")).Return(nil, nil) - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService, NotificationService: mockNotificationService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService, NotificationService: mockNotificationService}) + require.NoError(t, err) res, err := dummyGRPCServer.CreateAlerts(context.TODO(), dummyReq) assert.Equal(t, 1, len(res.GetAlerts())) assert.Equal(t, uint64(1), res.GetAlerts()[0].GetId()) @@ -297,8 +298,8 @@ func TestGRPCServer_CreateAlertHistory(t *testing.T) { t.Run("should return error Internal if getting alert history failed", func(t *testing.T) { mockedAlertService := &mocks.AlertService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService}) + require.NoError(t, err) mockedAlertService.EXPECT().CreateAlerts(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("string"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), payload). Return(nil, errors.New("random error")).Once() @@ -456,8 +457,8 @@ func TestGRPCServer_CreateAlertHistory(t *testing.T) { TriggeredAt: time.Now(), }} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService, NotificationService: mockNotificationService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{AlertService: mockedAlertService, NotificationService: mockNotificationService}) + require.NoError(t, err) mockedAlertService.EXPECT().CreateAlerts(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("string"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), payload). Return(dummyAlerts, nil).Once() mockNotificationService.EXPECT().Dispatch(mock.AnythingOfType("context.todoCtx"), mock.AnythingOfType("[]notification.Notification")).Return(nil, nil) diff --git a/internal/api/v1beta1/namespace_test.go b/internal/api/v1beta1/namespace_test.go index cbd06a4a..85055b8e 100644 --- a/internal/api/v1beta1/namespace_test.go +++ b/internal/api/v1beta1/namespace_test.go @@ -15,6 +15,7 @@ import ( sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/structpb" ) @@ -26,7 +27,8 @@ func TestGRPCServer_ListNamespaces(t *testing.T) { t.Run("should return list of all namespaces", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) dummyResult := []namespace.Namespace{ { ID: 1, @@ -53,7 +55,8 @@ func TestGRPCServer_ListNamespaces(t *testing.T) { t.Run("should return Internal if getting namespaces failed", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().List(mock.AnythingOfType("context.todoCtx")). Return(nil, errors.New("random error")).Once() res, err := dummyGRPCServer.ListNamespaces(context.TODO(), &sirenv1beta1.ListNamespacesRequest{}) @@ -63,7 +66,8 @@ func TestGRPCServer_ListNamespaces(t *testing.T) { t.Run("should return Internal if NewStruct conversion failed", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) credentials["bar"] = string([]byte{0xff}) dummyResult := []namespace.Namespace{ { @@ -110,7 +114,8 @@ func TestGRPCServer_CreateNamespaces(t *testing.T) { t.Run("should create a namespace", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Run(func(_a0 context.Context, _a1 *namespace.Namespace) { _a1.ID = generatedID }).Return(nil).Once() @@ -121,7 +126,8 @@ func TestGRPCServer_CreateNamespaces(t *testing.T) { t.Run("should return error Internal if creating namespaces failed", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.CreateNamespace(context.TODO(), request) assert.Nil(t, res) @@ -130,7 +136,8 @@ func TestGRPCServer_CreateNamespaces(t *testing.T) { t.Run("should return error Invalid Argument if create service return err invalid", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrInvalid).Once() res, err := dummyGRPCServer.CreateNamespace(context.TODO(), request) @@ -141,7 +148,8 @@ func TestGRPCServer_CreateNamespaces(t *testing.T) { t.Run("should return error AlreadyExists if create service return err conflict", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrConflict).Once() res, err := dummyGRPCServer.CreateNamespace(context.TODO(), request) @@ -159,7 +167,8 @@ func TestGRPCServer_GetNamespace(t *testing.T) { t.Run("should get the namespace", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) dummyResult := &namespace.Namespace{ ID: 1, Provider: provider.Provider{ @@ -184,7 +193,8 @@ func TestGRPCServer_GetNamespace(t *testing.T) { t.Run("should return error Invalid Argument if no namespace found", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), uint64(1)).Return(nil, errors.ErrNotFound.WithCausef("some error")).Once() res, err := dummyGRPCServer.GetNamespace(context.TODO(), &sirenv1beta1.GetNamespaceRequest{Id: uint64(1)}) @@ -194,7 +204,8 @@ func TestGRPCServer_GetNamespace(t *testing.T) { t.Run("should return error Internal if getting namespace fails", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), uint64(1)). Return(nil, errors.New("random error")).Once() res, err := dummyGRPCServer.GetNamespace(context.TODO(), @@ -205,7 +216,8 @@ func TestGRPCServer_GetNamespace(t *testing.T) { t.Run("should return error Internal if NewStruct conversion failed", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) credentials["bar"] = string([]byte{0xff}) dummyResult := &namespace.Namespace{ ID: 1, @@ -252,7 +264,8 @@ func TestGRPCServer_UpdateNamespace(t *testing.T) { t.Run("should update a namespace", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Run(func(_a0 context.Context, _a1 *namespace.Namespace) { _a1.ID = payload.ID }).Return(nil).Once() @@ -264,7 +277,8 @@ func TestGRPCServer_UpdateNamespace(t *testing.T) { t.Run("should return error Invalid Argument if namespace service return err invalid", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrInvalid).Once() res, err := dummyGRPCServer.UpdateNamespace(context.TODO(), request) @@ -274,7 +288,8 @@ func TestGRPCServer_UpdateNamespace(t *testing.T) { t.Run("should return error AlreadyExists if namespace service return err conflict", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrConflict).Once() res, err := dummyGRPCServer.UpdateNamespace(context.TODO(), request) @@ -285,7 +300,8 @@ func TestGRPCServer_UpdateNamespace(t *testing.T) { t.Run("should return error Internal if updating namespaces failed", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.UpdateNamespace(context.TODO(), request) @@ -303,7 +319,8 @@ func TestGRPCServer_DeleteNamespace(t *testing.T) { t.Run("should delete namespace object", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Delete(mock.AnythingOfType("context.todoCtx"), namespaceId).Return(nil).Once() res, err := dummyGRPCServer.DeleteNamespace(context.TODO(), dummyReq) assert.Nil(t, err) @@ -313,7 +330,8 @@ func TestGRPCServer_DeleteNamespace(t *testing.T) { t.Run("should return error Internal if deleting namespace failed", func(t *testing.T) { mockedNamespaceService := &mocks.NamespaceService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NamespaceService: mockedNamespaceService}) + require.NoError(t, err) mockedNamespaceService.EXPECT().Delete(mock.AnythingOfType("context.todoCtx"), namespaceId).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.DeleteNamespace(context.TODO(), dummyReq) assert.Nil(t, res) diff --git a/internal/api/v1beta1/notification.go b/internal/api/v1beta1/notification.go index 849dd1c8..5ffa8bc6 100644 --- a/internal/api/v1beta1/notification.go +++ b/internal/api/v1beta1/notification.go @@ -10,6 +10,8 @@ import ( "github.com/goto/siren/internal/api" "github.com/goto/siren/pkg/errors" sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" "google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/structpb" "google.golang.org/protobuf/types/known/timestamppb" @@ -67,6 +69,13 @@ func (s *GRPCServer) PostNotification(ctx context.Context, req *sirenv1beta1.Pos notificationTemplate = req.GetTemplate() } + s.metricNotificationReceiverSelectorCount.Record( + ctx, int64(len(receiverSelectors)), + metric.WithAttributes( + attribute.String("template", notificationTemplate), + ), + ) + notificationIDs, err := s.notificationService.Dispatch(ctx, []notification.Notification{ { Type: notification.TypeEvent, @@ -138,6 +147,10 @@ func (s *GRPCServer) PostBulkNotifications(ctx context.Context, req *sirenv1beta }) } + s.metricBulkNotificationsCount.Record( + ctx, int64(len(notifications)), + ) + notificationIDs, err := s.notificationService.Dispatch(ctx, notifications) if err != nil { if errors.Is(err, notification.ErrNoMessage) { diff --git a/internal/api/v1beta1/notification_test.go b/internal/api/v1beta1/notification_test.go index 4d887572..892d8f82 100644 --- a/internal/api/v1beta1/notification_test.go +++ b/internal/api/v1beta1/notification_test.go @@ -13,6 +13,7 @@ import ( sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "google.golang.org/grpc/metadata" ) @@ -100,13 +101,15 @@ func TestGRPCServer_PostNotification(t *testing.T) { tc.setup(mockNotificationService) } - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{ + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{ IdempotencyKey: idempotencyHeaderKey, }, &api.Deps{NotificationService: mockNotificationService}) + require.NoError(t, err) + ctx := metadata.NewIncomingContext(context.TODO(), metadata.New(map[string]string{ idempotencyHeaderKey: tc.idempotencyKey, })) - _, err := dummyGRPCServer.PostNotification(ctx, &sirenv1beta1.PostNotificationRequest{}) + _, err = dummyGRPCServer.PostNotification(ctx, &sirenv1beta1.PostNotificationRequest{}) if (err != nil) && tc.errString != err.Error() { t.Errorf("PostNotification() error = %v, wantErr %v", err, tc.errString) @@ -135,7 +138,8 @@ func TestGRPCServer_ListNotifications(t *testing.T) { } mockNotificationService := &mocks.NotificationService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NotificationService: mockNotificationService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NotificationService: mockNotificationService}) + require.NoError(t, err) mockNotificationService.EXPECT().List(mock.AnythingOfType("context.todoCtx"), notification.Filter{Type: "reciever"}).Return(dummyResult, nil).Once() res, err := dummyGRPCServer.ListNotifications(ctx, &sirenv1beta1.ListNotificationsRequest{Type: "reciever"}) assert.Nil(t, err) @@ -145,7 +149,8 @@ func TestGRPCServer_ListNotifications(t *testing.T) { t.Run("should return error if list notifications failed", func(t *testing.T) { mockNotificationService := &mocks.NotificationService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NotificationService: mockNotificationService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{NotificationService: mockNotificationService}) + require.NoError(t, err) mockNotificationService.EXPECT().List(mock.AnythingOfType("context.todoCtx"), notification.Filter{Type: "alert"}).Return(nil, errors.New("internal server error")).Once() res, err := dummyGRPCServer.ListNotifications(ctx, &sirenv1beta1.ListNotificationsRequest{Type: "alert"}) assert.Nil(t, res) diff --git a/internal/api/v1beta1/provider_test.go b/internal/api/v1beta1/provider_test.go index e973bd54..efd0c570 100644 --- a/internal/api/v1beta1/provider_test.go +++ b/internal/api/v1beta1/provider_test.go @@ -14,6 +14,7 @@ import ( sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/structpb" ) @@ -27,7 +28,8 @@ func TestGRPCServer_ListProvider(t *testing.T) { t.Run("should return list of all provider", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) dummyResult := []provider.Provider{ { @@ -53,7 +55,8 @@ func TestGRPCServer_ListProvider(t *testing.T) { t.Run("should return error Internal if getting providers failed", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().List(mock.AnythingOfType("context.todoCtx"), provider.Filter{}).Return(nil, errors.New("random error")).Once() res, err := dummyGRPCServer.ListProviders(ctx, &sirenv1beta1.ListProvidersRequest{}) @@ -63,7 +66,8 @@ func TestGRPCServer_ListProvider(t *testing.T) { t.Run("should return error Internal if NewStruct conversion failed", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) credentials["bar"] = string([]byte{0xff}) dummyResult := []provider.Provider{ @@ -114,7 +118,8 @@ func TestGRPCServer_CreateProvider(t *testing.T) { t.Run("should create provider object", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Run(func(_a0 context.Context, _a1 *provider.Provider) { _a1.ID = testID @@ -126,7 +131,8 @@ func TestGRPCServer_CreateProvider(t *testing.T) { t.Run("should return error Invalid Argument if provider return error invalid", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrInvalid).Once() res, err := dummyGRPCServer.CreateProvider(ctx, dummyReq) @@ -136,7 +142,8 @@ func TestGRPCServer_CreateProvider(t *testing.T) { t.Run("should return error AlreadyExist if provider return error conflict", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrConflict).Once() res, err := dummyGRPCServer.CreateProvider(ctx, dummyReq) @@ -146,7 +153,8 @@ func TestGRPCServer_CreateProvider(t *testing.T) { t.Run("should return error Internal if creating providers failed", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.CreateProvider(ctx, dummyReq) @@ -169,7 +177,9 @@ func TestGRPCServer_GetProvider(t *testing.T) { t.Run("should return a provider", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) + dummyResult := &provider.Provider{ ID: 1, Host: "foo", @@ -193,7 +203,8 @@ func TestGRPCServer_GetProvider(t *testing.T) { t.Run("should return error Not Found if no provider found", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), providerId). Return(nil, errors.ErrNotFound).Once() @@ -205,7 +216,9 @@ func TestGRPCServer_GetProvider(t *testing.T) { t.Run("should return error Internal if getting provider failed", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) + dummyResult := &provider.Provider{ ID: 1, Host: "foo", @@ -227,7 +240,8 @@ func TestGRPCServer_GetProvider(t *testing.T) { t.Run("should return error Internal if NewStruct conversion failed", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) credentials["bar"] = string([]byte{0xff}) dummyResult := &provider.Provider{ @@ -277,7 +291,8 @@ func TestGRPCServer_UpdateProvider(t *testing.T) { t.Run("should update provider object", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Run(func(_a0 context.Context, _a1 *provider.Provider) { _a1.ID = testID @@ -289,7 +304,8 @@ func TestGRPCServer_UpdateProvider(t *testing.T) { t.Run("should return error Invalid Argument if updating providers return err invalid", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrInvalid).Once() res, err := dummyGRPCServer.UpdateProvider(ctx, dummyReq) @@ -299,7 +315,8 @@ func TestGRPCServer_UpdateProvider(t *testing.T) { t.Run("should return error AlreadyExist if updating providers return err conflict", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrConflict).Once() res, err := dummyGRPCServer.UpdateProvider(ctx, dummyReq) @@ -309,8 +326,8 @@ func TestGRPCServer_UpdateProvider(t *testing.T) { t.Run("should return error Internal if updating providers failed", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.UpdateProvider(ctx, dummyReq) assert.Nil(t, res) @@ -326,7 +343,8 @@ func TestGRPCServer_DeleteProvider(t *testing.T) { t.Run("should delete provider object", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Delete(mock.AnythingOfType("context.todoCtx"), providerId).Return(nil).Once() res, err := dummyGRPCServer.DeleteProvider(context.TODO(), dummyReq) @@ -336,7 +354,8 @@ func TestGRPCServer_DeleteProvider(t *testing.T) { t.Run("should return error Internal if deleting providers failed", func(t *testing.T) { mockedProviderService := &mocks.ProviderService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ProviderService: mockedProviderService}) + require.NoError(t, err) mockedProviderService.EXPECT().Delete(mock.AnythingOfType("context.todoCtx"), providerId).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.DeleteProvider(context.TODO(), dummyReq) diff --git a/internal/api/v1beta1/receiver_test.go b/internal/api/v1beta1/receiver_test.go index 70f009c6..31eeae90 100644 --- a/internal/api/v1beta1/receiver_test.go +++ b/internal/api/v1beta1/receiver_test.go @@ -14,6 +14,7 @@ import ( sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/structpb" ) @@ -36,7 +37,9 @@ func TestGRPCServer_ListReceiver(t *testing.T) { t.Run("should return list of all receiver", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) + mockedReceiverService.EXPECT().List(mock.AnythingOfType("context.todoCtx"), receiver.Filter{}).Return(dummyResult, nil).Once() res, err := dummyGRPCServer.ListReceivers(context.TODO(), &sirenv1beta1.ListReceiversRequest{}) @@ -51,7 +54,8 @@ func TestGRPCServer_ListReceiver(t *testing.T) { t.Run("should return error Internal if getting providers failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().List(mock.AnythingOfType("context.todoCtx"), receiver.Filter{}). Return(nil, errors.New("random error")) @@ -62,7 +66,8 @@ func TestGRPCServer_ListReceiver(t *testing.T) { t.Run("should return error Internal if NewStruct conversion failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) configurations["foo"] = string([]byte{0xff}) dummyResult := []receiver.Receiver{ { @@ -110,7 +115,8 @@ func TestGRPCServer_CreateReceiver(t *testing.T) { t.Run("Should create a receiver object", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Run(func(ctx context.Context, rcv *receiver.Receiver) { rcv.ID = generatedID }).Return(nil).Once() @@ -121,7 +127,8 @@ func TestGRPCServer_CreateReceiver(t *testing.T) { t.Run("should return error Invalid Argument if create receiver failed with err invalid", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrInvalid).Once() @@ -134,7 +141,8 @@ func TestGRPCServer_CreateReceiver(t *testing.T) { t.Run("should return error Internal if creating receiver failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.CreateReceiver(context.TODO(), dummyReq) @@ -163,7 +171,8 @@ func TestGRPCServer_GetReceiver(t *testing.T) { t.Run("should return a receiver", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), receiverId, mock.AnythingOfType("receiver.GetOption")). Return(payload, nil).Once() @@ -177,7 +186,8 @@ func TestGRPCServer_GetReceiver(t *testing.T) { t.Run("should return error Not Found if no receiver found", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), receiverId, mock.AnythingOfType("receiver.GetOption")). Return(nil, errors.ErrNotFound).Once() @@ -188,7 +198,8 @@ func TestGRPCServer_GetReceiver(t *testing.T) { t.Run("should return error Internal if getting receiver failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), receiverId, mock.AnythingOfType("receiver.GetOption")). Return(payload, errors.New("random error")).Once() @@ -199,7 +210,8 @@ func TestGRPCServer_GetReceiver(t *testing.T) { t.Run("should return error Internal if NewStruct conversion of configuration failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) configurations["foo"] = string([]byte{0xff}) payload := &receiver.Receiver{ @@ -218,7 +230,8 @@ func TestGRPCServer_GetReceiver(t *testing.T) { t.Run("should return error Internal if data NewStruct conversion of data failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) data := make(map[string]any) data["channels"] = string([]byte{0xff}) payload := &receiver.Receiver{ @@ -263,7 +276,8 @@ func TestGRPCServer_UpdateReceiver(t *testing.T) { t.Run("should update receiver object", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Run(func(ctx context.Context, rcv *receiver.Receiver) { rcv.ID = payload.ID }).Return(nil).Once() @@ -275,7 +289,8 @@ func TestGRPCServer_UpdateReceiver(t *testing.T) { t.Run("should return error Invalid Argument if update receiver return invalid error", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.ErrInvalid).Once() @@ -287,7 +302,8 @@ func TestGRPCServer_UpdateReceiver(t *testing.T) { t.Run("should return error Internal if updating receiver failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Update(mock.AnythingOfType("context.todoCtx"), payload).Return(errors.New("random error")) res, err := dummyGRPCServer.UpdateReceiver(context.TODO(), dummyReq) @@ -304,7 +320,8 @@ func TestGRPCServer_DeleteReceiver(t *testing.T) { t.Run("should delete receiver object", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Delete(mock.AnythingOfType("context.todoCtx"), providerId). Return(nil).Once() @@ -315,7 +332,8 @@ func TestGRPCServer_DeleteReceiver(t *testing.T) { t.Run("should return error Internal if deleting receiver failed", func(t *testing.T) { mockedReceiverService := &mocks.ReceiverService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{ReceiverService: mockedReceiverService}) + require.NoError(t, err) mockedReceiverService.EXPECT().Delete(mock.AnythingOfType("context.todoCtx"), providerId). Return(errors.New("random error")).Once() diff --git a/internal/api/v1beta1/rule_test.go b/internal/api/v1beta1/rule_test.go index a01eac4d..67c8d954 100644 --- a/internal/api/v1beta1/rule_test.go +++ b/internal/api/v1beta1/rule_test.go @@ -14,6 +14,7 @@ import ( "github.com/goto/siren/internal/api/v1beta1" sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGRPCServer_ListRules(t *testing.T) { @@ -49,8 +50,8 @@ func TestGRPCServer_ListRules(t *testing.T) { }, } - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) - + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + require.NoError(t, err) mockedRuleService.EXPECT().List(ctx, rule.Filter{ Name: dummyPayload.Name, Namespace: dummyPayload.Namespace, @@ -73,7 +74,8 @@ func TestGRPCServer_ListRules(t *testing.T) { ctx := context.TODO() mockedRuleService := &mocks.RuleService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + require.NoError(t, err) mockedRuleService.EXPECT().List(ctx, rule.Filter{ Name: dummyPayload.Name, Namespace: dummyPayload.Namespace, @@ -124,7 +126,8 @@ func TestGRPCServer_UpdateRules(t *testing.T) { t.Run("should update rule", func(t *testing.T) { ctx := context.TODO() mockedRuleService := &mocks.RuleService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + require.NoError(t, err) dummyResult := &rule.Rule{} *dummyResult = *dummyPayload dummyResult.Enabled = false @@ -145,7 +148,8 @@ func TestGRPCServer_UpdateRules(t *testing.T) { ctx := context.TODO() mockedRuleService := &mocks.RuleService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + require.NoError(t, err) mockedRuleService.EXPECT().Upsert(ctx, dummyPayload).Return(errors.ErrConflict).Once() res, err := dummyGRPCServer.UpdateRule(ctx, dummyReq) @@ -157,7 +161,8 @@ func TestGRPCServer_UpdateRules(t *testing.T) { ctx := context.TODO() mockedRuleService := &mocks.RuleService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{RuleService: mockedRuleService}) + require.NoError(t, err) mockedRuleService.EXPECT().Upsert(ctx, dummyPayload).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.UpdateRule(ctx, dummyReq) diff --git a/internal/api/v1beta1/silence_test.go b/internal/api/v1beta1/silence_test.go index f3d02578..4917c8e0 100644 --- a/internal/api/v1beta1/silence_test.go +++ b/internal/api/v1beta1/silence_test.go @@ -15,6 +15,7 @@ import ( "github.com/goto/siren/pkg/errors" sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "google.golang.org/protobuf/testing/protocmp" "google.golang.org/protobuf/types/known/structpb" "google.golang.org/protobuf/types/known/timestamppb" @@ -80,7 +81,8 @@ func TestGRPCServer_CreateSilence(t *testing.T) { tt.setup(mockSilenceService) } - s := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + s, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + require.NoError(t, err) got, err := s.CreateSilence(ctx, tt.req) if (err != nil) != tt.wantErr { t.Errorf("GRPCServer.CreateSilence() error = %v, wantErr %v", err, tt.wantErr) @@ -145,7 +147,8 @@ func TestGRPCServer_ListSilences(t *testing.T) { tt.setup(mockSilenceService) } - s := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + s, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + require.NoError(t, err) got, err := s.ListSilences(ctx, &sirenv1beta1.ListSilencesRequest{}) if (err != nil) != tt.wantErr { t.Errorf("GRPCServer.ListSilences() error = %v, wantErr %v", err, tt.wantErr) @@ -215,7 +218,8 @@ func TestGRPCServer_GetSilence(t *testing.T) { tt.setup(mockSilenceService) } - s := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + s, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + require.NoError(t, err) got, err := s.GetSilence(ctx, tt.req) if (err != nil) != tt.wantErr { t.Errorf("GRPCServer.GetSilence() error = %v, wantErr %v", err, tt.wantErr) @@ -275,7 +279,8 @@ func TestGRPCServer_ExpireSilence(t *testing.T) { tt.setup(mockSilenceService) } - s := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + s, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SilenceService: mockSilenceService}) + require.NoError(t, err) got, err := s.ExpireSilence(ctx, tt.req) if (err != nil) != tt.wantErr { t.Errorf("GRPCServer.ExpireSilence() error = %v, wantErr %v", err, tt.wantErr) diff --git a/internal/api/v1beta1/subscription_test.go b/internal/api/v1beta1/subscription_test.go index e7ad31da..77077b5b 100644 --- a/internal/api/v1beta1/subscription_test.go +++ b/internal/api/v1beta1/subscription_test.go @@ -40,7 +40,8 @@ func TestGRPCServer_ListSubscriptions(t *testing.T) { t.Run("should return list of all subscriptions", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) dummyResult := []subscription.Subscription{ { @@ -70,7 +71,8 @@ func TestGRPCServer_ListSubscriptions(t *testing.T) { t.Run("should return error Internal if getting subscriptions fails", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().ListV2(context.TODO(), subscription.Filter{}).Return(nil, errors.New("random error")).Once() res, err := dummyGRPCServer.ListSubscriptions(context.TODO(), &sirenv1beta1.ListSubscriptionsRequest{}) @@ -82,7 +84,8 @@ func TestGRPCServer_ListSubscriptions(t *testing.T) { func TestGRPCServer_GetSubscription(t *testing.T) { t.Run("should return a subscription", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) dummyResult := &subscription.Subscription{ ID: 1, URN: "foo", @@ -107,7 +110,8 @@ func TestGRPCServer_GetSubscription(t *testing.T) { t.Run("should return error Not Found if subscriptions not found", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().GetV2(context.TODO(), uint64(1)).Return(nil, errors.ErrNotFound).Once() res, err := dummyGRPCServer.GetSubscription(context.TODO(), &sirenv1beta1.GetSubscriptionRequest{Id: 1}) assert.Nil(t, res) @@ -116,7 +120,8 @@ func TestGRPCServer_GetSubscription(t *testing.T) { t.Run("should return error Internal if getting subscription fails", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().GetV2(context.TODO(), uint64(1)). Return(nil, errors.New("random error")).Once() res, err := dummyGRPCServer.GetSubscription(context.TODO(), &sirenv1beta1.GetSubscriptionRequest{Id: 1}) @@ -154,7 +159,8 @@ func TestGRPCServer_CreateSubscription(t *testing.T) { t.Run("should create a subscription", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().CreateV2(context.TODO(), payload).Run(func(_a0 context.Context, _a1 *subscription.Subscription) { _a1.ID = dummyResult.ID @@ -172,7 +178,8 @@ func TestGRPCServer_CreateSubscription(t *testing.T) { t.Run("should return error InvalidArgument if creating subscriptions return err invalid", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().CreateV2(context.TODO(), payload).Run(func(_a0 context.Context, _a1 *subscription.Subscription) { _a1.ID = dummyResult.ID @@ -190,7 +197,8 @@ func TestGRPCServer_CreateSubscription(t *testing.T) { t.Run("should return error AlreadyExists if creating subscriptions return err conflict", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().CreateV2(context.TODO(), payload).Run(func(_a0 context.Context, _a1 *subscription.Subscription) { _a1.ID = dummyResult.ID @@ -208,7 +216,8 @@ func TestGRPCServer_CreateSubscription(t *testing.T) { t.Run("should return error Internal if creating subscriptions fails", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().CreateV2(context.TODO(), payload).Run(func(_a0 context.Context, _a1 *subscription.Subscription) { _a1.ID = dummyResult.ID @@ -249,7 +258,8 @@ func TestGRPCServer_UpdateSubscription(t *testing.T) { t.Run("should update a subscription", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().UpdateV2(context.TODO(), payload).Run(func(_a0 context.Context, _a1 *subscription.Subscription) { _a1.ID = uint64(1) @@ -270,7 +280,8 @@ func TestGRPCServer_UpdateSubscription(t *testing.T) { t.Run("should return error Invalid Argument if updating subscriptions return err invalid", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().UpdateV2(context.TODO(), payload).Return(errors.ErrInvalid).Once() res, err := dummyGRPCServer.UpdateSubscription(context.TODO(), &sirenv1beta1.UpdateSubscriptionRequest{ @@ -288,7 +299,8 @@ func TestGRPCServer_UpdateSubscription(t *testing.T) { t.Run("should return error AlreadyExist if updating subscriptions return err conflict", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().UpdateV2(context.TODO(), payload).Return(errors.ErrConflict).Once() res, err := dummyGRPCServer.UpdateSubscription(context.TODO(), &sirenv1beta1.UpdateSubscriptionRequest{ @@ -306,7 +318,8 @@ func TestGRPCServer_UpdateSubscription(t *testing.T) { t.Run("should return error Internal if updating subscriptions fails", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().UpdateV2(context.TODO(), payload).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.UpdateSubscription(context.TODO(), &sirenv1beta1.UpdateSubscriptionRequest{ @@ -324,7 +337,8 @@ func TestGRPCServer_UpdateSubscription(t *testing.T) { t.Run("should return error Invalid for bad requests", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().UpdateV2(context.TODO(), payload).Return(errors.ErrInvalid).Once() res, err := dummyGRPCServer.UpdateSubscription(context.TODO(), &sirenv1beta1.UpdateSubscriptionRequest{ @@ -344,7 +358,8 @@ func TestGRPCServer_UpdateSubscription(t *testing.T) { func TestGRPCServer_DeleteSubscription(t *testing.T) { t.Run("should delete a subscription", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().DeleteV2(context.TODO(), uint64(1)).Return(nil).Once() res, err := dummyGRPCServer.DeleteSubscription(context.TODO(), &sirenv1beta1.DeleteSubscriptionRequest{Id: 1}) @@ -354,7 +369,8 @@ func TestGRPCServer_DeleteSubscription(t *testing.T) { t.Run("should return error Internal if deleting subscription fails", func(t *testing.T) { mockedSubscriptionService := &mocks.SubscriptionService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{SubscriptionService: mockedSubscriptionService}) + require.NoError(t, err) mockedSubscriptionService.EXPECT().DeleteV2(context.TODO(), uint64(1)).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.DeleteSubscription(context.TODO(), &sirenv1beta1.DeleteSubscriptionRequest{Id: 1}) diff --git a/internal/api/v1beta1/template_test.go b/internal/api/v1beta1/template_test.go index 43bb14fb..7040bf93 100644 --- a/internal/api/v1beta1/template_test.go +++ b/internal/api/v1beta1/template_test.go @@ -13,13 +13,15 @@ import ( sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestGRPCServer_ListTemplates(t *testing.T) { t.Run("should return list of all templates", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.ListTemplatesRequest{} dummyResult := []template.Template{ @@ -53,7 +55,9 @@ func TestGRPCServer_ListTemplates(t *testing.T) { t.Run("should return list of all templates matching particular tag", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) + dummyReq := &sirenv1beta1.ListTemplatesRequest{ Tag: "foo", } @@ -89,7 +93,8 @@ func TestGRPCServer_ListTemplates(t *testing.T) { t.Run("should return error Internal if getting templates failed", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.ListTemplatesRequest{ Tag: "foo", } @@ -105,7 +110,8 @@ func TestGRPCServer_ListTemplates(t *testing.T) { func TestGRPCServer_GetTemplate(t *testing.T) { t.Run("should return template by name", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.GetTemplateRequest{ Name: "foo", } @@ -137,7 +143,8 @@ func TestGRPCServer_GetTemplate(t *testing.T) { t.Run("should return error Not Found if template does not exist", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.GetTemplateRequest{ Name: "foo", } @@ -151,7 +158,8 @@ func TestGRPCServer_GetTemplate(t *testing.T) { t.Run("should return error Internal if getting template by name failed", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.GetTemplateRequest{ Name: "foo", } @@ -196,7 +204,8 @@ func TestGRPCServer_UpsertTemplate(t *testing.T) { t.Run("should return template by name", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) mockedTemplateService.EXPECT().Upsert(mock.AnythingOfType("context.todoCtx"), tmpl).Run(func(_a0 context.Context, _a1 *template.Template) { _a1.ID = uint64(1) @@ -209,7 +218,8 @@ func TestGRPCServer_UpsertTemplate(t *testing.T) { t.Run("should return error AlreadyExists if upsert template return err conflict", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) mockedTemplateService.EXPECT().Upsert(mock.AnythingOfType("context.todoCtx"), tmpl).Return(errors.ErrConflict).Once() res, err := dummyGRPCServer.UpsertTemplate(context.TODO(), dummyReq) assert.Nil(t, res) @@ -219,7 +229,8 @@ func TestGRPCServer_UpsertTemplate(t *testing.T) { t.Run("should return error Internal if upsert template failed", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) mockedTemplateService.EXPECT().Upsert(mock.AnythingOfType("context.todoCtx"), tmpl).Return(errors.New("random error")).Once() res, err := dummyGRPCServer.UpsertTemplate(context.TODO(), dummyReq) assert.Nil(t, res) @@ -231,7 +242,8 @@ func TestGRPCServer_UpsertTemplate(t *testing.T) { func TestGRPCServer_DeleteTemplate(t *testing.T) { t.Run("should delete template", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.DeleteTemplateRequest{ Name: "foo", } @@ -246,7 +258,8 @@ func TestGRPCServer_DeleteTemplate(t *testing.T) { t.Run("should return error Internal if deleting template failed", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) dummyReq := &sirenv1beta1.DeleteTemplateRequest{ Name: "foo", } @@ -269,7 +282,8 @@ func TestGRPCServer_RenderTemplate(t *testing.T) { t.Run("should render template", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) mockedTemplateService.EXPECT().Render(mock.AnythingOfType("context.todoCtx"), "foo", dummyReq.GetVariables()). Return("random", nil).Once() @@ -281,7 +295,8 @@ func TestGRPCServer_RenderTemplate(t *testing.T) { t.Run("should return error Internal if rendering template failed", func(t *testing.T) { mockedTemplateService := &mocks.TemplateService{} - dummyGRPCServer := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + dummyGRPCServer, err := v1beta1.NewGRPCServer(log.NewNoop(), api.HeadersConfig{}, &api.Deps{TemplateService: mockedTemplateService}) + require.NoError(t, err) mockedTemplateService.EXPECT().Render(mock.AnythingOfType("context.todoCtx"), "foo", dummyReq.GetVariables()). Return("", errors.New("random error")).Once() res, err := dummyGRPCServer.RenderTemplate(context.TODO(), dummyReq) diff --git a/internal/api/v1beta1/v1beta1.go b/internal/api/v1beta1/v1beta1.go index a409b588..ec641497 100644 --- a/internal/api/v1beta1/v1beta1.go +++ b/internal/api/v1beta1/v1beta1.go @@ -2,6 +2,8 @@ package v1beta1 import ( "github.com/goto/salt/log" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/metric" "github.com/goto/siren/internal/api" sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1" @@ -37,13 +39,27 @@ type GRPCServer struct { subscriptionReceiverService api.SubscriptionReceiverService notificationService api.NotificationService silenceService api.SilenceService + + metricBulkNotificationsCount metric.Int64Gauge + metricNotificationReceiverSelectorCount metric.Int64Gauge } func NewGRPCServer( logger log.Logger, headers api.HeadersConfig, apiDeps *api.Deps, - opts ...GRPCServerOption) *GRPCServer { + opts ...GRPCServerOption) (*GRPCServer, error) { + + metricBulkNotificationsCount, err := otel.Meter("github.com/goto/siren/internal/api"). + Int64Gauge("api.bulknotifications.notifications") + if err != nil { + return nil, err + } + metricNotificationReceiverSelectorCount, err := otel.Meter("github.com/goto/siren/internal/api"). + Int64Gauge("api.notification.receiverselectors") + if err != nil { + return nil, err + } s := &GRPCServer{ headers: headers, @@ -58,11 +74,14 @@ func NewGRPCServer( subscriptionReceiverService: apiDeps.SubscriptionReceiverService, notificationService: apiDeps.NotificationService, silenceService: apiDeps.SilenceService, + + metricBulkNotificationsCount: metricBulkNotificationsCount, + metricNotificationReceiverSelectorCount: metricNotificationReceiverSelectorCount, } for _, opt := range opts { opt(s) } - return s + return s, nil } diff --git a/internal/server/server.go b/internal/server/server.go index 0ed9d93f..899182be 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -126,13 +126,17 @@ func RunServer( runtimeCtx, runtimeCancel := context.WithCancel(ctx) defer runtimeCancel() - sirenV1beta1ServiceRPC := v1beta1.NewGRPCServer( + sirenV1beta1ServiceRPC, err := v1beta1.NewGRPCServer( logger, c.APIHeaders, apiDeps, v1beta1.WithGlobalSubscription(c.UseGlobalSubscription), v1beta1.WithDebugRequest(c.DebugRequest), ) + if err != nil { + return err + } + sirenV1ServiceRPC := v1.NewGRPCServer( logger, c.APIHeaders,