Skip to content

test(subscriber/k8s): add unit and fuzz tests for label helper functions#5537

Open
yoonseo-han wants to merge 5 commits into
litmuschaos:masterfrom
yoonseo-han:test/fuzz-subscriber-k8s-labels
Open

test(subscriber/k8s): add unit and fuzz tests for label helper functions#5537
yoonseo-han wants to merge 5 commits into
litmuschaos:masterfrom
yoonseo-han:test/fuzz-subscriber-k8s-labels

Conversation

@yoonseo-han

Copy link
Copy Markdown

Proposed changes

Adds unit tests and fuzz tests for the updateLabels and addCustomLabels label helper functions in pkg/k8s.
Fixes #5536

The existing test coverage had two gaps:

  • updateLabels had no tests of any kind
  • addCustomLabels had a single unit test covering only the basic merge scenario

This PR fills those gaps by introducing a clear separation between unit tests and fuzz tests, where each layer has a distinct responsibility:

Unit tests (objects_test.go, operations_test.go) — deterministic, named scenarios that document behavioral contracts:

  • updateLabels: nil input, empty map
  • addCustomLabels: nil custom labels, object with no prior labels set, key overwrite behavior

Fuzz tests (labels_fuzz_test.go) — invariant verification over random inputs, focused on what the fuzzer can actually vary:

  • FuzzUpdateLabels: verifies output length, every key=value present, no duplicates
  • FuzzAddCustomLabels: verifies custom labels always win, non-overlapping existing labels are preserved

Types of changes

What types of changes does your code introduce to Litmus? Put an x in the boxes that apply

  • New feature (non-breaking change which adds functionality)
  • Bugfix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices applies)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have signed the commit for DCO to be passed.
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added necessary documentation (if appropriate)

Dependency

  • Please add the links to the dependent PR need to be merged before this (if any).

NA

Special notes for your reviewer:

Each of the tests were tested locally based on the following

Fuzz tests

go test ./pkg/k8s -run='^$' -fuzz=FuzzUpdateLabels -fuzztime=30s
go test ./pkg/k8s -run='^$' -fuzz=FuzzAddCustomLabels -fuzztime=30s

Unit tests

go test ./pkg/k8s -v

Note: subscriber is currently omitted from the backend-unit-tests Makefile target so unit tests will not run in standard CI. This is a pre-existing gap out of scope for this PR — all tests pass locally under go test ./....

Signed-off-by: Yoonseo Han <yooncer00@gmail.com>
Signed-off-by: Yoonseo Han <yooncer00@gmail.com>
Signed-off-by: Yoonseo Han <yooncer00@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds missing unit and fuzz test coverage for the subscriber’s Kubernetes label helper functions in chaoscenter/subscriber/pkg/k8s, closing the coverage gaps called out in #5536.

Changes:

  • Added deterministic unit tests for (*k8sSubscriber).updateLabels (nil, empty, basic map cases).
  • Expanded unit tests for addCustomLabels to cover unset object labels, nil custom labels, and overwrite behavior.
  • Added fuzz tests validating invariants for both helpers across randomized inputs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
chaoscenter/subscriber/pkg/k8s/operations_test.go Adds additional unit test cases for addCustomLabels edge/overwrite scenarios.
chaoscenter/subscriber/pkg/k8s/objects_test.go Introduces unit tests for updateLabels covering nil/empty/basic behavior.
chaoscenter/subscriber/pkg/k8s/labels_fuzz_test.go Adds fuzz tests asserting invariants for updateLabels and addCustomLabels.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@PriteshKiri

Copy link
Copy Markdown
Contributor

The Trivy check is failing for this PR. Could you please create a separate PR for the fix and share the link here once it's ready?

@yoonseo-han

Copy link
Copy Markdown
Author

Hi @PriteshKiri . Thanks for the check and rerun

Looks like the Trivy check passed after the rerun. Would a seperate PR still be necessary or are we good to proceed?

@PriteshKiri

Copy link
Copy Markdown
Contributor

This should be fine @yoonseo-han

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Fuzz test and Unit test for k8s label related helper functions

4 participants