From e4b0f2e0b7fb8c7781a4a8c9c23bc552f52593e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Sun, 29 Oct 2023 23:56:03 +0100 Subject: [PATCH] refactor: move test files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/commands/scan/command_test.go | 66 +++++------ pkg/commands/version/command.go | 2 +- pkg/commands/version/command_test.go | 107 ++++++++---------- .../{ => commands/scan}/dockerfile/Dockerfile | 0 test/{ => commands/scan}/dockerfile/README.md | 0 test/{ => commands/scan}/dockerfile/out.txt | 0 .../scan}/dockerfile/payload.json | 0 .../scan}/dockerfile/policy.yaml | 0 test/{ => commands/scan}/escaped/out.txt | 0 test/{ => commands/scan}/escaped/payload.yaml | 0 test/{ => commands/scan}/escaped/policy.yaml | 0 test/{ => commands/scan}/foo-bar/out.txt | 0 test/{ => commands/scan}/foo-bar/payload.yaml | 0 test/{ => commands/scan}/foo-bar/policy.yaml | 0 test/{ => commands/scan}/payload-yaml/out.txt | 0 .../scan}/payload-yaml/payload.yaml | 0 .../scan}/payload-yaml/policy.yaml | 0 .../scan}/pod-all-latest/out.txt | 0 .../scan}/pod-all-latest/payload.yaml | 0 .../scan}/pod-all-latest/policy.yaml | 0 .../{ => commands/scan}/pod-no-latest/out.txt | 0 .../scan}/pod-no-latest/payload.yaml | 0 .../scan}/pod-no-latest/policy.yaml | 0 test/{ => commands/scan}/scripted/out.txt | 0 .../{ => commands/scan}/scripted/payload.yaml | 0 test/{ => commands/scan}/scripted/policy.yaml | 0 test/{ => commands/scan}/tf-ec2/ec2.tf | 0 test/{ => commands/scan}/tf-ec2/out.txt | 0 test/{ => commands/scan}/tf-ec2/payload.json | 0 test/{ => commands/scan}/tf-ec2/policy.yaml | 0 test/{ => commands/scan}/tf-plan/out.txt | 0 test/{ => commands/scan}/tf-plan/payload.json | 0 test/{ => commands/scan}/tf-plan/policy.yaml | 0 test/{ => commands/scan}/tf-s3/bucket.tf | 0 test/{ => commands/scan}/tf-s3/out.txt | 0 test/{ => commands/scan}/tf-s3/payload.json | 0 test/{ => commands/scan}/tf-s3/policy.yaml | 0 test/{ => commands/scan}/wildcard/out.txt | 0 .../{ => commands/scan}/wildcard/payload.json | 0 test/{ => commands/scan}/wildcard/policy.yaml | 0 test/commands/version/help.txt | 12 ++ test/commands/version/out.txt | 3 + 42 files changed, 98 insertions(+), 92 deletions(-) rename test/{ => commands/scan}/dockerfile/Dockerfile (100%) rename test/{ => commands/scan}/dockerfile/README.md (100%) rename test/{ => commands/scan}/dockerfile/out.txt (100%) rename test/{ => commands/scan}/dockerfile/payload.json (100%) rename test/{ => commands/scan}/dockerfile/policy.yaml (100%) rename test/{ => commands/scan}/escaped/out.txt (100%) rename test/{ => commands/scan}/escaped/payload.yaml (100%) rename test/{ => commands/scan}/escaped/policy.yaml (100%) rename test/{ => commands/scan}/foo-bar/out.txt (100%) rename test/{ => commands/scan}/foo-bar/payload.yaml (100%) rename test/{ => commands/scan}/foo-bar/policy.yaml (100%) rename test/{ => commands/scan}/payload-yaml/out.txt (100%) rename test/{ => commands/scan}/payload-yaml/payload.yaml (100%) rename test/{ => commands/scan}/payload-yaml/policy.yaml (100%) rename test/{ => commands/scan}/pod-all-latest/out.txt (100%) rename test/{ => commands/scan}/pod-all-latest/payload.yaml (100%) rename test/{ => commands/scan}/pod-all-latest/policy.yaml (100%) rename test/{ => commands/scan}/pod-no-latest/out.txt (100%) rename test/{ => commands/scan}/pod-no-latest/payload.yaml (100%) rename test/{ => commands/scan}/pod-no-latest/policy.yaml (100%) rename test/{ => commands/scan}/scripted/out.txt (100%) rename test/{ => commands/scan}/scripted/payload.yaml (100%) rename test/{ => commands/scan}/scripted/policy.yaml (100%) rename test/{ => commands/scan}/tf-ec2/ec2.tf (100%) rename test/{ => commands/scan}/tf-ec2/out.txt (100%) rename test/{ => commands/scan}/tf-ec2/payload.json (100%) rename test/{ => commands/scan}/tf-ec2/policy.yaml (100%) rename test/{ => commands/scan}/tf-plan/out.txt (100%) rename test/{ => commands/scan}/tf-plan/payload.json (100%) rename test/{ => commands/scan}/tf-plan/policy.yaml (100%) rename test/{ => commands/scan}/tf-s3/bucket.tf (100%) rename test/{ => commands/scan}/tf-s3/out.txt (100%) rename test/{ => commands/scan}/tf-s3/payload.json (100%) rename test/{ => commands/scan}/tf-s3/policy.yaml (100%) rename test/{ => commands/scan}/wildcard/out.txt (100%) rename test/{ => commands/scan}/wildcard/payload.json (100%) rename test/{ => commands/scan}/wildcard/policy.yaml (100%) create mode 100644 test/commands/version/help.txt create mode 100644 test/commands/version/out.txt diff --git a/pkg/commands/scan/command_test.go b/pkg/commands/scan/command_test.go index 02c9997b..9617def4 100644 --- a/pkg/commands/scan/command_test.go +++ b/pkg/commands/scan/command_test.go @@ -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 { diff --git a/pkg/commands/version/command.go b/pkg/commands/version/command.go index 3234b634..d5a4f519 100644 --- a/pkg/commands/version/command.go +++ b/pkg/commands/version/command.go @@ -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{ diff --git a/pkg/commands/version/command_test.go b/pkg/commands/version/command_test.go index 9a8cd255..45692205 100644 --- a/pkg/commands/version/command_test.go +++ b/pkg/commands/version/command_test.go @@ -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)) + } + }) + } } diff --git a/test/dockerfile/Dockerfile b/test/commands/scan/dockerfile/Dockerfile similarity index 100% rename from test/dockerfile/Dockerfile rename to test/commands/scan/dockerfile/Dockerfile diff --git a/test/dockerfile/README.md b/test/commands/scan/dockerfile/README.md similarity index 100% rename from test/dockerfile/README.md rename to test/commands/scan/dockerfile/README.md diff --git a/test/dockerfile/out.txt b/test/commands/scan/dockerfile/out.txt similarity index 100% rename from test/dockerfile/out.txt rename to test/commands/scan/dockerfile/out.txt diff --git a/test/dockerfile/payload.json b/test/commands/scan/dockerfile/payload.json similarity index 100% rename from test/dockerfile/payload.json rename to test/commands/scan/dockerfile/payload.json diff --git a/test/dockerfile/policy.yaml b/test/commands/scan/dockerfile/policy.yaml similarity index 100% rename from test/dockerfile/policy.yaml rename to test/commands/scan/dockerfile/policy.yaml diff --git a/test/escaped/out.txt b/test/commands/scan/escaped/out.txt similarity index 100% rename from test/escaped/out.txt rename to test/commands/scan/escaped/out.txt diff --git a/test/escaped/payload.yaml b/test/commands/scan/escaped/payload.yaml similarity index 100% rename from test/escaped/payload.yaml rename to test/commands/scan/escaped/payload.yaml diff --git a/test/escaped/policy.yaml b/test/commands/scan/escaped/policy.yaml similarity index 100% rename from test/escaped/policy.yaml rename to test/commands/scan/escaped/policy.yaml diff --git a/test/foo-bar/out.txt b/test/commands/scan/foo-bar/out.txt similarity index 100% rename from test/foo-bar/out.txt rename to test/commands/scan/foo-bar/out.txt diff --git a/test/foo-bar/payload.yaml b/test/commands/scan/foo-bar/payload.yaml similarity index 100% rename from test/foo-bar/payload.yaml rename to test/commands/scan/foo-bar/payload.yaml diff --git a/test/foo-bar/policy.yaml b/test/commands/scan/foo-bar/policy.yaml similarity index 100% rename from test/foo-bar/policy.yaml rename to test/commands/scan/foo-bar/policy.yaml diff --git a/test/payload-yaml/out.txt b/test/commands/scan/payload-yaml/out.txt similarity index 100% rename from test/payload-yaml/out.txt rename to test/commands/scan/payload-yaml/out.txt diff --git a/test/payload-yaml/payload.yaml b/test/commands/scan/payload-yaml/payload.yaml similarity index 100% rename from test/payload-yaml/payload.yaml rename to test/commands/scan/payload-yaml/payload.yaml diff --git a/test/payload-yaml/policy.yaml b/test/commands/scan/payload-yaml/policy.yaml similarity index 100% rename from test/payload-yaml/policy.yaml rename to test/commands/scan/payload-yaml/policy.yaml diff --git a/test/pod-all-latest/out.txt b/test/commands/scan/pod-all-latest/out.txt similarity index 100% rename from test/pod-all-latest/out.txt rename to test/commands/scan/pod-all-latest/out.txt diff --git a/test/pod-all-latest/payload.yaml b/test/commands/scan/pod-all-latest/payload.yaml similarity index 100% rename from test/pod-all-latest/payload.yaml rename to test/commands/scan/pod-all-latest/payload.yaml diff --git a/test/pod-all-latest/policy.yaml b/test/commands/scan/pod-all-latest/policy.yaml similarity index 100% rename from test/pod-all-latest/policy.yaml rename to test/commands/scan/pod-all-latest/policy.yaml diff --git a/test/pod-no-latest/out.txt b/test/commands/scan/pod-no-latest/out.txt similarity index 100% rename from test/pod-no-latest/out.txt rename to test/commands/scan/pod-no-latest/out.txt diff --git a/test/pod-no-latest/payload.yaml b/test/commands/scan/pod-no-latest/payload.yaml similarity index 100% rename from test/pod-no-latest/payload.yaml rename to test/commands/scan/pod-no-latest/payload.yaml diff --git a/test/pod-no-latest/policy.yaml b/test/commands/scan/pod-no-latest/policy.yaml similarity index 100% rename from test/pod-no-latest/policy.yaml rename to test/commands/scan/pod-no-latest/policy.yaml diff --git a/test/scripted/out.txt b/test/commands/scan/scripted/out.txt similarity index 100% rename from test/scripted/out.txt rename to test/commands/scan/scripted/out.txt diff --git a/test/scripted/payload.yaml b/test/commands/scan/scripted/payload.yaml similarity index 100% rename from test/scripted/payload.yaml rename to test/commands/scan/scripted/payload.yaml diff --git a/test/scripted/policy.yaml b/test/commands/scan/scripted/policy.yaml similarity index 100% rename from test/scripted/policy.yaml rename to test/commands/scan/scripted/policy.yaml diff --git a/test/tf-ec2/ec2.tf b/test/commands/scan/tf-ec2/ec2.tf similarity index 100% rename from test/tf-ec2/ec2.tf rename to test/commands/scan/tf-ec2/ec2.tf diff --git a/test/tf-ec2/out.txt b/test/commands/scan/tf-ec2/out.txt similarity index 100% rename from test/tf-ec2/out.txt rename to test/commands/scan/tf-ec2/out.txt diff --git a/test/tf-ec2/payload.json b/test/commands/scan/tf-ec2/payload.json similarity index 100% rename from test/tf-ec2/payload.json rename to test/commands/scan/tf-ec2/payload.json diff --git a/test/tf-ec2/policy.yaml b/test/commands/scan/tf-ec2/policy.yaml similarity index 100% rename from test/tf-ec2/policy.yaml rename to test/commands/scan/tf-ec2/policy.yaml diff --git a/test/tf-plan/out.txt b/test/commands/scan/tf-plan/out.txt similarity index 100% rename from test/tf-plan/out.txt rename to test/commands/scan/tf-plan/out.txt diff --git a/test/tf-plan/payload.json b/test/commands/scan/tf-plan/payload.json similarity index 100% rename from test/tf-plan/payload.json rename to test/commands/scan/tf-plan/payload.json diff --git a/test/tf-plan/policy.yaml b/test/commands/scan/tf-plan/policy.yaml similarity index 100% rename from test/tf-plan/policy.yaml rename to test/commands/scan/tf-plan/policy.yaml diff --git a/test/tf-s3/bucket.tf b/test/commands/scan/tf-s3/bucket.tf similarity index 100% rename from test/tf-s3/bucket.tf rename to test/commands/scan/tf-s3/bucket.tf diff --git a/test/tf-s3/out.txt b/test/commands/scan/tf-s3/out.txt similarity index 100% rename from test/tf-s3/out.txt rename to test/commands/scan/tf-s3/out.txt diff --git a/test/tf-s3/payload.json b/test/commands/scan/tf-s3/payload.json similarity index 100% rename from test/tf-s3/payload.json rename to test/commands/scan/tf-s3/payload.json diff --git a/test/tf-s3/policy.yaml b/test/commands/scan/tf-s3/policy.yaml similarity index 100% rename from test/tf-s3/policy.yaml rename to test/commands/scan/tf-s3/policy.yaml diff --git a/test/wildcard/out.txt b/test/commands/scan/wildcard/out.txt similarity index 100% rename from test/wildcard/out.txt rename to test/commands/scan/wildcard/out.txt diff --git a/test/wildcard/payload.json b/test/commands/scan/wildcard/payload.json similarity index 100% rename from test/wildcard/payload.json rename to test/commands/scan/wildcard/payload.json diff --git a/test/wildcard/policy.yaml b/test/commands/scan/wildcard/policy.yaml similarity index 100% rename from test/wildcard/policy.yaml rename to test/commands/scan/wildcard/policy.yaml diff --git a/test/commands/version/help.txt b/test/commands/version/help.txt new file mode 100644 index 00000000..3632040e --- /dev/null +++ b/test/commands/version/help.txt @@ -0,0 +1,12 @@ +Print the version informations + +Usage: + version [flags] + +Examples: + # Print version infos + version + + +Flags: + -h, --help help for version diff --git a/test/commands/version/out.txt b/test/commands/version/out.txt new file mode 100644 index 00000000..11b63076 --- /dev/null +++ b/test/commands/version/out.txt @@ -0,0 +1,3 @@ +Version: +Time: --- +Git commit ID: ---