Production deployment patterns
Graceful shutdown, prod config, rollouts, readiness probes, and ship-with-confidence checks.
How to Build a GitOps Argo CD Sync Mock in Python
Simulate Argo CD-style GitOps deployment sync with Python dataclasses, random success rates, and force-sync retry logic.
import random
import time
from dataclasses import dataclass, field
from typing import List, Dict
@dataclass
class Application:
name: str
source_repo: str
target_revision: str
synced: bool = False
health_status: str = "Healthy"
history: List[Dict] = field(default_factory=list)
def sync(se…
How to Mock a CI Pipeline with Build, Test, and Deploy Stages in Python
Simulate a three-stage CI pipeline (build, test, deploy) in Python with random pass/fail logic, early exit on failure, and measured stage durations.
import time
import random
from dataclasses import dataclass
@dataclass
class StageResult:
name: str
status: str
duration: float
def run_stage(name: str, success_chance: float = 0.9) -> StageResult:
"""Simulate a pipeline stage with random success/failure."""
start = time.time()
time.sleep(r…
How to Mock a Container Registry in Python
Build an in-memory container registry mock with push, tag listing, and manifest retrieval logic for testing deployment tooling.
import json
from collections import defaultdict
class MockRegistry:
def __init__(self):
self.repositories = defaultdict(dict)
def push_image(self, repo: str, tag: str, layers: list[str]) -> None:
self.repositories[repo][tag] = {
"layers": layers,
"size": sum(len(layer…
How to Mock time.sleep in a Python PreStop Hook
This code simulates a Kubernetes PreStop hook that delays shutdown, then mocks time.sleep to verify the hook logic without real delay.
import subprocess
import sys
import time
from unittest.mock import patch
def pre_stop_hook():
"""Simulate a Kubernetes PreStop hook that sleeps before shutdown."""
print("PreStop hook started: delaying shutdown")
time.sleep(3)
print("PreStop hook completed: ready to shutdown")
if __name__ == "__main_…
How to Roll Back to a Previous Image Tag in Python
A dataclass-based mock registry that tracks image tag history and rolls back to the previous tag, useful for deployment rollback logic.
"""Demonstrates a rollback pattern for a Docker-style image tag registry."""
from dataclasses import dataclass, field
@dataclass
class ImageRegistry:
"""A minimal mock registry tracking current tags per image."""
tags: dict[str, list[str]] = field(default_factory=dict)
def push(self, image: str, tag: …
How to Smoke Test a Deployment in Python with unittest.mock
Run a post-deploy smoke test by mocking the deployment status check to verify your health-check logic returns PASS/FAIL.
import unittest
from unittest.mock import Mock, patch
class DeploymentService:
def check_status(self):
return "unknown"
def smoke_test_deploy():
service = DeploymentService()
with patch.object(service, "check_status", return_value="healthy") as mock_check:
status = service.check_status()
…
Browse by section
Each section groups closely related Python snippets.
Production deployment patterns — Python code examples
What you will find here
This page collects production deployment patterns snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.