Skip to content

Commit

Permalink
Pass APIOption to Execute
Browse files Browse the repository at this point in the history
Also:
- redirect stdout/stderr to buffer, so Execute's client can read the
  plugin output
- unwrap rpc status error
- add more tests
- add ChainInfo.Home (will be used for consumer plugin)
- update .gitignore in plugin template to ignore also *.ign files
  • Loading branch information
tbruyelle committed Jan 23, 2024
1 parent 8a8b7de commit 8e8cddc
Show file tree
Hide file tree
Showing 17 changed files with 639 additions and 42 deletions.
10 changes: 10 additions & 0 deletions ignite/services/plugin/client_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// ErrAppChainNotFound indicates that the plugin command is not running inside a blockchain app.
var ErrAppChainNotFound = errors.New("blockchain app not found")

//go:generate mockery --srcpkg . --name Chainer --structname ChainerInterface --filename chainer.go --with-expecter
type Chainer interface {
// AppPath returns the configured App's path.
AppPath() string
Expand All @@ -21,6 +22,9 @@ type Chainer interface {

// RPCPublicAddress returns the configured App's rpc endpoint.
RPCPublicAddress() (string, error)

// Home returns the App's home dir.
Home() (string, error)
}

// APIOption defines options for the client API.
Expand Down Expand Up @@ -66,11 +70,17 @@ func (api clientAPI) GetChainInfo(context.Context) (*ChainInfo, error) {
return nil, err
}

home, err := chain.Home()
if err != nil {
return nil, err
}

return &ChainInfo{
ChainId: chainID,
AppPath: chain.AppPath(),
ConfigPath: chain.ConfigPath(),
RpcAddress: rpc,
Home: home,
}, nil
}

Expand Down
28 changes: 18 additions & 10 deletions ignite/services/plugin/grpc/v1/client_api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions ignite/services/plugin/grpc/v1/interface.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions ignite/services/plugin/grpc/v1/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion ignite/services/plugin/grpc/v1/service_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 29 additions & 5 deletions ignite/services/plugin/internal/execute.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
package plugininternal

import (
"bytes"
"context"
"errors"
"time"

"google.golang.org/grpc/status"

pluginsconfig "github.com/ignite/cli/v28/ignite/config/plugins"
"github.com/ignite/cli/v28/ignite/services/plugin"
)

// Execute starts and executes a plugin, then shutdowns it.
func Execute(ctx context.Context, path string, args ...string) error {
plugins, err := plugin.Load(ctx, []pluginsconfig.Plugin{{Path: path}})
func Execute(ctx context.Context, path string, args []string, options ...plugin.APIOption) (string, error) {
var buf bytes.Buffer
plugins, err := plugin.Load(
ctx,
[]pluginsconfig.Plugin{{Path: path}},
plugin.RedirectStdout(&buf),
)
if err != nil {
return err
return "", err
}
defer plugins[0].KillClient()
if plugins[0].Error != nil {
return plugins[0].Error
return "", plugins[0].Error
}
err = plugins[0].Interface.Execute(
ctx,
&plugin.ExecutedCommand{Args: args},
plugin.NewClientAPI(options...),
)
if err != nil {
// Extract the rpc status message and create a simple error from it.
// We don't want Execute to return rpc errors.
err = errors.New(status.Convert(err).Message())
}
return plugins[0].Interface.Execute(ctx, &plugin.ExecutedCommand{Args: args}, plugin.NewClientAPI())
// NOTE(tb): This pause gives enough time for go-plugin to sync the
// output from stdout/stderr of the plugin. Without that pause, this
// output can be discarded and absent from buf.
time.Sleep(100 * time.Millisecond)
return buf.String(), err
}
53 changes: 38 additions & 15 deletions ignite/services/plugin/internal/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,69 @@ package plugininternal

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/ignite/cli/v28/ignite/services/plugin"
"github.com/ignite/cli/v28/ignite/services/plugin/mocks"
)

func TestPluginExecute(t *testing.T) {
tests := []struct {
name string
scaffoldPlugin func(t *testing.T) string
expectedError string
name string
pluginPath string
expectedOut string
expectedError string
}{
{
name: "fail: plugin doesnt exist",
scaffoldPlugin: func(t *testing.T) string {
return "/not/exists"
},
name: "fail: plugin doesnt exist",
pluginPath: "/not/exists",
expectedError: "local app path \"/not/exists\" not found: stat /not/exists: no such file or directory",
},
{
name: "ok: plugin exists",
scaffoldPlugin: func(t *testing.T) string {
path, err := plugin.Scaffold(context.Background(), t.TempDir(), "foo", false)
require.NoError(t, err)
return path
},
name: "ok: plugin execute ok ",
pluginPath: "testdata/execute_ok",
expectedOut: "ok args=[arg1 arg2] chaininfo=chain_id:\"id\" app_path:\"apppath\" config_path:\"configpath\" rpc_address:\"rpcPublicAddress\" 5:\"home\"\n",
},
{
name: "ok: plugin execute fail ",
pluginPath: "testdata/execute_fail",
expectedError: "fail",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.scaffoldPlugin(t)
pluginPath := tt.pluginPath
if !strings.HasPrefix(pluginPath, "/") {
// add working dir to relative paths
wd, err := os.Getwd()
require.NoError(t, err)
pluginPath = filepath.Join(wd, pluginPath)
}
chainer := mocks.NewChainerInterface(t)
chainer.EXPECT().ID().Return("id", nil).Maybe()
chainer.EXPECT().AppPath().Return("apppath").Maybe()
chainer.EXPECT().ConfigPath().Return("configpath").Maybe()
chainer.EXPECT().Home().Return("home", nil).Maybe()
chainer.EXPECT().RPCPublicAddress().Return("rpcPublicAddress", nil).Maybe()

err := Execute(context.Background(), path, "args")
out, err := Execute(
context.Background(),
pluginPath,
[]string{"arg1", "arg2"},
plugin.WithChain(chainer),
)

if tt.expectedError != "" {
require.EqualError(t, err, tt.expectedError)
return
}
require.NoError(t, err)
require.Equal(t, tt.expectedOut, out)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
execute_fail*
81 changes: 81 additions & 0 deletions ignite/services/plugin/internal/testdata/execute_fail/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module execute_fail

go 1.21.1

toolchain go1.21.5

require (
github.com/hashicorp/go-plugin v1.5.2
github.com/ignite/cli/v28 v28.0.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/charmbracelet/lipgloss v0.6.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.10.0 // indirect
github.com/gobuffalo/flect v0.3.0 // indirect
github.com/gobuffalo/genny/v2 v2.1.0 // indirect
github.com/gobuffalo/github_flavored_markdown v1.1.4 // indirect
github.com/gobuffalo/helpers v0.6.7 // indirect
github.com/gobuffalo/logger v1.0.7 // indirect
github.com/gobuffalo/packd v1.0.2 // indirect
github.com/gobuffalo/plush/v4 v4.1.19 // indirect
github.com/gobuffalo/tags/v3 v3.1.4 // indirect
github.com/gobuffalo/validate/v3 v3.3.3 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.14.0 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d // indirect
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 8e8cddc

Please sign in to comment.