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

refactor: move test files and add version command tests #158

Merged
merged 4 commits into from
Oct 30, 2023
Merged
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
66 changes: 33 additions & 33 deletions pkg/commands/scan/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,77 +20,77 @@ func Test_Execute(t *testing.T) {
out string
}{{
name: "foo-bar",
payload: "../../../test/foo-bar/payload.yaml",
policies: []string{"../../../test/foo-bar/policy.yaml"},
out: "../../../test/foo-bar/out.txt",
payload: "../../../test/commands/scan/foo-bar/payload.yaml",
policies: []string{"../../../test/commands/scan/foo-bar/policy.yaml"},
out: "../../../test/commands/scan/foo-bar/out.txt",
wantErr: false,
}, {
name: "wildcard",
payload: "../../../test/wildcard/payload.json",
policies: []string{"../../../test/wildcard/policy.yaml"},
payload: "../../../test/commands/scan/wildcard/payload.json",
policies: []string{"../../../test/commands/scan/wildcard/policy.yaml"},
identifier: "name",
out: "../../../test/wildcard/out.txt",
out: "../../../test/commands/scan/wildcard/out.txt",
wantErr: false,
}, {
name: "pod-no-latest",
payload: "../../../test/pod-no-latest/payload.yaml",
policies: []string{"../../../test/pod-no-latest/policy.yaml"},
payload: "../../../test/commands/scan/pod-no-latest/payload.yaml",
policies: []string{"../../../test/commands/scan/pod-no-latest/policy.yaml"},
identifier: "metadata.name",
out: "../../../test/pod-no-latest/out.txt",
out: "../../../test/commands/scan/pod-no-latest/out.txt",
wantErr: false,
}, {
name: "pod-all-latest",
payload: "../../../test/pod-all-latest/payload.yaml",
policies: []string{"../../../test/pod-all-latest/policy.yaml"},
payload: "../../../test/commands/scan/pod-all-latest/payload.yaml",
policies: []string{"../../../test/commands/scan/pod-all-latest/policy.yaml"},
identifier: "metadata.name",
out: "../../../test/pod-all-latest/out.txt",
out: "../../../test/commands/scan/pod-all-latest/out.txt",
wantErr: false,
}, {
name: "scripted",
payload: "../../../test/scripted/payload.yaml",
policies: []string{"../../../test/scripted/policy.yaml"},
out: "../../../test/scripted/out.txt",
payload: "../../../test/commands/scan/scripted/payload.yaml",
policies: []string{"../../../test/commands/scan/scripted/policy.yaml"},
out: "../../../test/commands/scan/scripted/out.txt",
wantErr: false,
}, {
name: "payload-yaml",
payload: "../../../test/payload-yaml/payload.yaml",
payload: "../../../test/commands/scan/payload-yaml/payload.yaml",
preprocessors: []string{"planned_values.root_module.resources"},
policies: []string{"../../../test/payload-yaml/policy.yaml"},
policies: []string{"../../../test/commands/scan/payload-yaml/policy.yaml"},
identifier: "address",
out: "../../../test/payload-yaml/out.txt",
out: "../../../test/commands/scan/payload-yaml/out.txt",
wantErr: false,
}, {
name: "tf-plan",
payload: "../../../test/tf-plan/payload.json",
payload: "../../../test/commands/scan/tf-plan/payload.json",
preprocessors: []string{"planned_values.root_module.resources"},
policies: []string{"../../../test/tf-plan/policy.yaml"},
policies: []string{"../../../test/commands/scan/tf-plan/policy.yaml"},
identifier: "address",
out: "../../../test/tf-plan/out.txt",
out: "../../../test/commands/scan/tf-plan/out.txt",
wantErr: false,
}, {
name: "escaped",
payload: "../../../test/escaped/payload.yaml",
policies: []string{"../../../test/escaped/policy.yaml"},
out: "../../../test/escaped/out.txt",
payload: "../../../test/commands/scan/escaped/payload.yaml",
policies: []string{"../../../test/commands/scan/escaped/policy.yaml"},
out: "../../../test/commands/scan/escaped/out.txt",
wantErr: false,
}, {
name: "dockerfile",
payload: "../../../test/dockerfile/payload.json",
policies: []string{"../../../test/dockerfile/policy.yaml"},
out: "../../../test/dockerfile/out.txt",
payload: "../../../test/commands/scan/dockerfile/payload.json",
policies: []string{"../../../test/commands/scan/dockerfile/policy.yaml"},
out: "../../../test/commands/scan/dockerfile/out.txt",
wantErr: false,
}, {
name: "tf-s3",
payload: "../../../test/tf-s3/payload.json",
policies: []string{"../../../test/tf-s3/policy.yaml"},
out: "../../../test/tf-s3/out.txt",
payload: "../../../test/commands/scan/tf-s3/payload.json",
policies: []string{"../../../test/commands/scan/tf-s3/policy.yaml"},
out: "../../../test/commands/scan/tf-s3/out.txt",
wantErr: false,
}, {
name: "tf-ec2",
payload: "../../../test/tf-ec2/payload.json",
payload: "../../../test/commands/scan/tf-ec2/payload.json",
preprocessors: []string{"planned_values.root_module.resources"},
policies: []string{"../../../test/tf-ec2/policy.yaml"},
out: "../../../test/tf-ec2/out.txt",
policies: []string{"../../../test/commands/scan/tf-ec2/policy.yaml"},
out: "../../../test/commands/scan/tf-ec2/out.txt",
wantErr: false,
}}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func Command(parents ...string) *cobra.Command {
doc := command.New(
command.WithParents(parents...),
command.WithDescription("Prints the version informations."),
command.WithDescription("Print the version informations"),
command.WithExample("Print version infos", "version"),
)
return &cobra.Command{
Expand Down
107 changes: 49 additions & 58 deletions pkg/commands/version/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,58 @@ package version
import (
"bytes"
"io"
"strings"
"os"
"testing"

"github.com/kyverno/kyverno-json/pkg/version"
"github.com/stretchr/testify/assert"
)

func TestCommand(t *testing.T) {
version.BuildVersion = "test"
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetOut(b)
err := cmd.Execute()
assert.NoError(t, err)
out, err := io.ReadAll(b)
assert.NoError(t, err)
expected := `
Version: test
Time: ---
Git commit ID: ---`
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
}

func TestCommandWithInvalidArg(t *testing.T) {
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetErr(b)
cmd.SetArgs([]string{"foo"})
err := cmd.Execute()
assert.Error(t, err)
out, err := io.ReadAll(b)
assert.NoError(t, err)
expected := `Error: unknown command "foo" for "version"`
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
}

func TestCommandWithInvalidFlag(t *testing.T) {
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetErr(b)
cmd.SetArgs([]string{"--xxx"})
err := cmd.Execute()
assert.Error(t, err)
out, err := io.ReadAll(b)
assert.NoError(t, err)
expected := `Error: unknown flag: --xxx`
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
}

func TestCommandHelp(t *testing.T) {
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"--help"})
err := cmd.Execute()
assert.NoError(t, err)
out, err := io.ReadAll(b)
assert.NoError(t, err)
assert.True(t, strings.HasPrefix(string(out), cmd.Long))
func Test_Execute(t *testing.T) {
tests := []struct {
name string
args []string
wantErr bool
out string
}{{
name: "help",
args: []string{
"--help",
},
out: "../../../test/commands/version/help.txt",
wantErr: false,
}, {
name: "version",
out: "../../../test/commands/version/out.txt",
wantErr: false,
}, {
name: "unknow flag",
args: []string{"--foo"},
wantErr: true,
}, {
name: "unknow arg",
args: []string{"foo"},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := Command()
assert.NotNil(t, cmd)
cmd.SetArgs(tt.args)
out := bytes.NewBufferString("")
cmd.SetOut(out)
err := cmd.Execute()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
actual, err := io.ReadAll(out)
assert.NoError(t, err)
if tt.out != "" {
expected, err := os.ReadFile(tt.out)
assert.NoError(t, err)
assert.Equal(t, string(expected), string(actual))
}
})
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions test/commands/version/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Print the version informations

Usage:
version [flags]

Examples:
# Print version infos
version


Flags:
-h, --help help for version
3 changes: 3 additions & 0 deletions test/commands/version/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version:
Time: ---
Git commit ID: ---
2 changes: 1 addition & 1 deletion website/docs/cli/commands/kyverno-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ kyverno-json [flags]
* [kyverno-json playground](kyverno-json_playground.md) - playground
* [kyverno-json scan](kyverno-json_scan.md) - scan
* [kyverno-json serve](kyverno-json_serve.md) - serve
* [kyverno-json version](kyverno-json_version.md) - Prints the version informations.
* [kyverno-json version](kyverno-json_version.md) - Print the version informations

4 changes: 2 additions & 2 deletions website/docs/cli/commands/kyverno-json_version.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kyverno-json version

Prints the version informations.
Print the version informations

### Synopsis

Prints the version informations.
Print the version informations


```
Expand Down