Skip to content

test(secretmaterial): cover ownership checks and Secret write paths - #3451

Merged
EItanya merged 1 commit into
kagent-dev:mainfrom
vedantlavale:test/secretmaterial-unit-tests
Nov 3, 2026
Merged

EItanya merged 1 commit into
kagent-dev:mainfrom
vedantlavale:test/secretmaterial-unit-tests

Conversation

@vedantlavale

@vedantlavale vedantlavale commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3450

secretmaterial had no test file of its own. The model and tool service tests reach about 71% of it, and they never run several branches. This PR adds secretmaterial_test.go for them.

What the new tests cover:

  • IsOwnedBy: mismatching apiVersion, kind, name and UID, an owner with no UID, and a matching reference among several.
  • DeleteOwnedSecret: a Secret the owner doesn't own is left in place, a missing Secret is not an error, and Get and Delete failures are returned.
  • CreateCompanionSecrets: an existing Secret that isn't Opaque is rejected, keys merge into an existing owned Secret, and a Secret owned by someone else is rejected without being changed.
  • CreateOrUpdateOwnedOpaqueSecret and CreateOwnedOpaqueSecret: create, update and error paths. An existing Secret is not overwritten by CreateOwnedOpaqueSecret.
  • ValidateMaterials: invalid secret names and keys, including a bad entry after a good one.
  • OwnerReferenceFor: the apiVersion for a named group and for the core group.

The pure functions use table-driven tests. The functions that write Secrets use the fake client with interceptor to inject failures.

Testing

From go/:

go test ./core/internal/service/secretmaterial/ -race -count=1 -cover

Statement coverage of the package goes from about 71% (indirect, through the model and tool service tests) to 100%. gofmt and go vet are clean. I couldn't run golangci-lint locally because the installed binary is built with Go 1.26 and this repo targets 1.27, so I'm relying on CI for lint.

No production code changes. The test file is about 430 lines, so I can split it into smaller PRs if you'd prefer.

@github-actions github-actions Bot added the testing Additional testing required label Oct 30, 2026
secretmaterial had no test file of its own. The model and tool service
tests reached about 71% of it indirectly, and never ran these branches:

- IsOwnedBy: apiVersion, kind, name and UID mismatches, and an owner
  with no UID
- DeleteOwnedSecret: a Secret the owner does not own is left alone, and
  a missing Secret is not an error
- CreateCompanionSecrets: an existing non-Opaque Secret is rejected, and
  keys merge into an existing owned Secret
- ValidateMaterials: an invalid key
- error paths of the Get, Create, Update and Delete calls

Add direct unit tests: table-driven for the pure functions, and the
fake client with interceptor funcs for the Secret-handling functions.
Statement coverage of the package goes from about 71% to 100%.

Signed-off-by: vedantlavale <vedantlavale@gmail.com>
@vedantlavale
vedantlavale force-pushed the test/secretmaterial-unit-tests branch from 7d53f11 to 66799a1 Compare October 30, 2026 05:47
@github-actions github-actions Bot added testing Additional testing required and removed testing Additional testing required labels Oct 30, 2026
@vedantlavale
vedantlavale marked this pull request as ready for review October 30, 2026 05:49
@vedantlavale
vedantlavale requested a review from a team as a code owner October 30, 2026 05:49
@vedantlavale

Copy link
Copy Markdown
Contributor Author

cc : @timflannagan

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.

🟢 Approval recommended

No active review issues remain in the added secretmaterial unit tests.

No open issues 🎉

What changed in this PR

Adds dedicated unit tests for secretmaterial, addressing #3450 by covering Secret ownership checks and write/delete behavior without changing production code.

Changes:

  • Adds table-driven tests for material validation, owner references, and ownership matching.
  • Tests Secret creation, key merging, rejection of incompatible or unowned Secrets, and deletion safeguards.
  • Covers injected client failures and owner rollback behavior.
File Description
go/​core/​internal/​service/​secretmaterial/​secretmaterial_test.go Adds unit tests for validation, ownership, Secret operations, error handling, and rollback.

Review effort: Balanced

@EItanya
EItanya added this pull request to the merge queue Nov 3, 2026
Merged via the queue into kagent-dev:main with commit fc8e431 Nov 3, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Additional testing required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for secretmaterial ownership checks

3 participants

⌂ 换网址