Skip to content

Commit

Permalink
chore: add base test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZexiao committed Jul 5, 2022
1 parent 5af8fba commit 22a9ad9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
4 changes: 4 additions & 0 deletions piecestorage/storagemgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,7 @@ func (p *PieceStorageManager) ListStorageInfos() types.PieceStorageInfos {
S3Storage: s3,
}
}

func (p *PieceStorageManager) GetLength() int {
return len(p.storages)
}
102 changes: 102 additions & 0 deletions piecestorage/storagemgr_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,114 @@
package piecestorage

import (
"context"
"crypto/rand"
"fmt"
"io"
"os"
path2 "path"
"testing"

"github.com/filecoin-project/venus-market/v2/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReWritei(t *testing.T) {
r := io.LimitReader(rand.Reader, 100)
path := os.TempDir()
name := "market-test-tmp"
filepath := path2.Join(path, name)
_ = os.Remove(filepath)

ctx := context.TODO()
ifs, err := NewFsPieceStorage(&config.FsPieceStorage{ReadOnly: false, Path: path})
require.NoErrorf(t, err, "open file storage")
wlen, err := ifs.SaveTo(ctx, name, r)

require.NoErrorf(t, err, "expect to write file ")
require.Equal(t, wlen, int64(100))
fs, err := os.Open(filepath)
if err != nil {
if !os.IsExist(err) {
require.NoErrorf(t, err, "expect file exit")
}
}

buf, err := io.ReadAll(fs)
require.NoErrorf(t, err, "expect read file success")
if len(buf) != 100 {
require.Equal(t, int64(len(buf)), int64(100))
}
}

func TestFsAddPieceStorage(t *testing.T) {
psm, err := NewPieceStorageManager(&config.PieceStorage{})
assert.Nil(t, err)
path := os.TempDir()

ps, err := NewFsPieceStorage(&config.FsPieceStorage{
ReadOnly: false,
Path: path,
Name: "test",
})
assert.Nil(t, err)

err = psm.AddPieceStorage(ps)
assert.Nil(t, err)

l := psm.GetLength()
assert.Equal(t, 1, l)
}

func TestRmPieceStorage(t *testing.T) {
psm, err := NewPieceStorageManager(&config.PieceStorage{})
assert.Nil(t, err)
path := os.TempDir()
name := "test"

ps, err := NewFsPieceStorage(&config.FsPieceStorage{
ReadOnly: false,
Path: path,
Name: name,
})
assert.Nil(t, err)

err = psm.AddPieceStorage(ps)
assert.Nil(t, err)

err = psm.RemovePieceStorage("test2")
ErrPieceStorageNotFound := fmt.Errorf("storage test2 not exist")

assert.Equal(t, ErrPieceStorageNotFound, err)

err = psm.RemovePieceStorage(name)
assert.Nil(t, err)

l := psm.GetLength()
assert.Equal(t, 0, l)
}

func TestListStorageInfos(t *testing.T) {
psm, err := NewPieceStorageManager(&config.PieceStorage{})
assert.Nil(t, err)
path := os.TempDir()
name := "test"

ps, err := NewFsPieceStorage(&config.FsPieceStorage{
ReadOnly: false,
Path: path,
Name: name,
})
assert.Nil(t, err)

err = psm.AddPieceStorage(ps)
assert.Nil(t, err)

info := psm.ListStorageInfos()
assert.Equal(t, 1, len(info.FsStorage))
}

func TestRandSelect(t *testing.T) {
psm, err := NewPieceStorageManager(&config.PieceStorage{})
assert.Nil(t, err)
Expand Down

0 comments on commit 22a9ad9

Please sign in to comment.