Skip to content

Commit c7a6fde

Browse files
committed
test: add test for object
1 parent 308be11 commit c7a6fde

File tree

10 files changed

+556
-244
lines changed

10 files changed

+556
-244
lines changed

api/jiaozifs.gen.go

Lines changed: 63 additions & 165 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yml

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -736,26 +736,6 @@ paths:
736736
schema:
737737
type: string
738738
format: binary
739-
740-
parameters:
741-
- in: query
742-
name: wipID
743-
description: working in process
744-
required: true
745-
schema:
746-
type: string
747-
format: uuid
748-
- in: header
749-
name: If-None-Match
750-
description: |
751-
Currently supports only "*" to allow uploading an object only if one doesn't exist yet.
752-
Deprecated, this capability will not be supported in future releases.
753-
example: "*"
754-
required: false
755-
deprecated: true
756-
schema:
757-
type: string
758-
pattern: '^\*$' # Currently, only "*" is supported
759739
responses:
760740
201:
761741
description: object metadata
@@ -780,14 +760,6 @@ paths:
780760
- objects
781761
operationId: deleteObject
782762
summary: delete object. Missing objects will not return a NotFound error.
783-
parameters:
784-
- in: query
785-
name: wipID
786-
description: working in process
787-
required: true
788-
schema:
789-
type: string
790-
format: uuid
791763
responses:
792764
204:
793765
description: object deleted successfully

controller/object_ctl.go

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"net/http"
1111
"time"
1212

13+
"github.com/jiaozifs/jiaozifs/utils/hash"
14+
1315
logging "github.com/ipfs/go-log/v2"
1416

1517
"github.com/go-openapi/swag"
@@ -39,7 +41,7 @@ type ObjectController struct {
3941
}
4042

4143
func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsResponse, r *http.Request, ownerName string, repositoryName string, params api.DeleteObjectParams) { //nolint
42-
user, err := auth.GetOperator(ctx)
44+
operator, err := auth.GetOperator(ctx)
4345
if err != nil {
4446
w.Error(err)
4547
return
@@ -51,7 +53,7 @@ func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsRes
5153
return
5254
}
5355

54-
if user.Name != ownerName { //todo check permission
56+
if operator.Name != ownerName { //todo check permission
5557
w.Forbidden()
5658
return
5759
}
@@ -73,6 +75,12 @@ func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsRes
7375
return
7476
}
7577

78+
wip, err := oct.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetCreatorID(operator.ID).SetRepositoryID(repository.ID).SetRefID(ref.ID))
79+
if err != nil {
80+
w.Error(err)
81+
return
82+
}
83+
7684
workTree, err := versionmgr.NewWorkTree(ctx, oct.Repo.FileTreeRepo(), models.NewRootTreeEntry(commit.TreeHash))
7785
if err != nil {
7886
w.Error(err)
@@ -81,11 +89,11 @@ func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsRes
8189

8290
err = workTree.RemoveEntry(ctx, params.Path)
8391
if errors.Is(err, versionmgr.ErrPathNotFound) {
84-
w.Code(http.StatusNotFound)
92+
w.BadRequest(fmt.Sprintf("path %s not found", params.Path))
8593
return
8694
}
8795

88-
err = oct.Repo.WipRepo().UpdateByID(ctx, models.NewUpdateWipParams(params.WipID).SetCurrentTree(workTree.Root().Hash()))
96+
err = oct.Repo.WipRepo().UpdateByID(ctx, models.NewUpdateWipParams(wip.ID).SetCurrentTree(workTree.Root().Hash()))
8997
if err != nil {
9098
w.Error(err)
9199
return
@@ -123,20 +131,28 @@ func (oct ObjectController) GetObject(ctx context.Context, w *api.JiaozifsRespon
123131
return
124132
}
125133

126-
commit, err := oct.Repo.CommitRepo().Commit(ctx, ref.CommitHash)
127-
if err != nil {
128-
w.Error(err)
129-
return
134+
treeHash := hash.EmptyHash
135+
if !ref.CommitHash.IsEmpty() {
136+
commit, err := oct.Repo.CommitRepo().Commit(ctx, ref.CommitHash)
137+
if err != nil {
138+
w.Error(err)
139+
return
140+
}
141+
treeHash = commit.TreeHash
130142
}
131143

132-
workTree, err := versionmgr.NewWorkTree(ctx, oct.Repo.FileTreeRepo(), models.NewRootTreeEntry(commit.TreeHash))
144+
workTree, err := versionmgr.NewWorkTree(ctx, oct.Repo.FileTreeRepo(), models.NewRootTreeEntry(treeHash))
133145
if err != nil {
134146
w.Error(err)
135147
return
136148
}
137149

138150
blob, name, err := workTree.FindBlob(ctx, params.Path)
139151
if err != nil {
152+
if errors.Is(err, versionmgr.ErrPathNotFound) {
153+
w.BadRequest(fmt.Sprintf("path %s not found", params.Path))
154+
return
155+
}
140156
w.Error(err)
141157
return
142158
}
@@ -160,7 +176,7 @@ func (oct ObjectController) GetObject(ctx context.Context, w *api.JiaozifsRespon
160176
w.Header().Set("Content-Length", fmt.Sprint(blob.Size))
161177
}
162178

163-
etag := httputil.ETag(blob.Hash.Hex())
179+
etag := httputil.ETag(blob.CheckSum.Hex())
164180
w.Header().Set("ETag", etag)
165181
lastModified := httputil.HeaderTimestamp(blob.CreatedAt)
166182
w.Header().Set("Last-Modified", lastModified)
@@ -212,44 +228,40 @@ func (oct ObjectController) HeadObject(ctx context.Context, w *api.JiaozifsRespo
212228
return
213229
}
214230

215-
commit, err := oct.Repo.CommitRepo().Commit(ctx, ref.CommitHash)
216-
if err != nil {
217-
w.Error(err)
218-
return
231+
treeHash := hash.EmptyHash
232+
if !ref.CommitHash.IsEmpty() {
233+
commit, err := oct.Repo.CommitRepo().Commit(ctx, ref.CommitHash)
234+
if err != nil {
235+
w.Error(err)
236+
return
237+
}
238+
treeHash = commit.TreeHash
219239
}
220240

221241
fileRepo := oct.Repo.FileTreeRepo()
222-
treeOp, err := versionmgr.NewWorkTree(ctx, fileRepo, models.NewRootTreeEntry(commit.TreeHash))
242+
workTree, err := versionmgr.NewWorkTree(ctx, fileRepo, models.NewRootTreeEntry(treeHash))
223243
if err != nil {
224244
w.Error(err)
225245
return
226246
}
227247

228-
existNodes, missingPath, err := treeOp.MatchPath(ctx, params.Path)
229-
if err != nil {
230-
w.Error(err)
231-
return
232-
}
233-
if len(missingPath) == 0 {
234-
w.Error(versionmgr.ErrPathNotFound)
235-
return
236-
}
237-
238-
objectWithName := existNodes[len(existNodes)-1]
239-
240-
blob, err := fileRepo.Blob(ctx, objectWithName.Node().Hash)
248+
blob, name, err := workTree.FindBlob(ctx, params.Path)
241249
if err != nil {
250+
if errors.Is(err, versionmgr.ErrPathNotFound) {
251+
w.BadRequest(fmt.Sprintf("path %s not found", params.Path))
252+
return
253+
}
242254
w.Error(err)
243255
return
244256
}
245257

246258
//lookup files
247-
etag := httputil.ETag(objectWithName.Node().Hash.Hex())
259+
etag := httputil.ETag(blob.CheckSum.Hex())
248260
w.Header().Set("ETag", etag)
249-
lastModified := httputil.HeaderTimestamp(objectWithName.Node().CreatedAt)
261+
lastModified := httputil.HeaderTimestamp(blob.CreatedAt)
250262
w.Header().Set("Last-Modified", lastModified)
251263
w.Header().Set("Accept-Ranges", "bytes")
252-
w.Header().Set("Content-Type", httputil.ExtensionsByType(objectWithName.Entry().Name))
264+
w.Header().Set("Content-Type", httputil.ExtensionsByType(name))
253265
// for security, make sure the browser and any proxies en route don't cache the response
254266
w.Header().Set("Cache-Control", "no-store, must-revalidate")
255267
w.Header().Set("Expires", "0")
@@ -315,7 +327,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
315327
}
316328
defer reader.Close() //nolint
317329

318-
user, err := auth.GetOperator(ctx)
330+
operator, err := auth.GetOperator(ctx)
319331
if err != nil {
320332
w.Error(err)
321333
return
@@ -327,18 +339,30 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
327339
return
328340
}
329341

330-
if user.Name != ownerName { //todo check permission
342+
if operator.Name != ownerName { //todo check permission
331343
w.Forbidden()
332344
return
333345
}
334346

335-
_, err = oct.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetOwnerID(owner.ID).SetName(repositoryName))
347+
repository, err := oct.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetOwnerID(owner.ID).SetName(repositoryName))
348+
if err != nil {
349+
w.Error(err)
350+
return
351+
}
352+
353+
ref, err := oct.Repo.RefRepo().Get(ctx, models.NewGetRefParams().SetName(params.Branch).SetRepositoryID(repository.ID))
354+
if err != nil {
355+
w.Error(err)
356+
return
357+
}
358+
359+
wip, err := oct.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetCreatorID(operator.ID).SetRepositoryID(repository.ID).SetRefID(ref.ID))
336360
if err != nil {
337361
w.Error(err)
338362
return
339363
}
340364

341-
stash, err := oct.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetID(params.WipID))
365+
stash, err := oct.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetID(wip.ID))
342366
if err != nil {
343367
w.Error(err)
344368
return
@@ -351,6 +375,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
351375
return err
352376
}
353377

378+
// todo move write blob out of transaction
354379
blob, err := workingTree.WriteBlob(ctx, oct.BlockAdapter, reader, r.ContentLength, models.DefaultLeafProperty())
355380
if err != nil {
356381
return err
@@ -361,7 +386,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
361386
return err
362387
}
363388
response = api.ObjectStats{
364-
Checksum: blob.Hash.Hex(),
389+
Checksum: blob.CheckSum.Hex(),
365390
Mtime: time.Now().Unix(),
366391
Path: params.Path,
367392
PathMode: utils.Uint32(uint32(filemode.Regular)),
@@ -377,5 +402,5 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
377402
return
378403
}
379404

380-
w.JSON(response)
405+
w.JSON(response, http.StatusCreated)
381406
}

controller/wip_ctl.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,26 @@ func (wipCtl WipController) CommitWip(ctx context.Context, w *api.JiaozifsRespon
202202
return
203203
}
204204

205-
commit, err := wipCtl.Repo.CommitRepo().Commit(ctx, ref.CommitHash)
206-
if err != nil {
207-
w.Error(err)
208-
return
209-
}
210-
211205
wip, err := wipCtl.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetRefID(ref.ID).SetCreatorID(operator.ID).SetRepositoryID(repository.ID))
212206
if err != nil {
213207
w.Error(err)
214208
return
215209
}
216210

217-
if !bytes.Equal(commit.Hash, wip.BaseCommit) {
211+
if !bytes.Equal(ref.CommitHash, wip.BaseCommit) {
218212
w.Error(fmt.Errorf("base commit not equal with branch, please update wip"))
219213
return
220214
}
215+
216+
var commit *models.Commit
217+
if !ref.CommitHash.IsEmpty() {
218+
commit, err = wipCtl.Repo.CommitRepo().Commit(ctx, ref.CommitHash)
219+
if err != nil {
220+
w.Error(err)
221+
return
222+
}
223+
}
224+
221225
var msg string
222226
if params.Msg != nil {
223227
msg = *params.Msg
@@ -232,7 +236,8 @@ func (wipCtl WipController) CommitWip(ctx context.Context, w *api.JiaozifsRespon
232236
}
233237

234238
wip.BaseCommit = commit.Commit().Hash //set for response
235-
err = repo.WipRepo().UpdateByID(ctx, models.NewUpdateWipParams(wip.ID).SetBaseCommit(wip.BaseCommit))
239+
wip.CurrentTree = commit.Commit().TreeHash
240+
err = repo.WipRepo().UpdateByID(ctx, models.NewUpdateWipParams(wip.ID).SetBaseCommit(wip.BaseCommit).SetCurrentTree(wip.CurrentTree))
236241
if err != nil {
237242
return err
238243
}
@@ -244,7 +249,7 @@ func (wipCtl WipController) CommitWip(ctx context.Context, w *api.JiaozifsRespon
244249
return
245250
}
246251

247-
w.JSON(wip)
252+
w.JSON(wip, http.StatusCreated)
248253
}
249254

250255
// DeleteWip delete a active working in process operator only can delete himself wip

integrationtest/helper.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package integrationtest
33
import (
44
"bytes"
55
"context"
6+
"crypto/rand"
67
"errors"
78
"fmt"
89
"io"
@@ -12,7 +13,9 @@ import (
1213
"testing"
1314
"time"
1415

16+
"github.com/google/uuid"
1517
"github.com/jiaozifs/jiaozifs/api"
18+
"github.com/jiaozifs/jiaozifs/utils"
1619
"github.com/smartystreets/goconvey/convey"
1720

1821
"github.com/jiaozifs/jiaozifs/testhelper"
@@ -141,3 +144,27 @@ func createRepo(ctx context.Context, c convey.C, client *api.Client, repoName st
141144
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
142145
})
143146
}
147+
148+
func uploadRandomObject(ctx context.Context, c convey.C, client *api.Client, user string, repoName string, refName string, path string) { //nolint
149+
c.Convey("upload object "+uuid.New().String(), func(c convey.C) {
150+
c.Convey("success upload object", func() {
151+
resp, err := client.UploadObjectWithBody(ctx, user, repoName, &api.UploadObjectParams{
152+
Branch: refName,
153+
Path: path,
154+
}, "application/octet-stream", io.LimitReader(rand.Reader, 50))
155+
convey.So(err, convey.ShouldBeNil)
156+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusCreated)
157+
})
158+
})
159+
}
160+
161+
func commitWip(ctx context.Context, c convey.C, client *api.Client, user string, repoName string, refName string) {
162+
c.Convey("commit wip "+uuid.New().String(), func() {
163+
resp, err := client.CommitWip(ctx, user, repoName, &api.CommitWipParams{
164+
RefName: refName,
165+
Msg: utils.String("test commit msg"),
166+
})
167+
convey.So(err, convey.ShouldBeNil)
168+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusCreated)
169+
})
170+
}

0 commit comments

Comments
 (0)