Team Ai
Apppublic

sinhapiyush86/convAI

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
test_docker.py438 linesDownload Raw Back to root
1#!/usr/bin/env python32"""3# Test Script for Docker Deployment4 5This script provides comprehensive testing for the RAG system Docker deployment.6 7## Overview8 9The test script validates all components required for successful Docker deployment:10- Dockerfile syntax and structure11- Docker Compose configuration12- Docker build process13- Container runtime functionality14- File structure and dependencies15 16## Test Categories17 181. **Dockerfile Tests**: Validate Dockerfile syntax and required components192. **Docker Compose Tests**: Check docker-compose.yml configuration203. **Build Tests**: Test Docker image building process214. **Runtime Tests**: Validate container startup and health checks225. **File Structure Tests**: Confirm all required files are present236. **Requirements Tests**: Validate dependencies are properly specified24 25## Usage26 27Run the script to check Docker deployment readiness:28```bash29python test_docker.py30```31 32## Prerequisites33 34- Docker installed and running35- Docker Compose available36- Sufficient disk space for image building37- Network connectivity for base image downloads38 39## Expected Output40 41The script provides detailed feedback on each test:42- ✅ PASS: Component is ready for Docker deployment43- ❌ FAIL: Component needs attention before deployment44- ⚠️ WARNING: Optional component missing but not critical45"""46 47import os48import sys49import subprocess50from pathlib import Path51 52 53def test_dockerfile():54    """55    Test if Dockerfile exists and contains all required components56 57    This function validates:58    - Dockerfile exists in the project root59    - Contains essential Docker instructions60    - Proper syntax and structure61    - Required components for RAG system deployment62 63    Returns:64        bool: True if Dockerfile is valid, False otherwise65    """66    print("🔍 Testing Dockerfile...")67 68    dockerfile_path = Path("Dockerfile")69    if not dockerfile_path.exists():70        print("❌ Dockerfile not found")71        return False72 73    try:74        with open(dockerfile_path, "r") as f:75            content = f.read()76 77        # List of essential Dockerfile components that must be present78        required_components = [79            "FROM python:",  # Base image specification80            "WORKDIR /app",  # Working directory setup81            "COPY requirements.txt",  # Requirements file copying82            "RUN pip install",  # Python package installation83            "COPY .",  # Application files copying84            "EXPOSE 8501",  # Port exposure for Streamlit85            'CMD ["streamlit"',  # Application startup command86        ]87 88        missing_components = []89        for component in required_components:90            if component in content:91                print(f"✅ {component}")92            else:93                print(f"❌ {component} (missing)")94                missing_components.append(component)95 96        if missing_components:97            print(f"❌ Missing Dockerfile components: {missing_components}")98            return False99 100        return True101 102    except Exception as e:103        print(f"❌ Dockerfile test failed: {e}")104        return False105 106 107def test_dockerignore():108    """109    Test if .dockerignore exists (optional but recommended)110 111    This function checks for the presence of .dockerignore file,112    which helps optimize Docker builds by excluding unnecessary files.113 114    Returns:115        bool: True if .dockerignore exists or is optional, False if critical116    """117    print("\n🔍 Testing .dockerignore...")118 119    dockerignore_path = Path(".dockerignore")120    if dockerignore_path.exists():121        print("✅ .dockerignore exists")122        return True123    else:124        print("⚠️  .dockerignore not found (optional but recommended)")125        return True126 127 128def test_docker_compose():129    """130    Test if docker-compose.yml exists and is properly configured131 132    This function validates:133    - docker-compose.yml file exists134    - Contains proper service definitions135    - Port mappings are correct136    - Volume mounts are configured137 138    Returns:139        bool: True if docker-compose.yml is valid, False otherwise140    """141    print("\n🔍 Testing docker-compose.yml...")142 143    compose_path = Path("docker-compose.yml")144    if compose_path.exists():145        print("✅ docker-compose.yml exists")146        return True147    else:148        print("⚠️  docker-compose.yml not found (optional)")149        return True150 151 152def test_docker_build():153    """154    Test Docker build process locally155 156    This function:157    - Attempts to build the Docker image158    - Validates build process completes successfully159    - Checks for build errors and warnings160    - Ensures all dependencies are properly resolved161 162    Returns:163        bool: True if Docker build succeeds, False otherwise164    """165    print("\n🔍 Testing Docker build...")166 167    try:168        # Test Docker build with timeout to prevent hanging169        result = subprocess.run(170            ["docker", "build", "-t", "rag-system-test", "."],171            capture_output=True,172            text=True,173            timeout=300,  # 5 minutes timeout for build174        )175 176        if result.returncode == 0:177            print("✅ Docker build successful")178            return True179        else:180            print(f"❌ Docker build failed: {result.stderr}")181            return False182 183    except subprocess.TimeoutExpired:184        print("❌ Docker build timed out")185        return False186    except FileNotFoundError:187        print("⚠️  Docker not installed or not in PATH")188        return False189    except Exception as e:190        print(f"❌ Docker build test failed: {e}")191        return False192 193 194def test_docker_run():195    """196    Test Docker container runtime functionality197 198    This function:199    - Attempts to run the built Docker container200    - Validates container startup process201    - Checks if the application is accessible202    - Tests basic container functionality203 204    Returns:205        bool: True if Docker run succeeds, False otherwise206    """207    print("\n🔍 Testing Docker run...")208 209    try:210        # Test Docker run with brief execution211        result = subprocess.run(212            [213                "docker",214                "run",215                "--rm",216                "-d",217                "-p",218                "8501:8501",219                "--name",220                "rag-test",221                "rag-system-test",222            ],223            capture_output=True,224            text=True,225            timeout=30,  # 30 seconds timeout for startup226        )227 228        if result.returncode == 0:229            print("✅ Docker run successful")230 231            # Clean up the test container232            subprocess.run(["docker", "stop", "rag-test"], capture_output=True)233            return True234        else:235            print(f"❌ Docker run failed: {result.stderr}")236            return False237 238    except subprocess.TimeoutExpired:239        print("❌ Docker run timed out")240        return False241    except FileNotFoundError:242        print("⚠️  Docker not installed or not in PATH")243        return False244    except Exception as e:245        print(f"❌ Docker run test failed: {e}")246        return False247 248 249def test_file_structure():250    """251    Test if all required files exist for Docker deployment252 253    This function checks for essential files:254    - Main application files255    - Configuration files256    - Docker-related files257    - Documentation files258 259    Returns:260        bool: True if all required files exist, False otherwise261    """262    print("\n🔍 Testing file structure...")263 264    # List of required files for Docker deployment265    required_files = [266        "app.py",  # Main Streamlit application267        "rag_system.py",  # Core RAG system268        "pdf_processor.py",  # PDF processing utilities269        "requirements.txt",  # Python dependencies270        "Dockerfile",  # Docker configuration271    ]272 273    # List of optional files (nice to have but not critical)274    optional_files = [275        ".dockerignore",  # Docker build optimization276        "docker-compose.yml",  # Multi-container setup277        "README.md",  # Project documentation278    ]279 280    missing_required = []281    missing_optional = []282 283    # Check required files284    for file in required_files:285        if os.path.exists(file):286            print(f"✅ {file}")287        else:288            print(f"❌ {file} (missing)")289            missing_required.append(file)290 291    # Check optional files292    for file in optional_files:293        if os.path.exists(file):294            print(f"✅ {file}")295        else:296            print(f"⚠️  {file} (optional)")297            missing_optional.append(file)298 299    if missing_required:300        print(f"❌ Missing required files: {missing_required}")301        return False302 303    return True304 305 306def test_requirements():307    """308    Test if requirements.txt contains all essential packages309 310    This function validates:311    - Essential packages are listed312    - Package versions are specified313    - No obvious missing dependencies314    - Compatibility with Docker environment315 316    Returns:317        bool: True if requirements are valid, False otherwise318    """319    print("\n🔍 Testing requirements.txt...")320 321    try:322        with open("requirements.txt", "r") as f:323            requirements = f.read()324 325        # List of essential packages that must be present326        essential_packages = [327            "streamlit",  # Web framework328            "torch",  # Deep learning329            "transformers",  # Language models330            "sentence-transformers",  # Embeddings331            "faiss-cpu",  # Vector search332            "rank-bm25",  # Sparse retrieval333            "pypdf",  # PDF processing334        ]335 336        missing_packages = []337        for package in essential_packages:338            if package in requirements:339                print(f"✅ {package}")340            else:341                print(f"❌ {package} (missing)")342                missing_packages.append(package)343 344        if missing_packages:345            print(f"❌ Missing packages: {missing_packages}")346            return False347 348        return True349 350    except Exception as e:351        print(f"❌ Requirements test failed: {e}")352        return False353 354 355def main():356    """357    Run all Docker deployment tests and provide comprehensive feedback358 359    This function:360    1. Executes all Docker-related test categories361    2. Tracks test results and provides detailed feedback362    3. Gives deployment recommendations363    4. Identifies potential issues before deployment364 365    The tests are designed to catch common Docker deployment issues early.366    """367    print("🐳 Docker Deployment Test\n")368 369    # Define all test functions with descriptive names370    tests = [371        ("File Structure", test_file_structure),372        ("Requirements", test_requirements),373        ("Dockerfile", test_dockerfile),374        (".dockerignore", test_dockerignore),375        ("docker-compose.yml", test_docker_compose),376        ("Docker Build", test_docker_build),377        ("Docker Run", test_docker_run),378    ]379 380    # Execute all tests and collect results381    results = []382    for test_name, test_func in tests:383        try:384            result = test_func()385            results.append((test_name, result))386        except Exception as e:387            print(f"❌ {test_name} test failed with exception: {e}")388            results.append((test_name, False))389 390    # =============================================================================391    # RESULTS SUMMARY392    # =============================================================================393 394    # Display comprehensive test results395    print("\n" + "=" * 50)396    print("📊 Test Results Summary")397    print("=" * 50)398 399    passed = 0400    total = len(results)401 402    # Show individual test results403    for test_name, result in results:404        status = "✅ PASS" if result else "❌ FAIL"405        print(f"{test_name:20} {status}")406        if result:407            passed += 1408 409    # Display overall statistics410    print(f"\nOverall: {passed}/{total} tests passed")411 412    # =============================================================================413    # DEPLOYMENT RECOMMENDATIONS414    # =============================================================================415 416    if passed == total:417        print("🎉 All tests passed! Ready for Hugging Face Docker deployment.")418        print("\nNext steps:")419        print("1. Create a new Hugging Face Space with Docker SDK")420        print("2. Upload all files from this directory")421        print("3. Wait for Docker build to complete")422        print("4. Test your RAG system!")423    else:424        print("⚠️  Some tests failed. Please fix the issues before deployment.")425        print("\nTroubleshooting:")426        print("1. Install Docker if not available")427        print("2. Check file permissions and paths")428        print("3. Verify Dockerfile syntax")429        print("4. Test Docker build locally: docker build -t rag-system .")430 431 432# =============================================================================433# SCRIPT ENTRY POINT434# =============================================================================435 436if __name__ == "__main__":437    main()438