2016-12-14 14:31:50 +00:00
|
|
|
package ipfscluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-12-16 11:40:28 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2017-02-01 17:16:09 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2016-12-14 14:31:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyToPin(t *testing.T) {
|
|
|
|
op := &clusterLogOp{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: testCid,
|
|
|
|
Type: LogOpPin,
|
|
|
|
ctx: context.Background(),
|
|
|
|
rpcClient: mockRPCClient(t),
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st := NewMapState()
|
|
|
|
op.ApplyTo(st)
|
|
|
|
pins := st.ListPins()
|
|
|
|
if len(pins) != 1 || pins[0].String() != testCid {
|
2017-01-15 15:06:47 +00:00
|
|
|
t.Error("the state was not modified correctly")
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyToUnpin(t *testing.T) {
|
|
|
|
op := &clusterLogOp{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: testCid,
|
|
|
|
Type: LogOpUnpin,
|
|
|
|
ctx: context.Background(),
|
|
|
|
rpcClient: mockRPCClient(t),
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st := NewMapState()
|
|
|
|
c, _ := cid.Decode(testCid)
|
|
|
|
st.AddPin(c)
|
|
|
|
op.ApplyTo(st)
|
|
|
|
pins := st.ListPins()
|
|
|
|
if len(pins) != 0 {
|
2017-01-15 15:06:47 +00:00
|
|
|
t.Error("the state was not modified correctly")
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyToBadState(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Error("should have recovered an error")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
op := &clusterLogOp{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: testCid,
|
|
|
|
Type: LogOpUnpin,
|
|
|
|
ctx: context.Background(),
|
|
|
|
rpcClient: mockRPCClient(t),
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var st interface{}
|
|
|
|
op.ApplyTo(st)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyToBadCid(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Error("should have recovered an error")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
op := &clusterLogOp{
|
2016-12-23 18:35:37 +00:00
|
|
|
Cid: "agadfaegf",
|
|
|
|
Type: LogOpPin,
|
|
|
|
ctx: context.Background(),
|
|
|
|
rpcClient: mockRPCClient(t),
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st := NewMapState()
|
|
|
|
op.ApplyTo(st)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanRaft() {
|
2016-12-16 21:00:08 +00:00
|
|
|
os.RemoveAll(testingConfig().ConsensusDataFolder)
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 18:08:46 +00:00
|
|
|
func testingConsensus(t *testing.T) *Consensus {
|
2016-12-14 14:31:50 +00:00
|
|
|
//logging.SetDebugLogging()
|
|
|
|
cfg := testingConfig()
|
|
|
|
ctx := context.Background()
|
|
|
|
h, err := makeHost(ctx, cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cannot create host:", err)
|
|
|
|
}
|
|
|
|
st := NewMapState()
|
2017-02-01 17:16:09 +00:00
|
|
|
cc, err := NewConsensus([]peer.ID{cfg.ID}, h, cfg.ConsensusDataFolder, st)
|
2016-12-14 14:31:50 +00:00
|
|
|
if err != nil {
|
2016-12-15 18:08:46 +00:00
|
|
|
t.Fatal("cannot create Consensus:", err)
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
2016-12-23 18:35:37 +00:00
|
|
|
cc.SetClient(mockRPCClient(t))
|
2017-01-30 12:12:25 +00:00
|
|
|
<-cc.Ready()
|
2016-12-14 14:31:50 +00:00
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
2016-12-15 18:08:46 +00:00
|
|
|
func TestShutdownConsensus(t *testing.T) {
|
2016-12-14 14:31:50 +00:00
|
|
|
// Bring it up twice to make sure shutdown cleans up properly
|
|
|
|
// but also to make sure raft comes up ok when re-initialized
|
|
|
|
defer cleanRaft()
|
2016-12-15 18:08:46 +00:00
|
|
|
cc := testingConsensus(t)
|
2016-12-14 14:31:50 +00:00
|
|
|
err := cc.Shutdown()
|
|
|
|
if err != nil {
|
2016-12-15 18:08:46 +00:00
|
|
|
t.Fatal("Consensus cannot shutdown:", err)
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
2016-12-15 13:07:19 +00:00
|
|
|
cc.Shutdown()
|
2016-12-15 18:08:46 +00:00
|
|
|
cc = testingConsensus(t)
|
2016-12-14 14:31:50 +00:00
|
|
|
err = cc.Shutdown()
|
|
|
|
if err != nil {
|
2016-12-15 18:08:46 +00:00
|
|
|
t.Fatal("Consensus cannot shutdown:", err)
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsensusPin(t *testing.T) {
|
2016-12-15 18:08:46 +00:00
|
|
|
cc := testingConsensus(t)
|
2016-12-14 14:31:50 +00:00
|
|
|
defer cleanRaft() // Remember defer runs in LIFO order
|
|
|
|
defer cc.Shutdown()
|
|
|
|
|
|
|
|
c, _ := cid.Decode(testCid)
|
2016-12-19 17:35:24 +00:00
|
|
|
err := cc.LogPin(c)
|
2016-12-14 14:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("the operation did not make it to the log:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
st, err := cc.State()
|
|
|
|
if err != nil {
|
2016-12-16 11:40:28 +00:00
|
|
|
t.Fatal("error gettinng state:", err)
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pins := st.ListPins()
|
|
|
|
if len(pins) != 1 || pins[0].String() != testCid {
|
|
|
|
t.Error("the added pin should be in the state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsensusUnpin(t *testing.T) {
|
2016-12-15 18:08:46 +00:00
|
|
|
cc := testingConsensus(t)
|
2016-12-14 14:31:50 +00:00
|
|
|
defer cleanRaft()
|
|
|
|
defer cc.Shutdown()
|
|
|
|
|
|
|
|
c, _ := cid.Decode(testCid2)
|
2016-12-19 17:35:24 +00:00
|
|
|
err := cc.LogUnpin(c)
|
2016-12-14 14:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("the operation did not make it to the log:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsensusLeader(t *testing.T) {
|
2016-12-15 18:08:46 +00:00
|
|
|
cc := testingConsensus(t)
|
2016-12-14 14:31:50 +00:00
|
|
|
cfg := testingConfig()
|
2016-12-28 15:25:24 +00:00
|
|
|
pID := cfg.ID
|
2016-12-14 14:31:50 +00:00
|
|
|
defer cleanRaft()
|
|
|
|
defer cc.Shutdown()
|
2016-12-16 11:40:28 +00:00
|
|
|
l, err := cc.Leader()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("No leader:", err)
|
|
|
|
}
|
|
|
|
|
2017-01-23 17:38:59 +00:00
|
|
|
if l != pID {
|
2016-12-28 15:25:24 +00:00
|
|
|
t.Errorf("expected %s but the leader appears as %s", pID, l)
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
}
|