cmd/geth: make console tests more robust

* use --port 0 to avoid p2p port conflicts
* use --maxpeers 0 so it doesn't connect to bootstrap nodes
* use geth.expectExit() to wait for termination
This commit is contained in:
Felix Lange 2016-06-02 22:33:57 +02:00
parent ad0e6e971e
commit b57b6e341e

View File

@ -27,7 +27,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -37,9 +36,10 @@ func TestConsoleWelcome(t *testing.T) {
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
// Start a geth console, make sure it's cleaned up and terminate the console // Start a geth console, make sure it's cleaned up and terminate the console
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "console") geth := runGeth(t,
defer geth.expectExit() "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
geth.stdin.Close() "--etherbase", coinbase, "--shh",
"console")
// Gather all the infos the welcome message needs to contain // Gather all the infos the welcome message needs to contain
geth.setTemplateFunc("goos", func() string { return runtime.GOOS }) geth.setTemplateFunc("goos", func() string { return runtime.GOOS })
@ -51,7 +51,6 @@ func TestConsoleWelcome(t *testing.T) {
sort.Strings(apis) sort.Strings(apis)
return apis return apis
}) })
geth.setTemplateFunc("prompt", func() string { return console.DefaultPrompt })
// Verify the actual welcome message to the required template // Verify the actual welcome message to the required template
geth.expect(` geth.expect(`
@ -63,52 +62,63 @@ at block: 0 ({{niltime}})
datadir: {{.Datadir}} datadir: {{.Datadir}}
modules:{{range apis}} {{.}}:1.0{{end}} modules:{{range apis}} {{.}}:1.0{{end}}
{{prompt}} > {{.InputLine "exit"}}
`) `)
geth.expectExit()
} }
// Tests that a console can be attached to a running node via various means. // Tests that a console can be attached to a running node via various means.
func TestIPCAttachWelcome(t *testing.T) { func TestIPCAttachWelcome(t *testing.T) {
// Configure the instance for IPC attachement // Configure the instance for IPC attachement
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
var ipc string var ipc string
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
ipc = `\\.\pipe\geth` + strconv.Itoa(rand.Int()) ipc = `\\.\pipe\geth` + strconv.Itoa(rand.Int())
} else { } else {
ws := tmpdir(t) ws := tmpdir(t)
defer os.RemoveAll(ws) defer os.RemoveAll(ws)
ipc = filepath.Join(ws, "geth.ipc") ipc = filepath.Join(ws, "geth.ipc")
} }
// Run the parent geth and attach with a child console // Note: we need --shh because testAttachWelcome checks for default
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "--ipcpath", ipc) // list of ipc modules and shh is included there.
defer geth.interrupt() geth := runGeth(t,
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
"--etherbase", coinbase, "--shh", "--ipcpath", ipc)
time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
testAttachWelcome(t, geth, "ipc:"+ipc) testAttachWelcome(t, geth, "ipc:"+ipc)
geth.interrupt()
geth.expectExit()
} }
func TestHTTPAttachWelcome(t *testing.T) { func TestHTTPAttachWelcome(t *testing.T) {
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
geth := runGeth(t,
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--rpc", "--rpcport", port) "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
defer geth.interrupt() "--etherbase", coinbase, "--rpc", "--rpcport", port)
time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
testAttachWelcome(t, geth, "http://localhost:"+port) testAttachWelcome(t, geth, "http://localhost:"+port)
geth.interrupt()
geth.expectExit()
} }
func TestWSAttachWelcome(t *testing.T) { func TestWSAttachWelcome(t *testing.T) {
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--ws", "--wsport", port) geth := runGeth(t,
defer geth.interrupt() "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
"--etherbase", coinbase, "--ws", "--wsport", port)
time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
testAttachWelcome(t, geth, "ws://localhost:"+port) testAttachWelcome(t, geth, "ws://localhost:"+port)
geth.interrupt()
geth.expectExit()
} }
func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) { func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
@ -135,7 +145,6 @@ func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
sort.Strings(apis) sort.Strings(apis)
return apis return apis
}) })
attach.setTemplateFunc("prompt", func() string { return console.DefaultPrompt })
// Verify the actual welcome message to the required template // Verify the actual welcome message to the required template
attach.expect(` attach.expect(`
@ -147,6 +156,7 @@ at block: 0 ({{niltime}}){{if ipc}}
datadir: {{datadir}}{{end}} datadir: {{datadir}}{{end}}
modules:{{range apis}} {{.}}:1.0{{end}} modules:{{range apis}} {{.}}:1.0{{end}}
{{prompt}} > {{.InputLine "exit" }}
`) `)
attach.expectExit()
} }