-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add an unit test to cover AsResult method
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |