1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

feat: add support for wildcard in CLI filters (#8216)

* feat: add wildcard support to test-case-selector flag in cli test command

Signed-off-by: Chandan-DK <chandandk468@gmail.com>

* fix logic

Signed-off-by: Chandan-DK <chandandk468@gmail.com>

* add unit tests

Signed-off-by: Chandan-DK <chandandk468@gmail.com>

* update description

Signed-off-by: Chandan-DK <chandandk468@gmail.com>

---------

Signed-off-by: Chandan-DK <chandandk468@gmail.com>
This commit is contained in:
Chandan-DK 2023-09-02 03:03:28 +05:30 committed by GitHub
parent b12d40a292
commit 51de975e7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 5 deletions

View file

@ -49,7 +49,7 @@ func Command() *cobra.Command {
}
cmd.Flags().StringVarP(&fileName, "file-name", "f", "kyverno-test.yaml", "test filename")
cmd.Flags().StringVarP(&gitBranch, "git-branch", "b", "", "test github repository branch")
cmd.Flags().StringVarP(&testCase, "test-case-selector", "t", "", `run some specific test cases by passing a string argument in double quotes to this flag like - "policy=<policy_name>, rule=<rule_name>, resource=<resource_name". The argument could be any combination of policy, rule and resource.`)
cmd.Flags().StringVarP(&testCase, "test-case-selector", "t", "", `run some specific test cases by passing a string argument in double quotes to this flag like - "policy=<policy_name>, rule=<rule_name>, resource=<resource_name". The argument could be any combination of policy, rule and resource. Wildcard is supported for values of policy, rule and resource`)
cmd.Flags().BoolVar(&registryAccess, "registry", false, "If set to true, access the image registry using local docker credentials to populate external data")
cmd.Flags().BoolVar(&failOnly, "fail-only", false, "If set to true, display all the failing test only as output for the test command")
cmd.Flags().BoolVar(&removeColor, "remove-color", false, "Remove any color from output")

View file

@ -5,6 +5,7 @@ import (
"strings"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/pkg/utils/wildcard"
)
type Filter interface {
@ -19,7 +20,7 @@ func (f policy) Apply(result api.TestResults) bool {
if result.Policy == "" {
return true
}
if result.Policy == f.value {
if wildcard.Match(f.value, result.Policy) {
return true
}
return false
@ -33,7 +34,7 @@ func (f rule) Apply(result api.TestResults) bool {
if result.Rule == "" {
return true
}
if result.Rule == f.value {
if wildcard.Match(f.value, result.Rule) {
return true
}
return false
@ -47,7 +48,7 @@ func (f resource) Apply(result api.TestResults) bool {
if result.Resource == "" {
return true
}
if result.Resource == f.value {
if wildcard.Match(f.value, result.Resource) {
return true
}
return false

View file

@ -45,6 +45,20 @@ func Test_policy_Apply(t *testing.T) {
Policy: "not-test",
},
want: false,
}, {
name: "wildcard match",
value: "disallow-*",
result: api.TestResults{
Policy: "disallow-latest-tag",
},
want: true,
}, {
name: "wildcard does not match",
value: "allow-*",
result: api.TestResults{
Policy: "disallow-latest-tag",
},
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -95,6 +109,20 @@ func Test_rule_Apply(t *testing.T) {
Rule: "not-test",
},
want: false,
}, {
name: "wildcard match",
value: "*-image-tag",
result: api.TestResults{
Rule: "validate-image-tag",
},
want: true,
}, {
name: "wildcard does not match",
value: "require-*",
result: api.TestResults{
Rule: "validate-image-tag",
},
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -145,6 +173,20 @@ func Test_resource_Apply(t *testing.T) {
Resource: "not-test",
},
want: false,
}, {
name: "wildcard match",
value: "good*01",
result: api.TestResults{
Resource: "good-deployment-01",
},
want: true,
}, {
name: "wildcard does not match",
value: "good*01",
result: api.TestResults{
Resource: "bad-deployment-01",
},
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View file

@ -148,7 +148,7 @@ For more information visit https://kyverno.io/docs/kyverno-cli/#test
-h, --help help for test
--registry If set to true, access the image registry using local docker credentials to populate external data
--remove-color Remove any color from output
-t, --test-case-selector string run some specific test cases by passing a string argument in double quotes to this flag like - "policy=<policy_name>, rule=<rule_name>, resource=<resource_name". The argument could be any combination of policy, rule and resource.
-t, --test-case-selector string run some specific test cases by passing a string argument in double quotes to this flag like - "policy=<policy_name>, rule=<rule_name>, resource=<resource_name". The argument could be any combination of policy, rule and resource. Wildcard is supported for values of policy, rule and resource
```
### Options inherited from parent commands