This directory contains comprehensive testing examples and patterns for the StorageGRID SDK, demonstrating both unit testing with mocks and integration testing with real StorageGRID environments.
testing/
├── README.md # This file - overview of testing approaches
├── unit-tests/
│ ├── README.md # Unit testing guide and best practices
│ └── main_test.go # Unit test examples with mocks
└── integration-tests/
├── README.md # Integration testing guide and setup
└── main_test.go # Integration test examples with real API calls
Purpose: Test your application logic in isolation using SDK mocks.
- Fast execution - No network calls or external dependencies
- Perfect for CI/CD - Reliable and deterministic
- Complete coverage - Test all code paths including error scenarios
- Mock-based - Uses the SDK's built-in mock services
When to use:
- Testing business logic that uses the SDK
- Validating error handling
- Continuous integration pipelines
- Development and debugging
Purpose: Validate that the SDK works correctly with real StorageGRID environments.
- Real API calls - Tests actual SDK-to-StorageGRID communication
- Environment validation - Ensures SDK works with your StorageGRID version
- End-to-end scenarios - Complete workflow testing
- Credential-based - Requires actual StorageGRID access
When to use:
- Validating SDK against your StorageGRID deployment
- Pre-production testing
- Nightly regression testing
- SDK development and validation
Unit tests run by default and require no setup:
cd unit-tests/
go test -vIntegration tests require environment setup:
# Set up environment
export STORAGEGRID_INTEGRATION_TESTS=true
export STORAGEGRID_HOST=https://your-storagegrid.com
export STORAGEGRID_USERNAME=your-username
export STORAGEGRID_PASSWORD=your-password
# Run tests
cd integration-tests/
go test -v// Create mock service
mockTenantService := &sgTesting.MockTenantService{}
// Configure mock behavior
mockTenantService.CreateFunc = func(ctx context.Context, tenant *models.Tenant) (*models.Tenant, error) {
tenant.Id = "mock-tenant-id"
return tenant, nil
}
// Use in your service
service := NewMyService(mockTenantService)
result, err := service.CreateTenant(ctx, "test-tenant")// Create real client
gridClient, err := client.NewGridClient(
client.WithEndpoint(host),
client.WithCredentials(credentials),
)
// Test real operations
tenant, err := gridClient.Tenant().Create(ctx, &models.Tenant{
Name: &tenantName,
})The SDK provides comprehensive mocks for all service interfaces:
| Service | Mock Class | Description |
|---|---|---|
| Tenant | MockTenantService |
Tenant lifecycle management |
| Bucket | MockBucketService |
Bucket operations |
| Health | MockHealthService |
System health monitoring |
| HAGroup | MockHAGroupService |
High availability groups |
| Gateway | MockGatewayConfigService |
Gateway configuration |
| Region | MockRegionService |
Region management |
| S3AccessKey | MockS3AccessKeyService |
S3 access key management |
| TenantGroup | MockTenantGroupService |
Tenant group management |
| TenantUser | MockTenantUserService |
Tenant user management |
- Use table-driven tests for comprehensive coverage
- Test both success and failure scenarios
- Mock external dependencies consistently
- Keep tests fast and deterministic
- Use meaningful test names
- Run against dedicated test environments
- Include proper cleanup logic
- Use timeouts for all operations
- Handle network failures gracefully
- Never run against production systems
- Write tests before implementing features (TDD)
- Maintain high test coverage (>80%)
- Use descriptive assertions and error messages
- Document complex test scenarios
- Keep tests simple and focused
- name: Run Unit Tests
run: |
cd examples/testing/unit-tests
go test -v -race -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html- name: Run Integration Tests
if: github.event_name == 'schedule' # Nightly only
env:
STORAGEGRID_INTEGRATION_TESTS: true
STORAGEGRID_HOST: ${{ secrets.STORAGEGRID_HOST }}
STORAGEGRID_USERNAME: ${{ secrets.STORAGEGRID_USERNAME }}
STORAGEGRID_PASSWORD: ${{ secrets.STORAGEGRID_PASSWORD }}
run: |
cd examples/testing/integration-tests
go test -v -timeout 10mNo special setup required - tests use mocks and run entirely in memory.
Requires access to a StorageGRID environment:
- Test Environment: Set up a dedicated StorageGRID test environment
- Credentials: Create service accounts with minimal required permissions
- Security: Use environment variables, never hardcode credentials
- Isolation: Ensure tests don't interfere with production data
- Import conflicts: Use
sgTestingalias for SDK testing package - Mock configuration: Ensure all required mock functions are set
- Test isolation: Reset mocks between test cases
- Authentication failures: Verify credentials and permissions
- Network timeouts: Check connectivity and increase timeouts if needed
- Resource cleanup: Review cleanup warnings and manual verification
When adding new tests:
- Unit tests should cover all new service methods and error conditions
- Integration tests should validate real-world usage scenarios
- Documentation should be updated to reflect new testing patterns
- CI/CD configurations should include new test paths