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"
|
2019-02-20 14:24:25 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/datastore/inmem"
|
|
|
|
"github.com/ipfs/ipfs-cluster/state/dsstate"
|
2017-02-13 15:46:53 +00:00
|
|
|
"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
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
st, err := dsstate.New(inmem.New(), "", dsstate.DefaultHandle())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-13 15:46:53 +00:00
|
|
|
op.ApplyTo(st)
|
2019-02-20 14:24:25 +00:00
|
|
|
|
2022-03-19 01:52:46 +00:00
|
|
|
ch, err := st.List(ctx)
|
2019-02-20 14:24:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 01:52:46 +00:00
|
|
|
|
|
|
|
var pins []api.Pin
|
|
|
|
for p := range ch {
|
|
|
|
pins = append(pins, p)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
st, err := dsstate.New(inmem.New(), "", dsstate.DefaultHandle())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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)
|
2019-02-20 14:24:25 +00:00
|
|
|
pins, err := st.List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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)
|
|
|
|
}
|