Skip to content

Commit c7bdbe1

Browse files
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: #3835
1 parent e44ab35 commit c7bdbe1

31 files changed

+7146
-20
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,35 @@ jobs:
7373
go tool cover -func=coverage-integration.out | grep total | awk '{print "Integration test coverage: " $3}'
7474
fi
7575
76+
- name: Check coverage threshold
77+
if: matrix.go-version == '1.25'
78+
run: |
79+
if [ ! -f coverage.out ]; then
80+
echo "::error::Coverage file not found"
81+
exit 1
82+
fi
83+
84+
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
85+
86+
if [ -z "$COVERAGE" ]; then
87+
echo "::error::Failed to extract coverage percentage"
88+
exit 1
89+
fi
90+
91+
THRESHOLD=80
92+
echo "Current coverage: ${COVERAGE}%"
93+
echo "Minimum threshold: ${THRESHOLD}%"
94+
95+
if (( $(echo "$COVERAGE < $THRESHOLD" | bc -l) )); then
96+
echo "::error::Coverage ${COVERAGE}% is below minimum threshold of ${THRESHOLD}%"
97+
exit 1
98+
fi
99+
echo "Coverage check passed!"
100+
76101
- name: Upload coverage to Codecov
77102
if: matrix.go-version == '1.25'
78103
uses: codecov/codecov-action@v4
104+
continue-on-error: true # Codecov is informational; inline check above is the hard gate
79105
with:
80106
files: ./coverage.out,./coverage-integration.out
81107
flags: unittests

Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
check-prerequisites check-docker check-kubectl \
2424
build test test-integration test-e2e test-all \
2525
lint fmt clean install-deps \
26-
docker-build docker-push
26+
docker-build docker-push \
27+
coverage-check
2728

2829
# ================================================================================================
2930
# Project Configuration
@@ -232,6 +233,20 @@ test-all:
232233
@go tool cover -func=coverage/coverage.out | grep total | awk '{print "Total coverage: " $$3}'
233234
@$(call print_success,"All tests passed - coverage report: coverage/coverage.html")
234235

236+
# Check coverage meets minimum threshold (80%)
237+
COVERAGE_THRESHOLD := 80
238+
coverage-check:
239+
@$(call print_status,"Checking coverage threshold (minimum $(COVERAGE_THRESHOLD)%)...")
240+
@mkdir -p coverage
241+
@go test ./... -covermode=atomic -coverprofile=coverage/coverage.out -short > /dev/null 2>&1
242+
@COVERAGE=$$(go tool cover -func=coverage/coverage.out | grep total | awk '{print $$3}' | sed 's/%//'); \
243+
echo "Current coverage: $${COVERAGE}%"; \
244+
if [ $$(echo "$${COVERAGE} < $(COVERAGE_THRESHOLD)" | bc -l) -eq 1 ]; then \
245+
$(call print_error,"Coverage $${COVERAGE}% is below minimum threshold of $(COVERAGE_THRESHOLD)%"); \
246+
exit 1; \
247+
fi
248+
@$(call print_success,"Coverage check passed (>= $(COVERAGE_THRESHOLD)%)")
249+
235250
# Lint code with golangci-lint
236251
lint:
237252
@$(call print_status,"Running golangci-lint...")
@@ -546,6 +561,7 @@ help:
546561
@echo " make test-integration Run integration tests"
547562
@echo " make test-e2e Run end-to-end tests"
548563
@echo " make test-all Run all tests with coverage report"
564+
@echo " make coverage-check Verify coverage >= 80% threshold"
549565
@echo " make lint Run golangci-lint"
550566
@echo " make fmt Format code with gofmt and goimports"
551567
@echo ""

0 commit comments

Comments
 (0)