805b867651
The former RPC stuff had become a monster, really hard to have an overview of the RPC api capabilities and with lots of magic. go-libp2p-rpc allows to have a clearly defined RPC api which shows which methods every component can use. A component to perform remote requests, and the convoluted LeaderRPC, BroadcastRPC methods are no longer necessary. Things are much simpler now, less goroutines are needed, the central channel handling bottleneck is gone, RPC requests are very streamlined in form. In the future, it would be inmediate to have components living on different libp2p hosts and it is way clearer how to plug into the advanced cluster rpc api. License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
123 lines
2.5 KiB
Go
123 lines
2.5 KiB
Go
package ipfscluster
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
)
|
|
|
|
func testIPFSConnectorConfig(mock *ipfsMock) *Config {
|
|
cfg := testingConfig()
|
|
cfg.IPFSAddr = mock.addr
|
|
cfg.IPFSPort = mock.port
|
|
return cfg
|
|
}
|
|
|
|
func testIPFSConnector(t *testing.T) (*IPFSHTTPConnector, *ipfsMock) {
|
|
mock := newIpfsMock()
|
|
cfg := testIPFSConnectorConfig(mock)
|
|
|
|
ipfs, err := NewIPFSHTTPConnector(cfg)
|
|
if err != nil {
|
|
t.Fatal("creating an IPFSConnector should work: ", err)
|
|
}
|
|
ipfs.SetClient(mockRPCClient(t))
|
|
return ipfs, mock
|
|
}
|
|
|
|
func TestNewIPFSHTTPConnector(t *testing.T) {
|
|
ipfs, mock := testIPFSConnector(t)
|
|
defer mock.Close()
|
|
defer ipfs.Shutdown()
|
|
}
|
|
|
|
func TestIPFSPin(t *testing.T) {
|
|
ipfs, mock := testIPFSConnector(t)
|
|
defer mock.Close()
|
|
defer ipfs.Shutdown()
|
|
c, _ := cid.Decode(testCid)
|
|
err := ipfs.Pin(c)
|
|
if err != nil {
|
|
t.Error("expected success pinning cid")
|
|
}
|
|
yes, err := ipfs.IsPinned(c)
|
|
if err != nil {
|
|
t.Fatal("expected success doing ls")
|
|
}
|
|
if !yes {
|
|
t.Error("cid should have been pinned")
|
|
}
|
|
|
|
c2, _ := cid.Decode(errorCid)
|
|
err = ipfs.Pin(c2)
|
|
if err == nil {
|
|
t.Error("expected error pinning cid")
|
|
}
|
|
}
|
|
|
|
func TestIPFSUnpin(t *testing.T) {
|
|
ipfs, mock := testIPFSConnector(t)
|
|
defer mock.Close()
|
|
defer ipfs.Shutdown()
|
|
c, _ := cid.Decode(testCid)
|
|
err := ipfs.Unpin(c)
|
|
if err != nil {
|
|
t.Error("expected success unpinning non-pinned cid")
|
|
}
|
|
ipfs.Pin(c)
|
|
err = ipfs.Unpin(c)
|
|
if err != nil {
|
|
t.Error("expected success unpinning pinned cid")
|
|
}
|
|
}
|
|
|
|
func TestIPFSIsPinned(t *testing.T) {
|
|
ipfs, mock := testIPFSConnector(t)
|
|
defer mock.Close()
|
|
defer ipfs.Shutdown()
|
|
c, _ := cid.Decode(testCid)
|
|
c2, _ := cid.Decode(testCid2)
|
|
|
|
ipfs.Pin(c)
|
|
isp, err := ipfs.IsPinned(c)
|
|
if err != nil || !isp {
|
|
t.Error("c should appear pinned")
|
|
}
|
|
|
|
isp, err = ipfs.IsPinned(c2)
|
|
if err != nil || isp {
|
|
t.Error("c2 should appear unpinned")
|
|
}
|
|
}
|
|
|
|
func TestIPFSProxy(t *testing.T) {
|
|
ipfs, mock := testIPFSConnector(t)
|
|
defer mock.Close()
|
|
defer ipfs.Shutdown()
|
|
|
|
cfg := testingConfig()
|
|
res, err := http.Get(fmt.Sprintf("http://%s:%d/api/v0/add?arg=%s",
|
|
cfg.IPFSAPIAddr,
|
|
cfg.IPFSAPIPort,
|
|
testCid))
|
|
if err != nil {
|
|
t.Fatal("should forward requests to ipfs host: ", err)
|
|
}
|
|
if res.StatusCode != http.StatusOK {
|
|
t.Error("the request should have succeeded")
|
|
}
|
|
}
|
|
|
|
func TestIPFSShutdown(t *testing.T) {
|
|
ipfs, mock := testIPFSConnector(t)
|
|
defer mock.Close()
|
|
if err := ipfs.Shutdown(); err != nil {
|
|
t.Error("expected a clean shutdown")
|
|
}
|
|
if err := ipfs.Shutdown(); err != nil {
|
|
t.Error("expected a second clean shutdown")
|
|
}
|
|
}
|