本文整理匯總了Golang中github.com/NebulousLabs/Sia/build.TempDir函數的典型用法代碼示例。如果您正苦於以下問題:Golang TempDir函數的具體用法?Golang TempDir怎麽用?Golang TempDir使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了TempDir函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestReadWriteFile
func TestReadWriteFile(t *testing.T) {
// standard
os.MkdirAll(build.TempDir("encoding"), 0777)
path := build.TempDir("encoding", "TestReadWriteFile")
err := WriteFile(path, testStructs[3])
if err != nil {
t.Fatal(err)
}
var obj test4
err = ReadFile(path, &obj)
if err != nil {
t.Error(err)
}
// bad paths
err = WriteFile("/foo/bar", "baz")
if err == nil {
t.Error("expected error, got nil")
}
err = ReadFile("/foo/bar", nil)
if err == nil {
t.Error("expected error, got nil")
}
}
示例2: TestRelativePathSafeFile
// TestRelativePathSafeFile tests creating and committing safe files with
// relative paths. Specifically, we test that calling os.Chdir between creating
// and committing a safe file doesn't affect the safe file's final path. The
// relative path tested is relative to the working directory.
func TestRelativePathSafeFile(t *testing.T) {
tmpDir := build.TempDir(persistDir, "TestRelativePathSafeFile")
err := os.MkdirAll(tmpDir, 0700)
if err != nil {
t.Fatal(err)
}
absPath := filepath.Join(tmpDir, "test")
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
relPath, err := filepath.Rel(wd, absPath)
// Create safe file.
sf, err := NewSafeFile(relPath)
defer sf.Close()
if err != nil {
t.Fatal(err)
}
// Check that the path of the file is not equal to the final path of the
// file.
if sf.Name() == absPath {
t.Errorf("safeFile created with filename: %s has temporary filename that is equivalent to finalName: %s\n", absPath, sf.Name())
}
// Write random data to the file.
data := make([]byte, 10)
rand.Read(data)
_, err = sf.Write(data)
if err != nil {
t.Fatal(err)
}
// Change directories and commit.
tmpChdir := build.TempDir(persistDir, "TestRelativePathSafeFileTmpChdir")
err = os.MkdirAll(tmpChdir, 0700)
if err != nil {
t.Fatal(err)
}
os.Chdir(tmpChdir)
defer os.Chdir(wd)
err = sf.CommitSync()
if err != nil {
t.Fatal(err)
}
// Check that the file exists and has same data that was written to it.
dataRead, err := ioutil.ReadFile(absPath)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, dataRead) {
t.Fatalf("Committed file has different data than was written to it: expected %v, got %v\n", data, dataRead)
}
}
示例3: TestSaveLoad
// TestSaveLoad tests that the contractor can save and load itself.
func TestSaveLoad(t *testing.T) {
// create contractor with mocked persist dependency
c := &Contractor{
contracts: make(map[types.FileContractID]modules.RenterContract),
}
c.persist = new(memPersist)
// add some fake contracts
c.contracts = map[types.FileContractID]modules.RenterContract{
{0}: {NetAddress: "foo"},
{1}: {NetAddress: "bar"},
{2}: {NetAddress: "baz"},
}
// save and reload
err := c.save()
if err != nil {
t.Fatal(err)
}
err = c.load()
if err != nil {
t.Fatal(err)
}
// check that contracts were restored
_, ok0 := c.contracts[types.FileContractID{0}]
_, ok1 := c.contracts[types.FileContractID{1}]
_, ok2 := c.contracts[types.FileContractID{2}]
if !ok0 || !ok1 || !ok2 {
t.Fatal("contracts were not restored properly:", c.contracts)
}
// use stdPersist instead of mock
c.persist = newPersist(build.TempDir("contractor", "TestSaveLoad"))
os.MkdirAll(build.TempDir("contractor", "TestSaveLoad"), 0700)
// save and reload
err = c.save()
if err != nil {
t.Fatal(err)
}
err = c.load()
if err != nil {
t.Fatal(err)
}
// check that contracts were restored
_, ok0 = c.contracts[types.FileContractID{0}]
_, ok1 = c.contracts[types.FileContractID{1}]
_, ok2 = c.contracts[types.FileContractID{2}]
if !ok0 || !ok1 || !ok2 {
t.Fatal("contracts were not restored properly:", c.contracts)
}
}
示例4: TestSaveLoad
// TestSaveLoad tests that the hostdb can save and load itself.
func TestSaveLoad(t *testing.T) {
// create hostdb with mocked persist dependency
hdb := bareHostDB()
hdb.persist = new(memPersist)
// add some fake contracts
hdb.contracts = map[types.FileContractID]hostContract{
{0}: {IP: "foo"},
{1}: {IP: "bar"},
{2}: {IP: "baz"},
}
// save and reload
err := hdb.save()
if err != nil {
t.Fatal(err)
}
err = hdb.load()
if err != nil {
t.Fatal(err)
}
// check that contracts were restored
_, ok0 := hdb.contracts[types.FileContractID{0}]
_, ok1 := hdb.contracts[types.FileContractID{1}]
_, ok2 := hdb.contracts[types.FileContractID{2}]
if !ok0 || !ok1 || !ok2 {
t.Fatal("contracts were not restored properly:", hdb.contracts)
}
// use stdPersist instead of mock
hdb.persist = newPersist(build.TempDir("hostdb", "TestSaveLoad"))
os.MkdirAll(build.TempDir("hostdb", "TestSaveLoad"), 0700)
// save and reload
err = hdb.save()
if err != nil {
t.Fatal(err)
}
err = hdb.load()
if err != nil {
t.Fatal(err)
}
// check that contracts were restored
_, ok0 = hdb.contracts[types.FileContractID{0}]
_, ok1 = hdb.contracts[types.FileContractID{1}]
_, ok2 = hdb.contracts[types.FileContractID{2}]
if !ok0 || !ok1 || !ok2 {
t.Fatal("contracts were not restored properly:", hdb.contracts)
}
}
示例5: TestErrIntegratedCheckMetadata
// TestErrIntegratedCheckMetadata checks that checkMetadata returns an error
// within OpenDatabase when OpenDatabase is called on a BoltDatabase that has
// already been set up with different metadata.
func TestErrIntegratedCheckMetadata(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
testDir := build.TempDir(persistDir, "TestErrIntegratedCheckMetadata")
err := os.MkdirAll(testDir, 0700)
if err != nil {
t.Fatal(err)
}
for i, in := range testInputs {
dbFilename := testFilenames[i%len(testFilenames)]
dbFilepath := filepath.Join(testDir, dbFilename)
boltDB, err := OpenDatabase(in.md, dbFilepath)
if err != nil {
t.Errorf("OpenDatabase failed on input %v, filename %v; error was %v", in, dbFilename, err)
continue
}
err = boltDB.Close()
if err != nil {
t.Fatal(err)
}
// Should return an error because boltDB was set up with metadata in.md, not in.newMd
boltDB, err = OpenDatabase(in.newMd, dbFilepath)
if err != in.err {
t.Errorf("expected error %v for input %v and filename %v; got %v instead", in.err, in, dbFilename, err)
}
err = os.Remove(dbFilepath)
if err != nil {
t.Fatal(err)
}
}
}
示例6: createExplorerTester
// createExplorerTester creates a tester object for the explorer module.
func createExplorerTester(name string) (*explorerTester, error) {
// Create and assemble the dependencies.
testdir := build.TempDir(modules.HostDir, name)
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.RenterDir))
if err != nil {
return nil, err
}
e, err := New(cs, filepath.Join(testdir, modules.ExplorerDir))
if err != nil {
return nil, err
}
et := &explorerTester{
cs: cs,
gateway: g,
miner: m,
tpool: tp,
wallet: w,
walletKey: key,
explorer: e,
}
// Mine until the wallet has money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := et.miner.FindBlock()
err = et.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return et, nil
}
示例7: createAuthenticatedServerTester
// createAuthenticatedServerTester creates an authenticated server tester
// object that is ready for testing, including money in the wallet and all
// modules initalized.
func createAuthenticatedServerTester(name string, password string) (*serverTester, error) {
// createAuthenticatedServerTester should not get called during short
// tests, as it takes a long time to run.
if testing.Short() {
panic("assembleServerTester called during short tests")
}
// Create the testing directory.
testdir := build.TempDir("authenticated-api", name)
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
st, err := assembleAuthenticatedServerTester(password, key, testdir)
if err != nil {
return nil, err
}
// Mine blocks until the wallet has confirmed money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
_, err := st.miner.AddBlock()
if err != nil {
return nil, err
}
}
return st, nil
}
示例8: TestIntegrationNewNilInputs
// TestIntegrationNewNilInputs tries to trigger a panic with nil inputs.
func TestIntegrationNewNilInputs(t *testing.T) {
// Create a gateway and consensus set.
testdir := build.TempDir(modules.TransactionPoolDir, "TestNewNilInputs")
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
t.Fatal(err)
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
t.Fatal(err)
}
// Try all combinations of nil inputs.
_, err = New(nil, nil)
if err == nil {
t.Error(err)
}
_, err = New(nil, g)
if err != errNilCS {
t.Error(err)
}
_, err = New(cs, nil)
if err != errNilGateway {
t.Error(err)
}
_, err = New(cs, g)
if err != nil {
t.Error(err)
}
}
示例9: TestStartDaemon
// TestStartDaemon probes the startDaemon function.
func TestStartDaemon(t *testing.T) {
testDir := build.TempDir("siad", "TestStartDaemon")
config.Siad.NoBootstrap = false
config.Siad.APIaddr = "localhost:45170"
config.Siad.RPCaddr = ":45171"
config.Siad.HostAddr = ":45172"
config.Siad.SiaDir = testDir
go func() {
err := startDaemon()
if err != nil {
t.Error(err)
}
}()
// Wait until the server has started, and then send a kill command to the
// daemon.
<-started
time.Sleep(250 * time.Millisecond)
resp, err := http.Get("http://localhost:45170/daemon/stop")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
resp.Body.Close()
}
示例10: createConsensusSetTester
// createConsensusSetTester creates a consensusSetTester that's ready for use.
func createConsensusSetTester(name string) (*consensusSetTester, error) {
testdir := build.TempDir(modules.ConsensusDir, name)
// Create modules.
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all objects into a consensusSetTester.
cst := &consensusSetTester{
gateway: g,
miner: m,
tpool: tp,
wallet: w,
walletKey: key,
cs: cs,
persistDir: testdir,
}
// Mine until the wallet has money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := cst.miner.FindBlock()
err = cst.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return cst, nil
}
示例11: newHostDBTester
// newHostDBTester creates a ready-to-use hostdb tester with money in the
// wallet.
func newHostDBTester(name string) (*hostdbTester, error) {
// Create the modules.
testdir := build.TempDir("hostdb", name)
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := modWallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
hdb, err := New(cs, w, tp, filepath.Join(testdir, modules.RenterDir))
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all pieces into a hostdb tester.
ht := &hostdbTester{
cs: cs,
gateway: g,
miner: m,
tpool: tp,
wallet: w,
hostdb: hdb,
}
// Mine blocks until there is money in the wallet.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
_, err := ht.miner.AddBlock()
if err != nil {
return nil, err
}
}
return ht, nil
}
示例12: TestCloseWallet
// TestCloseWallet tries to close the wallet.
func TestCloseWallet(t *testing.T) {
if testing.Short() {
t.Skip()
}
testdir := build.TempDir(modules.WalletDir, "TestCloseWallet")
g, err := gateway.New("localhost:0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
t.Fatal(err)
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
t.Fatal(err)
}
tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
if err != nil {
t.Fatal(err)
}
wdir := filepath.Join(testdir, modules.WalletDir)
w, err := New(cs, tp, wdir)
if err != nil {
t.Fatal(err)
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
}
示例13: TestLoggerCritical
// TestLoggerCritical prints a critical message from the logger.
func TestLoggerCritical(t *testing.T) {
// Create a folder for the log file.
testdir := build.TempDir(persistDir, "TestLogger")
err := os.MkdirAll(testdir, 0700)
if err != nil {
t.Fatal(err)
}
// Create the logger.
logFilename := filepath.Join(testdir, "test.log")
fl, err := NewLogger(logFilename)
if err != nil {
t.Fatal(err)
}
// Write a catch for a panic that should trigger when logger.Critical is
// called.
defer func() {
r := recover()
if r == nil {
t.Error("critical message was not thrown in a panic")
}
// Close the file logger to clean up the test.
err = fl.Close()
if err != nil {
t.Fatal(err)
}
}()
fl.Critical("a critical message")
}
示例14: TestGatewayPeerRemove
func TestGatewayPeerRemove(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
st, err := createServerTester("TestGatewayPeerRemove")
if err != nil {
t.Fatal(err)
}
peer, err := gateway.New(":0", build.TempDir("api", "TestGatewayPeerRemove", "gateway"))
if err != nil {
t.Fatal(err)
}
st.stdGetAPI("/gateway/peers/add?address=" + string(peer.Address()))
var info GatewayInfo
st.getAPI("/gateway/status", &info)
if len(info.Peers) != 1 || info.Peers[0] != peer.Address() {
t.Fatal("/gateway/peers/add did not add peer", peer.Address())
}
st.stdGetAPI("/gateway/peers/remove?address=" + string(peer.Address()))
st.getAPI("/gateway/status", &info)
if len(info.Peers) != 0 {
t.Fatal("/gateway/peer/add did not add peer", peer.Address())
}
}
示例15: createTpoolTester
// createTpoolTester returns a ready-to-use tpool tester, with all modules
// initialized.
func createTpoolTester(name string) (*tpoolTester, error) {
// Initialize the modules.
testdir := build.TempDir(modules.TransactionPoolDir, name)
g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
var key crypto.TwofishKey
_, err = rand.Read(key[:])
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all of the objects into a tpoolTester
tpt := &tpoolTester{
cs: cs,
gateway: g,
tpool: tp,
miner: m,
wallet: w,
walletKey: key,
persistDir: testdir,
}
// Mine blocks until there is money in the wallet.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := tpt.miner.FindBlock()
err = tpt.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return tpt, nil
}