aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/swarm/config_test.go
blob: 7ffe2cfb97a3f552a8af148b40ea5aa77379ebf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright 2017 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "os/exec"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/rpc"
    "github.com/ethereum/go-ethereum/swarm"
    "github.com/ethereum/go-ethereum/swarm/api"

    "github.com/docker/docker/pkg/reexec"
)

func TestDumpConfig(t *testing.T) {
    swarm := runSwarm(t, "dumpconfig")
    defaultConf := api.NewDefaultConfig()
    out, err := tomlSettings.Marshal(&defaultConf)
    if err != nil {
        t.Fatal(err)
    }
    swarm.Expect(string(out))
    swarm.ExpectExit()
}

func TestFailsSwapEnabledNoSwapApi(t *testing.T) {
    flags := []string{
        fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
        fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
        fmt.Sprintf("--%s", SwarmSwapEnabledFlag.Name),
    }

    swarm := runSwarm(t, flags...)
    swarm.Expect("Fatal: " + SWARM_ERR_SWAP_SET_NO_API + "\n")
    swarm.ExpectExit()
}

func TestFailsNoBzzAccount(t *testing.T) {
    flags := []string{
        fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
        fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
    }

    swarm := runSwarm(t, flags...)
    swarm.Expect("Fatal: " + SWARM_ERR_NO_BZZACCOUNT + "\n")
    swarm.ExpectExit()
}

func TestCmdLineOverrides(t *testing.T) {
    dir, err := ioutil.TempDir("", "bzztest")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)

    conf, account := getTestAccount(t, dir)
    node := &testNode{Dir: dir}

    // assign ports
    httpPort, err := assignTCPPort()
    if err != nil {
        t.Fatal(err)
    }

    flags := []string{
        fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
        fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
        fmt.Sprintf("--%s", SwarmSyncEnabledFlag.Name),
        fmt.Sprintf("--%s", CorsStringFlag.Name), "*",
        fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
        fmt.Sprintf("--%s", EnsAPIFlag.Name), "",
        "--datadir", dir,
        "--ipcpath", conf.IPCPath,
    }
    node.Cmd = runSwarm(t, flags...)
    node.Cmd.InputLine(testPassphrase)
    defer func() {
        if t.Failed() {
            node.Shutdown()
        }
    }()
    // wait for the node to start
    for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
        node.Client, err = rpc.Dial(conf.IPCEndpoint())
        if err == nil {
            break
        }
    }
    if node.Client == nil {
        t.Fatal(err)
    }

    // load info
    var info swarm.Info
    if err := node.Client.Call(&info, "bzz_info"); err != nil {
        t.Fatal(err)
    }

    if info.Port != httpPort {
        t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
    }

    if info.NetworkId != 42 {
        t.Fatalf("Expected network ID to be %d, got %d", 42, info.NetworkId)
    }

    if info.SyncEnabled != true {
        t.Fatal("Expected Sync to be enabled, but is false")
    }

    if info.Cors != "*" {
        t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
    }

    node.Shutdown()
}

func TestFileOverrides(t *testing.T) {

    // assign ports
    httpPort, err := assignTCPPort()
    if err != nil {
        t.Fatal(err)
    }

    //create a config file
    //first, create a default conf
    defaultConf := api.NewDefaultConfig()
    //change some values in order to test if they have been loaded
    defaultConf.SyncEnabled = true
    defaultConf.NetworkId = 54
    defaultConf.Port = httpPort
    defaultConf.StoreParams.DbCapacity = 9000000
    defaultConf.ChunkerParams.Branches = 64
    defaultConf.HiveParams.CallInterval = 6000000000
    defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
    defaultConf.SyncParams.KeyBufferSize = 512
    //create a TOML string
    out, err := tomlSettings.Marshal(&defaultConf)
    if err != nil {
        t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
    }
    //create file
    f, err := ioutil.TempFile("", "testconfig.toml")
    if err != nil {
        t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
    }
    //write file
    _, err = f.WriteString(string(out))
    if err != nil {
        t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
    }
    f.Sync()

    dir, err := ioutil.TempDir("", "bzztest")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)
    conf, account := getTestAccount(t, dir)
    node := &testNode{Dir: dir}

    flags := []string{
        fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(),
        fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
        "--ens-api", "",
        "--ipcpath", conf.IPCPath,
        "--datadir", dir,
    }
    node.Cmd = runSwarm(t, flags...)
    node.Cmd.InputLine(testPassphrase)
    defer func() {
        if t.Failed() {
            node.Shutdown()
        }
    }()
    // wait for the node to start
    for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
        node.Client, err = rpc.Dial(conf.IPCEndpoint())
        if err == nil {
            break
        }
    }
    if node.Client == nil {
        t.Fatal(err)
    }

    // load info
    var info swarm.Info
    if err := node.Client.Call(&info, "bzz_info"); err != nil {
        t.Fatal(err)
    }

    if info.Port != httpPort {
        t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
    }

    if info.NetworkId != 54 {
        t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId)
    }

    if info.SyncEnabled != true {
        t.Fatal("Expected Sync to be enabled, but is false")
    }

    if info.StoreParams.DbCapacity != 9000000 {
        t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId)
    }

    if info.ChunkerParams.Branches != 64 {
        t.Fatalf("Expected chunker params branches to be %d, got %d", 64, info.ChunkerParams.Branches)
    }

    if info.HiveParams.CallInterval != 6000000000 {
        t.Fatalf("Expected HiveParams CallInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.CallInterval))
    }

    if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
        t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
    }

    if info.SyncParams.KeyBufferSize != 512 {
        t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
    }

    node.Shutdown()
}

func TestEnvVars(t *testing.T) {
    // assign ports
    httpPort, err := assignTCPPort()
    if err != nil {
        t.Fatal(err)
    }

    envVars := os.Environ()
    envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmPortFlag.EnvVar, httpPort))
    envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmNetworkIdFlag.EnvVar, "999"))
    envVars = append(envVars, fmt.Sprintf("%s=%s", CorsStringFlag.EnvVar, "*"))
    envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmSyncEnabledFlag.EnvVar, "true"))

    dir, err := ioutil.TempDir("", "bzztest")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)
    conf, account := getTestAccount(t, dir)
    node := &testNode{Dir: dir}
    flags := []string{
        fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
        "--ens-api", "",
        "--datadir", dir,
        "--ipcpath", conf.IPCPath,
    }

    //node.Cmd = runSwarm(t,flags...)
    //node.Cmd.cmd.Env = envVars
    //the above assignment does not work, so we need a custom Cmd here in order to pass envVars:
    cmd := &exec.Cmd{
        Path:   reexec.Self(),
        Args:   append([]string{"swarm-test"}, flags...),
        Stderr: os.Stderr,
        Stdout: os.Stdout,
    }
    cmd.Env = envVars
    //stdout, err := cmd.StdoutPipe()
    //if err != nil {
    //  t.Fatal(err)
    //}
    //stdout = bufio.NewReader(stdout)
    var stdin io.WriteCloser
    if stdin, err = cmd.StdinPipe(); err != nil {
        t.Fatal(err)
    }
    if err := cmd.Start(); err != nil {
        t.Fatal(err)
    }

    //cmd.InputLine(testPassphrase)
    io.WriteString(stdin, testPassphrase+"\n")
    defer func() {
        if t.Failed() {
            node.Shutdown()
            cmd.Process.Kill()
        }
    }()
    // wait for the node to start
    for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
        node.Client, err = rpc.Dial(conf.IPCEndpoint())
        if err == nil {
            break
        }
    }

    if node.Client == nil {
        t.Fatal(err)
    }

    // load info
    var info swarm.Info
    if err := node.Client.Call(&info, "bzz_info"); err != nil {
        t.Fatal(err)
    }

    if info.Port != httpPort {
        t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
    }

    if info.NetworkId != 999 {
        t.Fatalf("Expected network ID to be %d, got %d", 999, info.NetworkId)
    }

    if info.Cors != "*" {
        t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
    }

    if info.SyncEnabled != true {
        t.Fatal("Expected Sync to be enabled, but is false")
    }

    node.Shutdown()
    cmd.Process.Kill()
}

func TestCmdLineOverridesFile(t *testing.T) {

    // assign ports
    httpPort, err := assignTCPPort()
    if err != nil {
        t.Fatal(err)
    }

    //create a config file
    //first, create a default conf
    defaultConf := api.NewDefaultConfig()
    //change some values in order to test if they have been loaded
    defaultConf.SyncEnabled = false
    defaultConf.NetworkId = 54
    defaultConf.Port = "8588"
    defaultConf.StoreParams.DbCapacity = 9000000
    defaultConf.ChunkerParams.Branches = 64
    defaultConf.HiveParams.CallInterval = 6000000000
    defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
    defaultConf.SyncParams.KeyBufferSize = 512
    //create a TOML file
    out, err := tomlSettings.Marshal(&defaultConf)
    if err != nil {
        t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
    }
    //write file
    f, err := ioutil.TempFile("", "testconfig.toml")
    if err != nil {
        t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
    }
    //write file
    _, err = f.WriteString(string(out))
    if err != nil {
        t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
    }
    f.Sync()

    dir, err := ioutil.TempDir("", "bzztest")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)
    conf, account := getTestAccount(t, dir)
    node := &testNode{Dir: dir}

    expectNetworkId := uint64(77)

    flags := []string{
        fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "77",
        fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
        fmt.Sprintf("--%s", SwarmSyncEnabledFlag.Name),
        fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(),
        fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
        "--ens-api", "",
        "--datadir", dir,
        "--ipcpath", conf.IPCPath,
    }
    node.Cmd = runSwarm(t, flags...)
    node.Cmd.InputLine(testPassphrase)
    defer func() {
        if t.Failed() {
            node.Shutdown()
        }
    }()
    // wait for the node to start
    for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
        node.Client, err = rpc.Dial(conf.IPCEndpoint())
        if err == nil {
            break
        }
    }
    if node.Client == nil {
        t.Fatal(err)
    }

    // load info
    var info swarm.Info
    if err := node.Client.Call(&info, "bzz_info"); err != nil {
        t.Fatal(err)
    }

    if info.Port != httpPort {
        t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
    }

    if info.NetworkId != expectNetworkId {
        t.Fatalf("Expected network ID to be %d, got %d", expectNetworkId, info.NetworkId)
    }

    if info.SyncEnabled != true {
        t.Fatal("Expected Sync to be enabled, but is false")
    }

    if info.StoreParams.DbCapacity != 9000000 {
        t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId)
    }

    if info.ChunkerParams.Branches != 64 {
        t.Fatalf("Expected chunker params branches to be %d, got %d", 64, info.ChunkerParams.Branches)
    }

    if info.HiveParams.CallInterval != 6000000000 {
        t.Fatalf("Expected HiveParams CallInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.CallInterval))
    }

    if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
        t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
    }

    if info.SyncParams.KeyBufferSize != 512 {
        t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
    }

    node.Shutdown()
}