2017-02-09 15:29:17 +00:00
|
|
|
package test
|
2016-12-23 18:35:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
|
2017-01-25 11:14:39 +00:00
|
|
|
rpc "github.com/hsanjuan/go-libp2p-gorpc"
|
2016-12-23 18:35:37 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
)
|
|
|
|
|
2017-02-15 14:16:34 +00:00
|
|
|
// ErrBadCid is returned when using ErrorCid. Operations with that CID always
|
|
|
|
// fail.
|
2017-02-09 15:29:17 +00:00
|
|
|
var ErrBadCid = errors.New("this is an expected error when using ErrorCid")
|
2016-12-23 18:35:37 +00:00
|
|
|
|
|
|
|
type mockService struct{}
|
|
|
|
|
2017-02-09 15:29:17 +00:00
|
|
|
// NewMockRPCClient creates a mock ipfs-cluster RPC server and returns
|
|
|
|
// a client to it.
|
|
|
|
func NewMockRPCClient(t *testing.T) *rpc.Client {
|
2016-12-23 18:35:37 +00:00
|
|
|
s := rpc.NewServer(nil, "mock")
|
|
|
|
c := rpc.NewClientWithServer(nil, "mock", s)
|
|
|
|
err := s.RegisterName("Cluster", &mockService{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) Pin(in api.PinSerial, out *struct{}) error {
|
2017-02-09 15:29:17 +00:00
|
|
|
if in.Cid == ErrorCid {
|
|
|
|
return ErrBadCid
|
2016-12-23 18:35:37 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) Unpin(in api.PinSerial, out *struct{}) error {
|
2017-02-09 15:29:17 +00:00
|
|
|
if in.Cid == ErrorCid {
|
|
|
|
return ErrBadCid
|
2016-12-23 18:35:37 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) PinList(in struct{}, out *[]api.PinSerial) error {
|
|
|
|
*out = []api.PinSerial{
|
2017-02-13 15:46:53 +00:00
|
|
|
{
|
|
|
|
Cid: TestCid1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cid: TestCid2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cid: TestCid3,
|
|
|
|
},
|
|
|
|
}
|
2016-12-23 18:35:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
func (mock *mockService) ID(in struct{}, out *api.IDSerial) error {
|
|
|
|
//_, pubkey, _ := crypto.GenerateKeyPair(
|
|
|
|
// DefaultConfigCrypto,
|
|
|
|
// DefaultConfigKeyLength)
|
|
|
|
*out = api.ID{
|
2017-02-09 15:29:17 +00:00
|
|
|
ID: TestPeerID1,
|
2017-02-08 17:04:08 +00:00
|
|
|
//PublicKey: pubkey,
|
|
|
|
Version: "0.0.mock",
|
|
|
|
IPFS: api.IPFSID{
|
2017-02-09 15:29:17 +00:00
|
|
|
ID: TestPeerID1,
|
2017-01-26 18:59:31 +00:00
|
|
|
},
|
|
|
|
}.ToSerial()
|
2017-01-24 15:19:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
func (mock *mockService) Version(in struct{}, out *api.Version) error {
|
2017-02-15 14:16:34 +00:00
|
|
|
*out = api.Version{
|
|
|
|
Version: "0.0.mock",
|
|
|
|
}
|
2016-12-23 18:35:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
func (mock *mockService) Peers(in struct{}, out *[]api.IDSerial) error {
|
|
|
|
id := api.IDSerial{}
|
2017-01-26 18:59:31 +00:00
|
|
|
mock.ID(in, &id)
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
*out = []api.IDSerial{id}
|
2016-12-23 18:35:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
func (mock *mockService) PeerAdd(in api.MultiaddrSerial, out *api.IDSerial) error {
|
|
|
|
id := api.IDSerial{}
|
2017-01-30 12:12:25 +00:00
|
|
|
mock.ID(struct{}{}, &id)
|
|
|
|
*out = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mock *mockService) PeerRemove(in peer.ID, out *struct{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-09 15:29:17 +00:00
|
|
|
// FIXME: dup from util.go
|
|
|
|
func globalPinInfoSliceToSerial(gpi []api.GlobalPinInfo) []api.GlobalPinInfoSerial {
|
|
|
|
gpis := make([]api.GlobalPinInfoSerial, len(gpi), len(gpi))
|
|
|
|
for i, v := range gpi {
|
|
|
|
gpis[i] = v.ToSerial()
|
|
|
|
}
|
|
|
|
return gpis
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
func (mock *mockService) StatusAll(in struct{}, out *[]api.GlobalPinInfoSerial) error {
|
2017-02-09 15:29:17 +00:00
|
|
|
c1, _ := cid.Decode(TestCid1)
|
|
|
|
c2, _ := cid.Decode(TestCid2)
|
|
|
|
c3, _ := cid.Decode(TestCid3)
|
2017-02-08 17:04:08 +00:00
|
|
|
*out = globalPinInfoSliceToSerial([]api.GlobalPinInfo{
|
2017-01-24 11:39:08 +00:00
|
|
|
{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: c1,
|
2017-02-08 17:04:08 +00:00
|
|
|
PeerMap: map[peer.ID]api.PinInfo{
|
2017-02-09 15:29:17 +00:00
|
|
|
TestPeerID1: {
|
2017-02-08 17:04:08 +00:00
|
|
|
Cid: c1,
|
2017-02-09 15:29:17 +00:00
|
|
|
Peer: TestPeerID1,
|
2017-02-08 17:04:08 +00:00
|
|
|
Status: api.TrackerStatusPinned,
|
2016-12-23 18:35:37 +00:00
|
|
|
TS: time.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-01-24 11:39:08 +00:00
|
|
|
{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: c2,
|
2017-02-08 17:04:08 +00:00
|
|
|
PeerMap: map[peer.ID]api.PinInfo{
|
2017-02-09 15:29:17 +00:00
|
|
|
TestPeerID1: {
|
2017-02-08 17:04:08 +00:00
|
|
|
Cid: c2,
|
2017-02-09 15:29:17 +00:00
|
|
|
Peer: TestPeerID1,
|
2017-02-08 17:04:08 +00:00
|
|
|
Status: api.TrackerStatusPinning,
|
2016-12-23 18:35:37 +00:00
|
|
|
TS: time.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-01-24 11:39:08 +00:00
|
|
|
{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: c3,
|
2017-02-08 17:04:08 +00:00
|
|
|
PeerMap: map[peer.ID]api.PinInfo{
|
2017-02-09 15:29:17 +00:00
|
|
|
TestPeerID1: {
|
2017-02-08 17:04:08 +00:00
|
|
|
Cid: c3,
|
2017-02-09 15:29:17 +00:00
|
|
|
Peer: TestPeerID1,
|
2017-02-08 17:04:08 +00:00
|
|
|
Status: api.TrackerStatusPinError,
|
2016-12-23 18:35:37 +00:00
|
|
|
TS: time.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-08 17:04:08 +00:00
|
|
|
})
|
2016-12-23 18:35:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) Status(in api.PinSerial, out *api.GlobalPinInfoSerial) error {
|
2017-02-09 15:29:17 +00:00
|
|
|
if in.Cid == ErrorCid {
|
|
|
|
return ErrBadCid
|
2016-12-23 18:35:37 +00:00
|
|
|
}
|
2017-02-09 15:29:17 +00:00
|
|
|
c1, _ := cid.Decode(TestCid1)
|
2017-02-08 17:04:08 +00:00
|
|
|
*out = api.GlobalPinInfo{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: c1,
|
2017-02-08 17:04:08 +00:00
|
|
|
PeerMap: map[peer.ID]api.PinInfo{
|
2017-02-09 15:29:17 +00:00
|
|
|
TestPeerID1: {
|
2017-02-08 17:04:08 +00:00
|
|
|
Cid: c1,
|
2017-02-09 15:29:17 +00:00
|
|
|
Peer: TestPeerID1,
|
2017-02-08 17:04:08 +00:00
|
|
|
Status: api.TrackerStatusPinned,
|
2016-12-23 18:35:37 +00:00
|
|
|
TS: time.Now(),
|
|
|
|
},
|
|
|
|
},
|
2017-02-08 17:04:08 +00:00
|
|
|
}.ToSerial()
|
2016-12-23 18:35:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
func (mock *mockService) SyncAll(in struct{}, out *[]api.GlobalPinInfoSerial) error {
|
2017-01-25 18:38:23 +00:00
|
|
|
return mock.StatusAll(in, out)
|
2016-12-23 18:35:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) Sync(in api.PinSerial, out *api.GlobalPinInfoSerial) error {
|
2017-01-25 18:38:23 +00:00
|
|
|
return mock.Status(in, out)
|
2016-12-23 18:35:37 +00:00
|
|
|
}
|
2017-01-23 11:08:04 +00:00
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
func (mock *mockService) StateSync(in struct{}, out *[]api.PinInfoSerial) error {
|
|
|
|
*out = make([]api.PinInfoSerial, 0, 0)
|
2017-01-23 11:08:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) Recover(in api.PinSerial, out *api.GlobalPinInfoSerial) error {
|
2017-01-25 18:38:23 +00:00
|
|
|
return mock.Status(in, out)
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) Track(in api.PinSerial, out *struct{}) error {
|
2017-01-23 11:08:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func (mock *mockService) Untrack(in api.PinSerial, out *struct{}) error {
|
2017-01-23 11:08:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
func (mock *mockService) PeerManagerPeers(in struct{}, out *[]peer.ID) error {
|
|
|
|
*out = []peer.ID{TestPeerID1, TestPeerID2, TestPeerID3}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-13 18:35:29 +00:00
|
|
|
|
|
|
|
func (mock *mockService) PeerManagerAddPeer(in api.MultiaddrSerial, out *struct{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rpcapi *mockService) PeerManagerRmPeer(in peer.ID, out *struct{}) error {
|
|
|
|
return nil
|
|
|
|
}
|