@@ -243,6 +243,79 @@ var _ = Describe("object rbac transformer test.", func() {
243243 Expect (reflect .DeepEqual (rb .RoleRef , cmpdRoleBinding .RoleRef )).To (BeTrue ())
244244 })
245245 })
246+
247+ Context ("tests serviceaccount rollback" , func () {
248+ It ("tests needRollbackServiceAccount basic cases" , func () {
249+ init (true , false )
250+ ctx := transCtx .(* componentTransformContext )
251+
252+ By ("create another cmpd" )
253+ anotherCompDef := testapps .NewComponentDefinitionFactory (compDefName ).
254+ WithRandomName ().
255+ SetDefaultSpec ().
256+ Create (& testCtx ).
257+ GetObject ()
258+
259+ // Case 1: No label, should return false
260+ needRollback , err := needRollbackServiceAccount (ctx )
261+ Expect (err ).Should (BeNil ())
262+ Expect (needRollback ).Should (BeFalse ())
263+
264+ // Case 2: With label but component definitions are the same
265+ ctx .Component .Labels [constant .ComponentLastServiceAccountNameLabelKey ] = "kb-" + compDefObj .Name
266+ needRollback , err = needRollbackServiceAccount (ctx )
267+ Expect (err ).Should (BeNil ())
268+ Expect (needRollback ).Should (BeFalse ())
269+
270+ // Case 3: Different cmpd, same spec
271+ ctx .Component .Labels [constant .ComponentLastServiceAccountNameLabelKey ] = "kb-" + anotherCompDef .Name
272+ needRollback , err = needRollbackServiceAccount (ctx )
273+ Expect (err ).Should (BeNil ())
274+ Expect (needRollback ).Should (BeTrue ())
275+
276+ // Case 4: Different cmpd, different policy rules
277+ Expect (testapps .ChangeObj (& testCtx , anotherCompDef , func (cmpd * appsv1.ComponentDefinition ) {
278+ cmpd .Spec .PolicyRules = []rbacv1.PolicyRule {
279+ {
280+ APIGroups : []string {"" },
281+ Resources : []string {"pods" },
282+ Verbs : []string {"get" },
283+ },
284+ }
285+ })).Should (Succeed ())
286+ needRollback , err = needRollbackServiceAccount (ctx )
287+ Expect (err ).Should (BeNil ())
288+ Expect (needRollback ).Should (BeFalse ())
289+
290+ // Case 5: Different cmpd, different lifecycle action
291+ Expect (testapps .ChangeObj (& testCtx , anotherCompDef , func (cmpd * appsv1.ComponentDefinition ) {
292+ cmpd .Spec .PolicyRules = nil
293+ cmpd .Spec .LifecycleActions = nil
294+ })).Should (Succeed ())
295+ needRollback , err = needRollbackServiceAccount (ctx )
296+ Expect (err ).Should (BeNil ())
297+ Expect (needRollback ).Should (BeFalse ())
298+ })
299+
300+ It ("tests needRollbackServiceAccount error cases" , func () {
301+ init (false , false )
302+ ctx := transCtx .(* componentTransformContext )
303+
304+ // Case 1: Invalid label format
305+ ctx .Component .Labels = map [string ]string {
306+ constant .ComponentLastServiceAccountNameLabelKey : "invalid-format" ,
307+ }
308+ _ , err := needRollbackServiceAccount (ctx )
309+ Expect (err ).Should (HaveOccurred ())
310+
311+ // Case 2: Component definition not found
312+ ctx .Component .Labels = map [string ]string {
313+ constant .ComponentLastServiceAccountNameLabelKey : "kb-non-existent" ,
314+ }
315+ _ , err = needRollbackServiceAccount (ctx )
316+ Expect (err ).Should (HaveOccurred ())
317+ })
318+ })
246319})
247320
248321func mockDAG (graphCli model.GraphClient , comp * appsv1.Component ) * graph.DAG {
0 commit comments