aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository/lfs_test.go
diff options
context:
space:
mode:
authorZettat123 <zettat123@gmail.com>2023-07-26 15:02:53 +0800
committerGitHub <noreply@github.com>2023-07-26 07:02:53 +0000
commitf3ed0ef692a2c73fdf491d86f27fad33da742174 (patch)
tree83ffdf2ea868a91934c2460665eb6da71316b3e4 /services/repository/lfs_test.go
parentdcd3a631288686a95cedbd4aa9cce245e896825d (diff)
downloadgitea-f3ed0ef692a2c73fdf491d86f27fad33da742174.tar.gz
gitea-f3ed0ef692a2c73fdf491d86f27fad33da742174.zip
Fix bugs in LFS meta garbage collection (#26122)
This PR - Fix #26093. Replace `time.Time` with `timeutil.TimeStamp` - Fix #26135. Add missing `xorm:"extends"` to `CountLFSMetaObject` for LFS meta object query - Add a unit test for LFS meta object garbage collection
Diffstat (limited to 'services/repository/lfs_test.go')
-rw-r--r--services/repository/lfs_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/services/repository/lfs_test.go b/services/repository/lfs_test.go
new file mode 100644
index 0000000000..e88befdfef
--- /dev/null
+++ b/services/repository/lfs_test.go
@@ -0,0 +1,64 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repository
+
+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"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestGarbageCollectLFSMetaObjects(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ setting.LFS.StartServer = true
+ err := storage.Init()
+ assert.NoError(t, err)
+
+ repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
+ assert.NoError(t, err)
+
+ // add lfs object
+ lfsContent := []byte("gitea1")
+ lfsOid := storeObjectInRepo(t, repo.ID, &lfsContent)
+
+ // gc
+ err = GarbageCollectLFSMetaObjects(context.Background(), GarbageCollectLFSMetaObjectsOptions{
+ AutoFix: true,
+ OlderThan: time.Now().Add(7 * 24 * time.Hour).Add(5 * 24 * time.Hour),
+ UpdatedLessRecentlyThan: time.Now().Add(7 * 24 * time.Hour).Add(3 * 24 * time.Hour),
+ })
+ 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)
+}
+
+func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string {
+ pointer, err := lfs.GeneratePointer(bytes.NewReader(*content))
+ assert.NoError(t, err)
+
+ _, err = git_model.NewLFSMetaObject(db.DefaultContext, &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID})
+ 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
+}