Run tests and fix some bugs

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-07-10 17:10:45 +02:00
parent 8cd484a8b0
commit d484e4ffb9
4 changed files with 105 additions and 51 deletions

View File

@ -32,6 +32,12 @@ type FsRepo struct {
var _ Repo = &FsRepo{} var _ Repo = &FsRepo{}
func NewFS(path string) (*FsRepo, error) {
return &FsRepo{
path: path,
}, nil
}
// APIEndpoint returns endpoint of API in this repo // APIEndpoint returns endpoint of API in this repo
func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) { func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
p := filepath.Join(fsr.path, fsAPI) p := filepath.Join(fsr.path, fsAPI)
@ -60,6 +66,14 @@ func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
// Lock acquires exclusive lock on this repo // Lock acquires exclusive lock on this repo
func (fsr *FsRepo) Lock() (LockedRepo, error) { func (fsr *FsRepo) Lock() (LockedRepo, error) {
locked, err := fslock.Locked(fsr.path, fsLock)
if err != nil {
return nil, xerrors.Errorf("could not check lock status: %w", err)
}
if locked {
return nil, ErrRepoAlreadyLocked
}
closer, err := fslock.Lock(fsr.path, fsLock) closer, err := fslock.Lock(fsr.path, fsLock)
if err != nil { if err != nil {
return nil, xerrors.Errorf("could not lock the repo: %w", err) return nil, xerrors.Errorf("could not lock the repo: %w", err)
@ -76,7 +90,13 @@ type fsLockedRepo struct {
} }
func (fsr *fsLockedRepo) Close() error { func (fsr *fsLockedRepo) Close() error {
err := fsr.closer.Close() err := os.Remove(fsr.join(fsAPI))
if err != nil && !os.IsNotExist(err) {
return xerrors.Errorf("could not remove API file: %w", err)
}
err = fsr.closer.Close()
fsr.closer = nil fsr.closer = nil
return err return err
} }
@ -150,7 +170,7 @@ func (fsr *fsLockedRepo) SetAPIEndpoint(ma multiaddr.Multiaddr) error {
if err := fsr.stillValid(); err != nil { if err := fsr.stillValid(); err != nil {
return err return err
} }
return ioutil.WriteFile(fsr.join(fsAPI), ma.Bytes(), 0666) return ioutil.WriteFile(fsr.join(fsAPI), []byte(ma.String()), 0666)
} }
func (fsr *fsLockedRepo) Wallet() (interface{}, error) { func (fsr *fsLockedRepo) Wallet() (interface{}, error) {

26
node/repo/fsrepo_test.go Normal file
View File

@ -0,0 +1,26 @@
package repo
import "io/ioutil"
import "os"
import "testing"
func genFsRepo(t *testing.T) (*FsRepo, func()) {
path, err := ioutil.TempDir("", "lotus-repo-*")
if err != nil {
t.Fatal(err)
}
repo, err := NewFS(path)
if err != nil {
t.Fatal(err)
}
return repo, func() {
os.RemoveAll(path)
}
}
func TestFsBasic(t *testing.T) {
repo, closer := genFsRepo(t)
defer closer()
basicTest(t, repo)
}

View File

@ -2,56 +2,9 @@ package repo
import ( import (
"testing" "testing"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
) )
func TestMemRepo(t *testing.T) { func TestMemBasic(t *testing.T) {
repo := NewMemory(nil) repo := NewMemory(nil)
apima, err := repo.APIEndpoint() basicTest(t, repo)
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")
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")
} }

55
node/repo/repo_test.go Normal file
View File

@ -0,0 +1,55 @@
package repo
import (
"testing"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
)
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")
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")
}