|
| 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