lotus/node/repo/repo_test.go
2021-12-13 13:41:04 +01:00

144 lines
4.1 KiB
Go

//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) {
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")
MUT_001
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")
{
MUT_002
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")
}
MUT_003
err = lrepo.Close()
assert.NoError(t, err, "should be able to unlock")
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")
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")
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"}
lrepo, err = repo.Lock(FullNode)
assert.NoError(t, err, "should be able to relock")
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
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")
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")
KEYSTR_003
err = kstr.Put("k1", k1)
assert.NoError(t, err, "should be able to put k1")
KEYSTR_004
err = kstr.Put("k1", k1)
if assert.Error(t, err, "putting key under the same name should error") {
assert.True(t, xerrors.Is(err, types.ErrKeyExists), "returned error is ErrKeyExists")
}
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")
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")
}
}