Skip to content

Fix/add probe uniqueness check#5519

Open
yoonseo-han wants to merge 7 commits into
litmuschaos:masterfrom
yoonseo-han:fix/add-probe-uniqueness-check
Open

Fix/add probe uniqueness check#5519
yoonseo-han wants to merge 7 commits into
litmuschaos:masterfrom
yoonseo-han:fix/add-probe-uniqueness-check

Conversation

@yoonseo-han

@yoonseo-han yoonseo-han commented May 21, 2026

Copy link
Copy Markdown

Proposed changes

Fixes #5518

AddProbe missed server-side uniqueness enforcement. Uniqueness was only guaranteed by the MongoDB partial compound index on (name, project_id) where is_removed: false, which prevented data corruption but surfaced raw E11000 duplicate key errors to non-UI callers instead of a clear application-level message.

Two entry points were affected:

  • Direct GraphQL/API clients calling addProbe with a duplicate name received an opaque Mongo error
  • Experiment import via chaos_experiment/ops/service.go calls AddProbe directly with no duplicate handling at all

Contribution points:

  • This PR wires the existing ValidateUniqueProbe method into AddProbe as an early return before any database write is attempted, making all entry points consistent.
  • Introduces the first unit tests for the probe handler layer: Previously pkg/probe/handler/ had no test coverage covering both the duplicate rejection behavior and the ValidateUniqueProbe method directly.

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)

Changes

pkg/probe/handler/handler.go: Added uniqueness check at the top of AddProbe using the existing ValidateUniqueProbe method, returning a clear "probe already exists" error before any object construction or DB write occurs.

ValidateUniqueProbe runs a CountDocuments query filtered on name, project_id, and is_removed= false : Filter is backed by the existing partial compound index on (name, project_id), so no collection scan occurs.

  • If the count is greater than zero the probe name is already active in that project and the request is rejected early.
  • Soft-deleted probes (is_removed: true) are excluded from the count, preserving the existing behavior that allows name reuse after deletion.

pkg/probe/handler/handler_test.go — Added unit tests covering:

  • Duplicate probe name rejection (TestAddProbe_DuplicateName): verifies Create is never called on duplicate
  • ValidateUniqueProbe returns true when name is unique (TestValidateUniqueProbe_Unique)
  • ValidateUniqueProbereturns false when duplicate exists (TestValidateUniqueProbe_Duplicate)
  • ValidateUniqueProbe propagates DB errors correctly (TestValidateUniqueProbe_DBError)

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:

  • The MongoDB partial unique index on (name, project_id) where is_removed: false remains the hard guarantee against duplicate data. This PR adds application-level enforcement on top of it, not instead of it.
  • The current approach was chosen for consistency with how infra registration and experiment naming handle the same problem in this codebase.
  • There are currently no other probe handler tests. The four tests added here are the first coverage of this layer.

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

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

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

This PR fixes missing application-level probe name uniqueness enforcement in the GraphQL server’s probe handler by wiring the existing ValidateUniqueProbe check into AddProbe, and introduces initial unit coverage for the probe handler layer to prevent regressions.

Changes:

  • Add an early uniqueness check in AddProbe via ValidateUniqueProbe, returning a clear "probe already exists" error before attempting any DB write.
  • Add first unit tests for probe handler uniqueness behavior (ValidateUniqueProbe true/false/error cases, and AddProbe duplicate rejection).

Reviewed changes

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

File Description
chaoscenter/graphql/server/pkg/probe/handler/handler.go Adds early uniqueness validation in AddProbe to avoid surfacing raw Mongo duplicate key errors in common duplicate-name cases.
chaoscenter/graphql/server/pkg/probe/handler/handler_test.go Adds initial unit tests for probe handler uniqueness logic and duplicate rejection behavior.

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

Comment on lines +29 to +32
assert.NoError(t, err)
assert.True(t, unique)
mockOp.AssertExpectations(t)
}
Comment on lines +73 to +76
assert.Error(t, err)
assert.Equal(t, "probe already exists", err.Error())
mockOp.AssertNotCalled(t, "Create", mock.Anything, mock.Anything, mock.Anything)
mockOp.AssertExpectations(t)
@yoonseo-han

Copy link
Copy Markdown
Author

Hi @ispeakc0de @PriteshKiri

Thanks for reviewing this PR. I have read through the comments that copilot has left. I believe the comments are pointing out a real issue in the shared mongo mock implementation where methods are currently defined with value receivers instead of pointer receivers.

This can make testify call-history based assertions unreliable when the mock is used via the mongodb.MongoOperator interface. So the issue raised by Copilot seems valid as a test-infra issue.

For this PR however, the probe handler test intent looks correct for validating unique probe and duplicate handling. The root cause appears to be in the shared mock type rather than the probe handler test logic itself.

Hence I wanted to confirm the preferred scope:

  1. I can include a focused fix here to update mongo mock methods to pointer receivers,
  2. I can keep this PR scoped to probe changes and open a separate issue/PR for the shared mongo mock fix.

Or any other ideas would be very much appreciated.

Thanks

@ispeakc0de

Copy link
Copy Markdown
Member

Hi @yoonseo-han ,
Looks like a straightforward change, so it's better to address it in this PR itself.

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

Copy link
Copy Markdown
Author

Thanks @ispeakc0de

Included the mongo mock fix in this PR. Updated the MongoOperator mock methods to pointer receivers so that testify assertions work correctly

Ready for another look when you are free

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.

Server-side uniqueness validation missing in AddProbe

5 participants