code.gitea.io/gitea@v1.21.7/services/repository/lfs_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package repository_test 5 6 import ( 7 "bytes" 8 "context" 9 "testing" 10 "time" 11 12 "code.gitea.io/gitea/models/db" 13 git_model "code.gitea.io/gitea/models/git" 14 repo_model "code.gitea.io/gitea/models/repo" 15 "code.gitea.io/gitea/models/unittest" 16 "code.gitea.io/gitea/modules/lfs" 17 "code.gitea.io/gitea/modules/setting" 18 "code.gitea.io/gitea/modules/storage" 19 repo_service "code.gitea.io/gitea/services/repository" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestGarbageCollectLFSMetaObjects(t *testing.T) { 25 unittest.PrepareTestEnv(t) 26 27 setting.LFS.StartServer = true 28 err := storage.Init() 29 assert.NoError(t, err) 30 31 repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1") 32 assert.NoError(t, err) 33 34 // add lfs object 35 lfsContent := []byte("gitea1") 36 lfsOid := storeObjectInRepo(t, repo.ID, &lfsContent) 37 38 // gc 39 err = repo_service.GarbageCollectLFSMetaObjects(context.Background(), repo_service.GarbageCollectLFSMetaObjectsOptions{ 40 AutoFix: true, 41 OlderThan: time.Now().Add(7 * 24 * time.Hour).Add(5 * 24 * time.Hour), 42 UpdatedLessRecentlyThan: time.Now().Add(7 * 24 * time.Hour).Add(3 * 24 * time.Hour), 43 }) 44 assert.NoError(t, err) 45 46 // lfs meta has been deleted 47 _, err = git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, lfsOid) 48 assert.ErrorIs(t, err, git_model.ErrLFSObjectNotExist) 49 } 50 51 func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string { 52 pointer, err := lfs.GeneratePointer(bytes.NewReader(*content)) 53 assert.NoError(t, err) 54 55 _, err = git_model.NewLFSMetaObject(db.DefaultContext, &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID}) 56 assert.NoError(t, err) 57 contentStore := lfs.NewContentStore() 58 exist, err := contentStore.Exists(pointer) 59 assert.NoError(t, err) 60 if !exist { 61 err := contentStore.Put(pointer, bytes.NewReader(*content)) 62 assert.NoError(t, err) 63 } 64 return pointer.Oid 65 }