fix: clash between daemon --api flag and cli tests

This commit is contained in:
Dirk McCormick 2020-10-08 14:15:09 +02:00
parent 8f35a5c036
commit cb7d99fcc7
4 changed files with 47 additions and 25 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@ -47,14 +48,44 @@ func NewCliError(s string) error {
type ApiConnector func() api.FullNode type ApiConnector func() api.FullNode
type APIInfo struct { type APIInfo struct {
Addr multiaddr.Multiaddr Addr string
Token []byte Token []byte
} }
func (a APIInfo) DialArgs() (string, error) { func (a APIInfo) DialArgs() (string, error) {
_, addr, err := manet.DialArgs(a.Addr) ma, err := multiaddr.NewMultiaddr(a.Addr)
if err == nil {
_, addr, err := manet.DialArgs(ma)
if err != nil {
return "", err
}
return "ws://" + addr + "/rpc/v0", err return "ws://" + addr + "/rpc/v0", nil
}
_, err = url.Parse(a.Addr)
if err != nil {
return "", err
}
return a.Addr + "/rpc/v0", nil
}
func (a APIInfo) Host() (string, error) {
ma, err := multiaddr.NewMultiaddr(a.Addr)
if err == nil {
_, addr, err := manet.DialArgs(ma)
if err != nil {
return "", err
}
return addr, nil
}
spec, err := url.Parse(a.Addr)
if err != nil {
return "", err
}
return spec.Host, nil
} }
func (a APIInfo) AuthHeader() http.Header { func (a APIInfo) AuthHeader() http.Header {
@ -72,11 +103,11 @@ func (a APIInfo) AuthHeader() http.Header {
func flagForAPI(t repo.RepoType) string { func flagForAPI(t repo.RepoType) string {
switch t { switch t {
case repo.FullNode: case repo.FullNode:
return "api" return "api-url"
case repo.StorageMiner: case repo.StorageMiner:
return "miner-api" return "miner-api-url"
case repo.Worker: case repo.Worker:
return "worker-api" return "worker-api-url"
default: default:
panic(fmt.Sprintf("Unknown repo type: %v", t)) panic(fmt.Sprintf("Unknown repo type: %v", t))
} }
@ -130,11 +161,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
strma := ctx.String(apiFlag) strma := ctx.String(apiFlag)
strma = strings.TrimSpace(strma) strma = strings.TrimSpace(strma)
apima, err := multiaddr.NewMultiaddr(strma) return APIInfo{Addr: strma}, nil
if err != nil {
return APIInfo{}, err
}
return APIInfo{Addr: apima}, nil
} }
envKey := envForRepo(t) envKey := envForRepo(t)
@ -152,12 +179,8 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
if len(sp) != 2 { if len(sp) != 2 {
log.Warnf("invalid env(%s) value, missing token or address", envKey) log.Warnf("invalid env(%s) value, missing token or address", envKey)
} else { } else {
ma, err := multiaddr.NewMultiaddr(sp[1])
if err != nil {
return APIInfo{}, xerrors.Errorf("could not parse multiaddr from env(%s): %w", envKey, err)
}
return APIInfo{ return APIInfo{
Addr: ma, Addr: sp[1],
Token: []byte(sp[0]), Token: []byte(sp[0]),
}, nil }, nil
} }
@ -186,7 +209,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
} }
return APIInfo{ return APIInfo{
Addr: ma, Addr: ma.String(),
Token: token, Token: token,
}, nil }, nil
} }

View File

@ -439,12 +439,12 @@ type mockCLI struct {
} }
func newMockCLI(t *testing.T) *mockCLI { func newMockCLI(t *testing.T) *mockCLI {
// Create a CLI App with an --api flag so that we can specify which node // Create a CLI App with an --api-url flag so that we can specify which node
// the command should be executed against // the command should be executed against
app := cli.NewApp() app := cli.NewApp()
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "api", Name: "api-url",
Hidden: true, Hidden: true,
}, },
} }
@ -476,8 +476,8 @@ func (c *mockCLIClient) runCmd(cmd *cli.Command, input []string) string {
} }
func (c *mockCLIClient) runCmdRaw(cmd *cli.Command, input []string) (string, error) { func (c *mockCLIClient) runCmdRaw(cmd *cli.Command, input []string) (string, error) {
// prepend --api=<node api listener address> // prepend --api-url=<node api listener address>
apiFlag := "--api=" + c.addr.String() apiFlag := "--api-url=" + c.addr.String()
input = append([]string{apiFlag}, input...) input = append([]string{apiFlag}, input...)
fs := c.flagSet(cmd) fs := c.flagSet(cmd)
@ -493,7 +493,7 @@ func (c *mockCLIClient) runCmdRaw(cmd *cli.Command, input []string) (string, err
} }
func (c *mockCLIClient) flagSet(cmd *cli.Command) *flag.FlagSet { func (c *mockCLIClient) flagSet(cmd *cli.Command) *flag.FlagSet {
// Apply app level flags (so we can process --api flag) // Apply app level flags (so we can process --api-url flag)
fs := &flag.FlagSet{} fs := &flag.FlagSet{}
for _, f := range c.cctx.App.Flags { for _, f := range c.cctx.App.Flags {
err := f.Apply(fs) err := f.Apply(fs)

View File

@ -9,7 +9,6 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo"
manet "github.com/multiformats/go-multiaddr/net"
) )
var pprofCmd = &cli.Command{ var pprofCmd = &cli.Command{
@ -37,7 +36,7 @@ var PprofGoroutines = &cli.Command{
if err != nil { if err != nil {
return xerrors.Errorf("could not get API info: %w", err) return xerrors.Errorf("could not get API info: %w", err)
} }
_, addr, err := manet.DialArgs(ainfo.Addr) addr, err := ainfo.Host()
if err != nil { if err != nil {
return err return err
} }

View File

@ -111,7 +111,7 @@ var consensusCheckCmd = &cli.Command{
if err != nil { if err != nil {
return err return err
} }
ainfo := lcli.APIInfo{Addr: apima} ainfo := lcli.APIInfo{Addr: apima.String()}
addr, err := ainfo.DialArgs() addr, err := ainfo.DialArgs()
if err != nil { if err != nil {
return err return err