Skip to content

Commit

Permalink
test(dsync): add test for remove hooks over HTTP
Browse files Browse the repository at this point in the history
found a few mistakes in the process
  • Loading branch information
b5 committed Aug 25, 2019
1 parent f3c7d67 commit e64a8bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
11 changes: 7 additions & 4 deletions dsync/dsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,18 @@ func New(localNodes ipld.NodeGetter, blockStore coreiface.BlockAPI, opts ...func
lng: localNodes,
bapi: blockStore,

requireAllBlocks: cfg.RequireAllBlocks,
allowRemoves: cfg.AllowRemoves,

preCheck: cfg.PushPreCheck,
finalCheck: cfg.PushFinalCheck,
onCompleteHook: cfg.PushComplete,
getDagInfoCheck: cfg.GetDagInfoCheck,
removeCheck: cfg.RemoveCheck,

requireAllBlocks: cfg.RequireAllBlocks,
sessionPool: map[string]*session{},
sessionCancels: map[string]context.CancelFunc{},
sessionTTLDur: time.Hour * 5,
sessionPool: map[string]*session{},
sessionCancels: map[string]context.CancelFunc{},
sessionTTLDur: time.Hour * 5,
}

if cfg.PinAPI != nil {
Expand Down Expand Up @@ -492,6 +494,7 @@ func (ds *Dsync) RemoveCID(ctx context.Context, cidStr string, meta map[string]s
return ErrRemoveNotSupported
}

log.Debug("removing cid", cidStr)
if ds.removeCheck != nil {
info := dag.Info{Manifest: &dag.Manifest{Nodes: []string{cidStr}}}
if err := ds.removeCheck(ctx, info, meta); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions dsync/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ func (rem *HTTPClient) RemoveCID(ctx context.Context, id string, meta map[string
return
}
q := u.Query()
q.Set("manifest", id)
q.Set("cid", id)
for key, val := range meta {
q.Set(key, val)
}
u.RawQuery = q.Encode()

req, err := http.NewRequest("GET", u.String(), nil)
req, err := http.NewRequest("DELETE", u.String(), nil)
if err != nil {
return err
}
Expand Down
70 changes: 29 additions & 41 deletions dsync/http_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package dsync

import (
"bytes"
"context"
"io/ioutil"
"net/http/httptest"
"strings"
"testing"

"github.com/qri-io/dag"

"github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files"
ipld "github.com/ipfs/go-ipld-format"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/qri-io/dag"
)

func TestSyncHTTP(t *testing.T) {
Expand All @@ -29,6 +24,7 @@ func TestSyncHTTP(t *testing.T) {
t.Fatal(err)
}

// yooooooooooooooooooooo
f := files.NewReaderFile(ioutil.NopCloser(strings.NewReader("y" + strings.Repeat("o", 350))))
path, err := a.Unixfs().Add(ctx, f)
if err != nil {
Expand All @@ -47,16 +43,24 @@ func TestSyncHTTP(t *testing.T) {
return nil
}

removeCheckCalled := make(chan struct{}, 1)
removeCheckHook := func(_ context.Context, _ dag.Info, _ map[string]string) error {
removeCheckCalled <- struct{}{}
return nil
}

bGetter := &dag.NodeGetter{Dag: b.Dag()}
ts, err := New(bGetter, b.Block(), func(cfg *Config) {
bdsync, err := New(bGetter, b.Block(), func(cfg *Config) {
cfg.AllowRemoves = true
cfg.PushPreCheck = func(context.Context, dag.Info, map[string]string) error { return nil }
cfg.PushComplete = onCompleteHook
cfg.RemoveCheck = removeCheckHook
})
if err != nil {
t.Fatal(err)
}

s := httptest.NewServer(HTTPRemoteHandler(ts))
s := httptest.NewServer(HTTPRemoteHandler(bdsync))
defer s.Close()

cli := &HTTPClient{URL: s.URL + "/dsync"}
Expand All @@ -77,49 +81,33 @@ func TestSyncHTTP(t *testing.T) {
}

<-onCompleteCalled
}

// remote implements the Remote interface on a single receive session at a time
type remote struct {
receive *session
lng ipld.NodeGetter
bapi coreiface.BlockAPI
}

func (r *remote) PushStart(info *dag.Info) (sid string, diff *dag.Manifest, err error) {
ctx := context.Background()
r.receive, err = newSession(ctx, r.lng, r.bapi, info, false, false, nil)
if err != nil {
return
if err := cli.RemoveCID(ctx, info.RootCID().String(), nil); err != nil {
t.Error(err)
}
sid = r.receive.id
diff = r.receive.diff
return
}

func (r *remote) PushBlock(sid, hash string, data []byte) ReceiveResponse {
return r.receive.ReceiveBlock(hash, bytes.NewReader(data))
<-removeCheckCalled
}

func (r *remote) PullManifest(ctx context.Context, hash string) (mfst *dag.Manifest, err error) {
id, err := cid.Parse(hash)
if err != nil {
return nil, err
}

return dag.NewManifest(ctx, r.lng, id)
}
func TestRemoveNotSupported(t *testing.T) {
ctx := context.Background()

func (r *remote) GetBlock(ctx context.Context, hash string) ([]byte, error) {
id, err := cid.Parse(hash)
_, b, err := makeAPI(ctx)
if err != nil {
return nil, err
t.Fatal(err)
}

node, err := r.lng.Get(ctx, id)
bGetter := &dag.NodeGetter{Dag: b.Dag()}
bdsync, err := New(bGetter, b.Block())
if err != nil {
return nil, err
t.Fatal(err)
}

return node.RawData(), nil
s := httptest.NewServer(HTTPRemoteHandler(bdsync))
defer s.Close()

cli := &HTTPClient{URL: s.URL + "/dsync"}
if err := cli.RemoveCID(ctx, "foo", nil); err != ErrRemoveNotSupported {
t.Errorf("expected error remoce not supported, got: %s", err.Error())
}
}
2 changes: 1 addition & 1 deletion dsync/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestNewP2P(t *testing.T) {
t.Fatal(err)
}

// We want to see progress, so we spin up a goroutine to listen for updates
// We want to see progress, so we spin up a goroutine to listen for updates
waitForFmt := make(chan struct{})
go func() {
updates := push.Updates()
Expand Down

0 comments on commit e64a8bd

Please sign in to comment.