This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathapi_test.go
110 lines (89 loc) · 2.32 KB
/
api_test.go
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
package httpapi
import (
"context"
"fmt"
"io/ioutil"
gohttp "net/http"
"os"
"path"
"strconv"
"testing"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/tests"
local "github.com/ipfs/iptb-plugins/local"
"github.com/ipfs/iptb/cli"
"github.com/ipfs/iptb/testbed"
"github.com/ipfs/iptb/testbed/interfaces"
)
type NodeProvider struct{}
func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]iface.CoreAPI, error) {
_, err := testbed.RegisterPlugin(testbed.IptbPlugin{
From: "<builtin>",
NewNode: local.NewNode,
GetAttrList: local.GetAttrList,
GetAttrDesc: local.GetAttrDesc,
PluginName: local.PluginName,
BuiltIn: true,
}, false)
if err != nil {
return nil, err
}
dir, err := ioutil.TempDir("", "httpapi-tb-")
if err != nil {
return nil, err
}
c := cli.NewCli()
initArgs := []string{"iptb", "--IPTB_ROOT", dir, "auto", "-type", "localipfs", "-count", strconv.FormatInt(int64(n), 10)}
if err := c.Run(initArgs); err != nil {
return nil, err
}
startArgs := []string{"iptb", "--IPTB_ROOT", dir, "start", "-wait", "--", "--offline=" + strconv.FormatBool(n == 1)}
if err := c.Run(startArgs); err != nil {
return nil, err
}
if n > 1 {
connectArgs := []string{"iptb", "--IPTB_ROOT", dir, "connect", fmt.Sprintf("[1-%d]", n - 1), "0"}
if err := c.Run(connectArgs); err != nil {
return nil, err
}
}
go func() {
<-ctx.Done()
defer os.Remove(dir)
defer func() {
_ = c.Run([]string{"iptb", "--IPTB_ROOT", dir, "stop"})
}()
}()
apis := make([]iface.CoreAPI, n)
for i := range apis {
tb := testbed.NewTestbed(path.Join(dir, "testbeds", "default"))
node, err := tb.Node(i)
if err != nil {
return nil, err
}
attrNode, ok := node.(testbedi.Attribute)
if !ok {
return nil, fmt.Errorf("node does not implement attributes")
}
pth, err := attrNode.Attr("path")
if err != nil {
return nil, err
}
a := ApiAddr(pth)
if a == nil {
return nil, fmt.Errorf("nil addr for node")
}
c := &gohttp.Client{
Transport: &gohttp.Transport{
Proxy: gohttp.ProxyFromEnvironment,
DisableKeepAlives: true,
DisableCompression: true,
},
}
apis[i] = NewApiWithClient(a, c)
}
return apis, nil
}
func TestHttpApi(t *testing.T) {
tests.TestApi(&NodeProvider{})(t)
}