mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
fix: bad test file causes all tests to pass with success (#8258)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
b495c6d112
commit
c0a74fe0d5
4 changed files with 65 additions and 68 deletions
|
@ -43,12 +43,7 @@ func Command() *cobra.Command {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
store.SetRegistryAccess(registryAccess)
|
store.SetRegistryAccess(registryAccess)
|
||||||
_, err = testCommandExecute(dirPath, fileName, gitBranch, testCase, failOnly, detailedResults)
|
return testCommandExecute(dirPath, fileName, gitBranch, testCase, failOnly, detailedResults)
|
||||||
if err != nil {
|
|
||||||
log.Log.V(3).Info("a directory is required")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().StringVarP(&fileName, "file-name", "f", "kyverno-test.yaml", "Test filename")
|
cmd.Flags().StringVarP(&fileName, "file-name", "f", "kyverno-test.yaml", "Test filename")
|
||||||
|
@ -74,10 +69,10 @@ func testCommandExecute(
|
||||||
testCase string,
|
testCase string,
|
||||||
failOnly bool,
|
failOnly bool,
|
||||||
detailedResults bool,
|
detailedResults bool,
|
||||||
) (rc *resultCounts, err error) {
|
) (err error) {
|
||||||
// check input dir
|
// check input dir
|
||||||
if len(dirPath) == 0 {
|
if len(dirPath) == 0 {
|
||||||
return rc, sanitizederror.NewWithError("a directory is required", err)
|
return sanitizederror.NewWithError("a directory is required", err)
|
||||||
}
|
}
|
||||||
// parse filter
|
// parse filter
|
||||||
filter, errors := filter.ParseFilter(testCase)
|
filter, errors := filter.ParseFilter(testCase)
|
||||||
|
@ -85,21 +80,20 @@ func testCommandExecute(
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Filter errors:")
|
fmt.Println("Filter errors:")
|
||||||
for _, e := range errors {
|
for _, e := range errors {
|
||||||
fmt.Printf(" %v \n", e.Error())
|
fmt.Println(" Error:", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// init openapi manager
|
// init openapi manager
|
||||||
openApiManager, err := openapi.NewManager(log.Log)
|
openApiManager, err := openapi.NewManager(log.Log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rc, fmt.Errorf("unable to create open api controller, %w", err)
|
return fmt.Errorf("unable to create open api controller, %w", err)
|
||||||
}
|
}
|
||||||
// load tests
|
// load tests
|
||||||
tests, err := loadTests(dirPath, fileName, gitBranch)
|
tests, err := loadTests(dirPath, fileName, gitBranch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Error loading tests:")
|
fmt.Println("Error loading tests:", err)
|
||||||
fmt.Printf(" %s\n", err)
|
return err
|
||||||
return rc, err
|
|
||||||
}
|
}
|
||||||
if len(tests) == 0 {
|
if len(tests) == 0 {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
@ -109,7 +103,8 @@ func testCommandExecute(
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Test errors:")
|
fmt.Println("Test errors:")
|
||||||
for _, e := range errs {
|
for _, e := range errs {
|
||||||
fmt.Printf(" %v \n", e.Error())
|
fmt.Println(" Path:", e.Path)
|
||||||
|
fmt.Println(" Error:", e.Err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(tests) == 0 {
|
if len(tests) == 0 {
|
||||||
|
@ -119,25 +114,25 @@ func testCommandExecute(
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rc = &resultCounts{}
|
rc := &resultCounts{}
|
||||||
var table table.Table
|
var table table.Table
|
||||||
for _, p := range tests {
|
for _, test := range tests {
|
||||||
resourcePath := filepath.Dir(p.Path)
|
if test.Err == nil {
|
||||||
if tests, responses, err := applyPoliciesFromPath(
|
resourcePath := filepath.Dir(test.Path)
|
||||||
p.Fs,
|
if tests, responses, err := applyPoliciesFromPath(
|
||||||
p.Test,
|
test,
|
||||||
p.Fs != nil,
|
resourcePath,
|
||||||
resourcePath,
|
rc,
|
||||||
rc,
|
openApiManager,
|
||||||
openApiManager,
|
filter,
|
||||||
filter,
|
false,
|
||||||
false,
|
); err != nil {
|
||||||
); err != nil {
|
return sanitizederror.NewWithError("failed to apply test command", err)
|
||||||
return rc, sanitizederror.NewWithError("failed to apply test command", err)
|
} else if t, err := printTestResult(tests, responses, rc, failOnly, detailedResults, test.Fs, resourcePath); err != nil {
|
||||||
} else if t, err := printTestResult(tests, responses, rc, failOnly, detailedResults, p.Fs, resourcePath); err != nil {
|
return sanitizederror.NewWithError("failed to print test result:", err)
|
||||||
return rc, sanitizederror.NewWithError("failed to print test result:", err)
|
} else {
|
||||||
} else {
|
table.AddFailed(t.RawRows...)
|
||||||
table.AddFailed(t.RawRows...)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !failOnly {
|
if !failOnly {
|
||||||
|
@ -152,8 +147,7 @@ func testCommandExecute(
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
return nil
|
||||||
return rc, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkResult(test api.TestResults, fs billy.Filesystem, resoucePath string, response engineapi.EngineResponse, rule engineapi.RuleResponse) (bool, string, string) {
|
func checkResult(test api.TestResults, fs billy.Filesystem, resoucePath string, response engineapi.EngineResponse, rule engineapi.RuleResponse) (bool, string, string) {
|
||||||
|
|
|
@ -4,9 +4,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/go-git/go-billy/v5"
|
|
||||||
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
||||||
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
||||||
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test"
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/filter"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/filter"
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
|
||||||
|
@ -26,38 +26,39 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func applyPoliciesFromPath(
|
func applyPoliciesFromPath(
|
||||||
fs billy.Filesystem,
|
testCase test.TestCase,
|
||||||
apiTest *api.Test,
|
|
||||||
isGit bool,
|
|
||||||
policyResourcePath string,
|
policyResourcePath string,
|
||||||
rc *resultCounts,
|
rc *resultCounts,
|
||||||
openApiManager openapi.Manager,
|
openApiManager openapi.Manager,
|
||||||
filter filter.Filter,
|
filter filter.Filter,
|
||||||
auditWarn bool,
|
auditWarn bool,
|
||||||
) ([]api.TestResults, []engineapi.EngineResponse, error) {
|
) ([]api.TestResults, []engineapi.EngineResponse, error) {
|
||||||
engineResponses := make([]engineapi.EngineResponse, 0)
|
var engineResponses []engineapi.EngineResponse
|
||||||
|
test := testCase.Test
|
||||||
|
fs := testCase.Fs
|
||||||
|
isGit := fs != nil
|
||||||
var dClient dclient.Interface
|
var dClient dclient.Interface
|
||||||
var resultCounts common.ResultCounts
|
var resultCounts common.ResultCounts
|
||||||
|
|
||||||
store.SetLocal(true)
|
store.SetLocal(true)
|
||||||
|
|
||||||
var filteredResults []api.TestResults
|
var filteredResults []api.TestResults
|
||||||
for _, res := range apiTest.Results {
|
for _, res := range test.Results {
|
||||||
if filter.Apply(res) {
|
if filter.Apply(res) {
|
||||||
filteredResults = append(filteredResults, res)
|
filteredResults = append(filteredResults, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
apiTest.Results = filteredResults
|
test.Results = filteredResults
|
||||||
|
|
||||||
if len(apiTest.Results) == 0 {
|
if len(test.Results) == 0 {
|
||||||
return nil, nil, nil
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nExecuting %s...\n", apiTest.Name)
|
fmt.Printf("\nExecuting %s...\n", test.Name)
|
||||||
valuesFile := apiTest.Variables
|
valuesFile := test.Variables
|
||||||
userInfoFile := apiTest.UserInfo
|
userInfoFile := test.UserInfo
|
||||||
|
|
||||||
variables, globalValMap, valuesMap, namespaceSelectorMap, subresources, err := common.GetVariable(nil, apiTest.Values, apiTest.Variables, fs, isGit, policyResourcePath)
|
variables, globalValMap, valuesMap, namespaceSelectorMap, subresources, err := common.GetVariable(nil, test.Values, test.Variables, fs, isGit, policyResourcePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !sanitizederror.IsErrorSanitized(err) {
|
if !sanitizederror.IsErrorSanitized(err) {
|
||||||
return nil, nil, sanitizederror.NewWithError("failed to decode yaml", err)
|
return nil, nil, sanitizederror.NewWithError("failed to decode yaml", err)
|
||||||
|
@ -76,8 +77,8 @@ func applyPoliciesFromPath(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
policyFullPath := pathutils.GetFullPaths(apiTest.Policies, policyResourcePath, isGit)
|
policyFullPath := pathutils.GetFullPaths(test.Policies, policyResourcePath, isGit)
|
||||||
resourceFullPath := pathutils.GetFullPaths(apiTest.Resources, policyResourcePath, isGit)
|
resourceFullPath := pathutils.GetFullPaths(test.Resources, policyResourcePath, isGit)
|
||||||
|
|
||||||
policies, validatingAdmissionPolicies, err := common.GetPoliciesFromPaths(fs, policyFullPath, isGit, policyResourcePath)
|
policies, validatingAdmissionPolicies, err := common.GetPoliciesFromPaths(fs, policyFullPath, isGit, policyResourcePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -87,7 +88,7 @@ func applyPoliciesFromPath(
|
||||||
|
|
||||||
var filteredPolicies []kyvernov1.PolicyInterface
|
var filteredPolicies []kyvernov1.PolicyInterface
|
||||||
for _, p := range policies {
|
for _, p := range policies {
|
||||||
for _, res := range apiTest.Results {
|
for _, res := range test.Results {
|
||||||
if p.GetName() == res.Policy {
|
if p.GetName() == res.Policy {
|
||||||
filteredPolicies = append(filteredPolicies, p)
|
filteredPolicies = append(filteredPolicies, p)
|
||||||
break
|
break
|
||||||
|
@ -97,7 +98,7 @@ func applyPoliciesFromPath(
|
||||||
|
|
||||||
var filteredVAPs []v1alpha1.ValidatingAdmissionPolicy
|
var filteredVAPs []v1alpha1.ValidatingAdmissionPolicy
|
||||||
for _, p := range validatingAdmissionPolicies {
|
for _, p := range validatingAdmissionPolicies {
|
||||||
for _, res := range apiTest.Results {
|
for _, res := range test.Results {
|
||||||
if p.GetName() == res.Policy {
|
if p.GetName() == res.Policy {
|
||||||
filteredVAPs = append(filteredVAPs, p)
|
filteredVAPs = append(filteredVAPs, p)
|
||||||
break
|
break
|
||||||
|
@ -111,7 +112,7 @@ func applyPoliciesFromPath(
|
||||||
var filteredRules []kyvernov1.Rule
|
var filteredRules []kyvernov1.Rule
|
||||||
|
|
||||||
for _, rule := range autogen.ComputeRules(p) {
|
for _, rule := range autogen.ComputeRules(p) {
|
||||||
for _, res := range apiTest.Results {
|
for _, res := range test.Results {
|
||||||
if res.IsValidatingAdmissionPolicy {
|
if res.IsValidatingAdmissionPolicy {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -153,7 +154,7 @@ func applyPoliciesFromPath(
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkableResources := selectResourcesForCheck(resources, apiTest)
|
checkableResources := selectResourcesForCheck(resources, test)
|
||||||
|
|
||||||
msgPolicies := "1 policy"
|
msgPolicies := "1 policy"
|
||||||
if len(policies)+len(validatingAdmissionPolicies) > 1 {
|
if len(policies)+len(validatingAdmissionPolicies) > 1 {
|
||||||
|
@ -234,7 +235,7 @@ func applyPoliciesFromPath(
|
||||||
engineResponses = append(engineResponses, ers...)
|
engineResponses = append(engineResponses, ers...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return apiTest.Results, engineResponses, nil
|
return test.Results, engineResponses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func selectResourcesForCheck(resources []*unstructured.Unstructured, values *api.Test) []*unstructured.Unstructured {
|
func selectResourcesForCheck(resources []*unstructured.Unstructured, values *api.Test) []*unstructured.Unstructured {
|
||||||
|
|
|
@ -19,11 +19,11 @@ type TestCase struct {
|
||||||
|
|
||||||
type TestCases []TestCase
|
type TestCases []TestCase
|
||||||
|
|
||||||
func (tc TestCases) Errors() []error {
|
func (tc TestCases) Errors() []TestCase {
|
||||||
var errors []error
|
var errors []TestCase
|
||||||
for _, test := range tc {
|
for _, test := range tc {
|
||||||
if test.Err != nil {
|
if test.Err != nil {
|
||||||
errors = append(errors, test.Err)
|
errors = append(errors, test)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errors
|
return errors
|
||||||
|
|
|
@ -13,7 +13,7 @@ func TestTestCases_Errors(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
tc TestCases
|
tc TestCases
|
||||||
want []error
|
want []TestCase
|
||||||
}{{
|
}{{
|
||||||
name: "nil",
|
name: "nil",
|
||||||
tc: nil,
|
tc: nil,
|
||||||
|
@ -31,9 +31,9 @@ func TestTestCases_Errors(t *testing.T) {
|
||||||
tc: []TestCase{{
|
tc: []TestCase{{
|
||||||
Err: errors.New("error 1"),
|
Err: errors.New("error 1"),
|
||||||
}},
|
}},
|
||||||
want: []error{
|
want: []TestCase{{
|
||||||
errors.New("error 1"),
|
Err: errors.New("error 1"),
|
||||||
},
|
}},
|
||||||
}, {
|
}, {
|
||||||
name: "two errors",
|
name: "two errors",
|
||||||
tc: []TestCase{{
|
tc: []TestCase{{
|
||||||
|
@ -41,10 +41,11 @@ func TestTestCases_Errors(t *testing.T) {
|
||||||
}, {
|
}, {
|
||||||
Err: errors.New("error 2"),
|
Err: errors.New("error 2"),
|
||||||
}},
|
}},
|
||||||
want: []error{
|
want: []TestCase{{
|
||||||
errors.New("error 1"),
|
Err: errors.New("error 1"),
|
||||||
errors.New("error 2"),
|
}, {
|
||||||
},
|
Err: errors.New("error 2"),
|
||||||
|
}},
|
||||||
}, {
|
}, {
|
||||||
name: "mixed",
|
name: "mixed",
|
||||||
tc: []TestCase{{
|
tc: []TestCase{{
|
||||||
|
@ -52,10 +53,11 @@ func TestTestCases_Errors(t *testing.T) {
|
||||||
}, {}, {
|
}, {}, {
|
||||||
Err: errors.New("error 2"),
|
Err: errors.New("error 2"),
|
||||||
}, {}},
|
}, {}},
|
||||||
want: []error{
|
want: []TestCase{{
|
||||||
errors.New("error 1"),
|
Err: errors.New("error 1"),
|
||||||
errors.New("error 2"),
|
}, {
|
||||||
},
|
Err: errors.New("error 2"),
|
||||||
|
}},
|
||||||
}}
|
}}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue