diff --git a/api/northbound_test.go b/api/northbound_test.go index e174ee8e..4493e975 100644 --- a/api/northbound_test.go +++ b/api/northbound_test.go @@ -30,7 +30,7 @@ func TestNorthboundDeleteWorkflowsNameHandle_NotFound(t *testing.T) { workflow := "wfx.workflow.test" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().DeleteWorkflow(ctx, workflow).Return(fault.Wrap(errors.New("workflow not found"), ftag.With(ftag.NotFound))) server := NewNorthboundServer(ctx, dbMock) @@ -50,7 +50,7 @@ func TestNorthboundDeleteWorkflowsNameHandle_InternalError(t *testing.T) { workflow := "wfx.workflow.test" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().DeleteWorkflow(ctx, workflow).Return(errors.New("something went wrong")) server := NewNorthboundServer(ctx, dbMock) @@ -64,7 +64,7 @@ func TestNorthboundPostWorkflows_InternalError(t *testing.T) { defer cancel() workflow := dau.DirectWorkflow() - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().CreateWorkflow(ctx, workflow).Return(nil, errors.New("something went wrong")) server := NewNorthboundServer(ctx, dbMock) @@ -79,7 +79,7 @@ func TestNorthboundPostWorkflows_AlreadyExists(t *testing.T) { workflow := dau.DirectWorkflow() - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().CreateWorkflow(ctx, workflow).Return(nil, fault.Wrap(errors.New("already exists"), ftag.With(ftag.AlreadyExists))) server := NewNorthboundServer(ctx, dbMock) @@ -99,7 +99,7 @@ func TestNorthboundPostWorkflows_InvalidWorkflow(t *testing.T) { workflow := &api.Workflow{Name: "foo"} - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) server := NewNorthboundServer(ctx, dbMock) resp, err := server.PostWorkflows(ctx, api.PostWorkflowsRequestObject{Body: workflow}) @@ -119,7 +119,7 @@ func TestNorthboundPostJobs_BadRequest(t *testing.T) { wf := dau.DirectWorkflow() jobRequest := api.JobRequest{Workflow: wf.Name} - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetWorkflow(ctx, wf.Name).Return(nil, fault.Wrap(errors.New("invalid"), ftag.With(ftag.NotFound))) server := NewNorthboundServer(ctx, dbMock) @@ -140,7 +140,7 @@ func TestNorthboundPostJobs_InternalError(t *testing.T) { wf := dau.DirectWorkflow() jobRequest := api.JobRequest{Workflow: wf.Name} - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetWorkflow(ctx, wf.Name).Return(wf, nil) dbMock.EXPECT().CreateJob(ctx, mock.Anything).Return(nil, errors.New("something went wrong")) @@ -156,7 +156,7 @@ func TestNorthboundDeleteJobsID_NotFound(t *testing.T) { jobID := "42" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound))) server := NewNorthboundServer(ctx, dbMock) @@ -176,7 +176,7 @@ func TestNorthboundDeleteJobsID_InternalError(t *testing.T) { jobID := "42" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(&api.Job{ID: jobID}, nil) dbMock.EXPECT().DeleteJob(ctx, jobID).Return(fault.Wrap(errors.New("something went wrong"), ftag.With(ftag.Internal))) @@ -192,7 +192,7 @@ func TestNorthboundPostJobsIDTags_NotFound(t *testing.T) { jobID := "42" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound))) server := NewNorthboundServer(ctx, dbMock) @@ -212,7 +212,7 @@ func TestNorthboundPostJobsIDTags_InternalError(t *testing.T) { jobID := "42" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("something went wrong"), ftag.With(ftag.Internal))) server := NewNorthboundServer(ctx, dbMock) @@ -227,7 +227,7 @@ func TestNorthboundDeleteJobsIDTags_NotFound(t *testing.T) { jobID := "42" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound))) server := NewNorthboundServer(ctx, dbMock) @@ -247,7 +247,7 @@ func TestNorthboundDeleteJobsIDTags_InternalError(t *testing.T) { jobID := "42" - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.Internal))) server := NewNorthboundServer(ctx, dbMock) diff --git a/api/southbound_test.go b/api/southbound_test.go index 111d16e8..6d35d1cc 100644 --- a/api/southbound_test.go +++ b/api/southbound_test.go @@ -22,7 +22,6 @@ import ( "github.com/siemens/wfx/generated/api" "github.com/siemens/wfx/persistence" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -47,7 +46,7 @@ func TestGetJobsIDStatus_NotFound(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound))) server := createServerForTesting(ctx, orientation, dbMock) @@ -70,7 +69,7 @@ func TestGetJobsIDStatus_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong")) server := createServerForTesting(ctx, orientation, dbMock) @@ -88,7 +87,7 @@ func TestPutJobsIDStatus_NotFound(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound))) server := createServerForTesting(ctx, orientation, dbMock) @@ -115,7 +114,7 @@ func TestPutJobsIDStatus_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong")) server := createServerForTesting(ctx, orientation, dbMock) @@ -132,7 +131,7 @@ func TestGetJobs_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT(). QueryJobs(ctx, persistence.FilterParams{}, persistence.SortParams{}, persistence.PaginationParams{Limit: 10}). Return(nil, errors.New("something went wrong")) @@ -152,7 +151,7 @@ func TestGetJobsID_NotFound(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound))) server := createServerForTesting(ctx, orientation, dbMock) @@ -176,7 +175,7 @@ func TestGetJobsID_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { history := true - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{History: history}).Return(nil, errors.New("something went wrong")) server := createServerForTesting(ctx, orientation, dbMock) @@ -194,7 +193,7 @@ func TestGetJobsIDDefinition_NotFound(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound))) server := createServerForTesting(ctx, orientation, dbMock) @@ -217,7 +216,7 @@ func TestGetJobsIDDefinition_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong")) server := createServerForTesting(ctx, orientation, dbMock) @@ -235,7 +234,7 @@ func TestPutJobsIDDefinition_NotFound(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound))) server := createServerForTesting(ctx, orientation, dbMock) @@ -258,7 +257,7 @@ func TestPutJobsIDDefinition_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong")) server := createServerForTesting(ctx, orientation, dbMock) @@ -276,7 +275,7 @@ func TestGetWorkflowsName_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetWorkflow(ctx, workflow).Return(nil, errors.New("something went wrong")) server := createServerForTesting(ctx, orientation, dbMock) @@ -293,7 +292,7 @@ func TestGetWorkflows_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().QueryWorkflows(ctx, persistence.PaginationParams{Limit: 10}).Return(nil, errors.New("something went wrong")) server := createServerForTesting(ctx, orientation, dbMock) @@ -310,7 +309,7 @@ func TestGetWorkflows_Empty(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().QueryWorkflows(ctx, persistence.PaginationParams{Limit: 10}).Return(&api.PaginatedWorkflowList{}, nil) server := createServerForTesting(ctx, orientation, dbMock) @@ -333,7 +332,7 @@ func TestGetJobsIDTags_NotFound(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound))) server := createServerForTesting(ctx, orientation, dbMock) @@ -356,7 +355,7 @@ func TestGetJobsIDTags_InternalError(t *testing.T) { for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("something went wrong"), ftag.With(ftag.Internal))) server := createServerForTesting(ctx, orientation, dbMock) @@ -372,8 +371,7 @@ func TestHealth(t *testing.T) { defer cancel() for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) - dbMock.EXPECT().CheckHealth(mock.Anything).Return(nil) + dbMock := persistence.NewHealthyMockStorage(t) server := createServerForTesting(ctx, orientation, dbMock) var response *http.Response for i := 0; i < 10; i++ { @@ -395,9 +393,9 @@ func TestHealth(t *testing.T) { func TestVersion(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + dbMock := persistence.NewHealthyMockStorage(t) for _, orientation := range allOrientations { t.Run(orientation, func(t *testing.T) { - dbMock := persistence.NewMockStorage(t) server := createServerForTesting(ctx, orientation, dbMock) resp, err := server.GetVersion(ctx, api.GetVersionRequestObject{}) require.NoError(t, err) diff --git a/api/wfx.go b/api/wfx.go index 64a7d240..99321cac 100644 --- a/api/wfx.go +++ b/api/wfx.go @@ -44,7 +44,7 @@ func (server WfxServer) Shutdown() { func newWfxServer(ctx context.Context, storage persistence.Storage) WfxServer { checker := health.NewChecker( health.WithTimeout(10*time.Second), - health.WithPeriodicCheck(30*time.Second, time.Second, health.Check{ + health.WithPeriodicCheck(30*time.Second, 0, health.Check{ Name: "persistence", Check: func(ctx context.Context) error { return fault.Wrap(storage.CheckHealth(ctx)) diff --git a/cmd/wfx/cmd/root/plugins_test.go b/cmd/wfx/cmd/root/plugins_test.go index 17b711f5..817c08c8 100644 --- a/cmd/wfx/cmd/root/plugins_test.go +++ b/cmd/wfx/cmd/root/plugins_test.go @@ -186,7 +186,7 @@ func TestCreatePluginMiddlewares(t *testing.T) { } func TestCreateNorthServer_PluginsDir(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) sc, err := createNorthServer(ctx, new(config.AppConfig), dbMock, []plugin.Plugin{}) @@ -195,7 +195,7 @@ func TestCreateNorthServer_PluginsDir(t *testing.T) { } func TestCreateSouthServer_PluginsDir(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) sc, err := createSouthServer(ctx, new(config.AppConfig), dbMock, []plugin.Plugin{}) diff --git a/internal/handler/job/delete_test.go b/internal/handler/job/delete_test.go index 7caa36db..6a515c17 100644 --- a/internal/handler/job/delete_test.go +++ b/internal/handler/job/delete_test.go @@ -48,7 +48,7 @@ func TestDeleteJob_NotFound(t *testing.T) { } func TestDeleteJob_Error(t *testing.T) { - dbMock := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) ctx := context.Background() jobID := "42" dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{History: false}).Return(&api.Job{ID: jobID}, nil) diff --git a/internal/handler/job/tags/add_test.go b/internal/handler/job/tags/add_test.go index b05fc035..60acef65 100644 --- a/internal/handler/job/tags/add_test.go +++ b/internal/handler/job/tags/add_test.go @@ -53,28 +53,28 @@ func TestAdd(t *testing.T) { } func TestAdd_FaultyStorageGet(t *testing.T) { - db := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) ctx := context.Background() expectedErr := errors.New("mock error") - db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr) + dbMock.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr) - tags, err := Add(ctx, db, "1", []string{"foo", "bar"}) + tags, err := Add(ctx, dbMock, "1", []string{"foo", "bar"}) assert.Nil(t, tags) assert.NotNil(t, err) } func TestAdd_FaultyStorageUpdate(t *testing.T) { - db := persistence.NewMockStorage(t) + dbMock := persistence.NewHealthyMockStorage(t) ctx := context.Background() expectedErr := errors.New("mock error") dummyJob := api.Job{ID: "1"} tags := []string{"foo", "bar"} - db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(&dummyJob, nil) - db.On("UpdateJob", ctx, &dummyJob, persistence.JobUpdate{AddTags: &tags}).Return(nil, expectedErr) + dbMock.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(&dummyJob, nil) + dbMock.On("UpdateJob", ctx, &dummyJob, persistence.JobUpdate{AddTags: &tags}).Return(nil, expectedErr) - tags, err := Add(ctx, db, "1", tags) + tags, err := Add(ctx, dbMock, "1", tags) assert.Nil(t, tags) assert.NotNil(t, err) } diff --git a/internal/handler/job/tags/delete_test.go b/internal/handler/job/tags/delete_test.go index ac94671b..0ead7c2a 100644 --- a/internal/handler/job/tags/delete_test.go +++ b/internal/handler/job/tags/delete_test.go @@ -50,7 +50,7 @@ func TestDelete(t *testing.T) { } func TestDelete_FaultyStorageGet(t *testing.T) { - db := persistence.NewMockStorage(t) + db := persistence.NewHealthyMockStorage(t) ctx := context.Background() expectedErr := errors.New("mock error") db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr) @@ -61,7 +61,7 @@ func TestDelete_FaultyStorageGet(t *testing.T) { } func TestDelete_FaultyStorageUpdate(t *testing.T) { - db := persistence.NewMockStorage(t) + db := persistence.NewHealthyMockStorage(t) ctx := context.Background() expectedErr := errors.New("mock error") diff --git a/internal/handler/workflow/get_test.go b/internal/handler/workflow/get_test.go index 5479363c..304d35c4 100644 --- a/internal/handler/workflow/get_test.go +++ b/internal/handler/workflow/get_test.go @@ -34,10 +34,10 @@ func TestGetWorkflow(t *testing.T) { func TestGetWorkflow_NotFound(t *testing.T) { ctx := context.Background() - db := persistence.NewMockStorage(t) - db.EXPECT().GetWorkflow(ctx, "foo").Return(nil, fault.Wrap(errors.New("Not found"), ftag.With(ftag.NotFound))) + dbMock := persistence.NewHealthyMockStorage(t) + dbMock.EXPECT().GetWorkflow(ctx, "foo").Return(nil, fault.Wrap(errors.New("Not found"), ftag.With(ftag.NotFound))) - wf, err := GetWorkflow(ctx, db, "foo") + wf, err := GetWorkflow(ctx, dbMock, "foo") assert.Nil(t, wf) assert.NotNil(t, err) } @@ -45,10 +45,10 @@ func TestGetWorkflow_NotFound(t *testing.T) { func TestGetWorkflow_Internal(t *testing.T) { ctx := context.Background() - db := persistence.NewMockStorage(t) - db.EXPECT().GetWorkflow(ctx, "foo").Return(nil, fault.Wrap(errors.New("Not found"), ftag.With(ftag.Internal))) + dbMock := persistence.NewHealthyMockStorage(t) + dbMock.EXPECT().GetWorkflow(ctx, "foo").Return(nil, fault.Wrap(errors.New("Not found"), ftag.With(ftag.Internal))) - wf, err := GetWorkflow(ctx, db, "foo") + wf, err := GetWorkflow(ctx, dbMock, "foo") assert.Nil(t, wf) assert.NotNil(t, err) } diff --git a/persistence/all_test.go b/persistence/all_test.go index fc017afd..3cef5c85 100644 --- a/persistence/all_test.go +++ b/persistence/all_test.go @@ -15,13 +15,13 @@ import ( ) func TestStorageAPI(t *testing.T) { - storage1 := NewMockStorage(t) + storage1 := NewHealthyMockStorage(t) RegisterStorage("storage1", storage1) actual := GetStorage("storage1") assert.Same(t, storage1, actual) - storage2 := NewMockStorage(t) + storage2 := NewHealthyMockStorage(t) RegisterStorage("storage2", storage2) all := Storages() assert.Len(t, all, 2) diff --git a/persistence/mock.go b/persistence/mock.go new file mode 100644 index 00000000..b555ba1f --- /dev/null +++ b/persistence/mock.go @@ -0,0 +1,22 @@ +package persistence + +/* + * SPDX-FileCopyrightText: 2024 Siemens AG + * + * SPDX-License-Identifier: Apache-2.0 + * + * Author: Michael Adler + */ + +import ( + "testing" + + mock "github.com/stretchr/testify/mock" +) + +func NewHealthyMockStorage(t *testing.T) *MockStorage { + m := new(MockStorage) + m.Mock.Test(t) + m.On("CheckHealth", mock.Anything).Return(nil) + return m +}