diff --git a/backend/plugins/opsgenie/tasks/incidents_collector.go b/backend/plugins/opsgenie/tasks/incidents_collector.go index 70100f149b9..6f346f19cb8 100644 --- a/backend/plugins/opsgenie/tasks/incidents_collector.go +++ b/backend/plugins/opsgenie/tasks/incidents_collector.go @@ -130,7 +130,7 @@ func CollectIncidents(taskCtx plugin.SubTaskContext) errors.Error { }, }) if err != nil { - return nil + return err } return collector.Execute() } diff --git a/backend/plugins/opsgenie/tasks/incidents_collector_test.go b/backend/plugins/opsgenie/tasks/incidents_collector_test.go new file mode 100644 index 00000000000..d0cfa0c8186 --- /dev/null +++ b/backend/plugins/opsgenie/tasks/incidents_collector_test.go @@ -0,0 +1,48 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package tasks + +import ( + "testing" + + "github.com/apache/devlake/core/errors" + "github.com/apache/devlake/helpers/unithelper" + mockdal "github.com/apache/devlake/mocks/core/dal" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +func TestCollectIncidentsReturnsCollectorSetupError(t *testing.T) { + // Loading the previous collector state fails, so the collector can't be created. + dbErr := errors.Default.New("db unavailable") + mockDal := new(mockdal.Dal) + mockDal.On("First", mock.Anything, mock.Anything).Return(dbErr).Once() + mockDal.On("IsErrorNotFound", dbErr).Return(false).Once() + + taskCtx := unithelper.DummySubTaskContext(mockDal) + taskCtx.On("GetData").Return(&OpsgenieTaskData{ + Options: &OpsgenieOptions{ConnectionId: 1, ServiceId: "service-1", ServiceName: "service"}, + }) + + err := CollectIncidents(taskCtx) + + if assert.NotNil(t, err) { + assert.Contains(t, err.Error(), "db unavailable") + } + mockDal.AssertExpectations(t) +} diff --git a/backend/plugins/pagerduty/tasks/incidents_collector.go b/backend/plugins/pagerduty/tasks/incidents_collector.go index 81cc2042689..6d1862cd14a 100644 --- a/backend/plugins/pagerduty/tasks/incidents_collector.go +++ b/backend/plugins/pagerduty/tasks/incidents_collector.go @@ -139,7 +139,7 @@ func CollectIncidents(taskCtx plugin.SubTaskContext) errors.Error { }, }) if err != nil { - return nil + return err } return collector.Execute() } diff --git a/backend/plugins/pagerduty/tasks/incidents_collector_test.go b/backend/plugins/pagerduty/tasks/incidents_collector_test.go new file mode 100644 index 00000000000..bc99d990c80 --- /dev/null +++ b/backend/plugins/pagerduty/tasks/incidents_collector_test.go @@ -0,0 +1,48 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package tasks + +import ( + "testing" + + "github.com/apache/devlake/core/errors" + "github.com/apache/devlake/helpers/unithelper" + mockdal "github.com/apache/devlake/mocks/core/dal" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +func TestCollectIncidentsReturnsCollectorSetupError(t *testing.T) { + // Loading the previous collector state fails, so the collector can't be created. + dbErr := errors.Default.New("db unavailable") + mockDal := new(mockdal.Dal) + mockDal.On("First", mock.Anything, mock.Anything).Return(dbErr).Once() + mockDal.On("IsErrorNotFound", dbErr).Return(false).Once() + + taskCtx := unithelper.DummySubTaskContext(mockDal) + taskCtx.On("GetData").Return(&PagerDutyTaskData{ + Options: &PagerDutyOptions{ConnectionId: 1, ServiceId: "service-1", ServiceName: "service"}, + }) + + err := CollectIncidents(taskCtx) + + if assert.NotNil(t, err) { + assert.Contains(t, err.Error(), "db unavailable") + } + mockDal.AssertExpectations(t) +}