Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8c59200

Browse files
committedOct 6, 2015
WIP: sharness iptb test
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 1169398 commit 8c59200

File tree

2 files changed

+180
-14
lines changed

2 files changed

+180
-14
lines changed
 

‎test/dependencies/iptb/main.go

+92-14
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
"syscall"
1717
"time"
1818

19-
kingpin "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/alecthomas/kingpin"
19+
kingpin "github.com/alecthomas/kingpin"
2020
serial "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
2121

22-
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
23-
manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
22+
manet "github.com/jbenet/go-multiaddr-net"
23+
ma "github.com/jbenet/go-multiaddr-net/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
2424
)
2525

2626
// GetNumNodes returns the number of testbed nodes configured in the testbed directory
@@ -73,13 +73,19 @@ type initCfg struct {
7373
Bootstrap string
7474
PortStart int
7575
Mdns bool
76+
Utp bool
7677
}
7778

7879
func (c *initCfg) swarmAddrForPeer(i int) string {
80+
str := "/ip4/0.0.0.0/tcp/%d"
81+
if c.Utp {
82+
str = "/ip4/0.0.0.0/udp/%d/utp"
83+
}
84+
7985
if c.PortStart == 0 {
80-
return "/ip4/0.0.0.0/tcp/0"
86+
return fmt.Sprintf(str, 0)
8187
}
82-
return fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", c.PortStart+i)
88+
return fmt.Sprintf(str, c.PortStart+i)
8389
}
8490

8591
func (c *initCfg) apiAddrForPeer(i int) string {
@@ -250,14 +256,28 @@ func IpfsKillAll() error {
250256
return nil
251257
}
252258

259+
func envForDaemon(n int) []string {
260+
envs := os.Environ()
261+
npath := "IPFS_PATH=" + IpfsDirN(n)
262+
for i, e := range envs {
263+
p := strings.Split(e, "=")
264+
if p[0] == "IPFS_PATH" {
265+
envs[i] = npath
266+
return envs
267+
}
268+
}
269+
270+
return append(envs, npath)
271+
}
272+
253273
func IpfsStart(waitall bool) error {
254274
var addrs []string
255275
n := GetNumNodes()
256276
for i := 0; i < n; i++ {
257277
dir := IpfsDirN(i)
258278
cmd := exec.Command("ipfs", "daemon")
259279
cmd.Dir = dir
260-
cmd.Env = append(os.Environ(), "IPFS_PATH="+dir)
280+
cmd.Env = envForDaemon(i)
261281

262282
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
263283

@@ -441,6 +461,11 @@ func IpfsShell(n int) error {
441461
}
442462

443463
func ConnectNodes(from, to int) error {
464+
if from == to {
465+
// skip connecting to self..
466+
return nil
467+
}
468+
fmt.Printf("connecting %d -> %d\n", from, to)
444469
cmd := exec.Command("ipfs", "id", "-f", "<addrs>")
445470
cmd.Env = []string{"IPFS_PATH=" + IpfsDirN(to)}
446471
out, err := cmd.Output()
@@ -449,7 +474,6 @@ func ConnectNodes(from, to int) error {
449474
return err
450475
}
451476
addr := strings.Split(string(out), "\n")[0]
452-
fmt.Println("ADDR: ", addr)
453477

454478
connectcmd := exec.Command("ipfs", "swarm", "connect", addr)
455479
connectcmd.Env = []string{"IPFS_PATH=" + IpfsDirN(from)}
@@ -461,6 +485,55 @@ func ConnectNodes(from, to int) error {
461485
return nil
462486
}
463487

488+
func parseRange(s string) ([]int, error) {
489+
if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
490+
ranges := strings.Split(s[1:len(s)-1], ",")
491+
var out []int
492+
for _, r := range ranges {
493+
rng, err := expandDashRange(r)
494+
if err != nil {
495+
return nil, err
496+
}
497+
498+
out = append(out, rng...)
499+
}
500+
return out, nil
501+
} else {
502+
i, err := strconv.Atoi(s)
503+
if err != nil {
504+
return nil, err
505+
}
506+
507+
return []int{i}, nil
508+
}
509+
}
510+
511+
func expandDashRange(s string) ([]int, error) {
512+
parts := strings.Split(s, "-")
513+
if len(parts) == 0 {
514+
i, err := strconv.Atoi(s)
515+
if err != nil {
516+
return nil, err
517+
}
518+
return []int{i}, nil
519+
}
520+
low, err := strconv.Atoi(parts[0])
521+
if err != nil {
522+
return nil, err
523+
}
524+
525+
hi, err := strconv.Atoi(parts[1])
526+
if err != nil {
527+
return nil, err
528+
}
529+
530+
var out []int
531+
for i := low; i <= hi; i++ {
532+
out = append(out, i)
533+
}
534+
return out, nil
535+
}
536+
464537
func GetAttr(attr string, node int) (string, error) {
465538
switch attr {
466539
case "id":
@@ -518,9 +591,10 @@ func main() {
518591
cfg := new(initCfg)
519592
kingpin.Flag("n", "number of ipfs nodes to initialize").Short('n').IntVar(&cfg.Count)
520593
kingpin.Flag("port", "port to start allocations from").Default("4002").Short('p').IntVar(&cfg.PortStart)
521-
kingpin.Flag("f", "force initialization (overwrite existing configs)").BoolVar(&cfg.Force)
594+
kingpin.Flag("force", "force initialization (overwrite existing configs)").Short('f').BoolVar(&cfg.Force)
522595
kingpin.Flag("mdns", "turn on mdns for nodes").BoolVar(&cfg.Mdns)
523596
kingpin.Flag("bootstrap", "select bootstrapping style for cluster").Default("star").StringVar(&cfg.Bootstrap)
597+
kingpin.Flag("utp", "use utp for addresses").BoolVar(&cfg.Utp)
524598

525599
wait := kingpin.Flag("wait", "wait for nodes to come fully online before exiting").Bool()
526600

@@ -576,22 +650,26 @@ func main() {
576650
os.Exit(1)
577651
}
578652

579-
from, err := strconv.Atoi(args[1])
653+
from, err := parseRange(args[1])
580654
if err != nil {
581655
fmt.Printf("failed to parse: %s\n", err)
582656
return
583657
}
584658

585-
to, err := strconv.Atoi(args[2])
659+
to, err := parseRange(args[2])
586660
if err != nil {
587661
fmt.Printf("failed to parse: %s\n", err)
588662
return
589663
}
590664

591-
err = ConnectNodes(from, to)
592-
if err != nil {
593-
fmt.Printf("failed to connect: %s\n", err)
594-
return
665+
for _, f := range from {
666+
for _, t := range to {
667+
err = ConnectNodes(f, t)
668+
if err != nil {
669+
fmt.Printf("failed to connect: %s\n", err)
670+
return
671+
}
672+
}
595673
}
596674

597675
case "get":

‎test/sharness/t0130-multinode.sh

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2015 Jeromy Johnson
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
#
6+
7+
test_description="Test multiple ipfs nodes"
8+
9+
. lib/test-lib.sh
10+
11+
export IPTB_ROOT="`pwd`/.iptb"
12+
13+
ipfsi() {
14+
dir="$1"
15+
shift
16+
IPFS_PATH="$IPTB_ROOT/$dir" ipfs $@
17+
}
18+
19+
check_has_connection() {
20+
node=$1
21+
ipfsi $node swarm peers | grep ipfs > /dev/null
22+
}
23+
24+
startup_cluster() {
25+
test_expect_success "start up nodes" '
26+
iptb start
27+
'
28+
29+
test_expect_success "connect nodes to eachother" '
30+
# todo: find out why it fails when you do "[1-4] 0"
31+
iptb connect [0-4] [0-4]
32+
'
33+
34+
test_expect_success "nodes are connected" '
35+
check_has_connection 0 &&
36+
check_has_connection 1 &&
37+
check_has_connection 2 &&
38+
check_has_connection 3 &&
39+
check_has_connection 4
40+
'
41+
}
42+
43+
check_file_fetch() {
44+
node=$1
45+
fhash=$2
46+
fname=$3
47+
48+
test_expect_success "can fetch file" '
49+
ipfsi $node cat $fhash > fetch_out
50+
'
51+
52+
test_expect_success "file looks good" '
53+
test_cmp $fname fetch_out
54+
'
55+
}
56+
57+
run_basic_test() {
58+
startup_cluster
59+
60+
test_expect_success "add a file on node1" '
61+
random 1000000 > filea &&
62+
FILEA_HASH=$(ipfsi 1 add -q filea)
63+
'
64+
65+
check_file_fetch 0 $FILEA_HASH filea
66+
check_file_fetch 1 $FILEA_HASH filea
67+
check_file_fetch 2 $FILEA_HASH filea
68+
check_file_fetch 3 $FILEA_HASH filea
69+
check_file_fetch 4 $FILEA_HASH filea
70+
71+
test_expect_success "shut down nodes" '
72+
iptb stop
73+
'
74+
}
75+
76+
test_expect_success "set up tcp testbed" '
77+
iptb init -n 5 -p 0 -f --bootstrap=none
78+
'
79+
80+
run_basic_test
81+
82+
test_expect_success "set up utp testbed" '
83+
iptb init -n 5 -p 0 -f --bootstrap=none --utp
84+
'
85+
86+
run_basic_test
87+
88+
test_done

0 commit comments

Comments
 (0)
Please sign in to comment.