Skip to content

Commit

Permalink
test: add an unit test to cover AsResult method
Browse files Browse the repository at this point in the history
Signed-off-by: Jiawei Zhao <Phoenxi500526@163.com>
  • Loading branch information
Jiawei Zhao authored and Jiawei Zhao committed Jan 14, 2024
1 parent 4932f0d commit 8e68fc1
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions internal/controller/sharded_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package controller

import (
"errors"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
update_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

func TestAsResult(t *testing.T) {
// Test case 1: No errors
errSet := StCtrlErrSet{}
result, err := errSet.AsResult()
assert.NoError(t, err)
assert.Equal(t, ctrl.Result{}, result)

// Test case 2: Update conflict error
statusErr := update_errors.StatusError{ErrStatus: metav1.Status{
Reason: metav1.StatusReasonConflict,
Code: http.StatusConflict,
}}
errSet = StCtrlErrSet{Update: error(&statusErr)}
result, err = errSet.AsResult()
assert.NoError(t, err)
assert.Equal(t, ctrl.Result{Requeue: true}, result)

// Test case 3: Other Errors
errSet = StCtrlErrSet{
Rec: errors.New("reconciliation error"),
Sync: errors.New("sync error"),
Update: errors.New("sync error"),
}
result, err = errSet.AsResult()
assert.Error(t, err)
assert.Equal(t, ctrl.Result{Requeue: true}, result)
}

0 comments on commit 8e68fc1

Please sign in to comment.