aboutsummaryrefslogtreecommitdiff
path: root/internal/route/lfs
diff options
context:
space:
mode:
Diffstat (limited to 'internal/route/lfs')
-rw-r--r--internal/route/lfs/basic.go8
-rw-r--r--internal/route/lfs/basic_test.go86
-rw-r--r--internal/route/lfs/batch.go2
-rw-r--r--internal/route/lfs/batch_test.go20
4 files changed, 61 insertions, 55 deletions
diff --git a/internal/route/lfs/basic.go b/internal/route/lfs/basic.go
index a0594839..cbfc724f 100644
--- a/internal/route/lfs/basic.go
+++ b/internal/route/lfs/basic.go
@@ -44,7 +44,7 @@ func (h *basicHandler) Storager(storage lfsutil.Storage) lfsutil.Storager {
// GET /{owner}/{repo}.git/info/lfs/object/basic/{oid}
func (h *basicHandler) serveDownload(c *macaron.Context, repo *db.Repository, oid lfsutil.OID) {
- object, err := db.LFS.GetObjectByOID(repo.ID, oid)
+ object, err := db.LFS.GetObjectByOID(c.Req.Context(), repo.ID, oid)
if err != nil {
if db.IsErrLFSObjectNotExist(err) {
responseJSON(c.Resp, http.StatusNotFound, responseError{
@@ -79,7 +79,7 @@ func (h *basicHandler) serveDownload(c *macaron.Context, repo *db.Repository, oi
func (h *basicHandler) serveUpload(c *macaron.Context, repo *db.Repository, oid lfsutil.OID) {
// NOTE: LFS client will retry upload the same object if there was a partial failure,
// therefore we would like to skip ones that already exist.
- _, err := db.LFS.GetObjectByOID(repo.ID, oid)
+ _, err := db.LFS.GetObjectByOID(c.Req.Context(), repo.ID, oid)
if err == nil {
// Object exists, drain the request body and we're good.
_, _ = io.Copy(ioutil.Discard, c.Req.Request.Body)
@@ -106,7 +106,7 @@ func (h *basicHandler) serveUpload(c *macaron.Context, repo *db.Repository, oid
return
}
- err = db.LFS.CreateObject(repo.ID, oid, written, s.Storage())
+ err = db.LFS.CreateObject(c.Req.Context(), repo.ID, oid, written, s.Storage())
if err != nil {
// NOTE: It is OK to leave the file when the whole operation failed
// with a DB error, a retry on client side can safely overwrite the
@@ -139,7 +139,7 @@ func (*basicHandler) serveVerify(c *macaron.Context, repo *db.Repository) {
return
}
- object, err := db.LFS.GetObjectByOID(repo.ID, request.Oid)
+ object, err := db.LFS.GetObjectByOID(c.Req.Context(), repo.ID, request.Oid)
if err != nil {
if db.IsErrLFSObjectNotExist(err) {
responseJSON(c.Resp, http.StatusNotFound, responseError{
diff --git a/internal/route/lfs/basic_test.go b/internal/route/lfs/basic_test.go
index 6343fdcf..85d03188 100644
--- a/internal/route/lfs/basic_test.go
+++ b/internal/route/lfs/basic_test.go
@@ -61,17 +61,17 @@ func Test_basicHandler_serveDownload(t *testing.T) {
tests := []struct {
name string
content string
- mockLFSStore *db.MockLFSStore
+ mockLFSStore func() db.LFSStore
expStatusCode int
expHeader http.Header
expBody string
}{
{
name: "object does not exist",
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return nil, db.ErrLFSObjectNotExist{}
- },
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(nil, db.ErrLFSObjectNotExist{})
+ return mock
},
expStatusCode: http.StatusNotFound,
expHeader: http.Header{
@@ -81,10 +81,10 @@ func Test_basicHandler_serveDownload(t *testing.T) {
},
{
name: "storage not found",
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return &db.LFSObject{Storage: "bad_storage"}, nil
- },
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(&db.LFSObject{Storage: "bad_storage"}, nil)
+ return mock
},
expStatusCode: http.StatusInternalServerError,
expHeader: http.Header{
@@ -96,13 +96,16 @@ func Test_basicHandler_serveDownload(t *testing.T) {
{
name: "object exists",
content: "Hello world!",
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return &db.LFSObject{
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(
+ &db.LFSObject{
Size: 12,
Storage: s.Storage(),
- }, nil
- },
+ },
+ nil,
+ )
+ return mock
},
expStatusCode: http.StatusOK,
expHeader: http.Header{
@@ -114,7 +117,7 @@ func Test_basicHandler_serveDownload(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
- db.SetMockLFSStore(t, test.mockLFSStore)
+ db.SetMockLFSStore(t, test.mockLFSStore())
s.buf = bytes.NewBufferString(test.content)
@@ -158,35 +161,32 @@ func Test_basicHandler_serveUpload(t *testing.T) {
tests := []struct {
name string
- mockLFSStore *db.MockLFSStore
+ mockLFSStore func() db.LFSStore
expStatusCode int
expBody string
}{
{
name: "object already exists",
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return &db.LFSObject{}, nil
- },
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(&db.LFSObject{}, nil)
+ return mock
},
expStatusCode: http.StatusOK,
},
{
name: "new object",
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return nil, db.ErrLFSObjectNotExist{}
- },
- MockCreateObject: func(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
- return nil
- },
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(nil, db.ErrLFSObjectNotExist{})
+ return mock
},
expStatusCode: http.StatusOK,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
- db.SetMockLFSStore(t, test.mockLFSStore)
+ db.SetMockLFSStore(t, test.mockLFSStore())
r, err := http.NewRequest("PUT", "/", strings.NewReader("Hello world!"))
if err != nil {
@@ -219,7 +219,7 @@ func Test_basicHandler_serveVerify(t *testing.T) {
tests := []struct {
name string
body string
- mockLFSStore *db.MockLFSStore
+ mockLFSStore func() db.LFSStore
expStatusCode int
expBody string
}{
@@ -232,10 +232,10 @@ func Test_basicHandler_serveVerify(t *testing.T) {
{
name: "object does not exist",
body: `{"oid":"ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"}`,
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return nil, db.ErrLFSObjectNotExist{}
- },
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(nil, db.ErrLFSObjectNotExist{})
+ return mock
},
expStatusCode: http.StatusNotFound,
expBody: `{"message":"Object does not exist"}` + "\n",
@@ -243,10 +243,10 @@ func Test_basicHandler_serveVerify(t *testing.T) {
{
name: "object size mismatch",
body: `{"oid":"ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"}`,
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return &db.LFSObject{Size: 12}, nil
- },
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(&db.LFSObject{Size: 12}, nil)
+ return mock
},
expStatusCode: http.StatusBadRequest,
expBody: `{"message":"Object size mismatch"}` + "\n",
@@ -255,17 +255,19 @@ func Test_basicHandler_serveVerify(t *testing.T) {
{
name: "object exists",
body: `{"oid":"ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size":12}`,
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectByOID: func(repoID int64, oid lfsutil.OID) (*db.LFSObject, error) {
- return &db.LFSObject{Size: 12}, nil
- },
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectByOIDFunc.SetDefaultReturn(&db.LFSObject{Size: 12}, nil)
+ return mock
},
expStatusCode: http.StatusOK,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
- db.SetMockLFSStore(t, test.mockLFSStore)
+ if test.mockLFSStore != nil {
+ db.SetMockLFSStore(t, test.mockLFSStore())
+ }
r, err := http.NewRequest("POST", "/", strings.NewReader(test.body))
if err != nil {
diff --git a/internal/route/lfs/batch.go b/internal/route/lfs/batch.go
index bfc364c2..bde3140d 100644
--- a/internal/route/lfs/batch.go
+++ b/internal/route/lfs/batch.go
@@ -75,7 +75,7 @@ func serveBatch(c *macaron.Context, owner *db.User, repo *db.Repository) {
for _, obj := range request.Objects {
oids = append(oids, obj.Oid)
}
- stored, err := db.LFS.GetObjectsByOIDs(repo.ID, oids...)
+ stored, err := db.LFS.GetObjectsByOIDs(c.Req.Context(), repo.ID, oids...)
if err != nil {
internalServerError(c.Resp)
log.Error("Failed to get objects [repo_id: %d, oids: %v]: %v", repo.ID, oids, err)
diff --git a/internal/route/lfs/batch_test.go b/internal/route/lfs/batch_test.go
index 67b85eeb..76c0a817 100644
--- a/internal/route/lfs/batch_test.go
+++ b/internal/route/lfs/batch_test.go
@@ -17,7 +17,6 @@ import (
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/lfsutil"
)
func Test_serveBatch(t *testing.T) {
@@ -35,7 +34,7 @@ func Test_serveBatch(t *testing.T) {
tests := []struct {
name string
body string
- mockLFSStore *db.MockLFSStore
+ mockLFSStore func() db.LFSStore
expStatusCode int
expBody string
}{
@@ -83,9 +82,10 @@ func Test_serveBatch(t *testing.T) {
{"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123},
{"oid": "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57", "size": 456}
]}`,
- mockLFSStore: &db.MockLFSStore{
- MockGetObjectsByOIDs: func(repoID int64, oids ...lfsutil.OID) ([]*db.LFSObject, error) {
- return []*db.LFSObject{
+ mockLFSStore: func() db.LFSStore {
+ mock := db.NewMockLFSStore()
+ mock.GetObjectsByOIDsFunc.SetDefaultReturn(
+ []*db.LFSObject{
{
OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
Size: 1234,
@@ -93,8 +93,10 @@ func Test_serveBatch(t *testing.T) {
OID: "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57",
Size: 456,
},
- }, nil
- },
+ },
+ nil,
+ )
+ return mock
},
expStatusCode: http.StatusOK,
expBody: `{
@@ -121,7 +123,9 @@ func Test_serveBatch(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
- db.SetMockLFSStore(t, test.mockLFSStore)
+ if test.mockLFSStore != nil {
+ db.SetMockLFSStore(t, test.mockLFSStore())
+ }
r, err := http.NewRequest("POST", "/", bytes.NewBufferString(test.body))
if err != nil {