Skip to content

Commit 3d69970

Browse files
fjlkaralabe
authored andcommitted
[release/1.4.6] 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 (cherry picked from commit b57b6e3)
1 parent 8b90a49 commit 3d69970

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

cmd/geth/consolecmd_test.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"testing"
2828
"time"
2929

30-
"github.com/ethereum/go-ethereum/console"
3130
"github.com/ethereum/go-ethereum/rpc"
3231
)
3332

@@ -37,9 +36,10 @@ func TestConsoleWelcome(t *testing.T) {
3736
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
3837

3938
// Start a geth console, make sure it's cleaned up and terminate the console
40-
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "console")
41-
defer geth.expectExit()
42-
geth.stdin.Close()
39+
geth := runGeth(t,
40+
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
41+
"--etherbase", coinbase, "--shh",
42+
"console")
4343

4444
// Gather all the infos the welcome message needs to contain
4545
geth.setTemplateFunc("goos", func() string { return runtime.GOOS })
@@ -51,7 +51,6 @@ func TestConsoleWelcome(t *testing.T) {
5151
sort.Strings(apis)
5252
return apis
5353
})
54-
geth.setTemplateFunc("prompt", func() string { return console.DefaultPrompt })
5554

5655
// Verify the actual welcome message to the required template
5756
geth.expect(`
@@ -63,52 +62,63 @@ at block: 0 ({{niltime}})
6362
datadir: {{.Datadir}}
6463
modules:{{range apis}} {{.}}:1.0{{end}}
6564
66-
{{prompt}}
65+
> {{.InputLine "exit"}}
6766
`)
67+
geth.expectExit()
6868
}
6969

7070
// Tests that a console can be attached to a running node via various means.
7171
func TestIPCAttachWelcome(t *testing.T) {
7272
// Configure the instance for IPC attachement
7373
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
74-
7574
var ipc string
7675
if runtime.GOOS == "windows" {
7776
ipc = `\\.\pipe\geth` + strconv.Itoa(rand.Int())
7877
} else {
7978
ws := tmpdir(t)
8079
defer os.RemoveAll(ws)
81-
8280
ipc = filepath.Join(ws, "geth.ipc")
8381
}
84-
// Run the parent geth and attach with a child console
85-
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "--ipcpath", ipc)
86-
defer geth.interrupt()
82+
// Note: we need --shh because testAttachWelcome checks for default
83+
// list of ipc modules and shh is included there.
84+
geth := runGeth(t,
85+
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
86+
"--etherbase", coinbase, "--shh", "--ipcpath", ipc)
8787

8888
time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
8989
testAttachWelcome(t, geth, "ipc:"+ipc)
90+
91+
geth.interrupt()
92+
geth.expectExit()
9093
}
9194

9295
func TestHTTPAttachWelcome(t *testing.T) {
9396
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
9497
port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
95-
96-
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--rpc", "--rpcport", port)
97-
defer geth.interrupt()
98+
geth := runGeth(t,
99+
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
100+
"--etherbase", coinbase, "--rpc", "--rpcport", port)
98101

99102
time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
100103
testAttachWelcome(t, geth, "http://localhost:"+port)
104+
105+
geth.interrupt()
106+
geth.expectExit()
101107
}
102108

103109
func TestWSAttachWelcome(t *testing.T) {
104110
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
105111
port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
106112

107-
geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--ws", "--wsport", port)
108-
defer geth.interrupt()
113+
geth := runGeth(t,
114+
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
115+
"--etherbase", coinbase, "--ws", "--wsport", port)
109116

110117
time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
111118
testAttachWelcome(t, geth, "ws://localhost:"+port)
119+
120+
geth.interrupt()
121+
geth.expectExit()
112122
}
113123

114124
func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
@@ -135,7 +145,6 @@ func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
135145
sort.Strings(apis)
136146
return apis
137147
})
138-
attach.setTemplateFunc("prompt", func() string { return console.DefaultPrompt })
139148

140149
// Verify the actual welcome message to the required template
141150
attach.expect(`
@@ -147,6 +156,7 @@ at block: 0 ({{niltime}}){{if ipc}}
147156
datadir: {{datadir}}{{end}}
148157
modules:{{range apis}} {{.}}:1.0{{end}}
149158
150-
{{prompt}}
159+
> {{.InputLine "exit" }}
151160
`)
161+
attach.expectExit()
152162
}

0 commit comments

Comments
 (0)