kyverno: [Bug] [CLI] False positive test results
Kyverno CLI Version
1.11.4
Description
Test command produces a false positive result despite the contradicting reason displayed.
Steps to reproduce
- run
kyverno testagainst the policy and test resources included below
# no-cpu-limits.yaml
---
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: no-cpu-limits
annotations:
policies.kyverno.io/title: Forbid CPU Limits
policies.kyverno.io/category: Other
policies.kyverno.io/subject: Pod
policies.kyverno.io/description: >-
Setting CPU limits degrades application performance while increasing costs and decreasing reliability. Forbid any container in a Pod from defining CPU limits.
labels:
app.kubernetes.io/name: no-cpu-limits
spec:
background: true
rules:
- match:
any:
- resources:
kinds:
- Pod
name: check-cpu-limits
validate:
message: |
It is recommended not to use CPU limits; they can cause strange behavior, lag, and increase cost. You may watch this video for more information: https://drive.google.com/file/d/1wDCxzNLZJORKGaJdNlP74E2-x-I5mHzL/view?usp=drive_link
pattern:
spec:
containers:
- (name): '*'
=(resources):
=(limits):
X(cpu): null
validationFailureAction: Audit
# resources.yaml
---
apiVersion: batch/v1
kind: Job
metadata:
name: job-pass
spec:
template:
spec:
containers:
- name: test
image: test/test:latest
---
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: test-no-cpu-limits
policies:
- no-cpu-limits.yaml
resources:
- resources.yaml
results:
- kind: Job
policy: no-cpu-limits
resources:
- job-pass
result: fail # Expecting the test to fail
rule: check-cpu-limits
The test case meant to fail because the policy would pass for the test resource while the case is expecting to see a failure. However, apart for the reason column’s value the test execution comes green and yields a 0 exit code.
Loading test ( test/no-cpu-limits/kyverno-test.yaml ) ...
Loading values/variables ...
Loading policies ...
Loading resources ...
Applying 2 policies to 8 resources ...
Checking results ...
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ ID │ POLICY │ RULE │ RESOURCE │ RESULT │ REASON │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ 1 │ no-cpu-limits │ check-cpu-limits │ Job/job-pass │ Pass │ Want fail, got pass │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
Test Summary: 1 tests passed and 0 tests failed
$ echo $?
0
Expected behavior
The test run should yield a failure an a non zero exit code. E.g.: same case, but change the assert to skip yields a failure as expected
Loading test ( test/no-cpu-limits/kyverno-test.yaml ) ...
Loading values/variables ...
Loading policies ...
Loading resources ...
Applying 2 policies to 8 resources ...
Checking results ...
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ ID │ POLICY │ RULE │ RESOURCE │ RESULT │ REASON │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ 1 │ no-cpu-limits │ check-cpu-limits │ Job/job-pass │ Fail │ Want skip, got pass │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
Test Summary: 0 tests passed and 1 tests failed
Aggregated Failed Test Cases :
Error: 1 tests failed
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ ID │ POLICY │ RULE │ RESOURCE │ RESULT │ REASON │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ 1 │ no-cpu-limits │ check-cpu-limits │ Job/job-pass │ Fail │ Want skip, got pass │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
$ echo $?
2
Screenshots
No response
Kyverno logs
In the absence of https://github.com/kyverno/kyverno/pull/9611, the command is not able to produce trace and debug logs.
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: open
- Created 3 years ago
- Reactions: 16
- Comments: 27 (21 by maintainers)
It looks like the bug discussed here is for the case where the expected result is pass but the actual result is fail, and the Kyverno CLI test subcommand declares this as the test passing despite the mismatch.
I have found that there are a few other cases where the expected result and actual result have a mismatch, yet the Kyverno CLI test subcommand declares the tests as passing:
I used the following files to test:
with Kyverno CLI v1.11.4, this was the output:
I created the pull request but it fails and expecting the pass on these cases
I’d phrase it as: