Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit dfbe002

Browse files
committed
Skeleton for tests
1 parent a23d794 commit dfbe002

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

api.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ func NewLocalApi() iface.CoreAPI {
3535
baseDir = DefaultPathRoot
3636
}
3737

38-
baseDir, err := homedir.Expand(baseDir)
38+
return NewPathApi(baseDir)
39+
}
40+
41+
func NewPathApi(p string) iface.CoreAPI {
42+
baseDir, err := homedir.Expand(p)
3943
if err != nil {
4044
return nil
4145
}
@@ -51,21 +55,31 @@ func NewLocalApi() iface.CoreAPI {
5155
return nil
5256
}
5357

54-
return NewApi(strings.TrimSpace(string(api)))
58+
maddr, err := ma.NewMultiaddr(strings.TrimSpace(string(api)))
59+
if err != nil {
60+
return nil
61+
}
62+
63+
return NewApi(maddr)
5564
}
5665

57-
func NewApi(url string) *HttpApi {
66+
func NewApi(a ma.Multiaddr) *HttpApi { // TODO: should be MAddr?
5867
c := &gohttp.Client{
5968
Transport: &gohttp.Transport{
6069
Proxy: gohttp.ProxyFromEnvironment,
6170
DisableKeepAlives: true,
6271
},
6372
}
6473

65-
return NewApiWithClient(url, c)
74+
return NewApiWithClient(a, c)
6675
}
6776

68-
func NewApiWithClient(url string, c *gohttp.Client) *HttpApi {
77+
func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) *HttpApi {
78+
_, url, err := manet.DialArgs(a)
79+
if err != nil {
80+
return nil // TODO: return that error
81+
}
82+
6983
if a, err := ma.NewMultiaddr(url); err == nil {
7084
_, host, err := manet.DialArgs(a)
7185
if err == nil {

api_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package httpapi
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/ipfs/iptb/testbed/interfaces"
7+
"io/ioutil"
8+
"os"
9+
"path"
10+
"strconv"
11+
"testing"
12+
13+
"github.com/ipfs/go-ipfs/core/coreapi/interface"
14+
"github.com/ipfs/go-ipfs/core/coreapi/interface/tests"
15+
16+
local "github.com/ipfs/iptb-plugins/local"
17+
"github.com/ipfs/iptb/cli"
18+
"github.com/ipfs/iptb/testbed"
19+
)
20+
21+
type NodeProvider struct{}
22+
23+
func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]iface.CoreAPI, error) {
24+
_, err := testbed.RegisterPlugin(testbed.IptbPlugin{
25+
From: "<builtin>",
26+
NewNode: local.NewNode,
27+
GetAttrList: local.GetAttrList,
28+
GetAttrDesc: local.GetAttrDesc,
29+
PluginName: local.PluginName,
30+
BuiltIn: true,
31+
}, false)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
dir, err := ioutil.TempDir("", "httpapi-tb-")
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
c := cli.NewCli()
42+
if err := c.Run([]string{"iptb", "--IPTB_ROOT", dir, "auto", "-type", "localipfs", "-count", strconv.FormatInt(int64(n), 10), "--start"}); err != nil {
43+
return nil, err
44+
}
45+
46+
go func() {
47+
<-ctx.Done()
48+
49+
defer os.Remove(dir)
50+
51+
defer func() {
52+
_ = c.Run([]string{"iptb", "--IPTB_ROOT", dir, "stop"})
53+
}()
54+
}()
55+
56+
apis := make([]iface.CoreAPI, n)
57+
58+
for i := range apis {
59+
tb := testbed.NewTestbed(path.Join(dir, "testbeds", "default"))
60+
61+
node, err := tb.Node(i)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
attrNode, ok := node.(testbedi.Attribute)
67+
if !ok {
68+
return nil, fmt.Errorf("node does not implement attributes")
69+
}
70+
71+
pth, err := attrNode.Attr("path")
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
apis[i] = NewPathApi(pth)
77+
}
78+
79+
return apis, nil
80+
}
81+
82+
func TestHttpApi(t *testing.T) {
83+
tests.TestApi(&NodeProvider{})(t)
84+
}

0 commit comments

Comments
 (0)