This directory contains comprehensive testing infrastructure for matlas-cli, including unit tests, integration tests, infrastructure tests, and test automation tools.
# Run all tests
./scripts/test-comprehensive.sh
# Run only unit tests
./scripts/test-comprehensive.sh unit
# Run integration tests with verbose output
./scripts/test-comprehensive.sh --verbose integration
# Run with custom coverage threshold
./scripts/test-comprehensive.sh --coverage-threshold 90 alltest/
├── README.md # This file
├── integration/ # Integration tests
│ ├── atlas/ # Atlas API integration tests
│ │ ├── projects_integration_test.go
│ │ └── clusters_integration_test.go
│ ├── database/ # Database integration tests
│ │ └── database_integration_test.go
│ └── apply/ # Apply engine integration tests
│ ├── executor_integration_test.go
│ └── setup_test.go
├── infrastructure/ # Infrastructure tests (existing)
│ ├── performance/
│ │ └── scale_test.go
│ └── reliability/
│ └── resilience_test.go
└── unit/ # Additional unit test helpers
└── apply/
└── executor_unit_test.go
- Location:
internal/ - Purpose: Test individual components in isolation
- Coverage: Services, validation, configuration, apply engine
- Dependencies: Mocked/stubbed
- Runtime: Fast (< 1 minute)
# Run unit tests only
./scripts/test-comprehensive.sh unit
# Run with coverage
go test -cover ./internal/...- Location:
test/integration/ - Purpose: Test component interactions with real services
- Coverage: Atlas API, Database operations, Apply workflows
- Dependencies: Live Atlas/MongoDB instances
- Runtime: Medium (2-10 minutes)
Tests Atlas commands against live Atlas API:
- Project management (list, get)
- Cluster operations (list, get)
- User management (list, get, delete)
- Error scenarios and edge cases
# Set up credentials
export ATLAS_PUB_KEY="your-public-key"
export ATLAS_API_KEY="your-private-key"
export PROJECT_ID="your-project-id"
export ATLAS_ORG_ID="your-org-id"
# Run Atlas integration tests
./scripts/test-comprehensive.sh atlasTests database commands against live MongoDB:
- Database listing and inspection
- Collection management (create, list, delete)
- Connection string handling
- Error scenarios
# Set up MongoDB connection
export MONGODB_CONNECTION_STRING="mongodb+srv://user:pass@cluster.mongodb.net/"
# Run database integration tests
./scripts/test-comprehensive.sh database- Location:
test/infrastructure/ - Purpose: Test system behavior under operational conditions
- Coverage: Performance, reliability, scale
- Dependencies: Live Atlas resources (may incur costs)
- Runtime: Long (10-60 minutes)
# Run infrastructure tests (requires confirmation)
./scripts/test-comprehensive.sh infrastructureexport ATLAS_PUB_KEY="your-atlas-public-key"
export ATLAS_API_KEY="your-atlas-private-key"export PROJECT_ID="your-test-project-id" # For project-specific tests
export ATLAS_ORG_ID="your-atlas-org-id" # For organization testsexport MONGODB_CONNECTION_STRING="mongodb+srv://..."export VERBOSE=true # Enable verbose output
export TEST_TIMEOUT=30m # Set test timeout
export COVERAGE_THRESHOLD=85 # Set coverage thresholdCreate a .env file in the project root:
# Atlas credentials
ATLAS_PUB_KEY=your-public-key
ATLAS_API_KEY=your-private-key
PROJECT_ID=your-project-id
ATLAS_ORG_ID=your-org-id
# Database credentials
MONGODB_CONNECTION_STRING=mongodb+srv://user:pass@cluster.mongodb.net/
# Test configuration
VERBOSE=false
TEST_TIMEOUT=60m
COVERAGE_THRESHOLD=80All tests generate comprehensive reports in the test-reports/ directory:
test-reports/
├── test-summary.md # Comprehensive test summary
├── unit-coverage.html # HTML coverage report
├── unit-coverage.txt # Text coverage summary
├── unit-tests.json # Detailed unit test results
├── atlas-integration-tests.json # Atlas integration results
├── database-integration-tests.json # Database integration results
├── infrastructure-tests.json # Infrastructure test results
└── benchmarks.txt # Benchmark results
The main test runner (scripts/test-comprehensive.sh) provides a unified interface:
# Show help
./scripts/test-comprehensive.sh --help
# Run all tests
./scripts/test-comprehensive.sh all
# Run specific test category
./scripts/test-comprehensive.sh unit
./scripts/test-comprehensive.sh integration
./scripts/test-comprehensive.sh atlas
./scripts/test-comprehensive.sh database
./scripts/test-comprehensive.sh infrastructure
# Run with options
./scripts/test-comprehensive.sh --verbose --coverage-threshold 90 unit
# Generate reports only
./scripts/test-comprehensive.sh reportFor development and debugging:
# Run unit tests with coverage
go test -v -race -cover ./internal/...
# Run specific integration tests
go test -v -tags=integration ./test/integration/atlas/...
# Run with build tags
go test -v -tags=infrastructure ./test/infrastructure/...
# Run benchmarks
go test -bench=. -benchmem ./internal/...- Unit Tests: 90%+ coverage
- Integration Tests: Critical path coverage
- Infrastructure Tests: Operational scenario coverage
- HTML reports:
test-reports/*-coverage.html - Text summaries:
test-reports/*-coverage.txt - Combined analysis:
test-reports/combined-coverage.html
- Check coverage reports for gaps
- Add unit tests for uncovered functions
- Add integration tests for untested workflows
- Use
go test -coverfor quick checks
func TestServiceMethod(t *testing.T) {
// Arrange
service := NewTestService()
// Act
result, err := service.Method(testInput)
// Assert
assert.NoError(t, err)
assert.Equal(t, expectedResult, result)
}//go:build integration
// +build integration
func TestIntegrationScenario(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup real dependencies
// Run test scenario
// Verify results
}integration: For tests requiring external servicesinfrastructure: For tests that may incur costs- No tags: Unit tests (run by default)
name: Tests
on: [push, pull_request]
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- run: ./scripts/test-comprehensive.sh unit
integration-tests:
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- env:
ATLAS_PUB_KEY: ${{ secrets.ATLAS_PUB_KEY }}
ATLAS_API_KEY: ${{ secrets.ATLAS_API_KEY }}
run: ./scripts/test-comprehensive.sh integration# Before committing
./scripts/test-comprehensive.sh unit
# Before pushing
./scripts/test-comprehensive.sh integration
# Before releases
./scripts/test-comprehensive.sh all# Check environment variables
echo $ATLAS_PUB_KEY
echo $ATLAS_API_KEY
# Verify .env file
cat .env# Test connection string
mongosh "$MONGODB_CONNECTION_STRING" --eval "db.runCommand('ping')"# Increase timeout
./scripts/test-comprehensive.sh --timeout 120m all# Check detailed coverage
go tool cover -html=test-reports/unit-coverage.out# Run with verbose output
./scripts/test-comprehensive.sh --verbose unit
# Run specific test
go test -v -run TestSpecificFunction ./internal/package
# Debug with dlv
dlv test ./internal/package -- -test.run TestSpecificFunction- Determine test category (unit/integration/infrastructure)
- Place in appropriate directory
- Follow naming conventions (
*_test.go) - Add build tags if needed
- Update test runner if necessary
- Test functions:
TestFunctionName_Scenario - Benchmark functions:
BenchmarkFunctionName - Example functions:
ExampleFunctionName
- Uses build tags
- Skips when credentials unavailable
- Cleans up resources
- Has timeout protection
- Logs useful information
- Test execution time
- Coverage percentage
- Success/failure rates
- Resource usage
- Track test performance over time
- Monitor integration test reliability
- Alert on coverage degradation
- Review infrastructure test costs