OpenSearch Testcontainer
A Rust testcontainer implementation for OpenSearch, enabling easy integration testing with OpenSearch instances in Docker containers.
Overview
This crate provides a testcontainers implementation for OpenSearch, allowing you to spin up OpenSearch instances programmatically for integration tests. It's built on top of the testcontainers library.
Features
- 🐳 Docker Integration - Seamlessly runs OpenSearch in Docker containers
- ⚡ Fast Setup - Quick container startup for testing
- 🔧 Configurable - Customizable cluster settings and environment variables
- 🔒 Security Ready - Built-in authentication support
- 🎯 Ready Conditions - Waits for OpenSearch to be fully initialized
- 📦 Multiple Versions - Support for different OpenSearch versions
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Quick Start
Basic Usage
use OpenSearch;
use AsyncRunner;
async
Custom Configuration
use OpenSearch;
use AsyncRunner;
async
Configuration Options
Builder Methods
The OpenSearch struct provides several builder methods for customization:
| Method | Description | Example |
|---|---|---|
with_name(name) |
Set custom Docker image name | .with_name("my-registry/opensearch") |
with_tag(tag) |
Set OpenSearch version | .with_tag("2.11.0") |
with_cluster_name(name) |
Set cluster name | .with_cluster_name("test-cluster") |
with_env_var(key, value) |
Add environment variable | .with_env_var("OPENSEARCH_JAVA_OPTS", "-Xms512m") |
Default Configuration
The default OpenSearch container comes preconfigured with:
- Image:
opensearchproject/opensearch:3.1.0 - Discovery: Single-node cluster
- Authentication: Username
admin, password?qbr9:6Y7nk6 - Ports: 9200 (HTTP), 9300 (Transport), 9600 (Performance Analyzer)
- Ready Conditions: Waits for cluster state to be GREEN and ML configuration
Authentication
let opensearch = default;
// Get credentials
let username = opensearch.username; // "admin"
let password = opensearch.password; // "?qbr9:6Y7nk6"
// Use in HTTP client
let client = builder
.danger_accept_invalid_certs
.build
.unwrap;
let response = client
.get
.basic_auth
.send
.await
.unwrap;
Common Patterns
Testing with opensearch-client
use ;
use OpenSearch;
use AsyncRunner;
async
Multiple Test Containers
use OpenSearch;
use AsyncRunner;
async
Resource Constraints
use OpenSearch;
use AsyncRunner;
async
Advanced Usage
Custom Wait Conditions
The container waits for specific log messages to ensure OpenSearch is ready:
[YELLOW] to [GREEN]- Cluster state becomes healthyML configuration initialized successfully- Machine learning is ready
Environment Variables
Common OpenSearch environment variables you might want to set:
let opensearch = default
.with_env_var
.with_env_var
.with_env_var
.with_env_var
.with_env_var
.with_env_var // Disable security
.start
.await
.unwrap;
Version Testing
Test against multiple OpenSearch versions:
async
Troubleshooting
Common Issues
Container fails to start
// Check Docker is running and you have the opensearch image
// Pull manually if needed: docker pull opensearchproject/opensearch:3.1.0
Connection refused errors
// Ensure you're connecting to the correct port
let host_port = container.get_host_port_ipv4.await.unwrap;
let url = format!; // Note: HTTPS, not HTTP
SSL/TLS errors
// OpenSearch uses self-signed certificates by default
let client = builder
.danger_accept_invalid_certs // Required for testing
.build
.unwrap;
Authentication errors
// Use the correct default credentials
.basic_auth
Debug Logging
Enable logging to see container startup:
// In your test
init;
// Or set environment variable
// RUST_LOG=debug cargo test
Performance Tips
- Reuse containers - For multiple tests, consider using the same container
- Resource limits - Set appropriate JVM heap sizes for faster startup
- Disable unnecessary features - Turn off security or ML if not needed
- Parallel tests - Use
#[tokio::test(flavor = "multi_thread")]for parallel execution
Examples
See the tests directory for more comprehensive examples:
- Basic connectivity testing
- Index creation and document operations
- Search and aggregation testing
- Bulk operations testing
- Authentication scenarios
Requirements
- Docker must be installed and running
- Rust 1.70+ (async/await support)
- Internet connection (to pull OpenSearch Docker images)
Related Crates
testcontainers- Core testcontainers functionalityopensearch-client- OpenSearch Rust clienttokio- Async runtime
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please see the Contributing Guide for details on how to contribute to this project.