Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions cmd/swarm_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package cmd_test

import (
"context"
"io/ioutil"
"testing"

th "github.com/filecoin-project/venus/pkg/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/venus/app/node/test"
th "github.com/filecoin-project/venus/pkg/testhelpers"
tf "github.com/filecoin-project/venus/pkg/testhelpers/testflags"

"github.com/stretchr/testify/assert"
)

func TestSwarmConnectPeersValid(t *testing.T) {
Expand Down Expand Up @@ -61,8 +59,7 @@ func TestPersistId(t *testing.T) {
tf.IntegrationTest(t)

// we need to control this
dir, err := ioutil.TempDir("", "go-fil-test")
require.NoError(t, err)
dir := t.TempDir()

// Start a demon in dir
d1 := th.NewDaemon(t, th.ContainerDir(dir)).Start()
Expand Down
46 changes: 10 additions & 36 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ func TestDefaults(t *testing.T) {
func TestWriteFile(t *testing.T) {
tf.UnitTest(t)

dir, err := ioutil.TempDir("", "config")
assert.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()

cfg := NewDefaultConfig()

Expand All @@ -54,11 +50,7 @@ func TestWriteFile(t *testing.T) {
func TestConfigRoundtrip(t *testing.T) {
tf.UnitTest(t)

dir, err := ioutil.TempDir("", "config")
assert.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()

cfg := NewDefaultConfig()

Expand All @@ -75,7 +67,7 @@ func TestConfigReadFileDefaults(t *testing.T) {
tf.UnitTest(t)

t.Run("all sections exist", func(t *testing.T) {
cfgpath, cleaner, err := createConfigFile(`
cfgpath, err := createConfigFile(t, `
{
"api": {
"apiAddress": "/ip4/127.0.0.1/tcp/9999",
Expand All @@ -86,9 +78,6 @@ func TestConfigReadFileDefaults(t *testing.T) {
}
}`)
assert.NoError(t, err)
defer func() {
require.NoError(t, cleaner())
}()
cfg, err := ReadFile(cfgpath)
assert.NoError(t, err)

Expand All @@ -97,17 +86,14 @@ func TestConfigReadFileDefaults(t *testing.T) {
})

t.Run("missing one section", func(t *testing.T) {
cfgpath, cleaner, err := createConfigFile(`
cfgpath, err := createConfigFile(t, `
{
"api": {
"apiAddress": "/ip4/127.0.0.1/tcp/9999",
"keyThatDoesntExit'": false
}
}`)
assert.NoError(t, err)
defer func() {
require.NoError(t, cleaner())
}()
cfg, err := ReadFile(cfgpath)
assert.NoError(t, err)

Expand All @@ -116,11 +102,8 @@ func TestConfigReadFileDefaults(t *testing.T) {
})

t.Run("empty file", func(t *testing.T) {
cfgpath, cleaner, err := createConfigFile("")
cfgpath, err := createConfigFile(t, "")
assert.NoError(t, err)
defer func() {
require.NoError(t, cleaner())
}()
cfg, err := ReadFile(cfgpath)
assert.NoError(t, err)

Expand Down Expand Up @@ -210,11 +193,8 @@ func TestConfigSet(t *testing.T) {
assert.Equal(t, cfg.Datastore.Type, "badgerbadgerbadgerds")
assert.Equal(t, cfg.Datastore.Path, "mushroom-mushroom")

cfg1path, cleaner, err := createConfigFile(fmt.Sprintf(`{"datastore": %s}`, jsonBlob))
cfg1path, err := createConfigFile(t, fmt.Sprintf(`{"datastore": %s}`, jsonBlob))
assert.NoError(t, err)
defer func() {
require.NoError(t, cleaner())
}()

cfg1, err := ReadFile(cfg1path)
assert.NoError(t, err)
Expand Down Expand Up @@ -274,18 +254,12 @@ path = "mushroom-mushroom"}`
})
}

func createConfigFile(content string) (string, func() error, error) {
dir, err := ioutil.TempDir("", "config")
if err != nil {
return "", nil, err
}
cfgpath := filepath.Join(dir, "config.json")
func createConfigFile(t *testing.T, content string) (string, error) {
cfgpath := filepath.Join(t.TempDir(), "config.json")

if err := ioutil.WriteFile(cfgpath, []byte(content), 0644); err != nil {
return "", nil, err
return "", err
}

return cfgpath, func() error {
return os.RemoveAll(dir)
}, nil
return cfgpath, nil
}
20 changes: 3 additions & 17 deletions pkg/repo/fskeystore/fskeystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"sort"
"testing"

"github.com/stretchr/testify/assert"

"github.com/libp2p/go-libp2p-core/crypto"

tf "github.com/filecoin-project/venus/pkg/testhelpers/testflags"
Expand Down Expand Up @@ -48,10 +45,7 @@ func assertDirContents(dir string, exp []string) error {

func TestKeystoreBasics(t *testing.T) {
tf.UnitTest(t)
tdir, err := ioutil.TempDir("", "keystore-test")
if err != nil {
t.Fatal(err)
}
tdir := t.TempDir()

ks, err := NewFSKeystore(tdir)
if err != nil {
Expand Down Expand Up @@ -169,12 +163,7 @@ func TestKeystoreBasics(t *testing.T) {
func TestInvalidKeyFiles(t *testing.T) {
tf.UnitTest(t)

tdir, err := ioutil.TempDir("", "keystore-test")

if err != nil {
t.Fatal(err)
}
defer assert.NoError(t, os.RemoveAll(tdir))
tdir := t.TempDir()

ks, err := NewFSKeystore(tdir)
if err != nil {
Expand Down Expand Up @@ -223,10 +212,7 @@ func TestInvalidKeyFiles(t *testing.T) {
func TestNonExistingKey(t *testing.T) {
tf.UnitTest(t)

tdir, err := ioutil.TempDir("", "keystore-test")
if err != nil {
t.Fatal(err)
}
tdir := t.TempDir()

ks, err := NewFSKeystore(tdir)
if err != nil {
Expand Down
81 changes: 23 additions & 58 deletions pkg/repo/fsrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,25 @@ func TestInitRepoDirect(t *testing.T) {
}

t.Run("successfully creates when directory exists", func(t *testing.T) {
dir, err := ioutil.TempDir("", "init")
require.NoError(t, err)
defer RequireRemoveAll(t, dir)
dir := t.TempDir()

_, err = initAndOpenRepoDirect(dir, 42, cfg)
_, err := initAndOpenRepoDirect(dir, 42, cfg)
assert.NoError(t, err)
checkNewRepoFiles(t, dir, 42)
})

t.Run("successfully creates when directory does not exist", func(t *testing.T) {
dir, err := ioutil.TempDir("", "init")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()

dir = filepath.Join(dir, "nested")
dir := filepath.Join(t.TempDir(), "nested")

_, err = initAndOpenRepoDirect(dir, 42, cfg)
_, err := initAndOpenRepoDirect(dir, 42, cfg)
assert.NoError(t, err)
checkNewRepoFiles(t, dir, 42)
})

t.Run("fails with error if directory is not writeable", func(t *testing.T) {
parentDir, err := ioutil.TempDir("", "init")
require.NoError(t, err)
defer RequireRemoveAll(t, parentDir)

// make read only dir
dir := filepath.Join(parentDir, "readonly")
err = os.Mkdir(dir, 0444)
dir := filepath.Join(t.TempDir(), "readonly")
err := os.Mkdir(dir, 0444)
assert.NoError(t, err)
assert.False(t, ConfigExists(dir))

Expand All @@ -71,11 +59,9 @@ func TestInitRepoDirect(t *testing.T) {
})

t.Run("fails with error if directory not empty", func(t *testing.T) {
dir, err := ioutil.TempDir("", "init")
require.NoError(t, err)
defer RequireRemoveAll(t, dir)
dir := t.TempDir()

err = ioutil.WriteFile(filepath.Join(dir, "hi"), []byte("hello"), 0644)
err := ioutil.WriteFile(filepath.Join(dir, "hi"), []byte("hello"), 0644)
assert.NoError(t, err)

_, err = initAndOpenRepoDirect(dir, 42, cfg)
Expand All @@ -87,47 +73,37 @@ func TestFSRepoOpen(t *testing.T) {
tf.UnitTest(t)

t.Run("[fail] repo version newer than binary", func(t *testing.T) {
container, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer RequireRemoveAll(t, container)
repoPath := path.Join(container, "repo")
repoPath := path.Join(t.TempDir(), "repo")

assert.NoError(t, InitFSRepo(repoPath, 1, config.NewDefaultConfig()))
// set wrong version
assert.NoError(t, WriteVersion(repoPath, 99))

_, err = OpenFSRepo(repoPath, 1)
_, err := OpenFSRepo(repoPath, 1)
expected := fmt.Sprintf("binary needs update to handle repo version, got 99 expected %d. Update binary to latest release", LatestVersion)
assert.EqualError(t, err, expected)
})

t.Run("[fail] version corrupt", func(t *testing.T) {
container, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer RequireRemoveAll(t, container)
repoPath := path.Join(container, "repo")
repoPath := path.Join(t.TempDir(), "repo")

assert.NoError(t, InitFSRepo(repoPath, 1, config.NewDefaultConfig()))
// set wrong version
assert.NoError(t, ioutil.WriteFile(filepath.Join(repoPath, versionFilename), []byte("v.8"), 0644))

_, err = OpenFSRepo(repoPath, 1)
_, err := OpenFSRepo(repoPath, 1)
assert.EqualError(t, err, "failed to read version: strconv.ParseUint: parsing \"v.8\": invalid syntax")
})
}

func TestFSRepoRoundtrip(t *testing.T) {
tf.UnitTest(t)

container, err := ioutil.TempDir("", "container")
require.NoError(t, err)
defer RequireRemoveAll(t, container)

cfg := config.NewDefaultConfig()
cfg.API.APIAddress = "foo" // testing that what we get back isnt just the default

repoPath := path.Join(container, "repo")
assert.NoError(t, err, InitFSRepo(repoPath, 42, cfg))
repoPath := path.Join(t.TempDir(), "repo")
assert.NoError(t, InitFSRepo(repoPath, 42, cfg))

r, err := OpenFSRepo(repoPath, 42)
assert.NoError(t, err)
Expand All @@ -149,14 +125,11 @@ func TestFSRepoRoundtrip(t *testing.T) {
func TestFSRepoReplaceAndSnapshotConfig(t *testing.T) {
tf.UnitTest(t)

container, err := ioutil.TempDir("", "container")
require.NoError(t, err)
defer RequireRemoveAll(t, container)
repoPath := path.Join(container, "repo")
repoPath := path.Join(t.TempDir(), "repo")

cfg := config.NewDefaultConfig()
cfg.API.APIAddress = "foo"
assert.NoError(t, err, InitFSRepo(repoPath, 42, cfg))
assert.NoError(t, InitFSRepo(repoPath, 42, cfg))

expSnpsht, err := ioutil.ReadFile(filepath.Join(repoPath, configFilename))
require.NoError(t, err)
Expand Down Expand Up @@ -189,13 +162,10 @@ func TestFSRepoReplaceAndSnapshotConfig(t *testing.T) {
func TestRepoLock(t *testing.T) {
tf.UnitTest(t)

container, err := ioutil.TempDir("", "container")
require.NoError(t, err)
defer RequireRemoveAll(t, container)
repoPath := path.Join(container, "repo")
repoPath := path.Join(t.TempDir(), "repo")

cfg := config.NewDefaultConfig()
assert.NoError(t, err, InitFSRepo(repoPath, 42, cfg))
assert.NoError(t, InitFSRepo(repoPath, 42, cfg))

r, err := OpenFSRepo(repoPath, 42)
assert.NoError(t, err)
Expand All @@ -212,20 +182,17 @@ func TestRepoLock(t *testing.T) {
func TestRepoLockFail(t *testing.T) {
tf.UnitTest(t)

container, err := ioutil.TempDir("", "container")
require.NoError(t, err)
defer RequireRemoveAll(t, container)
repoPath := path.Join(container, "repo")
repoPath := path.Join(t.TempDir(), "repo")

cfg := config.NewDefaultConfig()
assert.NoError(t, err, InitFSRepo(repoPath, 42, cfg))
assert.NoError(t, InitFSRepo(repoPath, 42, cfg))

// set invalid version, to make opening the repo fail
assert.NoError(t,
ioutil.WriteFile(filepath.Join(repoPath, versionFilename), []byte("hello"), 0644),
)

_, err = OpenFSRepo(repoPath, 42)
_, err := OpenFSRepo(repoPath, 42)
assert.Error(t, err)

_, err = os.Lstat(filepath.Join(repoPath, lockFile))
Expand Down Expand Up @@ -342,12 +309,10 @@ func getSnapshotFilenames(t *testing.T, dir string) []string {
}

func withFSRepo(t *testing.T, f func(*FSRepo)) {
dir, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer RequireRemoveAll(t, dir)
dir := t.TempDir()

cfg := config.NewDefaultConfig()
require.NoError(t, err, InitFSRepoDirect(dir, 42, cfg))
require.NoError(t, InitFSRepoDirect(dir, 42, cfg))

r, err := OpenFSRepo(dir, 42)
require.NoError(t, err)
Expand Down
6 changes: 0 additions & 6 deletions pkg/repo/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ func RequireMakeTempDir(t *testing.T, dirname string) string {
return newdir
}

// RequireRemoveAll ensures that the error condition is checked when we clean up
// after creating a temporary directory.
func RequireRemoveAll(t *testing.T, path string) {
require.NoError(t, os.RemoveAll(path))
}

// RequireOpenTempFile is a shortcut for opening a given temp file with a given
// suffix, then returning both a filename and a file pointer.
func RequireOpenTempFile(t *testing.T, suffix string) (*os.File, string) {
Expand Down