Skip to content

Commit

Permalink
refactor: move test files
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Oct 29, 2023
1 parent a33ddb1 commit e4b0f2e
Show file tree
Hide file tree
Showing 42 changed files with 98 additions and 92 deletions.
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.
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: ---

0 comments on commit e4b0f2e

Please sign in to comment.