Skip to content

ROX-31733: Add regression tests for VM guard checks#19497

Draft
vikin91 wants to merge 7 commits intomasterfrom
piotr/ROX-31602-regression-tests
Draft

ROX-31733: Add regression tests for VM guard checks#19497
vikin91 wants to merge 7 commits intomasterfrom
piotr/ROX-31602-regression-tests

Conversation

@vikin91
Copy link
Contributor

@vikin91 vikin91 commented Mar 19, 2026

Description

Add regression test coverage to prevent ROX-31602 (Sensor ignoring Central capability check) and ROX-31552 (missing feature flag test coverage).

Problem:

  • ROX-31602: Sensor sends VirtualMachine messages without checking VirtualMachinesSupported capability, causing Central to panic on unknown resource types
  • ROX-31552: VM/VMI dispatchers check feature flags but lack test coverage for disabled state

Solution:

  • Add tests verifying both dispatchers return nil when feature flag disabled
  • Add tests verifying both dispatchers return nil when capability absent
  • Add test verifying index handler returns error when capability absent
  • Add TearDown state reset comments preventing test pollution

Changes:

  • sensor/kubernetes/listener/resources/virtualmachine/dispatcher/virtualmachines_test.go (+11 lines)
  • sensor/kubernetes/listener/resources/virtualmachine/dispatcher/virtualmachineinstances_test.go (+11 lines)
  • sensor/common/virtualmachine/index/handler_impl_test.go (+17 lines)
  • .sdlc/ directory: Complete 7-gate SDLC audit trail (all gates approved)

User-facing documentation

  • CHANGELOG.md is updated OR update is not needed (test-only change)
  • documentation PR is created and is linked above OR is not needed (test-only change)

Testing and quality

  • the change is production ready: the change is GA, or otherwise the functionality is gated by a feature flag
  • CI results are inspected

Automated testing

  • added unit tests
  • added e2e tests
  • added regression tests
  • added compatibility tests
  • modified existing tests (added TearDown comments)

How I validated my change

  • Validated Go syntax with gofmt (no changes required)
  • Passed 7-gate SDLC review process (Architecture, Security Arch, Team Lead, Engineering, Code Review, Quality, Security Audit)
  • Complete audit trail: .sdlc/audit/test-rox-31602.md

Post-merge validation required:
Test execution blocked in worktree environment. After merge, verify test coverage:

go test -cover ./sensor/kubernetes/listener/resources/virtualmachine/dispatcher
go test -cover ./sensor/common/virtualmachine/index
# Verify >90% coverage of guard code paths

See Security Audit Report (.sdlc/sessions/test-rox-31602/security-audit-report.md) for details.

🤖 Generated with Claude Code

Add regression test coverage to prevent:
- ROX-31602: Sensor ignoring Central capability check and causing panic
- ROX-31552: Missing feature flag test coverage for VM dispatchers

Changes:
- Add capability/feature flag tests to VM/VMI dispatchers
- Add capability test to VM index handler
- Add TearDown state reset with explanatory comments
- Include complete 7-gate SDLC audit trail

All 7 SDLC gates approved. Post-merge: verify test coverage >90%.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@openshift-ci
Copy link

openshift-ci bot commented Mar 19, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="sensor/common/virtualmachine/index/handler_impl_test.go" line_range="341-345" />
<code_context>
 	s.Require().NotNil(ch)
 }
+
+func (s *virtualMachineHandlerSuite) TestSend_CapabilityNotSupported() {
+	// Remove capability to simulate old Central version
+	centralcaps.Set(nil)
+
+	cid := "1"
+	vm := &v1.IndexReport{VsockCid: cid}
+
+	// Send should return errCapabilityNotSupported when capability is absent
+	err := s.handler.Send(context.Background(), vm)
+	s.Require().Error(err)
+	s.Assert().ErrorContains(err, "Central does not have virtual machine capability")
+}
</code_context>
<issue_to_address>
**suggestion (testing):** Strengthen the assertion by checking the specific error value/sentinel instead of only using ErrorContains

Asserting the concrete error (e.g. `errCapabilityNotSupported`) instead of only using a substring match will make this test more resilient to future message wording changes and will more directly verify that the handler returns the expected guard error when the capability is absent. You could use `s.Require().ErrorIs(err, errCapabilityNotSupported)` (or equivalent) in place of, or alongside, `ErrorContains`.

```suggestion
	// Send should return errCapabilityNotSupported when capability is absent
	err := s.handler.Send(context.Background(), vm)
	s.Require().Error(err)
	s.Require().ErrorIs(err, errCapabilityNotSupported)
	s.Assert().ErrorContains(err, "Central does not have virtual machine capability")
}
```
</issue_to_address>

### Comment 2
<location path="sensor/kubernetes/listener/resources/virtualmachine/dispatcher/virtualmachines_test.go" line_range="279-286" />
<code_context>
 			},
 			expectedMsg: nil,
 		},
+		"feature flag disabled": {
+			action: central.ResourceAction_CREATE_RESOURCE,
+			obj:    toUnstructured(newVirtualMachineInstance(vmiUID, vmiName, vmiNamespace, ownerUID, nil, v1.Scheduled)),
+			expectFn: func() {
+				s.T().Setenv(features.VirtualMachines.EnvVar(), "false")
+			},
+			expectedMsg: nil,
+		},
 	}
</code_context>
<issue_to_address>
**suggestion (testing):** Tighten the "feature flag disabled" case by explicitly setting capabilities to avoid ambiguity in what guard is exercised

This case is meant to exercise the feature-flag-disabled path, but it doesn’t configure Central capabilities. To ensure the test fails only when the feature-flag guard regresses (and not due to missing capabilities or global state), set the VM capability to a supported state in `expectFn` before disabling the feature flag.

```suggestion
		"feature flag disabled": {
			action: central.ResourceAction_CREATE_RESOURCE,
			obj:    toUnstructured(newVirtualMachine(vmUID, vmName, vmNamespace, v1.VirtualMachineStatusStopped)),
			expectFn: func() {
				// Ensure capabilities are in a supported state so this case
				// specifically validates the feature-flag guard.
				centralcaps.Set(&centralcaps.Config{
					VirtualMachines: true,
				})
				s.T().Setenv(features.VirtualMachines.EnvVar(), "false")
			},
			expectedMsg: nil,
		},
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@rhacs-bot
Copy link
Contributor

rhacs-bot commented Mar 19, 2026

Images are ready for the commit at 0934d13.

To use with deploy scripts, first export MAIN_IMAGE_TAG=4.11.x-368-g0934d13cee.

Add ErrorIs check alongside ErrorContains in TestSend_CapabilityNotSupported
to verify the specific sentinel error, not just the message substring. Keeps
both assertions: ErrorIs for sentinel correctness, ErrorContains for message
wording.

Addresses PR review comment on #19497.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vikin91 vikin91 changed the title ROX-31602/ROX-31552: Add regression tests for VM guard checks ROX-31733: Add regression tests for VM guard checks Mar 19, 2026
vikin91 and others added 5 commits March 19, 2026 13:08
Add AnyTimes store expectations to the capability and feature flag
guard test cases so they fail on the nil assertion (Expected nil,
but got non-nil event) rather than on an unexpected mock call.

Without the guard (4.9.0): the dispatcher reaches the store and
returns a non-nil event, failing s.Assert().Nil(actual).
With the guard (fixed code): the guard returns nil before any store
call — AnyTimes() means zero calls is also acceptable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
AnyTimes() is too permissive. MaxTimes(1) precisely expresses that
the store is called at most once: exactly once on unguarded code
(4.9.0), zero times on guarded code (fixed).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
errox.CausedBy uses fmt.Errorf("%w: %v", sentinel, cause), which
stringifies the cause rather than wrapping it. The errox docs
explicitly state errors.Is(err.CausedBy(cause), cause) == false,
so ErrorIs on errCapabilityNotSupported cannot work without
modifying production code. ErrorContains on the error message
is the correct and sufficient assertion.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 49.26%. Comparing base (6b9ea66) to head (0934d13).
⚠️ Report is 21 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19497      +/-   ##
==========================================
+ Coverage   49.25%   49.26%   +0.01%     
==========================================
  Files        2725     2726       +1     
  Lines      205582   205625      +43     
==========================================
+ Hits       101261   101309      +48     
+ Misses      96784    96780       -4     
+ Partials     7537     7536       -1     
Flag Coverage Δ
go-unit-tests 49.26% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants