2023-07-26 07:02:53 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-10-19 13:16:11 +00:00
|
|
|
package repository_test
|
2023-07-26 07:02:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
"code.gitea.io/gitea/modules/lfs"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2023-10-19 13:16:11 +00:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2023-07-26 07:02:53 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGarbageCollectLFSMetaObjects(t *testing.T) {
|
2023-10-19 13:16:11 +00:00
|
|
|
unittest.PrepareTestEnv(t)
|
2023-07-26 07:02:53 +00:00
|
|
|
|
|
|
|
setting.LFS.StartServer = true
|
|
|
|
err := storage.Init()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-04-25 21:03:53 +00:00
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "lfs")
|
2023-07-26 07:02:53 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-04-25 21:03:53 +00:00
|
|
|
validLFSObjects, err := db.GetEngine(db.DefaultContext).Count(git_model.LFSMetaObject{RepositoryID: repo.ID})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, validLFSObjects, int64(1))
|
|
|
|
|
2023-07-26 07:02:53 +00:00
|
|
|
// add lfs object
|
|
|
|
lfsContent := []byte("gitea1")
|
|
|
|
lfsOid := storeObjectInRepo(t, repo.ID, &lfsContent)
|
|
|
|
|
|
|
|
// gc
|
2023-10-19 13:16:11 +00:00
|
|
|
err = repo_service.GarbageCollectLFSMetaObjects(context.Background(), repo_service.GarbageCollectLFSMetaObjectsOptions{
|
2023-07-26 07:02:53 +00:00
|
|
|
AutoFix: true,
|
|
|
|
OlderThan: time.Now().Add(7 * 24 * time.Hour).Add(5 * 24 * time.Hour),
|
2024-04-25 21:03:53 +00:00
|
|
|
UpdatedLessRecentlyThan: time.Time{}, // ensure that the models/fixtures/lfs_meta_object.yml objects are considered as well
|
|
|
|
LogDetail: t.Logf,
|
2023-07-26 07:02:53 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// lfs meta has been deleted
|
|
|
|
_, err = git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, lfsOid)
|
|
|
|
assert.ErrorIs(t, err, git_model.ErrLFSObjectNotExist)
|
2024-04-25 21:03:53 +00:00
|
|
|
|
|
|
|
remainingLFSObjects, err := db.GetEngine(db.DefaultContext).Count(git_model.LFSMetaObject{RepositoryID: repo.ID})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, validLFSObjects-1, remainingLFSObjects)
|
2023-07-26 07:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string {
|
|
|
|
pointer, err := lfs.GeneratePointer(bytes.NewReader(*content))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-12-07 07:27:36 +00:00
|
|
|
_, err = git_model.NewLFSMetaObject(db.DefaultContext, repositoryID, pointer)
|
2023-07-26 07:02:53 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
exist, err := contentStore.Exists(pointer)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !exist {
|
|
|
|
err := contentStore.Put(pointer, bytes.NewReader(*content))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
return pointer.Oid
|
|
|
|
}
|