2017-03-10 16:24:25 +00:00
|
|
|
package raft
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
import (
|
2018-06-27 04:03:15 +00:00
|
|
|
"context"
|
2017-02-13 15:46:53 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
"github.com/ipfs/ipfs-cluster/state/mapstate"
|
|
|
|
"github.com/ipfs/ipfs-cluster/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyToPin(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2018-01-24 17:47:44 +00:00
|
|
|
cc := testingConsensus(t, 1)
|
2017-02-13 15:46:53 +00:00
|
|
|
op := &LogOp{
|
2019-02-27 20:19:10 +00:00
|
|
|
Cid: api.PinCid(test.Cid1),
|
2017-02-13 15:46:53 +00:00
|
|
|
Type: LogOpPin,
|
2017-10-31 10:20:14 +00:00
|
|
|
consensus: cc,
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
2018-01-24 17:47:44 +00:00
|
|
|
defer cleanRaft(1)
|
2018-06-27 04:03:15 +00:00
|
|
|
defer cc.Shutdown(ctx)
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
st := mapstate.NewMapState()
|
|
|
|
op.ApplyTo(st)
|
2018-06-27 04:03:15 +00:00
|
|
|
pins := st.List(ctx)
|
2019-02-27 20:19:10 +00:00
|
|
|
if len(pins) != 1 || !pins[0].Cid.Equals(test.Cid1) {
|
2017-02-13 15:46:53 +00:00
|
|
|
t.Error("the state was not modified correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyToUnpin(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2018-01-24 17:47:44 +00:00
|
|
|
cc := testingConsensus(t, 1)
|
2017-02-13 15:46:53 +00:00
|
|
|
op := &LogOp{
|
2019-02-27 20:19:10 +00:00
|
|
|
Cid: api.PinCid(test.Cid1),
|
2017-02-13 15:46:53 +00:00
|
|
|
Type: LogOpUnpin,
|
2017-10-31 10:20:14 +00:00
|
|
|
consensus: cc,
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
2018-01-24 17:47:44 +00:00
|
|
|
defer cleanRaft(1)
|
2018-06-27 04:03:15 +00:00
|
|
|
defer cc.Shutdown(ctx)
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
st := mapstate.NewMapState()
|
2019-02-27 20:19:10 +00:00
|
|
|
st.Add(ctx, testPin(test.Cid1))
|
2017-02-13 15:46:53 +00:00
|
|
|
op.ApplyTo(st)
|
2018-06-27 04:03:15 +00:00
|
|
|
pins := st.List(ctx)
|
2017-02-13 15:46:53 +00:00
|
|
|
if len(pins) != 0 {
|
|
|
|
t.Error("the state was not modified correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyToBadState(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Error("should have recovered an error")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
op := &LogOp{
|
2019-02-27 20:19:10 +00:00
|
|
|
Cid: api.PinCid(test.Cid1),
|
2017-10-31 10:20:14 +00:00
|
|
|
Type: LogOpUnpin,
|
2017-02-13 15:46:53 +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 := &LogOp{
|
2017-03-08 15:57:27 +00:00
|
|
|
// Cid: api.PinSerial{Cid: "agadfaegf"},
|
2017-02-13 15:46:53 +00:00
|
|
|
// Type: LogOpPin,
|
|
|
|
// ctx: context.Background(),
|
|
|
|
// rpcClient: test.NewMockRPCClient(t),
|
|
|
|
// }
|
|
|
|
|
|
|
|
// st := mapstate.NewMapState()
|
|
|
|
// op.ApplyTo(st)
|
|
|
|
// }
|