Add unit test for rpc over unix socket
gammazero committed
Aug 19, 2024 at 19:00 UTC
893a94864a280653beb8698ca7b0bc7a1f917da8
2 files changed
+66
test/cli/harness/node.go
+11
@@ -349,6 +349,17 @@ func (n *Node) checkAPI(authorization string) bool {
349
log.Debugf("node %d API addr not available yet: %s", n.ID, err.Error())
350
return false
351
}
352
+
353
+ if unixAddr, err := apiAddr.ValueForProtocol(multiaddr.P_UNIX); err == nil {
354
+ parts := strings.SplitN(unixAddr, "/", 2)
355
+ if len(parts) < 1 {
356
+ panic("malformed unix socket address")
357
+ }
358
+ fileName := "/" + parts[1]
359
+ _, err := os.Stat(fileName)
360
+ return !errors.Is(err, fs.ErrNotExist)
361
+ }
362
+
363
ip, err := apiAddr.ValueForProtocol(multiaddr.P_IP4)
364
if err != nil {
365
panic(err)
test/cli/rpc_unixsocket_test.go
new
+55
@@ -0,0 +1,55 @@
1
+package cli
2
+
3
+import (
4
+ "context"
5
+ //"net"
6
+ //"net/http"
7
+ "path"
8
+ "testing"
9
+
10
+ rpcapi "github.com/ipfs/kubo/client/rpc"
11
+ ///"github.com/ipfs/kubo/client/rpc/auth"
12
+ "github.com/ipfs/kubo/config"
13
+ "github.com/ipfs/kubo/test/cli/harness"
14
+ "github.com/multiformats/go-multiaddr"
15
+ //manet "github.com/multiformats/go-multiaddr/net"
16
+ "github.com/stretchr/testify/require"
17
+)
18
+
19
+func TestRPCUnixSocket(t *testing.T) {
20
+ node := harness.NewT(t).NewNode().Init()
21
+
22
+ sockDir := node.Dir
23
+ sockAddr := path.Join("/unix", sockDir, "sock")
24
+
25
+ node.UpdateConfig(func(cfg *config.Config) {
26
+ //cfg.Addresses.API = append(cfg.Addresses.API, sockPath)
27
+ cfg.Addresses.API = []string{sockAddr}
28
+ })
29
+ t.Log("Starting daemon with unix socket:", sockAddr)
30
+ node.StartDaemon()
31
+
32
+ unixMaddr, err := multiaddr.NewMultiaddr(sockAddr)
33
+ require.NoError(t, err)
34
+
35
+ apiClient, err := rpcapi.NewApi(unixMaddr)
36
+ require.NoError(t, err)
37
+
38
+ var ver struct {
39
+ Version string
40
+ }
41
+ err = apiClient.Request("version").Exec(context.Background(), &ver)
42
+ require.NoError(t, err)
43
+ require.NotEmpty(t, ver)
44
+ t.Log("Got version:", ver.Version)
45
+
46
+ var res struct {
47
+ ID string
48
+ }
49
+ err = apiClient.Request("id").Exec(context.Background(), &res)
50
+ require.NoError(t, err)
51
+ require.NotEmpty(t, res)
52
+ t.Log("Got ID:", res.ID)
53
+
54
+ node.StopDaemon()
55
+}