# Test Module

## Overview

The Test module contains test-related code for the dataflow-service project, primarily focused on end-to-end (E2E) tests. These tests verify the functionality of the entire system by testing it as a whole, from end to end.

## Directory Structure

```
test/
├── e2e/               # End-to-end tests
│   ├── cases/          # Test cases
│   │   ├── dedicated/   # Tests for dedicated clusters
│   │   ├── premium/     # Tests for premium clusters
│   │   ├── base_case.go # Base test case implementation
│   │   └── interface.go  # Test case interface
│   ├── internal/        # Internal test utilities
│   │   ├── case_driver/  # Test case drivers
│   │   ├── config/       # Test configuration
│   │   ├── log/          # Test logging
│   │   ├── login/        # Test login utilities
│   │   ├── misc/         # Miscellaneous utilities
│   │   ├── model/        # Test models
│   │   ├── sample_data/  # Sample test data
│   │   └── tidbcloud/     # TiDB Cloud client utilities
│   ├── .gitignore
│   ├── Dockerfile
│   ├── Makefile
│   ├── Readme.md
│   ├── config.common.yaml
│   ├── config.dev.yaml
│   ├── config.prod.yaml
│   ├── config.staging.yaml
│   ├── go.mod
│   └── go.sum
└── import/             # Import-related tests
    ├── _util/
    ├── readme.md
    ├── run.sh
    ├── test_eks.sh
    ├── test_gke.sh
    └── test_serverless.sh
```

## Core Components

### 1. e2e/

End-to-end tests that verify the functionality of the entire system.

#### Subdirectories:
- **cases/**: Test cases for different scenarios
  - **dedicated/**: Tests specifically for dedicated clusters
  - **premium/**: Tests specifically for premium clusters
- **internal/**: Internal test utilities and helpers

### 2. import/

Import-related tests that verify the data import functionality.

## Test Cases

### Dedicated Cluster Tests

Test cases for dedicated clusters include:

- **Backup tests**: Snapshot backup, PITR backup, backup export, etc.
- **ChangeFeed tests**: Kafka, MySQL, S3, GCS, Azure Blob, Pulsar sinks, etc.
- **Configuration tests**: Cluster configuration operations, machine type validation, etc.

### Premium Cluster Tests

Test cases for premium clusters include:

- **ChangeFeed tests**: Kafka, MySQL, S3, GCS, Azure Blob, Pulsar sinks, etc.
- **Configuration tests**: Cluster configuration operations, machine type validation, etc.

## How to Run Tests

### Prerequisites

- Go 1.24 or later
- Docker (for running tests in containers)
- Access to TiDB Cloud resources
- Configuration files set up with appropriate credentials

### Running E2E Tests

1. **Set up configuration**: Update the appropriate config file (`config.dev.yaml`, `config.staging.yaml`, or `config.prod.yaml`) with your credentials and test parameters.

2. **Run tests**: Use the Makefile to run tests:

```bash
# Run all tests
make e2e-test-dev

# Run specific tests
make e2e-test-dev TEST_CASES="snapshot_backup_smoke"
```

### Running Import Tests

1. **Set up configuration**: Ensure you have the necessary credentials and resources set up.

2. **Run tests**: Use the provided scripts to run import tests:

```bash
# Run all import tests
./run.sh

# Run EKS-specific tests
./test_eks.sh

# Run GKE-specific tests
./test_gke.sh

# Run serverless-specific tests
./test_serverless.sh
```

## Test Configuration

Test configuration is managed through YAML files:

- **config.common.yaml**: Common configuration shared across all environments
- **config.dev.yaml**: Development environment configuration
- **config.staging.yaml**: Staging environment configuration
- **config.prod.yaml**: Production environment configuration

### Key Configuration Parameters

- **tidbCloudToken**: Token for authenticating with TiDB Cloud
- **orgID**: Organization ID for TiDB Cloud
- **projectID**: Project ID for TiDB Cloud
- **regions**: Regions to run tests in
- **tidbDedicatedClusterIds**: IDs of dedicated clusters to use for testing
- **kafka**: Kafka cluster configuration for tests
- **mysql**: MySQL database configuration for tests
- **s3**: S3 bucket configuration for tests
- **gcs**: GCS bucket configuration for tests
- **azblob**: Azure Blob storage configuration for tests

## Adding New Test Cases

To add a new test case:

1. **Create a new test file** in the appropriate directory under `cases/`
2. **Implement the test case** by extending `BaseCase` and implementing the required methods
3. **Register the test case** in the appropriate test registry (`cases_registry_dedicated.go` or `cases_registry_premium.go`)
4. **Add any required dependencies** in the case driver

### Example: Adding a New Test Case

1. Create a new file `cases/dedicated/backup/new_backup_test.go`:

```go
package backup

import (
	"context"
	"testing"

	"github.com/tidbcloud/dataflow-service/test/e2e/internal/model"
)

type NewBackupTest struct {
	BaseBackupTest
}

func (c *NewBackupTest) Name() string {
	return "new_backup_test"
}

func (c *NewBackupTest) Run(ctx context.Context, t *testing.T, env *model.Env) {
	// Test implementation here
}
```

2. Register the test case in `internal/case_driver/cases_registry_dedicated.go`:

```go
func init() {
	// Existing registrations...
	RegisterTestCase(&backup.NewBackupTest{})
}
```

## Best Practices

### Test Development

- **Keep tests focused**: Each test should verify a specific functionality
- **Use descriptive names**: Test names should clearly indicate what they're testing
- **Clean up resources**: Tests should clean up any resources they create
- **Handle errors properly**: Tests should properly handle and report errors
- **Use the base case**: Extend the base case for consistent test setup and teardown

### Test Execution

- **Run tests in appropriate environments**: Use dev for development, staging for pre-production validation
- **Monitor test execution**: Keep an eye on test progress and results
- **Analyze test failures**: Investigate and fix test failures promptly
- **Maintain test data**: Keep test data up-to-date and relevant

## Troubleshooting

### Common Issues

- **Authentication failures**: Check your TiDB Cloud token and permissions
- **Resource not found**: Ensure the specified resources exist in your TiDB Cloud account
- **Timeout errors**: Check network connectivity and increase timeouts if necessary
- **Configuration errors**: Validate your test configuration files

### Debugging Tips

- **Enable verbose logging**: Set `logLevel` to `debug` in the config file
- **Run specific tests**: Narrow down to specific tests to isolate issues
- **Check TiDB Cloud console**: Verify resources and operations in the TiDB Cloud console
- **Examine test logs**: Review test logs for detailed error information

## Notes

- E2E tests require actual TiDB Cloud resources and may incur costs
- Some tests may take a long time to run due to the nature of the operations being tested
- Ensure you have sufficient permissions in your TiDB Cloud account to perform the required operations
- Test configuration files may contain sensitive information, so handle them with care
