From 4c1a8336b0267204bacfd995904589601364ebee Mon Sep 17 00:00:00 2001 From: Shubham Nazare Date: Wed, 9 Mar 2022 13:10:53 +0530 Subject: [PATCH] Add new test-case-selector flag to test command (#3183) * added new test-case flag to test command Signed-off-by: Shubham Nazare Co-authored-by: Vyankatesh Kudtarkar Co-authored-by: Sambhav Kothari --- Makefile | 5 +- pkg/kyverno/test/test_command.go | 130 ++++++++++++++++++++++++++++--- 2 files changed, 123 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 33fd9dd4e9..d6a9e6e63f 100644 --- a/Makefile +++ b/Makefile @@ -225,7 +225,7 @@ test-clean: go clean -testcache ./... .PHONY: test-cli -test-cli: test-cli-policies test-cli-local test-cli-local-mutate +test-cli: test-cli-policies test-cli-local test-cli-local-mutate test-cli-test-case-selector-flag .PHONY: test-cli-policies test-cli-policies: cli @@ -239,6 +239,9 @@ test-cli-local: cli test-cli-local-mutate: cli cmd/cli/kubectl-kyverno/kyverno test ./test/cli/test +.PHONY: test-cli-test-case-selector-flag +test-cli-test-case-selector-flag: cli + cmd/cli/kubectl-kyverno/kyverno test ./test/cli/test --test-case-selector "policy=disallow-latest-tag, rule=require-image-tag, resource=test-require-image-tag-pass" # go get downloads and installs the binary # we temporarily add the GO_ACC to the path diff --git a/pkg/kyverno/test/test_command.go b/pkg/kyverno/test/test_command.go index 28cfa1b1ac..ca3cf887b7 100644 --- a/pkg/kyverno/test/test_command.go +++ b/pkg/kyverno/test/test_command.go @@ -16,6 +16,7 @@ import ( "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/kataras/tablewriter" + v1 "github.com/kyverno/kyverno/api/kyverno/v1" report "github.com/kyverno/kyverno/api/policyreport/v1alpha2" client "github.com/kyverno/kyverno/pkg/dclient" "github.com/kyverno/kyverno/pkg/engine/response" @@ -77,6 +78,21 @@ applying 1 policy to 4 resources... Test Summary: 4 tests passed and 0 tests failed +# Test some specific test cases out of many test cases in a local folder. +kyverno test . --test-case-selector "policy=disallow-latest-tag, rule=require-image-tag, resource=test-require-image-tag-pass" + +Executing test-simple... +applying 1 policy to 1 resource... + +│───│─────────────────────│───────────────────│─────────────────────────────────────────│────────│ +│ # │ POLICY │ RULE │ RESOURCE │ RESULT │ +│───│─────────────────────│───────────────────│─────────────────────────────────────────│────────│ +│ 1 │ disallow-latest-tag │ require-image-tag │ default/Pod/test-require-image-tag-pass │ Pass │ +│───│─────────────────────│───────────────────│─────────────────────────────────────────│────────│ + +Test Summary: 1 tests passed and 0 tests failed + + **TEST FILE STRUCTURE**: @@ -135,8 +151,9 @@ For more information visit https://kyverno.io/docs/kyverno-cli/#test // Command returns version command func Command() *cobra.Command { var cmd *cobra.Command + var testCase string var testFile []byte - var valuesFile, fileName, gitBranch string + var fileName, gitBranch string cmd = &cobra.Command{ Use: "test [flags]\n kyverno test --git-branch \n kyverno test --manifest-mutate > kyverno-test.yaml\n kyverno test --manifest-validate > kyverno-test.yaml", // Args: cobra.ExactArgs(1), @@ -194,7 +211,7 @@ results: fmt.Println(string(testFile)) return nil } - _, err = testCommandExecute(dirPath, valuesFile, fileName, gitBranch) + _, err = testCommandExecute(dirPath, fileName, gitBranch, testCase) if err != nil { log.Log.V(3).Info("a directory is required") return err @@ -205,7 +222,7 @@ results: } 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=, rule=, resource= 1 { msgPolicies = fmt.Sprintf("%d policies", len(policies))