2017-02-13 15:46:53 +00:00
|
|
|
package mapstate
|
|
|
|
|
|
|
|
import (
|
2017-02-27 16:28:41 +00:00
|
|
|
"bytes"
|
2017-02-13 15:46:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-11-28 22:45:10 +00:00
|
|
|
msgpack "github.com/multiformats/go-multicodec/msgpack"
|
|
|
|
|
2017-02-13 15:46:53 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testCid1, _ = cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmq")
|
|
|
|
var testPeerID1, _ = peer.IDB58Decode("QmXZrtE5jQwXNqCJMfHUTQkvhQ4ZAnqMnmzFMJfLewuabc")
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
var c = api.Pin{
|
2017-03-08 17:28:43 +00:00
|
|
|
Cid: testCid1,
|
|
|
|
Allocations: []peer.ID{testPeerID1},
|
|
|
|
ReplicationFactor: -1,
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdd(t *testing.T) {
|
|
|
|
ms := NewMapState()
|
|
|
|
ms.Add(c)
|
|
|
|
if !ms.Has(c.Cid) {
|
|
|
|
t.Error("should have added it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRm(t *testing.T) {
|
|
|
|
ms := NewMapState()
|
|
|
|
ms.Add(c)
|
|
|
|
ms.Rm(c.Cid)
|
|
|
|
if ms.Has(c.Cid) {
|
|
|
|
t.Error("should have removed it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Fatal("paniced")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ms := NewMapState()
|
|
|
|
ms.Add(c)
|
|
|
|
get := ms.Get(c.Cid)
|
|
|
|
if get.Cid.String() != c.Cid.String() ||
|
|
|
|
get.Allocations[0] != c.Allocations[0] ||
|
2017-03-08 17:28:43 +00:00
|
|
|
get.ReplicationFactor != c.ReplicationFactor {
|
2017-02-13 15:46:53 +00:00
|
|
|
t.Error("returned something different")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Fatal("paniced")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ms := NewMapState()
|
|
|
|
ms.Add(c)
|
|
|
|
list := ms.List()
|
|
|
|
if list[0].Cid.String() != c.Cid.String() ||
|
|
|
|
list[0].Allocations[0] != c.Allocations[0] ||
|
2017-03-08 17:28:43 +00:00
|
|
|
list[0].ReplicationFactor != c.ReplicationFactor {
|
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) {
|
2017-02-27 16:28:41 +00:00
|
|
|
ms := NewMapState()
|
|
|
|
ms.Add(c)
|
2017-11-28 22:45:10 +00:00
|
|
|
b, err := ms.Marshal()
|
2017-02-27 16:28:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ms2 := NewMapState()
|
2017-11-28 22:45:10 +00:00
|
|
|
err = ms2.Unmarshal(b)
|
2017-02-27 16:28:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-28 22:45:10 +00:00
|
|
|
if ms.Version != ms2.Version {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-27 16:28:41 +00:00
|
|
|
get := ms2.Get(c.Cid)
|
|
|
|
if get.Allocations[0] != testPeerID1 {
|
|
|
|
t.Error("expected different peer id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateFromV1(t *testing.T) {
|
2017-11-28 22:45:10 +00:00
|
|
|
// Construct the bytes of a v1 state
|
|
|
|
var v1State mapStateV1
|
|
|
|
v1State.PinMap = map[string]struct{}{
|
|
|
|
c.Cid.String(): {}}
|
|
|
|
v1State.Version = 1
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
enc := msgpack.Multicodec(msgpack.DefaultMsgpackHandle()).Encoder(buf)
|
|
|
|
err := enc.Encode(v1State)
|
2017-02-27 16:28:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-28 22:45:10 +00:00
|
|
|
vCodec := make([]byte, 1)
|
|
|
|
vCodec[0] = byte(v1State.Version)
|
|
|
|
v1Bytes := append(vCodec, buf.Bytes()...)
|
|
|
|
|
|
|
|
// Unmarshal first to check this is v1
|
|
|
|
ms := NewMapState()
|
|
|
|
err = ms.Unmarshal(v1Bytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if ms.Version != 1 {
|
|
|
|
t.Error("unmarshal picked up the wrong version")
|
|
|
|
}
|
|
|
|
// Migrate state to current version
|
|
|
|
r := bytes.NewBuffer(v1Bytes)
|
|
|
|
err = ms.Restore(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-02-27 16:28:41 +00:00
|
|
|
get := ms.Get(c.Cid)
|
|
|
|
if get.ReplicationFactor != -1 || !get.Cid.Equals(c.Cid) {
|
|
|
|
t.Error("expected something different")
|
|
|
|
t.Logf("%+v", get)
|
|
|
|
}
|
|
|
|
}
|