2019-07-10 15:10:45 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-18 14:01:39 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-12 15:22:29 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2019-07-10 15:10:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func basicTest(t *testing.T, repo Repo) {
|
|
|
|
apima, err := repo.APIEndpoint()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Equal(t, ErrNoAPIEndpoint, err)
|
|
|
|
}
|
|
|
|
assert.Nil(t, apima, "with no api endpoint, return should be nil")
|
|
|
|
|
|
|
|
lrepo, err := repo.Lock()
|
|
|
|
assert.NoError(t, err, "should be able to lock once")
|
|
|
|
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
|
|
|
|
|
|
|
|
{
|
|
|
|
lrepo2, err := repo.Lock()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Equal(t, ErrRepoAlreadyLocked, err)
|
|
|
|
}
|
|
|
|
assert.Nil(t, lrepo2, "with locked repo errors, nil should be returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lrepo.Close()
|
|
|
|
assert.NoError(t, err, "should be able to unlock")
|
|
|
|
|
|
|
|
lrepo, err = repo.Lock()
|
|
|
|
assert.NoError(t, err, "should be able to relock")
|
|
|
|
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
|
|
|
|
|
|
|
|
ma, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/43244")
|
|
|
|
assert.NoError(t, err, "creating multiaddr shouldn't error")
|
|
|
|
|
|
|
|
err = lrepo.SetAPIEndpoint(ma)
|
|
|
|
assert.NoError(t, err, "setting multiaddr shouldn't error")
|
|
|
|
|
|
|
|
apima, err = repo.APIEndpoint()
|
|
|
|
assert.NoError(t, err, "setting multiaddr shouldn't error")
|
|
|
|
assert.Equal(t, ma, apima, "returned API multiaddr should be the same")
|
|
|
|
|
2019-07-12 15:22:29 +00:00
|
|
|
cfg, err := lrepo.Config()
|
|
|
|
assert.Equal(t, config.Default(), cfg, "there should be a default config")
|
|
|
|
assert.NoError(t, err, "config should not error")
|
|
|
|
|
2019-07-10 15:10:45 +00:00
|
|
|
err = lrepo.Close()
|
|
|
|
assert.NoError(t, err, "should be able to close")
|
|
|
|
|
|
|
|
apima, err = repo.APIEndpoint()
|
|
|
|
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Equal(t, ErrNoAPIEndpoint, err, "after closing repo, api should be nil")
|
|
|
|
}
|
|
|
|
assert.Nil(t, apima, "with closed repo, apima should be set back to nil")
|
2019-07-18 14:01:39 +00:00
|
|
|
|
2019-07-18 14:57:49 +00:00
|
|
|
k1 := types.KeyInfo{Type: "foo"}
|
|
|
|
k2 := types.KeyInfo{Type: "bar"}
|
2019-07-18 14:01:39 +00:00
|
|
|
|
|
|
|
lrepo, err = repo.Lock()
|
|
|
|
assert.NoError(t, err, "should be able to relock")
|
|
|
|
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
|
|
|
|
|
|
|
|
kstr, err := lrepo.KeyStore()
|
|
|
|
assert.NoError(t, err, "should be able to get keystore")
|
|
|
|
assert.NotNil(t, lrepo, "keystore shouldn't be nil")
|
|
|
|
|
|
|
|
list, err := kstr.List()
|
|
|
|
assert.NoError(t, err, "should be able to list key")
|
|
|
|
assert.Empty(t, list, "there should be no keys")
|
|
|
|
|
|
|
|
err = kstr.Put("k1", k1)
|
|
|
|
assert.NoError(t, err, "should be able to put k1")
|
|
|
|
|
|
|
|
err = kstr.Put("k1", k1)
|
|
|
|
if assert.Error(t, err, "putting key under the same name should error") {
|
|
|
|
assert.True(t, xerrors.Is(err, ErrKeyExists), "returned error is ErrKeyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
k1prim, err := kstr.Get("k1")
|
|
|
|
assert.NoError(t, err, "should be able to get k1")
|
|
|
|
assert.Equal(t, k1, k1prim, "returned key should be the same")
|
|
|
|
|
|
|
|
k2prim, err := kstr.Get("k2")
|
|
|
|
if assert.Error(t, err, "should not be able to get k2") {
|
2019-10-17 10:18:40 +00:00
|
|
|
assert.True(t, xerrors.Is(err, types.ErrKeyInfoNotFound), "returned error is ErrKeyNotFound")
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
assert.Empty(t, k2prim, "there should be no output for k2")
|
|
|
|
|
|
|
|
err = kstr.Put("k2", k2)
|
|
|
|
assert.NoError(t, err, "should be able to put k2")
|
|
|
|
|
|
|
|
list, err = kstr.List()
|
|
|
|
assert.NoError(t, err, "should be able to list keys")
|
|
|
|
assert.ElementsMatch(t, []string{"k1", "k2"}, list, "returned elements match")
|
|
|
|
|
|
|
|
err = kstr.Delete("k2")
|
|
|
|
assert.NoError(t, err, "should be able to delete key")
|
|
|
|
|
|
|
|
list, err = kstr.List()
|
|
|
|
assert.NoError(t, err, "should be able to list keys")
|
|
|
|
assert.ElementsMatch(t, []string{"k1"}, list, "returned elements match")
|
|
|
|
|
|
|
|
err = kstr.Delete("k2")
|
|
|
|
if assert.Error(t, err) {
|
2019-10-17 10:18:40 +00:00
|
|
|
assert.True(t, xerrors.Is(err, types.ErrKeyInfoNotFound), "returned errror is ErrKeyNotFound")
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
2019-07-10 15:10:45 +00:00
|
|
|
}
|