@cryptotaxi247 / kubo / commits / fe4c9fd80

Skeleton for tests

This commit was moved from ipfs/go-ipfs-http-client@dfbe0026ad9438d4e795bbb43debeffb6bb9ca1a

Łukasz Magiera committed Dec 30, 2018 at 04:24 UTC fe4c9fd8033c94c5ea707b09e76479a6d018635d
2 files changed +103 -5
client/httpapi/api.go
+19 -5
@@ -35,7 +35,11 @@ func NewLocalApi() iface.CoreAPI {
35 baseDir = DefaultPathRoot
36 }
37
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)
43 if err != nil {
44 return nil
45 }
@@ -51,10 +55,15 @@ func NewLocalApi() iface.CoreAPI {
55 return nil
56 }
57
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)
64 }
65
57 -func NewApi(url string) *HttpApi {
66 +func NewApi(a ma.Multiaddr) *HttpApi { // TODO: should be MAddr?
67 c := &gohttp.Client{
68 Transport: &gohttp.Transport{
69 Proxy: gohttp.ProxyFromEnvironment,
@@ -62,10 +71,15 @@ func NewApi(url string) *HttpApi {
71 },
72 }
73
65 - return NewApiWithClient(url, c)
74 + return NewApiWithClient(a, c)
75 }
76
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 +
83 if a, err := ma.NewMultiaddr(url); err == nil {
84 _, host, err := manet.DialArgs(a)
85 if err == nil {
client/httpapi/api_test.go new
+84
@@ -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 +}