mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-09 10:18:04 +08:00
Add unit test for rpc over unix socket
This commit is contained in:
parent
23ca62ad2c
commit
893a94864a
@ -349,6 +349,17 @@ func (n *Node) checkAPI(authorization string) bool {
|
||||
log.Debugf("node %d API addr not available yet: %s", n.ID, err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
if unixAddr, err := apiAddr.ValueForProtocol(multiaddr.P_UNIX); err == nil {
|
||||
parts := strings.SplitN(unixAddr, "/", 2)
|
||||
if len(parts) < 1 {
|
||||
panic("malformed unix socket address")
|
||||
}
|
||||
fileName := "/" + parts[1]
|
||||
_, err := os.Stat(fileName)
|
||||
return !errors.Is(err, fs.ErrNotExist)
|
||||
}
|
||||
|
||||
ip, err := apiAddr.ValueForProtocol(multiaddr.P_IP4)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
55
test/cli/rpc_unixsocket_test.go
Normal file
55
test/cli/rpc_unixsocket_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
//"net"
|
||||
//"net/http"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
rpcapi "github.com/ipfs/kubo/client/rpc"
|
||||
///"github.com/ipfs/kubo/client/rpc/auth"
|
||||
"github.com/ipfs/kubo/config"
|
||||
"github.com/ipfs/kubo/test/cli/harness"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
//manet "github.com/multiformats/go-multiaddr/net"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRPCUnixSocket(t *testing.T) {
|
||||
node := harness.NewT(t).NewNode().Init()
|
||||
|
||||
sockDir := node.Dir
|
||||
sockAddr := path.Join("/unix", sockDir, "sock")
|
||||
|
||||
node.UpdateConfig(func(cfg *config.Config) {
|
||||
//cfg.Addresses.API = append(cfg.Addresses.API, sockPath)
|
||||
cfg.Addresses.API = []string{sockAddr}
|
||||
})
|
||||
t.Log("Starting daemon with unix socket:", sockAddr)
|
||||
node.StartDaemon()
|
||||
|
||||
unixMaddr, err := multiaddr.NewMultiaddr(sockAddr)
|
||||
require.NoError(t, err)
|
||||
|
||||
apiClient, err := rpcapi.NewApi(unixMaddr)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ver struct {
|
||||
Version string
|
||||
}
|
||||
err = apiClient.Request("version").Exec(context.Background(), &ver)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, ver)
|
||||
t.Log("Got version:", ver.Version)
|
||||
|
||||
var res struct {
|
||||
ID string
|
||||
}
|
||||
err = apiClient.Request("id").Exec(context.Background(), &res)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, res)
|
||||
t.Log("Got ID:", res.ID)
|
||||
|
||||
node.StopDaemon()
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user