Skip to content

Commit

Permalink
fixes(knative#814) allow plugins to extend the 'source' command group
Browse files Browse the repository at this point in the history
* introduce a list of command group that can be extended (currently only source)
* check if plugin main group is in that list and execute, otherwise fail
* add e2e test for both plugin that is allowed and not allowed to extend existing group
  • Loading branch information
maximilien committed Apr 21, 2020
1 parent 3c3b029 commit 458ad7c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
| Check DeleteTimestamp before updating resource
| https://github.com/knative/client/pull/805[#805]

| 🎁
| Allow plugins to extend fixed list of command groups, currently `source`
| https://github.com/knative/client/pull/818[#818]

|===

## v0.13.2 (2020-04-15)
Expand Down
17 changes: 16 additions & 1 deletion pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ import (
"knative.dev/client/pkg/kn/flags"
)

// AllowedExtensibleCommandGroups the list of command groups that can be
// extended with plugins, e.g., a plugin named `kn-source-kafka` for Kafka
// event sources is allowed. This is defined as a fixed [...]string since
// cannot defined Golang []string constants
var AllowedExtensibleCommandGroups = [...]string{"source"}

// NewDefaultKnCommand creates the default `kn` command with a default plugin handler
func NewDefaultKnCommand() *cobra.Command {
rootCmd := NewKnCommand()
Expand Down Expand Up @@ -81,7 +87,7 @@ func NewDefaultKnCommandWithArgs(rootCmd *cobra.Command,
// only look for suitable extension executables if
// the specified command does not already exist
foundCmd, innerArgs, err := rootCmd.Find(cmdPathPieces)
if err != nil {
if err != nil || inAllowedExtensibleCommandGroups(foundCmd.Name()) {
err := plugin.HandlePluginCommand(pluginHandler, cmdPathPieces)
if err != nil {
fmt.Fprintf(rootCmd.OutOrStderr(), "Error: unknown command '%s' \nRun 'kn --help' for usage.\n", args[1])
Expand Down Expand Up @@ -373,3 +379,12 @@ func showSubcommands(cmd *cobra.Command, args []string, innerArg string) string
}
return fmt.Sprintf("Error: unknown subcommand '%s' for '%s'. Available subcommands: %s\nRun 'kn --help' for usage.\n", innerArg, getCommands(args, innerArg), strings.Join(strs, ", "))
}

func inAllowedExtensibleCommandGroups(name string) bool {
for _, groupName := range AllowedExtensibleCommandGroups {
if name == groupName {
return true
}
}
return false
}
37 changes: 34 additions & 3 deletions test/e2e/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ func TestExecutePluginInPath(t *testing.T) {
runPlugin(r, knFlags, "hello2e2e", []string{}, []string{"Hello Knative, I'm a Kn plugin"})
}

func TestExecutePluginExtendingCommandGroup(t *testing.T) {
it, err := test.NewKnTest()
assert.NilError(t, err)

r := test.NewKnRunResultCollector(t, it)
defer r.DumpIfFailed()

pc, oldPath := setupPluginTestConfigWithNewPath(t)
defer tearDownWithPath(pc, oldPath)

pc.knPluginPath, err = createPluginFile("kn-source-test", TestPluginCode, pc.knPluginsDir, FileModeExecutable)
assert.NilError(t, err)

t.Log("execute plugin in $PATH that 'shadows' an allowed command group")
knFlags := []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir), "--lookup-plugins=true"}
runPlugin(r, knFlags, "source", []string{"test"}, []string{"Hello Knative, I'm a Kn plugin"})

t.Log("failed to execute plugin in $PATH that 'shadows' a non-allowed command group")
knFlags = []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir), "--lookup-plugins=true"}
runFailedPlugin(r, knFlags, "service", []string{"test"}, []string{"Hello Knative, I'm a Kn plugin"})
}

// Private

func setupPluginTestConfigWithNewPath(t *testing.T) (pluginTestConfig, string) {
pc := pluginTestConfig{}
assert.NilError(t, pc.setup())
Expand All @@ -180,8 +204,6 @@ func tearDownWithPath(pc pluginTestConfig, oldPath string) {
pc.teardown()
}

// Private

func listPlugin(r *test.KnRunResultCollector, knFlags []string, expectedPlugins []string, unexpectedPlugins []string) {
knArgs := append(knFlags, "plugin", "list")

Expand All @@ -193,12 +215,21 @@ func listPlugin(r *test.KnRunResultCollector, knFlags []string, expectedPlugins

func runPlugin(r *test.KnRunResultCollector, knFlags []string, pluginName string, args []string, expectedOutput []string) {
knArgs := append([]string{}, knFlags...)
knArgs = append(knArgs, pluginName)
knArgs = append(knArgs, args...)
knArgs = append(knArgs, pluginName)

out := test.Kn{}.Run(knArgs...)
r.AssertNoError(out)
for _, output := range expectedOutput {
assert.Check(r.T(), util.ContainsAll(out.Stdout, output))
}
}

func runFailedPlugin(r *test.KnRunResultCollector, knFlags []string, pluginName string, args []string, expectedOutput []string) {
knArgs := append([]string{}, knFlags...)
knArgs = append(knArgs, args...)
knArgs = append(knArgs, pluginName)

out := test.Kn{}.Run(knArgs...)
r.AssertError(out)
}

0 comments on commit 458ad7c

Please sign in to comment.