@@ -10,6 +10,7 @@ import (
1010 "fmt"
1111 "io"
1212 "net/http"
13+ "strconv"
1314 "strings"
1415 "testing"
1516
@@ -19,6 +20,7 @@ import (
1920 user_model "code.gitea.io/gitea/models/user"
2021 "code.gitea.io/gitea/modules/base"
2122 debian_module "code.gitea.io/gitea/modules/packages/debian"
23+ packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
2224 "code.gitea.io/gitea/tests"
2325
2426 "github.com/blakesmith/ar"
@@ -263,4 +265,37 @@ func TestPackageDebian(t *testing.T) {
263265 assert .Contains (t , body , "Components: " + strings .Join (components , " " )+ "\n " )
264266 assert .Contains (t , body , "Architectures: " + architectures [1 ]+ "\n " )
265267 })
268+
269+ t .Run ("Cleanup" , func (t * testing.T ) {
270+ defer tests .PrintCurrentTest (t )()
271+
272+ rule := & packages.PackageCleanupRule {
273+ Enabled : true ,
274+ RemovePattern : `.*` ,
275+ MatchFullName : true ,
276+ OwnerID : user .ID ,
277+ Type : packages .TypeDebian ,
278+ }
279+
280+ _ , err := packages .InsertCleanupRule (db .DefaultContext , rule )
281+ assert .NoError (t , err )
282+
283+ // When there were a lot of packages (> 50 or 100) and the code used "Iterate" to get all packages, it ever caused bugs,
284+ // because "Iterate" keeps a dangling SQL session but the callback function still uses the same session to execute statements.
285+ // The "Iterate" problem has been checked by TestContextSafety now, so here we only need to check the cleanup logic with a small number
286+ packagesCount := 2
287+ for i := 0 ; i < packagesCount ; i ++ {
288+ uploadURL := fmt .Sprintf ("%s/pool/%s/%s/upload" , rootURL , "test" , "main" )
289+ req := NewRequestWithBody (t , "PUT" , uploadURL , createArchive (packageName , "1.0." + strconv .Itoa (i ), "all" )).AddBasicAuth (user .Name )
290+ MakeRequest (t , req , http .StatusCreated )
291+ }
292+ req := NewRequest (t , "GET" , fmt .Sprintf ("%s/dists/%s/Release" , rootURL , "test" ))
293+ MakeRequest (t , req , http .StatusOK )
294+
295+ err = packages_cleanup_service .CleanupTask (db .DefaultContext , 0 )
296+ assert .NoError (t , err )
297+
298+ req = NewRequest (t , "GET" , fmt .Sprintf ("%s/dists/%s/Release" , rootURL , "test" ))
299+ MakeRequest (t , req , http .StatusNotFound )
300+ })
266301}
0 commit comments