Skip to content

obiMadu/backend.im-infra

Repository files navigation

Automated Commit Testing System for Backend.im

Progress Workflow Diagram

A distributed system for testing Git commits in isolated Kubernetes environments using WebSocket communication. Built to catch regressions before they hit production.

⚑️ Features

  • Real-time Test Pipeline - WebSocket-based test orchestration
  • K8s Isolation - Dedicated namespaces per test session
  • Commit-by-Commit Testing - Test multiple commits in sequence
  • Custom Test Commands - Override default test behavior
  • Failure Forensics - Detailed error logging and output capture

πŸ— Architecture

sequenceDiagram
    Client->>Server: Connect via WS
    Client->>Server: Send commit+test command
    Server->>K8s: Create namespace
    Server->>K8s: Deploy test pod
    K8s->>TestPod: Clone repo
    K8s->>TestPod: Checkout commit
    K8s->>TestPod: Run tests
    TestPod->>Server: JSON results
    Server->>Client: Return results
Loading

πŸ”Œ Cluster Connection Guide

Choose your connection method β†’ [ AWS EKS | Manual Kubeconfig ]

graph TD
    A[Connection Type] --> B{AWS EKS?}
    B -->|Yes| C[Use AWS Mode]
    B -->|No| D[Use Manual Config]
    D --> E[Standalone Kubeconfig]
Loading

1. AWS EKS Mode (Recommended for AWS users)

Requirements:

  • AWS credentials with EKS access
  • Cluster name and region
# .env configuration
KUBECONFIG_MODE=aws
AWS_ACCESS_KEY_ID=AKIA... 
AWS_SECRET_ACCESS_KEY=secret
AWS_DEFAULT_REGION=us-west-2
KUBE_CLUSTER_NAME=prod-cluster

2. Manual Kubeconfig Mode

For: Non-AWS clusters or pre-configured access

Steps:

  1. Place kubeconfig in project root:

    mkdir -p kubeconfig
    cp ~/.kube/config kubeconfig/prod-cluster.yaml
  2. Configure environment:

    # .env
    KUBECONFIG_MODE=manual
    KUBECONFIG_FILE=./kubeconfig/prod-cluster.yaml
  3. Verify connection:

    docker compose run --rm backendim-brain kubectl cluster-info

πŸ” Connection Troubleshooting

Error Solution
server has asked for credentials Ensure AWS creds are set even in manual mode for AWS-based configs
x509 certificate unknown Embed CA cert directly in kubeconfig
permission denied Check kubeconfig file permissions: chmod 600 kubeconfig/*
no such file Verify volume mount path in docker-compose.yml

πŸ“‚ Project Structure

backend.im-infra/
β”œβ”€β”€ deployments/               # Kubernetes deployment templates
β”‚   └── templates/
β”‚       └── fastapi/           # FastAPI-specific resources
β”‚           β”œβ”€β”€ Dockerfile     # Base image for FastAPI test pods
β”‚           └── test-pod.yaml  # Pod specification for FastAPI tests

β”œβ”€β”€ scripts/                   # Operational scripts
β”‚   β”œβ”€β”€ git_handler.sh         # Handles repo cloning/checkout
β”‚   β”œβ”€β”€ healthcheck.sh         # Service health monitoring
β”‚   β”œβ”€β”€ install-awscli.sh      # AWS CLI setup in containers
β”‚   β”œβ”€β”€ install-kubectl.sh     # Kubectl installation
β”‚   β”œβ”€β”€ kube-init.sh           # Cluster connection bootstrap
β”‚   β”œβ”€β”€ namespace_handler.py   # K8s namespace management
β”‚   └── test-runner.py         # Core test execution logic

β”œβ”€β”€ app/                       # Example FastAPI service (test subject)
β”‚   β”œβ”€β”€ main.py                # Sample API endpoints
β”‚   β”œβ”€β”€ tests/                 # Test cases for example service
β”‚   β”‚   β”œβ”€β”€ test_success.py    # Valid test cases
β”‚   β”‚   └── test_fail.py       # Intentional failure cases
β”‚   └── pyproject.toml         # Python project config

β”œβ”€β”€ internal/                  # Go server implementation
β”‚   β”œβ”€β”€ handlers/              # WebSocket connection handling
β”‚   β”‚   └── websocket.go       # WS message processing
β”‚   β”œβ”€β”€ models/                # Data structures
β”‚   β”‚   └── message.go         # WS message formats
β”‚   └── services/              # Core business logic
β”‚       β”œβ”€β”€ namespace_service.go # K8s ns operations
β”‚       └── test_service.go    # Test execution orchestration

β”œβ”€β”€ cmd/                       # Server entrypoints
β”‚   └── server/
β”‚       └── main.go            # WebSocket server main

β”œβ”€β”€ config/                    # Configuration files
β”‚   β”œβ”€β”€ config.json            # Active configuration
β”‚   └── config.example.json    # Configuration template

β”œβ”€β”€ docker/                    # Container definitions
β”‚   β”œβ”€β”€ Dockerfile             # Main application image
β”‚   └── docker-compose.yml     # Local development setup

└── .github/
    └── workflows/             # CI/CD pipelines
        └── cd.yml             # Deployment automation

πŸ›  Prerequisites

  • Kubernetes cluster (Docker Desktop K8s works)
  • kubectl configured and working
  • Python 3.9+ (client side)
  • Go 1.18+ (server side)

πŸš€ Quick Start

1. Clone and Prepare

git clone https://github.com/obiMadu/backend.im-infra
cd backend.im-infra

2. Server Setup

# Install dependencies
make deps

# Start server (port 8080)
make run

3. Client Setup

python3 -m venv .venv
source .venv/bin/activate
pip install -r app/requirements.txt

# Copy and edit config
cp config.example.json config.json
nano config.json  # Set your repo and commits

4. Run Tests

python3 scripts/client.py

βš™οΈ Configuration

config.json

{
  "ws_url": "ws://your-server-url/ws",
  "repo_url": "https://github.com/your/repo.git",
  "user_id": "your-user-id",
  "chat_id": "your-chat-id",
  "project_type": "fastapi",
  "test_command": "pytest -v tests/network/",
  "commits": ["HEAD~2", "HEAD~1", "HEAD"]
}

πŸ”₯ Custom Test Commands

Override default test behavior in config.json:

{
  "test_command": "mvn test -Dtest=SmokeTestSuite"
}

Supported formats:

  • Simple commands: npm test
  • Chained commands: make test && coverage report
  • Script paths: bash tests/e2e.sh

🚨 Troubleshooting

"Missing deployment template" Error

Create required K8s templates:

mkdir -p deployments/templates/fastapi/
# Add your test-pod.yaml here

WebSocket Connection Issues

# Verify server is running
curl -I http://localhost:8080/health

# Check firewall rules
sudo ufw allow 8080/tcp

K8s Pod Failing

Check pod logs:

kubectl logs -n im-chat-14-user-14 test-pod

πŸ“‹ Test Flow Breakdown

  1. Client connects via WebSocket
  2. Server creates isolated K8s namespace
  3. Test pod deploys with project-specific template
  4. Repo is cloned/updated in pod
  5. Target commit is checked out
  6. Custom test command executes
  7. Results stream back via WebSocket

⁉️ FAQ

Q: How are resources cleaned up?
Namespaces persist for debugging. Manual cleanup:

kubectl delete namespace im-chat-14-user-14

Q: Can I modify the test pod?
Edit templates in deployments/templates/<project-type>/test-pod.yaml

Q: What's the performance overhead?
Each test run uses ~300MB RAM. Scale namespaces as needed.

πŸ“œ License

AGPL-3.0 - Fuck around and find out edition

About

Automated Commit Testing and Deployment System for BackendIM

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors