|
17 | 17 | package kubeclient |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "testing" |
21 | | - |
| 20 | + "github.com/fluid-cloudnative/fluid/pkg/utils/fake" |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
22 | 23 | corev1 "k8s.io/api/core/v1" |
23 | 24 | rbacv1 "k8s.io/api/rbac/v1" |
24 | 25 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
25 | 26 | "k8s.io/apimachinery/pkg/runtime" |
26 | | - |
27 | | - "github.com/fluid-cloudnative/fluid/pkg/utils/fake" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
28 | 28 | ) |
29 | 29 |
|
30 | | -func TestDeleteServiceAccount(t *testing.T) { |
31 | | - namespace := "default" |
32 | | - testSAInput := []*corev1.ServiceAccount{{ |
33 | | - ObjectMeta: metav1.ObjectMeta{Name: "test1", Namespace: namespace}, |
34 | | - }} |
35 | | - |
36 | | - testServiceAccounts := []runtime.Object{} |
37 | | - |
38 | | - for _, ns := range testSAInput { |
39 | | - testServiceAccounts = append(testServiceAccounts, ns.DeepCopy()) |
40 | | - } |
41 | | - |
42 | | - fakeClient := fake.NewFakeClientWithScheme(testScheme, testServiceAccounts...) |
43 | | - type args struct { |
44 | | - name string |
45 | | - namespace string |
46 | | - } |
47 | | - tests := []struct { |
48 | | - name string |
49 | | - args args |
50 | | - wantErr bool |
51 | | - }{ |
52 | | - { |
53 | | - name: "sa doesn't exist", |
54 | | - args: args{ |
55 | | - name: "notExist", |
56 | | - namespace: namespace, |
57 | | - }, |
58 | | - wantErr: false, |
59 | | - }, |
60 | | - { |
61 | | - name: "sa exist", |
62 | | - args: args{ |
63 | | - name: "test1", |
64 | | - namespace: namespace, |
| 30 | +var _ = Describe("DeleteServiceAccount", func() { |
| 31 | + var ( |
| 32 | + namespace string |
| 33 | + testSAInput []*corev1.ServiceAccount |
| 34 | + testServiceAccounts []runtime.Object |
| 35 | + fakeClient client.Client |
| 36 | + ) |
| 37 | + |
| 38 | + BeforeEach(func() { |
| 39 | + namespace = "default" |
| 40 | + testSAInput = []*corev1.ServiceAccount{ |
| 41 | + { |
| 42 | + ObjectMeta: metav1.ObjectMeta{Name: "test1", Namespace: namespace}, |
65 | 43 | }, |
66 | | - wantErr: false, |
67 | | - }, |
68 | | - } |
69 | | - for _, tt := range tests { |
70 | | - t.Run(tt.name, func(t *testing.T) { |
71 | | - if err := DeleteServiceAccount(fakeClient, tt.args.name, tt.args.namespace); (err != nil) != tt.wantErr { |
72 | | - t.Errorf("DeleteServiceAccount() error = %v, wantErr %v", err, tt.wantErr) |
73 | | - } |
| 44 | + } |
| 45 | + |
| 46 | + testServiceAccounts = []runtime.Object{} |
| 47 | + for _, ns := range testSAInput { |
| 48 | + testServiceAccounts = append(testServiceAccounts, ns.DeepCopy()) |
| 49 | + } |
| 50 | + |
| 51 | + fakeClient = fake.NewFakeClientWithScheme(testScheme, testServiceAccounts...) |
| 52 | + }) |
| 53 | + |
| 54 | + Context("when service account doesn't exist", func() { |
| 55 | + It("should not return an error", func() { |
| 56 | + err := DeleteServiceAccount(fakeClient, "notExist", namespace) |
| 57 | + Expect(err).NotTo(HaveOccurred()) |
74 | 58 | }) |
75 | | - } |
76 | | -} |
77 | | - |
78 | | -func TestDeleteRole(t *testing.T) { |
79 | | - namespace := "default" |
80 | | - testRoleInput := []*rbacv1.Role{{ |
81 | | - ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: namespace}, |
82 | | - }} |
83 | | - |
84 | | - testRole := []runtime.Object{} |
85 | | - |
86 | | - for _, ns := range testRoleInput { |
87 | | - testRole = append(testRole, ns.DeepCopy()) |
88 | | - } |
89 | | - |
90 | | - fakeClient := fake.NewFakeClientWithScheme(testScheme, testRole...) |
91 | | - type args struct { |
92 | | - name string |
93 | | - namespace string |
94 | | - } |
95 | | - tests := []struct { |
96 | | - name string |
97 | | - args args |
98 | | - wantErr bool |
99 | | - }{ |
100 | | - { |
101 | | - name: "test role not exist", |
102 | | - args: args{ |
103 | | - name: "notExist", |
104 | | - namespace: namespace, |
105 | | - }, |
106 | | - wantErr: false, |
107 | | - }, |
108 | | - { |
109 | | - name: "test role exist", |
110 | | - args: args{ |
111 | | - name: "test", |
112 | | - namespace: namespace, |
113 | | - }, |
114 | | - wantErr: false, |
115 | | - }, |
116 | | - } |
117 | | - for _, tt := range tests { |
118 | | - t.Run(tt.name, func(t *testing.T) { |
119 | | - if err := DeleteRole(fakeClient, tt.args.name, tt.args.namespace); (err != nil) != tt.wantErr { |
120 | | - t.Errorf("DeleteRole() error = %v, wantErr %v", err, tt.wantErr) |
121 | | - } |
| 59 | + }) |
| 60 | + |
| 61 | + Context("when service account exists", func() { |
| 62 | + It("should delete successfully without error", func() { |
| 63 | + err := DeleteServiceAccount(fakeClient, "test1", namespace) |
| 64 | + Expect(err).NotTo(HaveOccurred()) |
122 | 65 | }) |
123 | | - } |
124 | | -} |
125 | | - |
126 | | -func TestDeleteRoleBinding(t *testing.T) { |
127 | | - namespace := "default" |
128 | | - testRoleBindingInput := []*rbacv1.RoleBinding{{ |
129 | | - ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: namespace}, |
130 | | - }} |
131 | | - |
132 | | - testRoleBinding := []runtime.Object{} |
133 | | - |
134 | | - for _, ns := range testRoleBindingInput { |
135 | | - testRoleBinding = append(testRoleBinding, ns.DeepCopy()) |
136 | | - } |
137 | | - |
138 | | - fakeClient := fake.NewFakeClientWithScheme(testScheme, testRoleBinding...) |
139 | | - type args struct { |
140 | | - name string |
141 | | - namespace string |
142 | | - } |
143 | | - tests := []struct { |
144 | | - name string |
145 | | - args args |
146 | | - wantErr bool |
147 | | - }{ |
148 | | - { |
149 | | - name: "test rolebinding not exist", |
150 | | - args: args{ |
151 | | - name: "notExist", |
152 | | - namespace: namespace, |
| 66 | + }) |
| 67 | +}) |
| 68 | + |
| 69 | +var _ = Describe("DeleteRole", func() { |
| 70 | + var ( |
| 71 | + namespace string |
| 72 | + testRoleInput []*rbacv1.Role |
| 73 | + testRole []runtime.Object |
| 74 | + fakeClient client.Client |
| 75 | + ) |
| 76 | + |
| 77 | + BeforeEach(func() { |
| 78 | + namespace = "default" |
| 79 | + testRoleInput = []*rbacv1.Role{ |
| 80 | + { |
| 81 | + ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: namespace}, |
153 | 82 | }, |
154 | | - wantErr: false, |
155 | | - }, |
156 | | - { |
157 | | - name: "test rolebinding exist", |
158 | | - args: args{ |
159 | | - name: "test", |
160 | | - namespace: namespace, |
| 83 | + } |
| 84 | + |
| 85 | + testRole = []runtime.Object{} |
| 86 | + for _, ns := range testRoleInput { |
| 87 | + testRole = append(testRole, ns.DeepCopy()) |
| 88 | + } |
| 89 | + |
| 90 | + fakeClient = fake.NewFakeClientWithScheme(testScheme, testRole...) |
| 91 | + }) |
| 92 | + |
| 93 | + Context("when role doesn't exist", func() { |
| 94 | + It("should not return an error", func() { |
| 95 | + err := DeleteRole(fakeClient, "notExist", namespace) |
| 96 | + Expect(err).NotTo(HaveOccurred()) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + Context("when role exists", func() { |
| 101 | + It("should delete successfully without error", func() { |
| 102 | + err := DeleteRole(fakeClient, "test", namespace) |
| 103 | + Expect(err).NotTo(HaveOccurred()) |
| 104 | + }) |
| 105 | + }) |
| 106 | +}) |
| 107 | + |
| 108 | +var _ = Describe("DeleteRoleBinding", func() { |
| 109 | + var ( |
| 110 | + namespace string |
| 111 | + testRoleBindingInput []*rbacv1.RoleBinding |
| 112 | + testRoleBinding []runtime.Object |
| 113 | + fakeClient client.Client |
| 114 | + ) |
| 115 | + |
| 116 | + BeforeEach(func() { |
| 117 | + namespace = "default" |
| 118 | + testRoleBindingInput = []*rbacv1.RoleBinding{ |
| 119 | + { |
| 120 | + ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: namespace}, |
161 | 121 | }, |
162 | | - wantErr: false, |
163 | | - }, |
164 | | - } |
165 | | - for _, tt := range tests { |
166 | | - t.Run(tt.name, func(t *testing.T) { |
167 | | - if err := DeleteRoleBinding(fakeClient, tt.args.name, tt.args.namespace); (err != nil) != tt.wantErr { |
168 | | - t.Errorf("DeleteRoleBinding() error = %v, wantErr %v", err, tt.wantErr) |
169 | | - } |
| 122 | + } |
| 123 | + |
| 124 | + testRoleBinding = []runtime.Object{} |
| 125 | + for _, ns := range testRoleBindingInput { |
| 126 | + testRoleBinding = append(testRoleBinding, ns.DeepCopy()) |
| 127 | + } |
| 128 | + |
| 129 | + fakeClient = fake.NewFakeClientWithScheme(testScheme, testRoleBinding...) |
| 130 | + }) |
| 131 | + |
| 132 | + Context("when role binding doesn't exist", func() { |
| 133 | + It("should not return an error", func() { |
| 134 | + err := DeleteRoleBinding(fakeClient, "notExist", namespace) |
| 135 | + Expect(err).NotTo(HaveOccurred()) |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + Context("when role binding exists", func() { |
| 140 | + It("should delete successfully without error", func() { |
| 141 | + err := DeleteRoleBinding(fakeClient, "test", namespace) |
| 142 | + Expect(err).NotTo(HaveOccurred()) |
170 | 143 | }) |
171 | | - } |
172 | | -} |
| 144 | + }) |
| 145 | +}) |
0 commit comments