diff --git a/api/gitops/workflows/delete_test.go b/api/gitops/workflows/delete_test.go index 8a1333f50c..afc72e0c59 100644 --- a/api/gitops/workflows/delete_test.go +++ b/api/gitops/workflows/delete_test.go @@ -1,9 +1,11 @@ package workflows import ( + "errors" "testing" portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/datastore" "github.com/stretchr/testify/require" @@ -45,6 +47,18 @@ func TestDeleteIfSingleArtifact_MultipleArtifactsAreKept(t *testing.T) { require.NoError(t, err) } +// TestDeleteIfSingleArtifact_ReadErrorPropagates covers the branch where the Workflow lookup +// fails for a reason other than not-found (e.g. a corrupt record); that error must still +// surface to the caller instead of being swallowed alongside the not-found case. +func TestDeleteIfSingleArtifact_ReadErrorPropagates(t *testing.T) { + t.Parallel() + wantErr := errors.New("boom") + store := erroringWorkflowStore{err: wantErr} + + err := DeleteIfSingleArtifact(store, 1) + require.ErrorIs(t, err, wantErr) +} + func TestDeleteIfSingleArtifact_SingleArtifactIsDeleted(t *testing.T) { t.Parallel() _, store := datastore.MustNewTestStore(t, false, true) @@ -61,3 +75,22 @@ func TestDeleteIfSingleArtifact_SingleArtifactIsDeleted(t *testing.T) { _, err = store.Workflow().Read(wf.ID) require.Error(t, err) } + +// erroringWorkflowStore is a workflowDeleteStore whose Workflow().Read always fails with a +// non-not-found error, to exercise the error-propagation branch of DeleteIfSingleArtifact. +type erroringWorkflowStore struct { + err error +} + +func (s erroringWorkflowStore) Workflow() dataservices.WorkflowService { + return erroringWorkflowService{err: s.err} +} + +type erroringWorkflowService struct { + dataservices.WorkflowService + err error +} + +func (s erroringWorkflowService) Read(portainer.WorkflowID) (*portainer.Workflow, error) { + return nil, s.err +}