Remove error return from constructor.

This commit is contained in:
jregan
2019-11-02 14:26:20 -07:00
parent 690e01c2ba
commit 4c15c42447
12 changed files with 67 additions and 47 deletions

View File

@@ -41,7 +41,7 @@ func DefaultSrcRoot() (string, error) {
}
nope = append(nope, root)
root = pgmconfig.DefaultPluginConfig().AbsPluginHome
root = pgmconfig.DefaultAbsPluginHome()
if FileExists(root) {
return root, nil
}

View File

@@ -44,15 +44,19 @@ type ExecPlugin struct {
h *resmap.PluginHelpers
}
func NewExecPlugin(p string) (*ExecPlugin, error) {
f, err := os.Stat(p)
func NewExecPlugin(p string) *ExecPlugin {
return &ExecPlugin{path: p}
}
func (p *ExecPlugin) ErrIfNotExecutable() error {
f, err := os.Stat(p.path)
if err != nil {
return nil, err
return err
}
if f.Mode()&0111 == 0000 {
return nil, fmt.Errorf("unable to execute plugin on path: %s", p)
return fmt.Errorf("unexecutable plugin at: %s", p.path)
}
return &ExecPlugin{path: p}, nil
return nil
}
func (p *ExecPlugin) Path() string {

View File

@@ -5,7 +5,6 @@ package execplugin_test
import (
"fmt"
"os"
"strings"
"testing"
@@ -44,13 +43,15 @@ s/$BAR/bar/g
\ \ \
`))
p, err := NewExecPlugin(
p := NewExecPlugin(
loader.AbsolutePluginPath(
pgmconfig.DefaultPluginConfig(),
pgmconfig.DisabledPluginConfig(),
pluginConfig.OrgId()))
if err != nil {
t.Fatalf("unexpected error: %v", err.Error())
}
// Not checking to see if the plugin is executable,
// because this test does not run it.
// This tests only covers sending configuration
// to the plugin wrapper object and confirming
// that it's properly prepared for execution.
yaml, err := pluginConfig.AsYAML()
if err != nil {
@@ -58,7 +59,7 @@ s/$BAR/bar/g
}
p.Config(resmap.NewPluginHelpers(ldr, v, rf), yaml)
expected := "/kustomize/plugin/someteam.example.com/v1/sedtransformer/SedTransformer"
expected := "someteam.example.com/v1/sedtransformer/SedTransformer"
if !strings.HasSuffix(p.Path(), expected) {
t.Fatalf("expected suffix '%s', got '%s'", expected, p.Path())
}
@@ -118,9 +119,9 @@ func strptr(s string) *string {
}
func TestUpdateResourceOptions(t *testing.T) {
p, err := NewExecPlugin("")
if !os.IsNotExist(err) {
t.Fatalf("unexpected error: %v", err.Error())
p := NewExecPlugin("")
if err := p.ErrIfNotExecutable(); err == nil {
t.Fatalf("expected unexecutable error")
}
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
in := resmap.New()
@@ -166,9 +167,9 @@ func TestUpdateResourceOptions(t *testing.T) {
}
func TestUpdateResourceOptionsWithInvalidHashAnnotationValues(t *testing.T) {
p, err := NewExecPlugin("")
if !os.IsNotExist(err) {
t.Fatalf("unexpected error: %v", err.Error())
p := NewExecPlugin("")
if err := p.ErrIfNotExecutable(); err == nil {
t.Fatalf("expected unexecutable error")
}
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
cases := []string{

View File

@@ -146,13 +146,23 @@ func (l *Loader) makeBuiltinPlugin(r resid.Gvk) (resmap.Configurable, error) {
}
func (l *Loader) loadPlugin(resId resid.ResId) (resmap.Configurable, error) {
p, err := execplugin.NewExecPlugin(l.absolutePluginPath(resId))
// First try to load the plugin as an executable.
p := execplugin.NewExecPlugin(l.absolutePluginPath(resId))
err := p.ErrIfNotExecutable()
if err == nil {
return p, nil
}
if !os.IsNotExist(err) {
// The file exists, but something else is wrong,
// likely it's not executable.
// Assume the user forgot to set the exec bit,
// and return an error, rather than adding ".so"
// to the name and attempting to load it as a Go
// plugin, which will likely fail and result
// in an obscure message.
return nil, err
}
// Failing the above, try loading it as a Go plugin.
c, err := l.loadGoPlugin(resId)
if err != nil {
return nil, err
@@ -186,7 +196,7 @@ func (l *Loader) loadGoPlugin(id resid.ResId) (resmap.Configurable, error) {
}
c, ok := symbol.(resmap.Configurable)
if !ok {
return nil, fmt.Errorf("plugin %s not configurable", regId)
return nil, fmt.Errorf("plugin '%s' not configurable", regId)
}
registry[regId] = c
return copyPlugin(c), nil

View File

@@ -57,7 +57,7 @@ func TestLoader(t *testing.T) {
ldr := loadertest.NewFakeLoader("/foo")
pLdr := NewLoader(pgmconfig.ActivePluginConfig(), rmF)
pLdr := NewLoader(pgmconfig.EnabledPluginConfig(), rmF)
if pLdr == nil {
t.Fatal("expect non-nil loader")
}