lotus/node/repo/repo_test.go

144 lines
4.2 KiB
Go
Raw Normal View History

2021-10-31 11:24:07 +00:00
//stm: #unit
package repo
import (
"testing"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/config"
"github.com/stretchr/testify/require"
)
func basicTest(t *testing.T, repo Repo) {
2021-10-31 11:24:07 +00:00
//stm: @REPO_NET_001
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")
2021-10-31 11:24:07 +00:00
//stm: @REPO_MUT_001
2019-11-12 17:59:38 +00:00
lrepo, err := repo.Lock(FullNode)
assert.NoError(t, err, "should be able to lock once")
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
{
2021-10-31 11:24:07 +00:00
//stm: @REPO_MUT_002
2019-11-12 17:59:38 +00:00
lrepo2, err := repo.Lock(FullNode)
if assert.Error(t, err) {
assert.Equal(t, ErrRepoAlreadyLocked, err)
}
assert.Nil(t, lrepo2, "with locked repo errors, nil should be returned")
}
2021-10-31 11:24:07 +00:00
//stm: @REPO_MUT_003
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)
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")
2021-10-31 11:24:07 +00:00
//stm: @REPO_NET_002
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")
c1, err := lrepo.Config()
assert.Equal(t, config.DefaultFullNode(), c1, "there should be a default config")
assert.NoError(t, err, "config should not error")
// mutate config and persist back to repo
err = lrepo.SetConfig(func(c interface{}) {
cfg := c.(*config.FullNode)
cfg.Client.IpfsMAddr = "duvall"
})
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")
err = lrepo.Close()
assert.NoError(t, err, "should be able to close")
2021-10-31 11:24:07 +00:00
//stm: @REPO_NET_003
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")
k1 := types.KeyInfo{Type: "foo"}
k2 := types.KeyInfo{Type: "bar"}
2019-11-12 17:59:38 +00:00
lrepo, err = repo.Lock(FullNode)
assert.NoError(t, err, "should be able to relock")
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
2021-10-31 11:24:07 +00:00
//stm: @REPO_KEYSTR_001
kstr, err := lrepo.KeyStore()
assert.NoError(t, err, "should be able to get keystore")
assert.NotNil(t, lrepo, "keystore shouldn't be nil")
2021-10-31 11:24:07 +00:00
//stm: @REPO_KEYSTR_002
list, err := kstr.List()
assert.NoError(t, err, "should be able to list key")
assert.Empty(t, list, "there should be no keys")
2021-10-31 11:24:07 +00:00
//stm: @REPO_KEYSTR_003
err = kstr.Put("k1", k1)
assert.NoError(t, err, "should be able to put k1")
2021-10-31 11:24:07 +00:00
//stm: @REPO_KEYSTR_004
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")
}
2021-10-31 11:24:07 +00:00
//stm: @REPO_KEYSTR_005
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") {
assert.True(t, xerrors.Is(err, types.ErrKeyInfoNotFound), "returned error is ErrKeyNotFound")
}
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")
2021-10-31 11:24:07 +00:00
//stm: @REPO_KEYSTR_006
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) {
assert.True(t, xerrors.Is(err, types.ErrKeyInfoNotFound), "returned errror is ErrKeyNotFound")
}
}