@@ -10,6 +10,7 @@ import (
10
10
"fmt"
11
11
"io"
12
12
"net/http"
13
+ "strconv"
13
14
"strings"
14
15
"testing"
15
16
@@ -19,6 +20,7 @@ import (
19
20
user_model "code.gitea.io/gitea/models/user"
20
21
"code.gitea.io/gitea/modules/base"
21
22
debian_module "code.gitea.io/gitea/modules/packages/debian"
23
+ packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
22
24
"code.gitea.io/gitea/tests"
23
25
24
26
"github.com/blakesmith/ar"
@@ -263,4 +265,37 @@ func TestPackageDebian(t *testing.T) {
263
265
assert .Contains (t , body , "Components: " + strings .Join (components , " " )+ "\n " )
264
266
assert .Contains (t , body , "Architectures: " + architectures [1 ]+ "\n " )
265
267
})
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
+ })
266
301
}
0 commit comments