Skip to content

Conversation

@jra3
Copy link
Collaborator

@jra3 jra3 commented Nov 17, 2025

Summary

This PR adds comprehensive error path testing for hardware graph components to achieve >80% test coverage for previously under-tested error scenarios, addressing issue #160.

The implementation follows a three-phase testing approach covering critical error paths, data validation edge cases, and recovery scenarios.

What Changed

New Test File

  • internal/hardware/graph/error_paths_test.go (812 lines)
    • MockStore implementation for controlled error injection testing
    • 15 comprehensive test functions covering all three testing phases
    • Systematic coverage of error scenarios across hardware graph operations

Dependencies

  • Added github.com/stretchr/objx v0.5.2 (required by testify/mock)

Test Coverage by Phase

Phase 1: Critical Error Paths

  • File System Access (getOSInfo): Missing files, permission denied, malformed content, empty files, quote handling
  • System Information (getSystemInfo): Graceful error handling with fallback values for uname failures
  • Resource Store Failures: System node creation, CPU package storage, CPU core storage, and relationship persistence errors

Phase 2: Data Validation

  • Network Interface Enums: Unknown duplex modes, interface types, operational states, empty values, case variations
  • Bus Connection Enums: All bus types (PCI, PCIe, NVMe, SATA, USB, I2C, SPI, etc.) including unknown value handling
  • Protobuf Marshaling: Nil pointer handling and error propagation through relationship creation
  • Containment Types: Physical, logical, partition mappings with proper unknown value handling

Phase 3: Recovery Testing

  • Partial Failures: Missing physical IDs for CPU cores, duplicate processor IDs in CPU info
  • NUMA Topology Errors: Store failures during node creation, invalid node distance references
  • Disk Topology Errors: Partition storage failures with proper error propagation
  • Duplicate Handling: Graceful skipping of existing relationships, proper query error handling
  • Edge Cases: Bus type inference for all device name patterns, nil snapshot component handling

Test Results

Coverage Improvement:

  • Before: 70.9%
  • After: 80.4%
  • Improvement: +9.5 percentage points ✅

All tests passing:

ok  	github.com/antimetal/agent/internal/hardware/graph	32.709s	coverage: 80.4% of statements

Success Criteria Met

✅ Increase error path coverage to >80% for identified functions
✅ Test all identified failure scenarios from issue analysis
✅ Validate proper error messages and context propagation
✅ Ensure no panics on invalid input or edge cases

Testing Instructions

# Run all hardware graph tests
go test ./internal/hardware/graph/...

# Run with coverage report
go test ./internal/hardware/graph/... -cover

# Run specific error path tests
go test ./internal/hardware/graph/... -run "Error|EdgeCase|Recovery"

# Generate detailed coverage profile
go test ./internal/hardware/graph/... -coverprofile=coverage.out
go tool cover -html=coverage.out

Implementation Details

MockStore Pattern

Uses testify/mock to create a controllable mock of the resource store interface, enabling:

  • Precise error injection at specific operations
  • Verification of call patterns and arguments
  • Simulation of race conditions and partial failures

Error Injection Strategy

Each test systematically exercises error paths:

  1. Setup mock to return specific error at exact operation
  2. Execute function under test
  3. Verify error is properly propagated with context
  4. Confirm no panic or undefined behavior

Edge Case Coverage

Tests handle realistic failure scenarios:

  • Filesystem errors (ENOENT, EACCES)
  • Malformed data files (invalid JSON, missing fields)
  • Concurrent access patterns
  • Resource exhaustion situations

Closes #160

Implement extensive error path testing for hardware graph components
to increase test coverage from 70.9% to 80.4%, exceeding the >80%
target for issue #160.

The test suite covers three phases:

Phase 1 - Critical error paths:
- File access errors in getOSInfo (missing files, permission denied,
  malformed content)
- System info error handling for uname failures
- Resource store failures (system node creation, CPU packages,
  relationships)

Phase 2 - Data validation:
- Enum mapping edge cases (network interface modes, bus types,
  containment types)
- Protobuf marshaling error scenarios
- Bus type inference edge cases for PCI and network devices

Phase 3 - Recovery testing:
- Partial failure recovery in NUMA node creation
- Duplicate relationship handling
- Invalid resource reference handling
- CPU package creation with missing cores

Add MockStore implementation using testify/mock for controlled error
injection during testing.

Closes #160

Co-Authored-By: Claude <[email protected]>
Signed-off-by: John Allen <[email protected]>
@jra3 jra3 requested a review from haq204 November 17, 2025 16:16
@jra3 jra3 marked this pull request as ready for review November 17, 2025 16:16
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 error path testing for hardware graph components

2 participants