|
|
|
@ -19,6 +19,7 @@ import (
|
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
|
|
|
|
packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
|
|
|
|
|
"code.gitea.io/gitea/tests"
|
|
|
|
@ -235,16 +236,35 @@ func TestPackageQuota(t *testing.T) {
|
|
|
|
|
func TestPackageCleanup(t *testing.T) {
|
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
|
|
|
|
|
|
duration, _ := time.ParseDuration("-1h")
|
|
|
|
|
|
|
|
|
|
t.Run("Common", func(t *testing.T) {
|
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
|
|
// Upload and delete a generic package and upload a container blob
|
|
|
|
|
data, _ := util.CryptoRandomBytes(5)
|
|
|
|
|
url := fmt.Sprintf("/api/packages/%s/generic/cleanup-test/1.1.1/file.bin", user.Name)
|
|
|
|
|
req := NewRequestWithBody(t, "PUT", url, bytes.NewReader(data))
|
|
|
|
|
AddBasicAuthHeader(req, user.Name)
|
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
|
|
req = NewRequest(t, "DELETE", url)
|
|
|
|
|
AddBasicAuthHeader(req, user.Name)
|
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
|
|
|
|
|
|
data, _ = util.CryptoRandomBytes(5)
|
|
|
|
|
url = fmt.Sprintf("/v2/%s/cleanup-test/blobs/uploads?digest=sha256:%x", user.Name, sha256.Sum256(data))
|
|
|
|
|
req = NewRequestWithBody(t, "POST", url, bytes.NewReader(data))
|
|
|
|
|
AddBasicAuthHeader(req, user.Name)
|
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
|
|
pbs, err := packages_model.FindExpiredUnreferencedBlobs(db.DefaultContext, duration)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, pbs)
|
|
|
|
|
|
|
|
|
|
_, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, 2, packages_model.TypeContainer, "test", container_model.UploadVersion)
|
|
|
|
|
_, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, "cleanup-test", container_model.UploadVersion)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
err = packages_cleanup_service.Cleanup(db.DefaultContext, duration)
|
|
|
|
@ -254,15 +274,13 @@ func TestPackageCleanup(t *testing.T) {
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Empty(t, pbs)
|
|
|
|
|
|
|
|
|
|
_, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, 2, packages_model.TypeContainer, "test", container_model.UploadVersion)
|
|
|
|
|
_, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, "cleanup-test", container_model.UploadVersion)
|
|
|
|
|
assert.ErrorIs(t, err, packages_model.ErrPackageNotExist)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("CleanupRules", func(t *testing.T) {
|
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
|
|
|
|
|
|
type version struct {
|
|
|
|
|
Version string
|
|
|
|
|
ShouldExist bool
|
|
|
|
|