Skip to content

Commit

Permalink
Merge branch 'master' into kn-plugins4
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilien committed Jul 10, 2019
2 parents a4c94ae + 9e29ce8 commit 0c6ebac
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 33 deletions.
62 changes: 62 additions & 0 deletions pkg/kn/commands/completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
"testing"

"github.com/spf13/cobra"
"gotest.tools/assert"
)

func TestCompletion(t *testing.T) {
var (
fakeRootCmd, completionCmd *cobra.Command
knParams *KnParams
)

setup := func() {
knParams = &KnParams{}
completionCmd = NewCompletionCommand(knParams)

fakeRootCmd = &cobra.Command{}
fakeRootCmd.AddCommand(completionCmd)
}

t.Run("creates a CompletionCommand", func(t *testing.T) {
setup()
assert.Equal(t, completionCmd.Use, "completion")
assert.Equal(t, completionCmd.Short, "Output shell completion code (default Bash)")
assert.Assert(t, completionCmd.RunE == nil)
})

t.Run("returns completion code for BASH", func(t *testing.T) {
setup()
CaptureStdout(t)
defer ReleaseStdout(t)

completionCmd.Run(fakeRootCmd, []string{})
assert.Assert(t, ReadStdout(t) != "")
})

t.Run("returns completion code for ZSH", func(t *testing.T) {
setup()
CaptureStdout(t)
defer ReleaseStdout(t)

completionCmd.Run(fakeRootCmd, []string{"--zsh"})
assert.Assert(t, ReadStdout(t) != "")
})
}
10 changes: 5 additions & 5 deletions pkg/kn/commands/plugin/path_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
verifier *CommandOverrideVerifier
)

var setup = func() {
var setup = func(t *testing.T) {
knParams := &commands.KnParams{}
rootCmd, _, _ = commands.CreateTestKnCommand(NewPluginCommand(knParams), knParams)
verifier = &CommandOverrideVerifier{
Expand All @@ -47,7 +47,7 @@ var cleanup = func(t *testing.T) {

func TestCommandOverrideVerifier_Verify_with_nil_root_command(t *testing.T) {
t.Run("returns error verifying path", func(t *testing.T) {
setup()
setup(t)
defer cleanup(t)
verifier.Root = nil

Expand All @@ -60,7 +60,7 @@ func TestCommandOverrideVerifier_Verify_with_nil_root_command(t *testing.T) {

func TestCommandOverrideVerifier_Verify_with_root_command(t *testing.T) {
t.Run("when plugin in path not executable", func(t *testing.T) {
setup()
setup(t)
defer cleanup(t)
pluginPath = CreateTestPlugin(t, KnTestPluginName, KnTestPluginScript, FileModeReadable)

Expand All @@ -74,7 +74,7 @@ func TestCommandOverrideVerifier_Verify_with_root_command(t *testing.T) {
})

t.Run("when kn plugin in path is executable", func(t *testing.T) {
setup()
setup(t)
defer cleanup(t)
pluginPath = CreateTestPlugin(t, KnTestPluginName, KnTestPluginScript, FileModeExecutable)

Expand All @@ -94,7 +94,7 @@ func TestCommandOverrideVerifier_Verify_with_root_command(t *testing.T) {
})

t.Run("when kn plugin in path overwrites existing command", func(t *testing.T) {
setup()
setup(t)
defer cleanup(t)
var overwritingPluginPath = CreateTestPlugin(t, "kn-plugin", KnTestPluginScript, FileModeExecutable)
defer DeleteTestPlugin(t, overwritingPluginPath)
Expand Down
126 changes: 126 additions & 0 deletions pkg/kn/commands/plugin/plugin_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plugin

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"gotest.tools/assert"
)

func TestPluginHandler(t *testing.T) {
var (
pluginHandler PluginHandler
pluginPath, pluginName, tmpPathDir string
err error
)

setup := func(t *testing.T) {
pluginHandler = &DefaultPluginHandler{
ValidPrefixes: []string{"kn"},
}
assert.Assert(t, pluginHandler != nil)

tmpPathDir, err = ioutil.TempDir("", "plugin_list")
assert.Assert(t, err == nil)
}

cleanup := func(t *testing.T) {
err = os.RemoveAll(tmpPathDir)
assert.Assert(t, err == nil)
}

beforeEach := func(t *testing.T) {
pluginName = "fake-plugin-name"
pluginPath = CreateTestPluginInPath(t, "kn-"+pluginName, KnTestPluginScript, FileModeExecutable, tmpPathDir)
assert.Assert(t, pluginPath != "")

err = os.Setenv("PATH", tmpPathDir)
assert.Assert(t, err == nil)
}

t.Run("#NewDefaultPluginHandler", func(t *testing.T) {
setup(t)
defer cleanup(t)

pHandler := NewDefaultPluginHandler([]string{"kn"})
assert.Assert(t, pHandler != nil)
})

t.Run("#Lookup", func(t *testing.T) {
t.Run("returns the first filepath matching prefix", func(t *testing.T) {
setup(t)
defer cleanup(t)
beforeEach(t)

path, exists := pluginHandler.Lookup(pluginName)
assert.Assert(t, path != "", fmt.Sprintf("no path when Lookup(%s)", pluginName))
assert.Assert(t, exists == true, fmt.Sprintf("could not Lookup(%s)", pluginName))
})

t.Run("returns empty filepath when no matching prefix found", func(t *testing.T) {
setup(t)
defer cleanup(t)

path, exists := pluginHandler.Lookup("bogus-plugin-name")
assert.Assert(t, path == "", fmt.Sprintf("unexpected plugin: kn-bogus-plugin-name"))
assert.Assert(t, exists == false, fmt.Sprintf("unexpected plugin: kn-bogus-plugin-name"))
})
})

t.Run("#Execute", func(t *testing.T) {
t.Run("success while executing the executable with args and environment", func(t *testing.T) {
setup(t)
defer cleanup(t)
beforeEach(t)

err = pluginHandler.Execute(pluginPath, []string{pluginPath}, os.Environ())
assert.Assert(t, err == nil, fmt.Sprintf("test plugin %s failed executing", pluginName))
})

t.Run("fails while executing the executable with args and environment", func(t *testing.T) {
setup(t)
defer cleanup(t)
beforeEach(t)

bogusPath := filepath.Join(filepath.Dir(pluginPath), "kn-bogus-plugin-name")
err = pluginHandler.Execute(bogusPath, []string{bogusPath}, os.Environ())
assert.Assert(t, err != nil, fmt.Sprintf("bogus plugin in path %s unexpectedly executed OK", bogusPath))
})
})

t.Run("HandlePluginCommand", func(t *testing.T) {
t.Run("sucess handling", func(t *testing.T) {
setup(t)
defer cleanup(t)
beforeEach(t)

err = HandlePluginCommand(pluginHandler, []string{pluginPath, pluginName})
assert.Assert(t, err == nil, fmt.Sprintf("test plugin %s failed executing", pluginName))
})

t.Run("fails handling", func(t *testing.T) {
setup(t)
defer cleanup(t)

err = HandlePluginCommand(pluginHandler, []string{pluginPath, "kn-bogus-plugin-name"})
assert.Assert(t, err != nil, fmt.Sprintf("test plugin %s expected to fail executing", pluginName))
})
})
}
2 changes: 1 addition & 1 deletion pkg/kn/commands/plugin/plugin_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
echo "I am a test Kn plugin"
`
FileModeReadable = 0644
FileModeExecutable = 777
FileModeExecutable = 0777
)

// FindSubCommand return the sub-command by name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"flag"
"io"
"os"
"strings"
"testing"

"github.com/knative/client/pkg/serving/v1alpha1"
Expand Down Expand Up @@ -55,16 +54,6 @@ func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command
return knCommand, fakeServing, buf
}

// TestContains is a test helper function, checking if a substring is present in given
// output string
func TestContains(t *testing.T, output string, sub []string, element string) {
for _, each := range sub {
if !strings.Contains(output, each) {
t.Errorf("Missing %s: %s", element, each)
}
}
}

// CaptureStdout collects the current content of os.Stdout
func CaptureStdout(t *testing.T) {
oldStdout = os.Stdout
Expand Down
103 changes: 103 additions & 0 deletions pkg/kn/commands/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
"bytes"
"testing"
"text/template"

"github.com/spf13/cobra"
"gotest.tools/assert"
)

type versionOutput struct {
Version string
BuildDate string
GitRevision string
ServingVersion string
}

var versionOutputTemplate = `Version: {{.Version}}
Build Date: {{.BuildDate}}
Git Revision: {{.GitRevision}}
Dependencies:
- serving: {{.ServingVersion}}
`

const (
fakeVersion = "fake-version"
fakeBuildDate = "fake-build-date"
fakeGitRevision = "fake-git-revision"
fakeServingVersion = "fake-serving-version"
)

func TestVersion(t *testing.T) {
var (
versionCmd *cobra.Command
knParams *KnParams
expectedVersionOutput string
)

setup := func() {
Version = fakeVersion
BuildDate = fakeBuildDate
GitRevision = fakeGitRevision
ServingVersion = fakeServingVersion

expectedVersionOutput = genVersionOuput(t, versionOutputTemplate,
versionOutput{
Version: fakeVersion,
BuildDate: fakeBuildDate,
GitRevision: fakeGitRevision,
ServingVersion: fakeServingVersion})

knParams = &KnParams{}
versionCmd = NewVersionCommand(knParams)
}

t.Run("creates a VersionCommand", func(t *testing.T) {
setup()
CaptureStdout(t)
defer ReleaseStdout(t)

assert.Equal(t, versionCmd.Use, "version")
assert.Equal(t, versionCmd.Short, "Prints the client version")
assert.Assert(t, versionCmd.RunE != nil)
})

t.Run("prints version, build date, git revision, and serving version string", func(t *testing.T) {
setup()
CaptureStdout(t)
defer ReleaseStdout(t)

err := versionCmd.RunE(nil, []string{})
assert.Assert(t, err == nil)
assert.Equal(t, ReadStdout(t), expectedVersionOutput)
})
}

// Private

func genVersionOuput(t *testing.T, templ string, vOutput versionOutput) string {
tmpl, err := template.New("versionOutput").Parse(versionOutputTemplate)
assert.Assert(t, err == nil)

buf := bytes.Buffer{}
err = tmpl.Execute(&buf, vOutput)
assert.Assert(t, err == nil)

return buf.String()
}
Loading

0 comments on commit 0c6ebac

Please sign in to comment.