Commit c7bdbe1
authored
feat: Complete Test Coverage Improvements Feature (#4)
* test: Add comprehensive tests for cmd/node-doctor
Add extensive test coverage for cmd/node-doctor/main.go including:
- Table-driven tests for createMonitors with various scenarios
- NoopExporter tests (ExportStatus, ExportProblem, Stop, concurrent access)
- MonitorFactoryAdapter tests for interface compliance
- DumpConfiguration tests with output validation
- Configuration loading tests with testdata fixtures
- CreateExporters tests with lifecycle management
New testdata fixtures:
- valid_config.yaml: Basic valid configuration
- invalid_syntax.yaml: Malformed YAML for error testing
- empty_monitors.yaml: Edge case with empty monitor list
- full_config.yaml: Complete configuration with all options
Coverage improved from 20.7% to 22.0%. The theoretical maximum for
this package is ~25-30% due to the untestable main() function which
contains signal handling, os.Exit calls, and infinite loops.
Ref: TaskForge #3836
* test: Improve Prometheus exporter test coverage to 93%
Add comprehensive tests for previously uncovered functions:
- TestIsReloadable: Verify interface implementation
- TestReload: Test configuration reload with various scenarios
- TestReloadNotStarted: Test reload on non-started exporter
- TestNeedsServerRestart: Test server restart detection logic
- TestNeedsMetricsRecreation: Test metrics recreation detection
- TestLabelsEqual: Test label comparison edge cases
- TestIsValidMetricName: Test metric name validation
- TestValidateConfig: Test configuration validation
- TestShutdownServer: Test server shutdown scenarios
- TestExportProblemBeforeStart: Test error handling
Add helper function waitForServerReady for robust server
readiness checking instead of time.Sleep.
Improve test reliability with t.Cleanup() for resource
management and proper HTTP client timeouts.
Coverage increased from 63.5% to 93.2%.
* test: Improve pkg/reload test coverage from 68.5% to 93.8%
Add comprehensive tests for configuration validation and file watching:
- Validator tests for all monitor types (kubelet, capacity, log-pattern,
script, prometheus) with valid configs and error conditions
- Security tests for script path validation (shell metacharacters) and
file path validation (absolute path requirements)
- Exporter validation tests (Kubernetes, Prometheus, HTTP)
- Remediation and webhook configuration tests
- Watcher tests for empty path handling and default debounce behavior
- Tests for Kubernetes ConfigMap atomic update handling
* test: Improve pkg/detector test coverage from 69.6% to 84.2%
- Fix integration tests by handling both ReloadCoordinator and detector event naming
- Add webhook content comparison in exportersEqual (fixes config change detection bug)
- Add comprehensive statistics tests for GetTotalExports, GetExportSuccessRate, GetDeduplicationRate
- Add MonitorHandle tests for GetConfig, IsRunning, and Stop methods
- Add ProblemDetector.Run() test coverage
- Replace deprecated ioutil functions with os package equivalents
- All tests pass with race detection enabled
Task: #3829
* test: Improve pkg/exporters/kubernetes test coverage from 74.7% to 83.3%
Add comprehensive tests for previously uncovered functions:
- IsReloadable, Reload, needsClientRecreation, annotationsEqual
- performHealthCheck, logMetrics, StatsWithLastError
- ReloadWithClientRecreation
- ConditionUpdate.String
- ConvertConditionsToNodeConditions with status mapping and type prefixing
All tests use table-driven patterns and cover edge cases including
nil inputs, empty collections, and error conditions.
* test: Improve pkg/exporters/http test coverage from 75.6% to 88.3%
Add comprehensive tests for HTTP exporter functionality including:
- IsReloadable and Reload methods with various config scenarios
- needsWorkerPoolRecreation logic for detecting config changes
- performHealthCheck for webhook health monitoring
- min helper function edge cases
Create new stats_test.go with tests for all stats functions:
- RecordDroppedRequest and RecordRetryAttempt tracking
- GetTotalFailures, GetStatusSuccessRate, GetProblemSuccessRate
- WebhookStatsSnapshot GetSuccessRate and IsHealthy methods
- recordRequest with response time averaging
- Thread-safe concurrent access validation
TaskForge: #3831
* test: Improve pkg/monitors/network test coverage from 76.8% to 80.2%
Add comprehensive tests for network monitoring functions:
- TestClassifyHTTPError_NetworkErrors: DNS, dial, and generic network errors
- TestValidateURL: URL validation edge cases
- TestIsValidHTTPMethod: HTTP method validation
- TestGatewayMonitor_getGatewayIP: Gateway IP resolution
- TestParseEndpointConfig_EdgeCases: Endpoint config type validation
- TestNewConnectivityMonitor_EdgeCases: Monitor creation limits
- TestValidateDNSConfig_EdgeCases: DNS config validation
- TestCheckCustomQueries: Custom DNS query handling
- TestParseConnectivityConfig_EdgeCases: Connectivity config parsing
Coverage improvements:
- classifyHTTPError: 57.9% → 89.5%
- validateURL: 60% → 90%
- isValidHTTPMethod: 75% → 100%
- getGatewayIP: 40% → 80%
- checkCustomQueries: 73.3% → 100%
- parseConnectivityConfig: 82.6% → 100%
* test: Improve pkg/types test coverage from 78.1% to 80.3%
Add comprehensive tests for core types package:
- Add ExporterReloadSummary tests (exporter_test.go):
- TestExporterReloadSummary_AddResult with success/failure scenarios
- TestExporterReloadResult_Fields for field validation
- TestExporterReloadSummary_InitialState and order preservation tests
- Add edge case validation tests (types_test.go):
- TestProblemWithMetadata_NilMetadataMap for nil metadata handling
- TestConditionValidation_EdgeCases for missing reason/message/transition
- TestStatusValidation_EdgeCases for missing timestamp/invalid condition
- TestProblemValidation_EdgeCases for missing severity/message/detected time
- TestGetMetadata_NilMetadataMap for nil metadata retrieval
Coverage: 78.1% -> 80.3% (exceeds 80% target)
All tests pass with race detection.
* test: Improve pkg/remediators test coverage from 78.1% to 80.4%
Add comprehensive tests for nil logger handling and log methods across
all remediator types:
- base_test.go: Tests for logWarnf/logErrorf with nil and set logger
- network_test.go: Tests for log methods and verifyRoutingTable direct calls
- custom_test.go: Tests for GetScriptPath and log methods
- disk_test.go: Tests for log methods with nil and set logger
- registry_test.go: Tests for log methods with nil and set logger
All tests use proper encapsulation via SetLogger() method calls.
* test: Improve cmd/node-doctor createExporters coverage from 29.8% to 61.7%
Add 9 new test functions for createExporters function:
- TestCreateExporters_HTTPExporterEnabled
- TestCreateExporters_PrometheusExporterEnabled
- TestCreateExporters_HealthServerEnabled
- TestCreateExporters_MultipleExportersEnabled
- TestCreateExporters_HTTPExporterWithValidConfig
- TestCreateExporters_PrometheusWithMetricsPath
- TestCreateExporters_HTTPWithRetryConfig
- TestCreateExporters_HealthServerWithCustomPaths
- TestCreateExporters_ExporterCleanup
Coverage target of 80%+ not achievable due to:
- HTTP exporter success path requires valid kubeconfig/auth config
- Kubernetes exporter requires actual kubeconfig
- Would require integration tests or extensive mocking
Task: #3856
* feat(ci): Add 80% coverage threshold enforcement
- Add coverage-check target to Makefile with 80% threshold
- Add coverage threshold check step to CI workflow for Go 1.25
- Create codecov.yml with project/patch coverage targets (80%)
- Add input validation for missing coverage file and empty results
- Keep Codecov as informational only (inline check is the hard gate)
Task: #38351 parent e44ab35 commit c7bdbe1
File tree
31 files changed
+7146
-20
lines changed- .github/workflows
- cmd/node-doctor
- testdata
- pkg
- detector
- exporters
- http
- kubernetes
- prometheus
- monitors/network
- reload
- remediators
- types
31 files changed
+7146
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
76 | 101 | | |
77 | 102 | | |
78 | 103 | | |
| 104 | + | |
79 | 105 | | |
80 | 106 | | |
81 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
232 | 233 | | |
233 | 234 | | |
234 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
235 | 250 | | |
236 | 251 | | |
237 | 252 | | |
| |||
546 | 561 | | |
547 | 562 | | |
548 | 563 | | |
| 564 | + | |
549 | 565 | | |
550 | 566 | | |
551 | 567 | | |
| |||
0 commit comments