Skip to content

Commit

Permalink
feat(routing): add Put test
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed May 2, 2023
1 parent acfca33 commit f2e8db2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
37 changes: 25 additions & 12 deletions coreiface/tests/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@ import (

var errAPINotImplemented = errors.New("api not implemented")

func (tp *TestSuite) makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
api, err := tp.MakeAPISwarm(ctx, false, 1)
if err != nil {
return nil, err
}

return api[0], nil
}

type Provider interface {
// Make creates n nodes. fullIdentity set to false can be ignored
MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error)
MakeAPISwarm(ctx context.Context, fullIdentity bool, online bool, n int) ([]coreiface.CoreAPI, error)
}

func (tp *TestSuite) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) {
func (tp *TestSuite) makeAPISwarm(ctx context.Context, fullIdentity bool, online bool, n int) ([]coreiface.CoreAPI, error) {
if tp.apis != nil {
tp.apis <- 1
go func() {
Expand All @@ -34,7 +25,29 @@ func (tp *TestSuite) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int)
}()
}

return tp.Provider.MakeAPISwarm(ctx, fullIdentity, n)
return tp.Provider.MakeAPISwarm(ctx, fullIdentity, online, n)
}

func (tp *TestSuite) makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
api, err := tp.makeAPISwarm(ctx, false, false, 1)
if err != nil {
return nil, err
}

return api[0], nil
}

func (tp *TestSuite) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) {
return tp.makeAPISwarm(ctx, fullIdentity, fullIdentity, n)
}

func (tp *TestSuite) makeAPIWithIdentityAndOffline(ctx context.Context) (coreiface.CoreAPI, error) {
api, err := tp.makeAPISwarm(ctx, true, false, 1)
if err != nil {
return nil, err
}

return api[0], nil
}

type TestSuite struct {
Expand Down
42 changes: 41 additions & 1 deletion coreiface/tests/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gogo/protobuf/proto"
iface "github.com/ipfs/boxo/coreiface"
"github.com/ipfs/boxo/coreiface/options"
ipns_pb "github.com/ipfs/boxo/ipns/pb"
)

Expand All @@ -20,6 +21,7 @@ func (tp *TestSuite) TestRouting(t *testing.T) {

t.Run("TestRoutingGet", tp.TestRoutingGet)
t.Run("TestRoutingPut", tp.TestRoutingPut)
t.Run("TestRoutingPutOffline", tp.TestRoutingPutOffline)
}

func (tp *TestSuite) testRoutingPublishKey(t *testing.T, ctx context.Context, api iface.CoreAPI) iface.IpnsEntry {
Expand All @@ -28,7 +30,12 @@ func (tp *TestSuite) testRoutingPublishKey(t *testing.T, ctx context.Context, ap
t.Fatal(err)
}

entry, err := api.Name().Publish(ctx, p)
// TODO: make sure we are fine with that implicit allow offline
opts := []options.NamePublishOption{
options.Name.AllowOffline(true),
}

entry, err := api.Name().Publish(ctx, p, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -90,3 +97,36 @@ func (tp *TestSuite) TestRoutingPut(t *testing.T) {
t.Fatal(err)
}
}

func (tp *TestSuite) TestRoutingPutOffline(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// init a swarm & publish an IPNS entry to get a valid payload
apis, err := tp.MakeAPISwarm(ctx, 2)
if err != nil {
t.Fatal(err)
}

ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0])
data, err := apis[0].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name())
if err != nil {
t.Fatal(err)
}

// init our offline node and try to put the payload
api, err := tp.makeAPIWithIdentityAndOffline(ctx)
if err != nil {
t.Fatal(err)
}

err = api.Routing().Put(ctx, "/ipns/"+ipnsEntry.Name(), data)
if err == nil {
t.Fatal("this operation should fail because we are offline")
}

err = api.Routing().Put(ctx, "/ipns/"+ipnsEntry.Name(), data, options.Put.AllowOffline(true))
if err != nil {
t.Fatal(err)
}
}

0 comments on commit f2e8db2

Please sign in to comment.