Merge pull request #2076 from nspcc-dev/fix-occasional-bolt-test-failures

Improve temp file/dir handling in tests
This commit is contained in:
Roman Khimov 2021-07-20 16:53:54 +03:00 committed by GitHub
commit caf07c1ee7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 79 additions and 61 deletions

View file

@ -58,12 +58,13 @@ func TestCalcHash(t *testing.T) {
"--in", "./testdata/verify.nef123", "--manifest", manifestPath)...) "--in", "./testdata/verify.nef123", "--manifest", manifestPath)...)
}) })
t.Run("invalid file", func(t *testing.T) { t.Run("invalid file", func(t *testing.T) {
p := path.Join(os.TempDir(), "neogo.calchash.verify.nef") p, err := ioutil.TempFile("", "neogo.calchash.verify.nef")
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Remove(p) os.Remove(p.Name())
}) })
require.NoError(t, ioutil.WriteFile(p, src[:4], os.ModePerm)) require.NoError(t, ioutil.WriteFile(p.Name(), src[:4], os.ModePerm))
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p, "--manifest", manifestPath)...) e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p.Name(), "--manifest", manifestPath)...)
}) })
cmd = append(cmd, "--in", nefPath, "--manifest", manifestPath) cmd = append(cmd, "--in", nefPath, "--manifest", manifestPath)
@ -83,8 +84,8 @@ func TestCalcHash(t *testing.T) {
} }
func TestContractInitAndCompile(t *testing.T) { func TestContractInitAndCompile(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.inittest") tmpDir, err := ioutil.TempDir("", "neogo.inittest")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })
@ -148,8 +149,8 @@ func TestDeployBigContract(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := path.Join(os.TempDir(), "neogo.test.deployfail") tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })
@ -174,8 +175,8 @@ func TestContractDeployWithData(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := path.Join(os.TempDir(), "neogo.test.deployfail") tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })
@ -262,8 +263,8 @@ func TestComlileAndInvokeFunction(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := path.Join(os.TempDir(), "neogo.test.compileandinvoke") tmpDir, err := ioutil.TempDir("", "neogo.test.compileandinvoke")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })
@ -515,8 +516,8 @@ func TestContractInspect(t *testing.T) {
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
const srcPath = "testdata/deploy/main.go" const srcPath = "testdata/deploy/main.go"
tmpDir := path.Join(os.TempDir(), "neogo.test.contract.inspect") tmpDir, err := ioutil.TempDir("", "neogo.test.contract.inspect")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })
@ -553,8 +554,6 @@ func TestCompileExamples(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := os.TempDir()
e := newExecutor(t, false) e := newExecutor(t, false)
for _, info := range infos { for _, info := range infos {
@ -568,19 +567,21 @@ func TestCompileExamples(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it") require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it")
outPath := path.Join(tmpDir, info.Name()+".nef") outF, err := ioutil.TempFile("", info.Name()+".nef")
manifestPath := path.Join(tmpDir, info.Name()+".manifest.json") require.NoError(t, err)
manifestF, err := ioutil.TempFile("", info.Name()+".manifest.json")
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Remove(outPath) os.Remove(outF.Name())
os.Remove(manifestPath) os.Remove(manifestF.Name())
}) })
cfgName := filterFilename(infos, ".yml") cfgName := filterFilename(infos, ".yml")
opts := []string{ opts := []string{
"neo-go", "contract", "compile", "neo-go", "contract", "compile",
"--in", path.Join(examplePath, info.Name()), "--in", path.Join(examplePath, info.Name()),
"--out", outPath, "--out", outF.Name(),
"--manifest", manifestPath, "--manifest", manifestF.Name(),
"--config", path.Join(examplePath, info.Name(), cfgName), "--config", path.Join(examplePath, info.Name(), cfgName),
} }
e.Run(t, opts...) e.Run(t, opts...)
@ -590,16 +591,18 @@ func TestCompileExamples(t *testing.T) {
t.Run("invalid events in manifest", func(t *testing.T) { t.Run("invalid events in manifest", func(t *testing.T) {
const dir = "./testdata/" const dir = "./testdata/"
for _, name := range []string{"invalid1", "invalid2", "invalid3"} { for _, name := range []string{"invalid1", "invalid2", "invalid3"} {
outPath := path.Join(tmpDir, name+".nef") outF, err := ioutil.TempFile("", name+".nef")
manifestPath := path.Join(tmpDir, name+".manifest.json") require.NoError(t, err)
manifestF, err := ioutil.TempFile("", name+".manifest.json")
require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Remove(outPath) os.Remove(outF.Name())
os.Remove(manifestPath) os.Remove(manifestF.Name())
}) })
e.RunWithError(t, "neo-go", "contract", "compile", e.RunWithError(t, "neo-go", "contract", "compile",
"--in", path.Join(dir, name), "--in", path.Join(dir, name),
"--out", outPath, "--out", outF.Name(),
"--manifest", manifestPath, "--manifest", manifestF.Name(),
"--config", path.Join(dir, name, "invalid.yml"), "--config", path.Join(dir, name, "invalid.yml"),
) )
} }

View file

@ -12,8 +12,8 @@ import (
) )
func TestDBRestore(t *testing.T) { func TestDBRestore(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.restoretest") tmpDir, err := ioutil.TempDir("", "neogo.restoretest")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"path" "path"
@ -27,12 +28,12 @@ func TestSignMultisigTx(t *testing.T) {
multisigAddr := address.Uint160ToString(multisigHash) multisigAddr := address.Uint160ToString(multisigHash)
// Create 2 wallets participating in multisig. // Create 2 wallets participating in multisig.
tmpDir := os.TempDir() tmpDir, err := ioutil.TempDir("", "neogo.wallettest")
require.NoError(t, err)
wallet1Path := path.Join(tmpDir, "multiWallet1.json") wallet1Path := path.Join(tmpDir, "multiWallet1.json")
wallet2Path := path.Join(tmpDir, "multiWallet2.json") wallet2Path := path.Join(tmpDir, "multiWallet2.json")
t.Cleanup(func() { t.Cleanup(func() {
os.Remove(wallet1Path) os.RemoveAll(tmpDir)
os.Remove(wallet2Path)
}) })
addAccount := func(w string, wif string) { addAccount := func(w string, wif string) {

View file

@ -31,9 +31,10 @@ const (
func TestNEP11Import(t *testing.T) { func TestNEP11Import(t *testing.T) {
e := newExecutor(t, true) e := newExecutor(t, true)
tmpDir := os.TempDir() tmpDir, err := ioutil.TempDir("", "neogo.nep11import")
require.NoError(t, err)
walletPath := path.Join(tmpDir, "walletForImport.json") walletPath := path.Join(tmpDir, "walletForImport.json")
defer os.Remove(walletPath) t.Cleanup(func() { os.RemoveAll(tmpDir) })
// deploy NFT NeoNameService contract // deploy NFT NeoNameService contract
nnsContractHash := deployNNSContract(t, e) nnsContractHash := deployNNSContract(t, e)

View file

@ -2,6 +2,7 @@ package main
import ( import (
"io" "io"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"path" "path"
@ -260,9 +261,10 @@ func TestNEP17MultiTransfer(t *testing.T) {
func TestNEP17ImportToken(t *testing.T) { func TestNEP17ImportToken(t *testing.T) {
e := newExecutor(t, true) e := newExecutor(t, true)
tmpDir := os.TempDir() tmpDir, err := ioutil.TempDir("", "neogo.nep17import")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(tmpDir) })
walletPath := path.Join(tmpDir, "walletForImport.json") walletPath := path.Join(tmpDir, "walletForImport.json")
defer os.Remove(walletPath)
neoContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Neo) neoContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Neo)
require.NoError(t, err) require.NoError(t, err)

View file

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"path" "path"
@ -19,8 +20,8 @@ import (
) )
func TestWalletInit(t *testing.T) { func TestWalletInit(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.test.walletinit") tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })
@ -243,12 +244,13 @@ func TestImportDeployed(t *testing.T) {
e := newExecutor(t, true) e := newExecutor(t, true)
h := deployVerifyContract(t, e) h := deployVerifyContract(t, e)
tmpDir := os.TempDir() tmpDir, err := ioutil.TempDir("", "neogo.importdeployed")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(tmpDir)
})
walletPath := path.Join(tmpDir, "wallet.json") walletPath := path.Join(tmpDir, "wallet.json")
e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath) e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath)
t.Cleanup(func() {
os.Remove(walletPath)
})
priv, err := keys.NewPrivateKey() priv, err := keys.NewPrivateKey()
require.NoError(t, err) require.NoError(t, err)
@ -373,8 +375,8 @@ func TestDumpKeys(t *testing.T) {
// Testcase is the wallet of privnet validator. // Testcase is the wallet of privnet validator.
func TestWalletConvert(t *testing.T) { func TestWalletConvert(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.test.convert") tmpDir, err := ioutil.TempDir("", "neogo.test.convert")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })

View file

@ -2,6 +2,7 @@ package core
import ( import (
"errors" "errors"
"io/ioutil"
"os" "os"
"path" "path"
"sort" "sort"
@ -78,9 +79,11 @@ func TestStateRoot(t *testing.T) {
updateIndex := bc.BlockHeight() updateIndex := bc.BlockHeight()
transferTokenFromMultisigAccount(t, bc, h, bc.contracts.GAS.Hash, 1_0000_0000) transferTokenFromMultisigAccount(t, bc, h, bc.contracts.GAS.Hash, 1_0000_0000)
tmpDir := path.Join(os.TempDir(), "neogo.initsnz") tmpDir, err := ioutil.TempDir("", "neogo.test.stateroot")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
defer os.RemoveAll(tmpDir) t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass") w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass")
cfg := createStateRootConfig(w.Path(), "pass") cfg := createStateRootConfig(w.Path(), "pass")
srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil) srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil)
@ -148,9 +151,11 @@ func TestStateRootInitNonZeroHeight(t *testing.T) {
_, err := persistBlock(bc) _, err := persistBlock(bc)
require.NoError(t, err) require.NoError(t, err)
tmpDir := path.Join(os.TempDir(), "neogo.initsnz") tmpDir, err := ioutil.TempDir("", "neogo.initsnz")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
defer os.RemoveAll(tmpDir) t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass") w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass")
cfg := createStateRootConfig(w.Path(), "pass") cfg := createStateRootConfig(w.Path(), "pass")
srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil) srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil)
@ -190,9 +195,11 @@ func createStateRootConfig(walletPath, password string) config.StateRoot {
} }
func TestStateRootFull(t *testing.T) { func TestStateRootFull(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.stateroot4") tmpDir, err := ioutil.TempDir("", "neogo.stateroot4")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
defer os.RemoveAll(tmpDir) t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
bc := newTestChain(t) bc := newTestChain(t)

View file

@ -12,12 +12,12 @@ func newBoltStoreForTesting(t *testing.T) Store {
testFileName := "test_bolt_db" testFileName := "test_bolt_db"
file, err := ioutil.TempFile("", testFileName) file, err := ioutil.TempFile("", testFileName)
t.Cleanup(func() { t.Cleanup(func() {
err := os.RemoveAll(testFileName) err := os.RemoveAll(file.Name())
require.NoError(t, err) require.NoError(t, err)
}) })
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, file.Close()) require.NoError(t, file.Close())
boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: testFileName}) boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: file.Name()})
require.NoError(t, err) require.NoError(t, err)
return boltDBStore return boltDBStore
} }

View file

@ -162,8 +162,8 @@ func TestLoad(t *testing.T) {
return a * b return a * b
} }
}` }`
tmpDir := path.Join(os.TempDir(), "vmcliloadtest") tmpDir, err := ioutil.TempDir("", "neogo.vmcliloadtest")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
}) })
@ -262,11 +262,13 @@ func TestRunWithDifferentArguments(t *testing.T) {
return arg return arg
}` }`
filename := path.Join(os.TempDir(), "run_vmtestcontract.go") tmpDir, err := ioutil.TempDir("", "neogo.vmcliruntest")
require.NoError(t, ioutil.WriteFile(filename, []byte(src), os.ModePerm)) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
os.Remove(filename) os.RemoveAll(tmpDir)
}) })
filename := path.Join(tmpDir, "run_vmtestcontract.go")
require.NoError(t, ioutil.WriteFile(filename, []byte(src), os.ModePerm))
e := newTestVMCLI(t) e := newTestVMCLI(t)
e.runProg(t, e.runProg(t,