Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: plugin error message #1217

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion cmd/notation/plugin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import (
"errors"
"fmt"
"io/fs"
"os"
"runtime"
"syscall"
"text/tabwriter"

"github.com/notaryproject/notation-go/dir"
Expand Down Expand Up @@ -68,7 +71,27 @@
}
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%v\t%v\t\n",
n, metaData.Description, metaData.Version, metaData.Capabilities, err)
n, metaData.Description, metaData.Version, metaData.Capabilities, userFriendlyError(err))
}
return tw.Flush()
}

// userFriendlyError optimizes the error message for the user.
func userFriendlyError(err error) error {
if err == nil {
return nil
}
var pathError *fs.PathError
if errors.As(err, &pathError) {
// for plugin does not exist
if errors.Is(pathError, fs.ErrNotExist) {
return fmt.Errorf("%w. Each plugin executable must be located under $PLUGIN_DIRECTORY/{plugin-name} directory, with executable named as notation-{plugin-name}", pathError)
}

// for plugin is not executable
if pathError.Op == "fork/exec" && pathError.Err == syscall.Errno(8) {
return fmt.Errorf("%w. Please ensure that the plugin executable file is compatible with %s/%s", pathError, runtime.GOOS, runtime.GOARCH)
}
}
return err

Check warning on line 96 in cmd/notation/plugin/list.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/plugin/list.go#L96

Added line #L96 was not covered by tests
}
40 changes: 40 additions & 0 deletions test/e2e/suite/plugin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package plugin

import (
"os"
"runtime"

. "github.com/notaryproject/notation/test/e2e/internal/notation"
"github.com/notaryproject/notation/test/e2e/internal/utils"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -37,4 +40,41 @@ var _ = Describe("notation plugin list", func() {
MatchKeyWords("ERROR", "<nil>")
})
})

It("missing plugin binary", func() {
Host(nil, func(notation *utils.ExecOpts, _ *Artifact, vhost *utils.VirtualHost) {
// create azure-kv plugin directory
pluginDir := vhost.AbsolutePath(NotationDirName, "plugins", "azure-kv")
if err := os.MkdirAll(pluginDir, os.ModePerm); err != nil {
Fail(err.Error())
}

notation.Exec("plugin", "list").
MatchKeyWords("azure-kv").
MatchKeyWords("no such file or directory")
})
})

It("with invalid binary file", func() {
Host(nil, func(notation *utils.ExecOpts, _ *Artifact, vhost *utils.VirtualHost) {
// create azure-kv plugin directory
pluginDir := vhost.AbsolutePath(NotationDirName, "plugins", "azure-kv")
if err := os.MkdirAll(pluginDir, os.ModePerm); err != nil {
Fail(err.Error())
}

// create invalid plugin binary
invalidPluginBinary := vhost.AbsolutePath(NotationDirName, "plugins", "azure-kv", "notation-azure-kv")
if runtime.GOOS == "windows" {
invalidPluginBinary += ".exe"
}
if err := os.WriteFile(invalidPluginBinary, []byte("invalid"), 0755); err != nil {
Fail(err.Error())
}

notation.Exec("plugin", "list").
MatchKeyWords("azure-kv").
MatchKeyWords("exec format error")
})
})
})
Loading