Code Coverage Monitoring
This document describes the comprehensive code coverage monitoring system implemented for the NetBird API Exporter project.
Overview
The coverage monitoring system provides:
- Automated coverage tracking on every pull request and push
- Coverage gates that prevent code quality degradation
- Detailed coverage reports and badges
- Integration with Codecov for trending analysis
- Local development tools for coverage validation
System Components
1. GitHub Actions Workflow (.github/workflows/coverage.yml
)
Main Jobs:
- test-coverage: Runs unit tests with coverage analysis
- integration-tests: Runs integration tests when API tokens are available
- performance-tests: Runs performance and benchmark tests
- coverage-comparison: Compares PR coverage against main branch
Features:
- Coverage threshold enforcement (80% minimum)
- Automatic PR comments with coverage details
- HTML and badge generation
- Codecov integration
- Artifact uploading for 30-day retention
2. Coverage Configuration Files
.coverage.yml
- Local Coverage Configuration
coverage:
global_threshold: 80.0
packages:
"netbird-api-exporter/pkg/exporters": 90.0
"netbird-api-exporter/pkg/netbird": 95.0
"netbird-api-exporter/pkg/utils": 95.0
codecov.yml
- Codecov Integration
- Project-level coverage targeting 80%
- Patch coverage targeting 80%
- Proper ignore patterns for test files
- PR comment configuration
3. Local Development Scripts
scripts/check-coverage.sh
- Coverage Validation Script
# Basic usage
./scripts/check-coverage.sh
# With custom threshold
./scripts/check-coverage.sh -t 85.0
# Generate all reports
./scripts/check-coverage.sh --html --json --badge
# Compare with previous run
./scripts/check-coverage.sh --compare previous-coverage.out
scripts/test-coverage-workflow.sh
- Workflow Validation
# Validate entire coverage system
./scripts/test-coverage-workflow.sh
Current Coverage Status
Baseline Coverage: 84.2%
Package | Coverage | Status |
---|---|---|
pkg/exporters | 95.5% | ✅ Excellent |
pkg/netbird | 100.0% | ✅ Perfect |
pkg/utils | 100.0% | ✅ Perfect |
main | 0.0% | ⚠️ Expected (integration testing) |
Usage Guide
For Developers
Running Tests Locally
# Run all tests with coverage
make test-all
# Run specific test types
make test-unit
make test-integration # Requires NETBIRD_API_TOKEN
make test-performance
Checking Coverage Before Commit
# Quick coverage check
./scripts/check-coverage.sh
# Detailed coverage with reports
./scripts/check-coverage.sh --html --verbose
Improving Coverage
- Identify uncovered code:
./scripts/check-coverage.sh --html # Open coverage.html in browser
- Focus on critical functions:
- API endpoints in
pkg/exporters/
- Business logic in
pkg/netbird/
- Utility functions in
pkg/utils/
- API endpoints in
- Add meaningful tests:
- Unit tests for individual functions
- Integration tests for API interactions
- Error handling scenarios
For CI/CD
Workflow Triggers
- Push to main/develop: Full coverage analysis
- Pull Requests: Coverage analysis with comparison
- Integration tests: Only when
NETBIRD_API_TOKEN
is available
Required Secrets
# GitHub Repository Secrets
CODECOV_TOKEN: <codecov-upload-token>
NETBIRD_API_TOKEN: <optional-for-integration-tests>
Workflow Outputs
- Coverage percentage and status
- HTML coverage reports
- Coverage badges
- PR comments with detailed analysis
- Codecov integration for trending
Coverage Gates and Quality Control
Automatic Failing Conditions
- Overall coverage below 80%
- Coverage decrease > 1% from main branch
- Missing critical test coverage in new code
Warning Conditions
- Coverage between 78-80%
- Significant coverage changes without explanation
Manual Review Required
- Integration test failures
- Performance test degradation
- Coverage configuration changes
Troubleshooting
Common Issues
“Coverage file not found”
# Generate coverage file first
go test -coverprofile=coverage.out ./...
“Module path issues”
# Ensure GO111MODULE is enabled
GO111MODULE=on go test -coverprofile=coverage.out ./...
“Integration tests skipped”
# Set API token for integration tests
export NETBIRD_API_TOKEN="your-token-here"
make test-integration
“Codecov upload fails”
- Verify
CODECOV_TOKEN
is set in repository secrets - Check Codecov project configuration
- Review network connectivity in CI environment
Debug Commands
# Check coverage file contents
go tool cover -func=coverage.out
# Generate HTML report manually
go tool cover -html=coverage.out -o coverage.html
# Test coverage script
./scripts/check-coverage.sh --verbose
# Validate workflow configuration
./scripts/test-coverage-workflow.sh
Best Practices
Writing Tests for Coverage
- Test all public functions
- Cover error paths and edge cases
- Use table-driven tests for multiple scenarios
- Mock external dependencies
- Test concurrent behavior where applicable
Coverage Strategy
- Aim for 90%+ on critical business logic
- 80%+ overall project coverage
- 100% on utility and helper functions
- Focus on quality over quantity
Maintenance
- Review coverage trends monthly
- Update thresholds as code matures
- Add tests for new features immediately
- Remove obsolete test exclusions
Integration with Development Workflow
Pre-commit Hooks
# Install coverage check as pre-commit hook
make install-hooks
IDE Integration
Most IDEs support Go coverage visualization:
- VSCode: Go extension with coverage highlighting
- GoLand: Built-in coverage analysis
- Vim/Neovim: Various coverage plugins
Continuous Monitoring
- Codecov dashboard for trend analysis
- GitHub Actions for immediate feedback
- Weekly coverage reports (can be automated)
Advanced Configuration
Custom Coverage Thresholds
Edit .coverage.yml
to set package-specific thresholds:
packages:
"your/critical/package": 95.0
"your/standard/package": 80.0
Excluding Files from Coverage
exclude:
- "**/*_test.go"
- "**/testdata/**"
- "**/generated/**"
Notification Integration
Configure Slack/email notifications for coverage changes:
notifications:
slack:
enabled: true
webhook_url: "your-webhook-url"
Metrics and Reporting
Available Reports
- HTML Coverage Report (
coverage.html
) - JSON Coverage Data (
coverage.json
) - Coverage Badge (
coverage-badge.svg
) - Text Summary (console output)
Key Metrics
- Overall coverage percentage
- Per-package coverage breakdown
- Coverage trend over time
- Test execution time
- Code quality indicators
Support and Maintenance
Regular Tasks
- Review coverage reports weekly
- Update coverage thresholds quarterly
- Verify CI/CD pipeline monthly
- Update dependencies as needed
Getting Help
- Check this documentation first
- Run validation scripts for diagnostics
- Review GitHub Actions logs
- Check Codecov dashboard for insights
For additional questions or issues, please:
- Open a GitHub issue with the
coverage
label - Include relevant logs and configuration
- Describe expected vs actual behavior