Skip to content

Commit

Permalink
chore: Improve error message in case of plugin timeout (#472)
Browse files Browse the repository at this point in the history
When a plugin exceeds the specified timeout or deadline for content
processing, the current error message displayed is ```signal: killed```.
This PR updates the error message to a more informative message:
```[plugin_name] [command_name] command execution timeout: signal:
killed```

---------

Signed-off-by: Pritesh Bandi <priteshbandi@gmail.com>
  • Loading branch information
priteshbandi authored Nov 14, 2024
1 parent 8797d86 commit 7b9636e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ func (c execCommander) Output(ctx context.Context, name string, command plugin.C
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, stderr.Bytes(), fmt.Errorf("'%s %s' command execution timeout: %w", name, string(command), err);
}
return nil, stderr.Bytes(), err
}
return stdout.Bytes(), nil, nil
Expand Down
23 changes: 22 additions & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"errors"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"

"github.com/notaryproject/notation-go/plugin/proto"
)
Expand Down Expand Up @@ -181,7 +183,7 @@ func TestValidateMetadata(t *testing.T) {
}
}

func TestNewCLIPlugin_PathError(t *testing.T) {
func TestNewCLIPlugin_Error(t *testing.T) {
ctx := context.Background()
t.Run("plugin directory exists without executable.", func(t *testing.T) {
p, err := NewCLIPlugin(ctx, "emptyplugin", "./testdata/plugins/emptyplugin/notation-emptyplugin")
Expand All @@ -203,6 +205,25 @@ func TestNewCLIPlugin_PathError(t *testing.T) {
t.Errorf("NewCLIPlugin() plugin = %v, want nil", p)
}
})

t.Run("plugin timeout error", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on Windows")
}
expectedErrMsg := "'sleep 2' command execution timeout: signal: killed"
ctxWithTimout, cancel := context.WithTimeout(ctx, 10 * time.Millisecond)
defer cancel()

var twoSeconds proto.Command
twoSeconds = "2"
_, _, err := execCommander{}.Output(ctxWithTimout, "sleep", twoSeconds, nil);
if err == nil {
t.Errorf("execCommander{}.Output() expected error = %v, got nil", expectedErrMsg)
}
if err.Error() != expectedErrMsg {
t.Errorf("execCommander{}.Output() error = %v, want %v", err, expectedErrMsg)
}
})
}

func TestNewCLIPlugin_ValidError(t *testing.T) {
Expand Down

0 comments on commit 7b9636e

Please sign in to comment.