Skip to content

Commit af0e5f3

Browse files
holimanrjl493456442
authored andcommitted
cmd/geth: make tests run quicker + use less memory and disk (ethereum#21919)
1 parent 9c06acb commit af0e5f3

File tree

5 files changed

+71
-89
lines changed

5 files changed

+71
-89
lines changed

cmd/geth/accountcmd_test.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ func tmpDatadirWithKeystore(t *testing.T) string {
4343
}
4444

4545
func TestAccountListEmpty(t *testing.T) {
46-
geth := runGeth(t, "account", "list")
46+
geth := runGeth(t, "--nousb", "account", "list")
4747
geth.ExpectExit()
4848
}
4949

5050
func TestAccountList(t *testing.T) {
5151
datadir := tmpDatadirWithKeystore(t)
52-
geth := runGeth(t, "account", "list", "--datadir", datadir)
52+
geth := runGeth(t, "--nousb", "account", "list", "--datadir", datadir)
5353
defer geth.ExpectExit()
5454
if runtime.GOOS == "windows" {
5555
geth.Expect(`
@@ -138,7 +138,7 @@ Fatal: Passwords do not match
138138

139139
func TestAccountUpdate(t *testing.T) {
140140
datadir := tmpDatadirWithKeystore(t)
141-
geth := runGeth(t, "account", "update",
141+
geth := runGeth(t, "--nousb", "account", "update",
142142
"--datadir", datadir, "--lightkdf",
143143
"f466859ead1932d743d622cb74fc058882e8648a")
144144
defer geth.ExpectExit()
@@ -153,7 +153,7 @@ Repeat password: {{.InputLine "foobar2"}}
153153
}
154154

155155
func TestWalletImport(t *testing.T) {
156-
geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
156+
geth := runGeth(t, "--nousb", "wallet", "import", "--lightkdf", "testdata/guswallet.json")
157157
defer geth.ExpectExit()
158158
geth.Expect(`
159159
!! Unsupported terminal, password will be echoed.
@@ -168,7 +168,7 @@ Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
168168
}
169169

170170
func TestWalletImportBadPassword(t *testing.T) {
171-
geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
171+
geth := runGeth(t, "--nousb", "wallet", "import", "--lightkdf", "testdata/guswallet.json")
172172
defer geth.ExpectExit()
173173
geth.Expect(`
174174
!! Unsupported terminal, password will be echoed.
@@ -178,11 +178,8 @@ Fatal: could not decrypt key with given password
178178
}
179179

180180
func TestUnlockFlag(t *testing.T) {
181-
datadir := tmpDatadirWithKeystore(t)
182-
geth := runGeth(t,
183-
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "256", "--ipcdisable",
184-
"--datadir", datadir, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
185-
"js", "testdata/empty.js")
181+
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
182+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")
186183
geth.Expect(`
187184
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
188185
!! Unsupported terminal, password will be echoed.
@@ -202,10 +199,9 @@ Password: {{.InputLine "foobar"}}
202199
}
203200

204201
func TestUnlockFlagWrongPassword(t *testing.T) {
205-
datadir := tmpDatadirWithKeystore(t)
206-
geth := runGeth(t,
207-
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
208-
"--datadir", datadir, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
202+
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
203+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")
204+
209205
defer geth.ExpectExit()
210206
geth.Expect(`
211207
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
@@ -221,10 +217,9 @@ Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could
221217

222218
// https://github.com/ethereum/go-ethereum/issues/1785
223219
func TestUnlockFlagMultiIndex(t *testing.T) {
224-
datadir := tmpDatadirWithKeystore(t)
225-
geth := runGeth(t,
226-
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
227-
"--datadir", datadir, "--unlock", "0,2", "js", "testdata/empty.js")
220+
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
221+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--unlock", "0,2", "js", "testdata/empty.js")
222+
228223
geth.Expect(`
229224
Unlocking account 0 | Attempt 1/3
230225
!! Unsupported terminal, password will be echoed.
@@ -247,11 +242,9 @@ Password: {{.InputLine "foobar"}}
247242
}
248243

249244
func TestUnlockFlagPasswordFile(t *testing.T) {
250-
datadir := tmpDatadirWithKeystore(t)
251-
geth := runGeth(t,
252-
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
253-
"--datadir", datadir, "--password", "testdata/passwords.txt", "--unlock", "0,2",
254-
"js", "testdata/empty.js")
245+
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
246+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password", "testdata/passwords.txt", "--unlock", "0,2", "js", "testdata/empty.js")
247+
255248
geth.ExpectExit()
256249

257250
wantMessages := []string{
@@ -267,10 +260,9 @@ func TestUnlockFlagPasswordFile(t *testing.T) {
267260
}
268261

269262
func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
270-
datadir := tmpDatadirWithKeystore(t)
271-
geth := runGeth(t,
272-
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
273-
"--datadir", datadir, "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
263+
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
264+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password",
265+
"testdata/wrong-passwords.txt", "--unlock", "0,2")
274266
defer geth.ExpectExit()
275267
geth.Expect(`
276268
Fatal: Failed to unlock account 0 (could not decrypt key with given password)
@@ -279,9 +271,9 @@ Fatal: Failed to unlock account 0 (could not decrypt key with given password)
279271

280272
func TestUnlockFlagAmbiguous(t *testing.T) {
281273
store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
282-
geth := runGeth(t,
283-
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
284-
"--keystore", store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
274+
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
275+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore",
276+
store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
285277
"js", "testdata/empty.js")
286278
defer geth.ExpectExit()
287279

@@ -317,9 +309,10 @@ In order to avoid this warning, you need to remove the following duplicate key f
317309

318310
func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
319311
store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
320-
geth := runGeth(t,
321-
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
322-
"--keystore", store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
312+
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
313+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore",
314+
store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
315+
323316
defer geth.ExpectExit()
324317

325318
// Helper for the expect template, returns absolute keystore path.

cmd/geth/consolecmd_test.go

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,25 @@ const (
3535
httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0"
3636
)
3737

38+
// spawns geth with the given command line args, using a set of flags to minimise
39+
// memory and disk IO. If the args don't set --datadir, the
40+
// child g gets a temporary data directory.
41+
func runMinimalGeth(t *testing.T, args ...string) *testgeth {
42+
// --ropsten to make the 'writing genesis to disk' faster (no accounts)
43+
// --networkid=1337 to avoid cache bump
44+
// --syncmode=full to avoid allocating fast sync bloom
45+
allArgs := []string{"--ropsten", "--nousb", "--networkid", "1337", "--syncmode=full", "--port", "0",
46+
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64"}
47+
return runGeth(t, append(allArgs, args...)...)
48+
}
49+
3850
// Tests that a node embedded within a console can be started up properly and
3951
// then terminated by closing the input stream.
4052
func TestConsoleWelcome(t *testing.T) {
4153
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
4254

4355
// Start a geth console, make sure it's cleaned up and terminate the console
44-
geth := runGeth(t,
45-
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
46-
"--etherbase", coinbase,
47-
"console")
56+
geth := runMinimalGeth(t, "--etherbase", coinbase, "console")
4857

4958
// Gather all the infos the welcome message needs to contain
5059
geth.SetTemplateFunc("goos", func() string { return runtime.GOOS })
@@ -73,62 +82,42 @@ To exit, press ctrl-d
7382
}
7483

7584
// Tests that a console can be attached to a running node via various means.
76-
func TestIPCAttachWelcome(t *testing.T) {
85+
func TestAttachWelcome(t *testing.T) {
86+
var (
87+
ipc string
88+
httpPort string
89+
wsPort string
90+
)
7791
// Configure the instance for IPC attachment
78-
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
79-
var ipc string
8092
if runtime.GOOS == "windows" {
8193
ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
8294
} else {
8395
ws := tmpdir(t)
8496
defer os.RemoveAll(ws)
8597
ipc = filepath.Join(ws, "geth.ipc")
8698
}
87-
geth := runGeth(t,
88-
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
89-
"--etherbase", coinbase, "--ipcpath", ipc)
90-
91-
defer func() {
92-
geth.Interrupt()
93-
geth.ExpectExit()
94-
}()
95-
96-
waitForEndpoint(t, ipc, 3*time.Second)
97-
testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
98-
99-
}
100-
101-
func TestHTTPAttachWelcome(t *testing.T) {
102-
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
103-
port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
104-
geth := runGeth(t,
105-
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
106-
"--etherbase", coinbase, "--http", "--http.port", port)
107-
defer func() {
108-
geth.Interrupt()
109-
geth.ExpectExit()
110-
}()
111-
112-
endpoint := "http://127.0.0.1:" + port
113-
waitForEndpoint(t, endpoint, 3*time.Second)
114-
testAttachWelcome(t, geth, endpoint, httpAPIs)
115-
}
116-
117-
func TestWSAttachWelcome(t *testing.T) {
118-
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
119-
port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
120-
121-
geth := runGeth(t,
122-
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
123-
"--etherbase", coinbase, "--ws", "--ws.port", port)
124-
defer func() {
125-
geth.Interrupt()
126-
geth.ExpectExit()
127-
}()
128-
129-
endpoint := "ws://127.0.0.1:" + port
130-
waitForEndpoint(t, endpoint, 3*time.Second)
131-
testAttachWelcome(t, geth, endpoint, httpAPIs)
99+
// And HTTP + WS attachment
100+
p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P
101+
httpPort = strconv.Itoa(p)
102+
wsPort = strconv.Itoa(p + 1)
103+
geth := runMinimalGeth(t, "--etherbase", "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182",
104+
"--ipcpath", ipc,
105+
"--http", "--http.port", httpPort,
106+
"--ws", "--ws.port", wsPort)
107+
t.Run("ipc", func(t *testing.T) {
108+
waitForEndpoint(t, ipc, 3*time.Second)
109+
testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
110+
})
111+
t.Run("http", func(t *testing.T) {
112+
endpoint := "http://127.0.0.1:" + httpPort
113+
waitForEndpoint(t, endpoint, 3*time.Second)
114+
testAttachWelcome(t, geth, endpoint, httpAPIs)
115+
})
116+
t.Run("ws", func(t *testing.T) {
117+
endpoint := "ws://127.0.0.1:" + wsPort
118+
waitForEndpoint(t, endpoint, 3*time.Second)
119+
testAttachWelcome(t, geth, endpoint, httpAPIs)
120+
})
132121
}
133122

134123
func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {

cmd/geth/dao_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBloc
115115
if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil {
116116
t.Fatalf("test %d: failed to write genesis file: %v", test, err)
117117
}
118-
runGeth(t, "--datadir", datadir, "init", json).WaitExit()
118+
runGeth(t, "--datadir", datadir, "--nousb", "--networkid", "1337", "init", json).WaitExit()
119119
} else {
120120
// Force chain initialization
121-
args := []string{"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--datadir", datadir}
121+
args := []string{"--port", "0", "--nousb", "--networkid", "1337", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--datadir", datadir}
122122
runGeth(t, append(args, []string{"--exec", "2+2", "console"}...)...).WaitExit()
123123
}
124124
// Retrieve the DAO config flag from the database

cmd/geth/genesis_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestCustomGenesis(t *testing.T) {
8484
runGeth(t, "--nousb", "--datadir", datadir, "init", json).WaitExit()
8585

8686
// Query the custom genesis block
87-
geth := runGeth(t, "--nousb",
87+
geth := runGeth(t, "--nousb", "--networkid", "1337", "--syncmode=full",
8888
"--datadir", datadir, "--maxpeers", "0", "--port", "0",
8989
"--nodiscover", "--nat", "none", "--ipcdisable",
9090
"--exec", tt.query, "console")

cmd/geth/les_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func initGeth(t *testing.T) string {
159159
func startLightServer(t *testing.T) *gethrpc {
160160
datadir := initGeth(t)
161161
t.Logf("Importing keys to geth")
162-
runGeth(t, "--nousb", "--datadir", datadir, "--password", "./testdata/password.txt", "account", "import", "./testdata/key.prv").WaitExit()
162+
runGeth(t, "--nousb", "--datadir", datadir, "--password", "./testdata/password.txt", "account", "import", "./testdata/key.prv", "--lightkdf").WaitExit()
163163
account := "0x02f0d131f1f97aef08aec6e3291b957d9efe7105"
164164
server := startGethWithIpc(t, "lightserver", "--allow-insecure-unlock", "--datadir", datadir, "--password", "./testdata/password.txt", "--unlock", account, "--mine", "--light.serve=100", "--light.maxpeers=1", "--nodiscover", "--nat=extip:127.0.0.1", "--verbosity=4")
165165
return server

0 commit comments

Comments
 (0)