2019-02-20 14:24:25 +00:00
|
|
|
package dsstate
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-27 16:28:41 +00:00
|
|
|
"bytes"
|
2018-06-27 04:03:15 +00:00
|
|
|
"context"
|
2017-02-13 15:46:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
"github.com/ipfs/ipfs-cluster/datastore/inmem"
|
2017-11-28 22:45:10 +00:00
|
|
|
|
2017-02-13 15:46:53 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testCid1, _ = cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmq")
|
|
|
|
var testPeerID1, _ = peer.IDB58Decode("QmXZrtE5jQwXNqCJMfHUTQkvhQ4ZAnqMnmzFMJfLewuabc")
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
var c = &api.Pin{
|
2018-07-23 09:14:43 +00:00
|
|
|
Cid: testCid1,
|
2019-02-19 17:59:46 +00:00
|
|
|
Type: api.DataType,
|
2018-07-23 09:14:43 +00:00
|
|
|
Allocations: []peer.ID{testPeerID1},
|
|
|
|
MaxDepth: -1,
|
|
|
|
PinOptions: api.PinOptions{
|
|
|
|
ReplicationFactorMax: -1,
|
|
|
|
ReplicationFactorMin: -1,
|
|
|
|
Name: "test",
|
|
|
|
},
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
func newState(t *testing.T) *State {
|
|
|
|
store := inmem.New()
|
|
|
|
ds, err := New(store, "", DefaultHandle())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:46:53 +00:00
|
|
|
func TestAdd(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2019-02-20 14:24:25 +00:00
|
|
|
st := newState(t)
|
|
|
|
st.Add(ctx, c)
|
|
|
|
if ok, err := st.Has(ctx, c.Cid); !ok || err != nil {
|
2017-02-13 15:46:53 +00:00
|
|
|
t.Error("should have added it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRm(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2019-02-20 14:24:25 +00:00
|
|
|
st := newState(t)
|
|
|
|
st.Add(ctx, c)
|
|
|
|
st.Rm(ctx, c.Cid)
|
|
|
|
if ok, err := st.Has(ctx, c.Cid); ok || err != nil {
|
2017-02-13 15:46:53 +00:00
|
|
|
t.Error("should have removed it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2017-02-13 15:46:53 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Fatal("paniced")
|
|
|
|
}
|
|
|
|
}()
|
2019-02-20 14:24:25 +00:00
|
|
|
st := newState(t)
|
|
|
|
st.Add(ctx, c)
|
|
|
|
get, err := st.Get(ctx, c.Cid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if get == nil {
|
2019-02-19 17:59:46 +00:00
|
|
|
t.Fatal("not found")
|
|
|
|
}
|
2019-02-20 14:24:25 +00:00
|
|
|
|
2019-02-19 17:59:46 +00:00
|
|
|
if get.Cid.String() != c.Cid.String() {
|
|
|
|
t.Error("bad cid decoding: ", get.Cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if get.Allocations[0] != c.Allocations[0] {
|
|
|
|
t.Error("bad allocations decoding:", get.Allocations)
|
|
|
|
}
|
|
|
|
|
|
|
|
if get.ReplicationFactorMax != c.ReplicationFactorMax ||
|
2018-01-12 17:04:46 +00:00
|
|
|
get.ReplicationFactorMin != c.ReplicationFactorMin {
|
2019-02-19 17:59:46 +00:00
|
|
|
t.Error("bad replication factors decoding")
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2017-02-13 15:46:53 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Fatal("paniced")
|
|
|
|
}
|
|
|
|
}()
|
2019-02-20 14:24:25 +00:00
|
|
|
st := newState(t)
|
|
|
|
st.Add(ctx, c)
|
|
|
|
list, err := st.List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-13 15:46:53 +00:00
|
|
|
if list[0].Cid.String() != c.Cid.String() ||
|
|
|
|
list[0].Allocations[0] != c.Allocations[0] ||
|
2018-01-12 17:04:46 +00:00
|
|
|
list[0].ReplicationFactorMax != c.ReplicationFactorMax ||
|
|
|
|
list[0].ReplicationFactorMin != c.ReplicationFactorMin {
|
2017-02-13 15:46:53 +00:00
|
|
|
t.Error("returned something different")
|
|
|
|
}
|
|
|
|
}
|
2017-02-27 16:28:41 +00:00
|
|
|
|
2017-11-28 22:45:10 +00:00
|
|
|
func TestMarshalUnmarshal(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2019-02-20 14:24:25 +00:00
|
|
|
st := newState(t)
|
|
|
|
st.Add(ctx, c)
|
2019-02-19 17:59:46 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2019-02-20 14:24:25 +00:00
|
|
|
err := st.Marshal(buf)
|
2017-02-27 16:28:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-20 14:24:25 +00:00
|
|
|
st2 := newState(t)
|
|
|
|
err = st2.Unmarshal(buf)
|
2017-02-27 16:28:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-20 14:24:25 +00:00
|
|
|
|
|
|
|
get, err := st2.Get(ctx, c.Cid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-11-28 22:45:10 +00:00
|
|
|
}
|
2019-02-20 14:24:25 +00:00
|
|
|
if get == nil {
|
2019-02-19 17:59:46 +00:00
|
|
|
t.Fatal("cannot get pin")
|
|
|
|
}
|
2017-02-27 16:28:41 +00:00
|
|
|
if get.Allocations[0] != testPeerID1 {
|
|
|
|
t.Error("expected different peer id")
|
|
|
|
}
|
2019-02-19 17:59:46 +00:00
|
|
|
if !get.Cid.Equals(c.Cid) {
|
|
|
|
t.Error("expected different cid")
|
|
|
|
}
|
2017-02-27 16:28:41 +00:00
|
|
|
}
|