From 157090942f20ca9cab226f12444ab385c54c9f92 Mon Sep 17 00:00:00 2001 From: Jiahui <4543bxy@gmail.com> Date: Mon, 10 Jul 2023 18:03:38 +0800 Subject: [PATCH] fix account cause error (#3485) --- .../account/controllers/account_controller.go | 8 ++++-- .../controllers/account_controller_test.go | 25 ++++++++++--------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/controllers/account/controllers/account_controller.go b/controllers/account/controllers/account_controller.go index d9b09fb8ef7..beb101335ce 100644 --- a/controllers/account/controllers/account_controller.go +++ b/controllers/account/controllers/account_controller.go @@ -212,7 +212,10 @@ func (r *AccountReconciler) syncAccount(ctx context.Context, name, accountNamesp if err := r.syncRoleAndRoleBinding(ctx, name, userNamespace); err != nil { return nil, fmt.Errorf("sync role and rolebinding failed: %v", err) } - + err := r.initBalance(&account) + if err != nil { + return nil, fmt.Errorf("sync init balance failed: %v", err) + } // add account balance when account is new user stringAmount := os.Getenv(NEWACCOUNTAMOUNTENV) if stringAmount == "" { @@ -497,6 +500,7 @@ const ( Threshold3 = 1999 * BaseUnit Threshold4 = 4999 * BaseUnit Threshold5 = 19999 * BaseUnit + Ratio0 int64 = 0 Ratio1 int64 = 10 Ratio2 int64 = 15 Ratio3 int64 = 20 @@ -508,7 +512,7 @@ func giveGift(amount int64) int64 { var ratio int64 switch { case amount < Threshold1: - return 0 + ratio = Ratio0 case amount < Threshold2: ratio = Ratio1 case amount < Threshold3: diff --git a/controllers/account/controllers/account_controller_test.go b/controllers/account/controllers/account_controller_test.go index 9d86b3be240..e667b266273 100644 --- a/controllers/account/controllers/account_controller_test.go +++ b/controllers/account/controllers/account_controller_test.go @@ -29,18 +29,19 @@ func Test_giveGift(t *testing.T) { want int64 }{ // [1-298] -> 0%, [299-598] -> 10%, [599-1998] -> 15%, [1999-4998] -> 20%, [4999-19998] -> 25%, [19999+] -> 30% - {name: "0% less than 299", args: args{amount: 100 * BaseUnit}, want: 0}, - {name: "10% between 299 and 599", args: args{amount: 299 * BaseUnit}, want: 29_900_000}, - {name: "10% between 299 and 599", args: args{amount: 300 * BaseUnit}, want: 30_000_000}, - {name: "15% between 599 and 1999", args: args{amount: 599 * BaseUnit}, want: 89_850_000}, - {name: "15% between 599 and 1999", args: args{amount: 600 * BaseUnit}, want: 90_000_000}, - {name: "20% between 1999 and 4999", args: args{amount: 1999 * BaseUnit}, want: 399_800_000}, - {name: "20% between 1999 and 4999", args: args{amount: 2000 * BaseUnit}, want: 400_000_000}, - {name: "25% between 4999 and 19999", args: args{amount: 4999 * BaseUnit}, want: 1249_750_000}, - {name: "25% between 4999 and 19999", args: args{amount: 5000 * BaseUnit}, want: 1250_000_000}, - {name: "30% more than 19999", args: args{amount: 19999 * BaseUnit}, want: 5999_700_000}, - {name: "30% more than 19999", args: args{amount: 20000 * BaseUnit}, want: 6000_000_000}, - {name: "30% more than 19999", args: args{amount: 99999 * BaseUnit}, want: 29999_700_000}, + {name: "0% less than 299", args: args{amount: 100 * BaseUnit}, want: 0 + 100*BaseUnit}, + {name: "10% between 299 and 599", args: args{amount: 299 * BaseUnit}, want: 29_900_000 + 299*BaseUnit}, + {name: "10% between 299 and 599", args: args{amount: 300 * BaseUnit}, want: 30_000_000 + 300*BaseUnit}, + {name: "15% between 599 and 1999", args: args{amount: 599 * BaseUnit}, want: 89_850_000 + 599*BaseUnit}, + {name: "15% between 599 and 1999", args: args{amount: 600 * BaseUnit}, want: 90_000_000 + 600*BaseUnit}, + {name: "20% between 1999 and 4999", args: args{amount: 1999 * BaseUnit}, want: 399_800_000 + 1999*BaseUnit}, + {name: "20% between 1999 and 4999", args: args{amount: 2000 * BaseUnit}, want: 400_000_000 + 2000*BaseUnit}, + {name: "25% between 4999 and 19999", args: args{amount: 4999 * BaseUnit}, want: 1249_750_000 + 4999*BaseUnit}, + {name: "25% between 4999 and 19999", args: args{amount: 5000 * BaseUnit}, want: 1250_000_000 + 5000*BaseUnit}, + {name: "30% more than 19999", args: args{amount: 19999 * BaseUnit}, want: 5999_700_000 + 19999*BaseUnit}, + {name: "30% more than 19999", args: args{amount: 20000 * BaseUnit}, want: 6000_000_000 + 20000*BaseUnit}, + {name: "30% more than 19999", args: args{amount: 99999 * BaseUnit}, want: 29999_700_000 + 99999*BaseUnit}, + {"0% less than 299", args{amount: 1 * BaseUnit}, 1 * BaseUnit}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {