2022-08-29 14:25:30 +00:00
|
|
|
// stm: #unit
|
2019-07-10 15:10:45 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
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) {
|
2023-11-15 04:38:04 +00:00
|
|
|
assert.ErrorContains(t, err, ErrNoAPIEndpoint.Error())
|
2019-07-10 15:10:45 +00:00
|
|
|
}
|
|
|
|
assert.Nil(t, apima, "with no api endpoint, return should be nil")
|
|
|
|
|
2019-11-12 17:59:38 +00:00
|
|
|
lrepo, err := repo.Lock(FullNode)
|
2019-07-10 15:10:45 +00:00
|
|
|
assert.NoError(t, err, "should be able to lock once")
|
|
|
|
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
|
|
|
|
|
|
|
|
{
|
2019-11-12 17:59:38 +00:00
|
|
|
lrepo2, err := repo.Lock(FullNode)
|
2019-07-10 15:10:45 +00:00
|
|
|
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")
|
|
|
|
|
2019-11-12 17:59:38 +00:00
|
|
|
lrepo, err = repo.Lock(FullNode)
|
2019-07-10 15:10:45 +00:00
|
|
|
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")
|
|
|
|
|
2020-06-09 23:37:18 +00:00
|
|
|
c1, err := lrepo.Config()
|
|
|
|
assert.Equal(t, config.DefaultFullNode(), c1, "there should be a default config")
|
2019-07-12 15:22:29 +00:00
|
|
|
assert.NoError(t, err, "config should not error")
|
|
|
|
|
2020-06-09 23:37:18 +00:00
|
|
|
// mutate config and persist back to repo
|
2020-06-10 16:07:47 +00:00
|
|
|
err = lrepo.SetConfig(func(c interface{}) {
|
|
|
|
cfg := c.(*config.FullNode)
|
|
|
|
cfg.Client.IpfsMAddr = "duvall"
|
|
|
|
})
|
2020-06-09 23:37:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// load config and verify changes
|
|
|
|
c2, err := lrepo.Config()
|
|
|
|
require.NoError(t, err)
|
|
|
|
cfg2 := c2.(*config.FullNode)
|
|
|
|
require.Equal(t, cfg2.Client.IpfsMAddr, "duvall")
|
|
|
|
|
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) {
|
2023-11-15 04:38:04 +00:00
|
|
|
assert.ErrorContains(t, err, ErrNoAPIEndpoint.Error(), "after closing repo, api should be nil")
|
2019-07-10 15:10:45 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2019-11-12 17:59:38 +00:00
|
|
|
lrepo, err = repo.Lock(FullNode)
|
2019-07-18 14:01:39 +00:00
|
|
|
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") {
|
2019-10-18 11:39:31 +00:00
|
|
|
assert.True(t, xerrors.Is(err, types.ErrKeyExists), "returned error is ErrKeyExists")
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|