From e5d2c54c3d7e6cc889f58dc24b89d75dbc1e78c7 Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Mon, 29 Apr 2024 15:28:56 +0800 Subject: [PATCH] test(kubernetes): DeleteClusterRole Signed-off-by: Chin-Ya Huang --- kubernetes/clusterrole_test.go | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/kubernetes/clusterrole_test.go b/kubernetes/clusterrole_test.go index 63fea84d..ff8654a4 100644 --- a/kubernetes/clusterrole_test.go +++ b/kubernetes/clusterrole_test.go @@ -6,9 +6,11 @@ import ( "github.com/longhorn/go-common-libs/test" . "gopkg.in/check.v1" + "k8s.io/client-go/kubernetes/fake" + rbacv1 "k8s.io/api/rbac/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes/fake" ) func (s *TestSuite) TestCreateClusterRole(c *C) { @@ -54,3 +56,50 @@ func (s *TestSuite) TestCreateClusterRole(c *C) { c.Assert(clusterRole.Name, Equals, testCase.clusterRole.Name, Commentf(test.ErrResultFmt, testName)) } } + +func (s *TestSuite) TestDeleteClusterRole(c *C) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + type testCase struct { + clusterRole *rbacv1.ClusterRole + expectNotFound bool + } + testCases := map[string]testCase{ + "DeleteClusterRole(...):": { + clusterRole: &rbacv1.ClusterRole{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + }, + }, + }, + "DeleteClusterRole(...): not found": { + clusterRole: &rbacv1.ClusterRole{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + }, + }, + expectNotFound: true, + }, + } + for testName, testCase := range testCases { + c.Logf("testing kubernetes.%v", testName) + + kubeClient := fake.NewSimpleClientset() + + if !testCase.expectNotFound { + _, err := kubeClient.RbacV1().ClusterRoles().Create(ctx, testCase.clusterRole, metav1.CreateOptions{}) + c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName)) + + clusterRole, err := kubeClient.RbacV1().ClusterRoles().Get(ctx, testCase.clusterRole.Name, metav1.GetOptions{}) + c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName)) + c.Assert(clusterRole.Name, Equals, testCase.clusterRole.Name, Commentf(test.ErrResultFmt, testName)) + } + + err := DeleteClusterRole(kubeClient, testCase.clusterRole.Name) + c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName)) + + _, err = kubeClient.RbacV1().ClusterRoles().Get(ctx, testCase.clusterRole.Name, metav1.GetOptions{}) + c.Assert(apierrors.IsNotFound(err), Equals, true, Commentf(test.ErrResultFmt, testName)) + } +}