Skip to content

Commit da290e9

Browse files
acudfjl
authored andcommitted
cmd/swarm: speed up tests (#17878)
These minor changes already shaved off around 30s.
1 parent 0fe9a37 commit da290e9

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

cmd/swarm/access_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,16 @@ var DefaultCurve = crypto.S256()
5454
// is then fetched through 2nd node. since the tested code is not key-aware - we can just
5555
// fetch from the 2nd node using HTTP BasicAuth
5656
func TestAccessPassword(t *testing.T) {
57-
cluster := newTestCluster(t, 1)
58-
defer cluster.Shutdown()
59-
proxyNode := cluster.Nodes[0]
57+
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
58+
defer srv.Close()
6059

6160
dataFilename := testutil.TempFileWithContent(t, data)
6261
defer os.RemoveAll(dataFilename)
6362

6463
// upload the file with 'swarm up' and expect a hash
6564
up := runSwarm(t,
6665
"--bzzapi",
67-
proxyNode.URL, //it doesn't matter through which node we upload content
66+
srv.URL, //it doesn't matter through which node we upload content
6867
"up",
6968
"--encrypt",
7069
dataFilename)
@@ -138,7 +137,7 @@ func TestAccessPassword(t *testing.T) {
138137
if a.Publisher != "" {
139138
t.Fatal("should be empty")
140139
}
141-
client := swarm.NewClient(cluster.Nodes[0].URL)
140+
client := swarm.NewClient(srv.URL)
142141

143142
hash, err := client.UploadManifest(&m, false)
144143
if err != nil {
@@ -147,7 +146,7 @@ func TestAccessPassword(t *testing.T) {
147146

148147
httpClient := &http.Client{}
149148

150-
url := cluster.Nodes[0].URL + "/" + "bzz:/" + hash
149+
url := srv.URL + "/" + "bzz:/" + hash
151150
response, err := httpClient.Get(url)
152151
if err != nil {
153152
t.Fatal(err)
@@ -189,7 +188,7 @@ func TestAccessPassword(t *testing.T) {
189188
//download file with 'swarm down' with wrong password
190189
up = runSwarm(t,
191190
"--bzzapi",
192-
proxyNode.URL,
191+
srv.URL,
193192
"down",
194193
"bzz:/"+hash,
195194
tmp,

cmd/swarm/manifest_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/ethereum/go-ethereum/swarm/api"
2828
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
29+
"github.com/ethereum/go-ethereum/swarm/testutil"
2930
)
3031

3132
// TestManifestChange tests manifest add, update and remove
@@ -57,8 +58,8 @@ func TestManifestChangeEncrypted(t *testing.T) {
5758
// Argument encrypt controls whether to use encryption or not.
5859
func testManifestChange(t *testing.T, encrypt bool) {
5960
t.Parallel()
60-
cluster := newTestCluster(t, 1)
61-
defer cluster.Shutdown()
61+
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
62+
defer srv.Close()
6263

6364
tmp, err := ioutil.TempDir("", "swarm-manifest-test")
6465
if err != nil {
@@ -94,7 +95,7 @@ func testManifestChange(t *testing.T, encrypt bool) {
9495

9596
args := []string{
9697
"--bzzapi",
97-
cluster.Nodes[0].URL,
98+
srv.URL,
9899
"--recursive",
99100
"--defaultpath",
100101
indexDataFilename,
@@ -109,7 +110,7 @@ func testManifestChange(t *testing.T, encrypt bool) {
109110

110111
checkHashLength(t, origManifestHash, encrypt)
111112

112-
client := swarm.NewClient(cluster.Nodes[0].URL)
113+
client := swarm.NewClient(srv.URL)
113114

114115
// upload a new file and use its manifest to add it the original manifest.
115116
t.Run("add", func(t *testing.T) {
@@ -122,14 +123,14 @@ func testManifestChange(t *testing.T, encrypt bool) {
122123

123124
humansManifestHash := runSwarmExpectHash(t,
124125
"--bzzapi",
125-
cluster.Nodes[0].URL,
126+
srv.URL,
126127
"up",
127128
humansDataFilename,
128129
)
129130

130131
newManifestHash := runSwarmExpectHash(t,
131132
"--bzzapi",
132-
cluster.Nodes[0].URL,
133+
srv.URL,
133134
"manifest",
134135
"add",
135136
origManifestHash,
@@ -177,14 +178,14 @@ func testManifestChange(t *testing.T, encrypt bool) {
177178

178179
robotsManifestHash := runSwarmExpectHash(t,
179180
"--bzzapi",
180-
cluster.Nodes[0].URL,
181+
srv.URL,
181182
"up",
182183
robotsDataFilename,
183184
)
184185

185186
newManifestHash := runSwarmExpectHash(t,
186187
"--bzzapi",
187-
cluster.Nodes[0].URL,
188+
srv.URL,
188189
"manifest",
189190
"add",
190191
origManifestHash,
@@ -237,14 +238,14 @@ func testManifestChange(t *testing.T, encrypt bool) {
237238

238239
indexManifestHash := runSwarmExpectHash(t,
239240
"--bzzapi",
240-
cluster.Nodes[0].URL,
241+
srv.URL,
241242
"up",
242243
indexDataFilename,
243244
)
244245

245246
newManifestHash := runSwarmExpectHash(t,
246247
"--bzzapi",
247-
cluster.Nodes[0].URL,
248+
srv.URL,
248249
"manifest",
249250
"update",
250251
origManifestHash,
@@ -295,14 +296,14 @@ func testManifestChange(t *testing.T, encrypt bool) {
295296

296297
humansManifestHash := runSwarmExpectHash(t,
297298
"--bzzapi",
298-
cluster.Nodes[0].URL,
299+
srv.URL,
299300
"up",
300301
robotsDataFilename,
301302
)
302303

303304
newManifestHash := runSwarmExpectHash(t,
304305
"--bzzapi",
305-
cluster.Nodes[0].URL,
306+
srv.URL,
306307
"manifest",
307308
"update",
308309
origManifestHash,
@@ -348,7 +349,7 @@ func testManifestChange(t *testing.T, encrypt bool) {
348349
t.Run("remove", func(t *testing.T) {
349350
newManifestHash := runSwarmExpectHash(t,
350351
"--bzzapi",
351-
cluster.Nodes[0].URL,
352+
srv.URL,
352353
"manifest",
353354
"remove",
354355
origManifestHash,
@@ -376,7 +377,7 @@ func testManifestChange(t *testing.T, encrypt bool) {
376377
t.Run("remove nested", func(t *testing.T) {
377378
newManifestHash := runSwarmExpectHash(t,
378379
"--bzzapi",
379-
cluster.Nodes[0].URL,
380+
srv.URL,
380381
"manifest",
381382
"remove",
382383
origManifestHash,
@@ -429,8 +430,8 @@ func TestNestedDefaultEntryUpdateEncrypted(t *testing.T) {
429430

430431
func testNestedDefaultEntryUpdate(t *testing.T, encrypt bool) {
431432
t.Parallel()
432-
cluster := newTestCluster(t, 1)
433-
defer cluster.Shutdown()
433+
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
434+
defer srv.Close()
434435

435436
tmp, err := ioutil.TempDir("", "swarm-manifest-test")
436437
if err != nil {
@@ -458,7 +459,7 @@ func testNestedDefaultEntryUpdate(t *testing.T, encrypt bool) {
458459

459460
args := []string{
460461
"--bzzapi",
461-
cluster.Nodes[0].URL,
462+
srv.URL,
462463
"--recursive",
463464
"--defaultpath",
464465
indexDataFilename,
@@ -473,7 +474,7 @@ func testNestedDefaultEntryUpdate(t *testing.T, encrypt bool) {
473474

474475
checkHashLength(t, origManifestHash, encrypt)
475476

476-
client := swarm.NewClient(cluster.Nodes[0].URL)
477+
client := swarm.NewClient(srv.URL)
477478

478479
newIndexData := []byte("<h1>Ethereum Swarm</h1>")
479480
newIndexDataFilename := filepath.Join(tmp, "index.html")
@@ -484,14 +485,14 @@ func testNestedDefaultEntryUpdate(t *testing.T, encrypt bool) {
484485

485486
newIndexManifestHash := runSwarmExpectHash(t,
486487
"--bzzapi",
487-
cluster.Nodes[0].URL,
488+
srv.URL,
488489
"up",
489490
newIndexDataFilename,
490491
)
491492

492493
newManifestHash := runSwarmExpectHash(t,
493494
"--bzzapi",
494-
cluster.Nodes[0].URL,
495+
srv.URL,
495496
"manifest",
496497
"update",
497498
origManifestHash,

cmd/swarm/run_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ import (
4040
"github.com/ethereum/go-ethereum/p2p"
4141
"github.com/ethereum/go-ethereum/rpc"
4242
"github.com/ethereum/go-ethereum/swarm"
43+
"github.com/ethereum/go-ethereum/swarm/api"
44+
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
45+
"github.com/ethereum/go-ethereum/swarm/testutil"
4346
)
4447

4548
var loglevel = flag.Int("loglevel", 3, "verbosity of logs")
@@ -55,6 +58,9 @@ func init() {
5558
})
5659
}
5760

61+
func serverFunc(api *api.API) testutil.TestServer {
62+
return swarmhttp.NewServer(api, "")
63+
}
5864
func TestMain(m *testing.M) {
5965
// check if we have been reexec'd
6066
if reexec.Init() {

cmd/swarm/upload_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/ethereum/go-ethereum/log"
3434
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
35+
"github.com/ethereum/go-ethereum/swarm/testutil"
3536
"github.com/mattn/go-colorable"
3637
)
3738

@@ -298,8 +299,8 @@ func TestCLISwarmUpDefaultPath(t *testing.T) {
298299
}
299300

300301
func testCLISwarmUpDefaultPath(toEncrypt bool, absDefaultPath bool, t *testing.T) {
301-
cluster := newTestCluster(t, 1)
302-
defer cluster.Shutdown()
302+
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
303+
defer srv.Close()
303304

304305
tmp, err := ioutil.TempDir("", "swarm-defaultpath-test")
305306
if err != nil {
@@ -323,7 +324,7 @@ func testCLISwarmUpDefaultPath(toEncrypt bool, absDefaultPath bool, t *testing.T
323324

324325
args := []string{
325326
"--bzzapi",
326-
cluster.Nodes[0].URL,
327+
srv.URL,
327328
"--recursive",
328329
"--defaultpath",
329330
defaultPath,
@@ -340,7 +341,7 @@ func testCLISwarmUpDefaultPath(toEncrypt bool, absDefaultPath bool, t *testing.T
340341
up.ExpectExit()
341342
hash := matches[0]
342343

343-
client := swarm.NewClient(cluster.Nodes[0].URL)
344+
client := swarm.NewClient(srv.URL)
344345

345346
m, isEncrypted, err := client.DownloadManifest(hash)
346347
if err != nil {

0 commit comments

Comments
 (0)