Skip to content

Commit

Permalink
test(kubernetes): DeleteClusterRole
Browse files Browse the repository at this point in the history
Signed-off-by: Chin-Ya Huang <[email protected]>
  • Loading branch information
c3y1huang committed Apr 29, 2024
1 parent f7c1fc3 commit e5d2c54
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion kubernetes/clusterrole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}
}

0 comments on commit e5d2c54

Please sign in to comment.