Skip to content

test(hardware): add comprehensive unit tests for hardware manager#242

Closed
jra3 wants to merge 1 commit into
mainfrom
feat/issue-159
Closed

test(hardware): add comprehensive unit tests for hardware manager#242
jra3 wants to merge 1 commit into
mainfrom
feat/issue-159

Conversation

@jra3
Copy link
Copy Markdown
Collaborator

@jra3 jra3 commented Nov 15, 2025

Summary

Adds comprehensive unit tests for the hardware manager (internal/hardware/manager.go) which previously had 0% test coverage.

Builds on top of PR #241 (CI bpftool fix).

Tests Added

Constructor & Configuration

  • TestNewManager_RequiresStore - Validates store requirement
  • TestNewManager_AppliesDefaults - Verifies default interval (5min)
  • TestNewManager_CustomInterval - Tests custom update intervals
  • TestManager_CollectionConfigDefaults - Validates ApplyDefaults() call

Lifecycle Management

  • TestManager_Lifecycle_StartAndStop - Basic start/stop flow
  • TestManager_InitialDiscoveryNonFatal - Startup succeeds even when discovery fails
  • TestManager_ContextCancellation - Graceful shutdown on context cancel
  • TestManager_NeedLeaderElection - Validates runs on all nodes

Periodic Updates

  • TestManager_PeriodicUpdates - Validates timer-based updates
  • TestManager_GetLastUpdateTime - Tracks last update time
  • TestManager_ForceUpdate - Manual update triggering

Collector Integration

  • TestManager_CollectHardwareSnapshot_Success - Successful snapshot collection
  • TestManager_CollectHardwareSnapshot_WithInvalidPaths - Graceful failure handling
  • TestManager_CollectHardwareSnapshot_SnapshotFields - Snapshot field validation
  • TestManager_UpdateHardwareGraph_ContextTimeout - Context timeout handling

Thread Safety

  • TestManager_ConcurrentGetLastUpdateTime - Concurrent access validation

Coverage

  • macOS: 64.9% (limited by platform-specific collector paths)
  • Linux: Expected >80% (CI will exercise all collector integration paths)

Per-function coverage on macOS:

  • NewManager: 100%
  • Start: 80%
  • NeedLeaderElection: 100%
  • runPeriodicUpdates: 87.5%
  • updateHardwareGraph: 84.6%
  • GetLastUpdateTime: 100%
  • ForceUpdate: 100%
  • collectHardwareSnapshot: 38.9% (low due to macOS limitations)

Testing

go test ./internal/hardware -v -timeout 120s

All 15 tests pass successfully.

Closes #159

Related

…ecycle

Add unit tests for hardware manager covering:
- Constructor validation and defaults
- Start/Stop lifecycle coordination
- Context cancellation and graceful shutdown
- Periodic update scheduling
- Error handling for collector failures
- Thread-safe concurrent access
- Force update functionality
- Hardware snapshot collection

Test coverage: 64.9% on macOS (will be >80% on Linux where collectors fully function)
@jra3 jra3 requested a review from haq204 November 17, 2025 16:17
@jra3 jra3 closed this Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: Add comprehensive unit tests for hardware manager lifecycle

1 participant