kyverno: [Bug] [CLI] Incorrect/missing rule name has different test result from incorrect/missing policy name

Kyverno CLI Version

1.11.2

Description

When doing a kyverno test where the test manifest (kyverno-test.yaml) names a policy name in the results[] array which cannot be found in the corresponding policy files in the policies[] array, the CLI shows the result as Fail with a reason of Not found. But if instead it is the rule name which is incorrect/missing the result is instead Pass with a reason of Excluded. This doesn’t make any sense and is inconsistent.

Furthermore, these checks are considered basic validations and it would be better for the CLI to exit even before trying to test them. For example, when either a policy file or resource file is not found, the CLI does not begin testing and shows a message why. This same behavior should be extended to basic validations such as missing policy or rule names in the results[] array.

Steps to reproduce

  1. Test the prevent-bare-pods policy with the following test manifest:
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
  name: prevent-bare-pods
policies:
  - barecpol.yaml
resources:
  - bareresources.yaml
results:
  - kind: Pod
    policy: prevent-bare-pods
    resources:
      - mypod
    result: fail
    rule: bare-pods
  1. Use some basic Pod for the resource…it doesn’t matter.
  2. Notice how this test should succeed.
  3. Change the name of results[].policy so the name is wrong.
  4. Retest.
  5. See the result is as follows:
│────│──────────────────│───────────│───────────│────────│───────────│
│ ID │ POLICY           │ RULE      │ RESOURCE  │ RESULT │ REASON    │
│────│──────────────────│───────────│───────────│────────│───────────│
│ 1  │ prevent-bare-pod │ bare-pods │ Pod/mypod │ Fail   │ Not found │
│────│──────────────────│───────────│───────────│────────│───────────│
Error: 1 tests failed
  1. Put the policy name back so it’s correct and this time change the rule name so it’s incorrect.
  2. Retest.
  3. See a different result as follows:
│────│───────────────────│──────────│───────────│────────│──────────│
│ ID │ POLICY            │ RULE     │ RESOURCE  │ RESULT │ REASON   │
│────│───────────────────│──────────│───────────│────────│──────────│
│ 1  │ prevent-bare-pods │ bare-pod │ Pod/mypod │ Pass   │ Excluded │
│────│───────────────────│──────────│───────────│────────│──────────│
  1. Notice how this time the result passes with a reason of Excluded which is misleading and inconsistent with the result from step 6.

Expected behavior

At a minimum, the results should be identical:

│────│──────────────────│───────────│───────────│────────│───────────│
│ ID │ POLICY           │ RULE      │ RESOURCE  │ RESULT │ REASON    │
│────│──────────────────│───────────│───────────│────────│───────────│
│ 1  │ prevent-bare-pods │ bare-pod │ Pod/mypod │ Fail   │ Not found │
│────│──────────────────│───────────│───────────│────────│───────────│

At best, the CLI fails before attempting any testing if the policy and/or rule name cannot be found.

Screenshots

No response

Kyverno logs

No response

Slack discussion

No response

Troubleshooting

  • I have read and followed the troubleshooting guide.
  • I have searched other issues in this repository and mine is not recorded.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Just a note; for testing a ValidatingAdmissionPolicy, the rule field is unset and the isValidatingAdmissionPolicy must be set to true.

So we should check first the isValidatingAdmissionPolicy field and depending on its value, check the rule field.