From 8e68fc1a64f0d63aaeea46b33b9491b0db7a6199 Mon Sep 17 00:00:00 2001 From: Jiawei Zhao Date: Sat, 13 Jan 2024 22:15:00 +0800 Subject: [PATCH] test: add an unit test to cover AsResult method Signed-off-by: Jiawei Zhao --- internal/controller/sharded_test.go | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 internal/controller/sharded_test.go diff --git a/internal/controller/sharded_test.go b/internal/controller/sharded_test.go new file mode 100644 index 0000000..15f2ce6 --- /dev/null +++ b/internal/controller/sharded_test.go @@ -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) +}