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

  1. run kyverno test against 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)

Most upvoted comments

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:

  • expected result: pass, actual result: skip
  • expected result: fail, actual result: skip
  • expected result: any arbitrary garbage that is not pass/fail/skip, actual result: skip

I used the following files to test:

# kyverno-test.yaml
---
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
  name: test-no-cpu-limits
policies:
  - no-cpu-limits.yaml
resources:
  - resources.yaml
results:
  # expected Kyverno CLI result: pass, actual Kyverno CLI result: pass
  # expected result: pass, actual result: pass
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-pass
    result: pass
    rule: check-cpu-limits

  # [BUG] - already discussed in https://github.com/kyverno/kyverno/issues/9804
  # expected Kyverno CLI result: fail, actual Kyverno CLI result: pass
  # expected result: pass, actual result: fail
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-pass
    result: fail
    rule: check-cpu-limits

  # [BUG]
  # expected Kyverno CLI result: fail, actual Kyverno CLI result: pass
  # expected result: pass, actual result: skip
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-pass-2
    result: fail
    rule: check-cpu-limits
  
  # expected Kyverno CLI result: pass, actual Kyverno CLI result: pass
  # expected result: fail, actual result: fail
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-fail
    result: fail
    rule: check-cpu-limits

  # expected Kyverno CLI result: fail, actual Kyverno CLI result: fail
  # expected result: fail, actual result: pass
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-fail
    result: pass
    rule: check-cpu-limits

  # [BUG]
  # expected Kyverno CLI result: fail, actual Kyverno CLI result: pass
  # expected result: fail, actual result: skip
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-skip
    result: fail
    rule: check-cpu-limits
  
  # expected Kyverno CLI result: pass, actual Kyverno CLI result: pass
  # expected result: skip, actual result: skip
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-skip
    result: skip
    rule: check-cpu-limits
  
  # expected Kyverno CLI result: fail, actual Kyverno CLI result: fail
  # expected result: skip, actual result: pass
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-pass
    result: skip
    rule: check-cpu-limits

  # expected Kyverno CLI result: fail, actual Kyverno CLI result: fail
  # expected result: skip, actual result: fail
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-fail
    result: skip
    rule: check-cpu-limits

  # [BUG]
  # expected Kyverno CLI result: fail, actual Kyverno CLI result: pass
  # expected result: garbage, actual result: skip
  - kind: Job
    policy: no-cpu-limits
    resources:
      - job-skip
    result: garbage
    rule: check-cpu-limits
# resources.yaml
---
apiVersion: batch/v1
kind: Job
metadata:
  name: job-fail
  namespace: test
spec:
  template:
    spec:
      containers:
        - name: test
          image: test/test:latest
          resources:
            limits:
              cpu: 1.0
---
apiVersion: batch/v1
kind: Job
metadata:
  name: job-pass
  namespace: test
spec:
  template:
    spec:
      containers:
        - name: test
          image: test/test:latest
---
apiVersion: batch/v1
kind: Job
metadata:
  name: job-pass-2
  namespace: test2
spec:
  template:
    spec:
      containers:
        - name: test
          image: test/test:latest
---
apiVersion: batch/v1
kind: Job
metadata:
  name: job-skip
  namespace: test2
spec:
  template:
    spec:
      containers:
        - name: test
          image: test/test:latest
# 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
          namespaces:
          - test
    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

with Kyverno CLI v1.11.4, this was the output:

Loading test  ( kyverno-cli-skip-result/kyverno-test.yaml ) ...
  Loading values/variables ...
  Loading policies ...
  Loading resources ...
  Applying 1 policy to 4 resources ...
  Checking results ...

│────│───────────────│──────────────────│────────────────│────────│─────────────────────│
│ ID │ POLICY        │ RULE             │ RESOURCE       │ RESULT │ REASON              │
│────│───────────────│──────────────────│────────────────│────────│─────────────────────│
│ 1  │ no-cpu-limits │ check-cpu-limits │ Job/job-pass   │ Pass   │ Ok                  │
│ 2  │ no-cpu-limits │ check-cpu-limits │ Job/job-pass   │ Pass   │ Want fail, got pass │
│ 3  │ no-cpu-limits │ check-cpu-limits │ Job/job-pass-2 │ Pass   │ Excluded            │
│ 4  │ no-cpu-limits │ check-cpu-limits │ Job/job-fail   │ Pass   │ Ok                  │
│ 5  │ no-cpu-limits │ check-cpu-limits │ Job/job-fail   │ Fail   │ Want pass, got fail │
│ 6  │ no-cpu-limits │ check-cpu-limits │ Job/job-skip   │ Pass   │ Excluded            │
│ 7  │ no-cpu-limits │ check-cpu-limits │ Job/job-skip   │ Pass   │ Excluded            │
│ 8  │ no-cpu-limits │ check-cpu-limits │ Job/job-pass   │ Fail   │ Want skip, got pass │
│ 9  │ no-cpu-limits │ check-cpu-limits │ Job/job-fail   │ Fail   │ Want skip, got fail │
│ 10 │ no-cpu-limits │ check-cpu-limits │ Job/job-skip   │ Pass   │ Excluded            │
│────│───────────────│──────────────────│────────────────│────────│─────────────────────│


Test Summary: 7 tests passed and 3 tests failed

Aggregated Failed Test Cases : 
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ ID │ POLICY        │ RULE             │ RESOURCE     │ RESULT │ REASON              │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
│ 1  │ no-cpu-limits │ check-cpu-limits │ Job/job-fail │ Fail   │ Want pass, got fail │
│ 2  │ no-cpu-limits │ check-cpu-limits │ Job/job-pass │ Fail   │ Want skip, got pass │
│ 3  │ no-cpu-limits │ check-cpu-limits │ Job/job-fail │ Fail   │ Want skip, got fail │
│────│───────────────│──────────────────│──────────────│────────│─────────────────────│
Error: 3 tests failed

I created the pull request but it fails and expecting the pass on these cases

I’d phrase it as:

  • unless the apply’s result matching with the test case’s assert it has to give fail