2017-03-10 16:24:25 +00:00
|
|
|
package raft
|
2016-12-14 14:31:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-03-13 18:35:29 +00:00
|
|
|
"fmt"
|
2016-12-14 14:31:50 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-02-13 15:46:53 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
2017-02-09 15:29:17 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/state/mapstate"
|
|
|
|
"github.com/ipfs/ipfs-cluster/test"
|
|
|
|
|
2016-12-16 11:40:28 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2017-03-13 18:35:29 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2017-03-10 16:24:25 +00:00
|
|
|
crypto "github.com/libp2p/go-libp2p-crypto"
|
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
2017-02-01 17:16:09 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2017-03-10 16:24:25 +00:00
|
|
|
peerstore "github.com/libp2p/go-libp2p-peerstore"
|
|
|
|
swarm "github.com/libp2p/go-libp2p-swarm"
|
|
|
|
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2016-12-14 14:31:50 +00:00
|
|
|
)
|
|
|
|
|
2017-03-13 18:35:29 +00:00
|
|
|
var p2pPort = 10000
|
|
|
|
var p2pPortAlt = 11000
|
|
|
|
|
|
|
|
func cleanRaft(port int) {
|
2017-03-14 13:34:23 +00:00
|
|
|
os.RemoveAll(fmt.Sprintf("raftFolderFromTests%d", port))
|
2017-03-13 18:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
_ = logging.LevelDebug
|
|
|
|
//logging.SetLogLevel("consensus", "DEBUG")
|
2017-03-10 16:24:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:35:29 +00:00
|
|
|
func makeTestingHost(t *testing.T, port int) host.Host {
|
2017-03-10 16:24:25 +00:00
|
|
|
priv, pub, _ := crypto.GenerateKeyPair(crypto.RSA, 2048)
|
|
|
|
pid, _ := peer.IDFromPublicKey(pub)
|
2017-03-13 18:35:29 +00:00
|
|
|
maddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
|
2017-03-10 16:24:25 +00:00
|
|
|
ps := peerstore.NewPeerstore()
|
|
|
|
ps.AddPubKey(pid, pub)
|
|
|
|
ps.AddPrivKey(pid, priv)
|
|
|
|
n, _ := swarm.NewNetwork(
|
|
|
|
context.Background(),
|
|
|
|
[]ma.Multiaddr{maddr},
|
|
|
|
pid, ps, nil)
|
|
|
|
return basichost.New(n)
|
2016-12-14 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:35:29 +00:00
|
|
|
func testingConsensus(t *testing.T, port int) *Consensus {
|
|
|
|
h := makeTestingHost(t, port)
|
2017-02-09 15:29:17 +00:00
|
|
|
st := mapstate.NewMapState()
|
2017-03-13 18:35:29 +00:00
|
|
|
cc, err := NewConsensus([]peer.ID{h.ID()},
|
2017-03-14 13:34:23 +00:00
|
|
|
h, fmt.Sprintf("raftFolderFromTests%d", port), 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
|
|
|
}
|
2017-02-09 15:29:17 +00:00
|
|
|
cc.SetClient(test.NewMockRPCClient(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
|
2017-03-13 18:35:29 +00:00
|
|
|
defer cleanRaft(p2pPort)
|
|
|
|
cc := testingConsensus(t, p2pPort)
|
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()
|
2017-03-13 18:35:29 +00:00
|
|
|
cc = testingConsensus(t, p2pPort)
|
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) {
|
2017-03-13 18:35:29 +00:00
|
|
|
cc := testingConsensus(t, p2pPort)
|
|
|
|
defer cleanRaft(p2pPort) // Remember defer runs in LIFO order
|
2016-12-14 14:31:50 +00:00
|
|
|
defer cc.Shutdown()
|
|
|
|
|
2017-02-09 15:29:17 +00:00
|
|
|
c, _ := cid.Decode(test.TestCid1)
|
2017-03-08 17:28:43 +00:00
|
|
|
err := cc.LogPin(api.Pin{Cid: c, ReplicationFactor: -1})
|
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
|
|
|
}
|
|
|
|
|
2017-02-13 15:46:53 +00:00
|
|
|
pins := st.List()
|
|
|
|
if len(pins) != 1 || pins[0].Cid.String() != test.TestCid1 {
|
2016-12-14 14:31:50 +00:00
|
|
|
t.Error("the added pin should be in the state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsensusUnpin(t *testing.T) {
|
2017-03-13 18:35:29 +00:00
|
|
|
cc := testingConsensus(t, p2pPort)
|
|
|
|
defer cleanRaft(p2pPort)
|
2016-12-14 14:31:50 +00:00
|
|
|
defer cc.Shutdown()
|
|
|
|
|
2017-02-09 15:29:17 +00:00
|
|
|
c, _ := cid.Decode(test.TestCid2)
|
2017-03-08 15:57:27 +00:00
|
|
|
err := cc.LogUnpin(api.PinCid(c))
|
2016-12-14 14:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("the operation did not make it to the log:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:35:29 +00:00
|
|
|
func TestConsensusLogAddPeer(t *testing.T) {
|
|
|
|
cc := testingConsensus(t, p2pPort)
|
|
|
|
cc2 := testingConsensus(t, p2pPortAlt)
|
|
|
|
t.Log(cc.host.ID().Pretty())
|
|
|
|
t.Log(cc2.host.ID().Pretty())
|
|
|
|
defer cleanRaft(p2pPort)
|
|
|
|
defer cleanRaft(p2pPortAlt)
|
|
|
|
defer cc.Shutdown()
|
|
|
|
defer cc2.Shutdown()
|
|
|
|
|
|
|
|
addr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortAlt))
|
|
|
|
haddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", cc2.host.ID().Pretty()))
|
|
|
|
cc.host.Peerstore().AddAddr(cc2.host.ID(), addr, peerstore.TempAddrTTL)
|
|
|
|
err := cc.LogAddPeer(addr.Encapsulate(haddr))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("the operation did not make it to the log:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsensusLogRmPeer(t *testing.T) {
|
|
|
|
cc := testingConsensus(t, p2pPort)
|
|
|
|
defer cleanRaft(p2pPort)
|
|
|
|
defer cc.Shutdown()
|
|
|
|
|
|
|
|
err := cc.LogRmPeer(test.TestPeerID1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("the operation did not make it to the log:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:31:50 +00:00
|
|
|
func TestConsensusLeader(t *testing.T) {
|
2017-03-13 18:35:29 +00:00
|
|
|
cc := testingConsensus(t, p2pPort)
|
2017-03-10 16:24:25 +00:00
|
|
|
pID := cc.host.ID()
|
2017-03-13 18:35:29 +00:00
|
|
|
defer cleanRaft(p2pPort)
|
2016-12-14 14:31:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|