Skip to content

πŸ§ͺ Scenario 02: Testcontainers Chaos

Overview

This scenario demonstrates how to use Testcontainers in Jenkins to run real database integration tests in isolated Docker containers.


πŸ“ Directory Structure

Jenkins/jenkins_scenarios/scenario_02_testcontainers/
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Jenkinsfile
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
└── tests/
    β”œβ”€β”€ test_postgres_pass.py
    β”œβ”€β”€ test_postgres_fail.py
    β”œβ”€β”€ test_redis_pass.py
    └── test_redis_fail.py

βœ… How to Set Up the Pipeline in Jenkins UI

  1. Open Jenkins in your browser.
  2. Click "New Item".
  3. Enter a name (e.g., scenario_02_testcontainers), select Pipeline, and click OK.
  4. In the pipeline config:
  5. Under Pipeline script, select Pipeline script from SCM.
  6. Set SCM to Git and enter your repository URL.
  7. Set Script Path to Jenkins/jenkins_scenarios/scenario_02_testcontainers/Jenkinsfile.
  8. Click Save.

βœ… How to Run the Pipeline

  1. Click "Build with Parameters".
  2. Set the TEST_MODE parameter to pass (for passing tests) or fail (for chaos/failing tests).
  3. Click Build.
  4. Watch the console output for test execution and results.
  5. Check for success or failure messages.

βœ… What the Pipeline Does

  • Builds a Docker image with all dependencies
  • Runs Testcontainers-based integration tests for Postgres and Redis
  • Supports both passing and intentionally failing test modes
  • Cleans up containers after tests

πŸ§ͺ Chaos Testing Scenarios

βœ… Scenario 1: Database Connection Failures

def test_postgres_connection_failure():
    """Simulate PostgreSQL connection failures in CI/CD"""
    with PostgresContainer("postgres:15") as postgres:
        # Simulate network partition
        postgres.get_docker_client().pause(postgres.get_container_id())

        # Verify our app handles the failure gracefully
        with pytest.raises(ConnectionError):
            create_connection(postgres.get_connection_url())

βœ… Scenario 2: Slow Database Queries

def test_redis_slow_operations():
    """Simulate slow Redis operations in CI/CD"""
    with RedisContainer("redis:7-alpine") as redis:
        client = redis.get_client()

        # Simulate slow operation
        import time
        start_time = time.time()

        # Perform operation
        client.set("test", "value")
        client.get("test")

        # Verify it completes within reasonable time
        assert time.time() - start_time < 5.0

βœ… Scenario 3: Resource Constraints

def test_memory_constrained_database():
    """Test database behavior under memory constraints"""
    with PostgresContainer("postgres:15") as postgres:
        # Set memory limit
        postgres.with_memory_limit("50m")

        conn = create_connection(postgres.get_connection_url())

        # Try to insert large dataset
        try:
            for i in range(1000):
                conn.execute(text(f"INSERT INTO test_table VALUES ({i}, 'data');"))
        except Exception as e:
            # Handle memory constraint gracefully
            assert "memory" in str(e).lower() or "resource" in str(e).lower()

βœ… Troubleshooting

  • Tests fail to start:
  • Ensure Docker is running and accessible from Jenkins.
  • Check that the Docker socket is mounted in Jenkins.
  • Database containers not starting:
  • Check for port conflicts or resource limits on the Jenkins agent.
  • Permission errors:
  • Make sure Jenkins has permission to run Docker commands.
  • Test mode confusion:
  • Double-check the TEST_MODE parameter value (pass or fail).

βœ… Useful Commands

  • See running containers: bash docker ps
  • Check logs for a container: bash docker logs <container_id>
  • Remove a container: bash docker rm -f <container_id>

πŸ“Š Monitoring & Reporting

βœ… Test Metrics

  • Test execution time
  • Container startup time
  • Database connection success rate
  • Test pass/fail ratio

βœ… Chaos Metrics

  • Number of simulated failures
  • Recovery time from database failures
  • System resilience under stress

Next: Scenario 01: Docker Build | Scenario 03: HTML Reports | Scenario 04: Manage Secrets | Scenario 05: Deploy to EKS


This scenario helps you master integration testing with real services in Jenkins using Testcontainers! πŸ”₯