chore: initialize containerized Python learning workbook (2026 DevOps/SRE)
This commit is contained in:
commit
ae4c421815
63 files changed
+1273
No files matched your search
@@ -0,0 +1,11 @@
|
||||
# 03 — Automatyzacja DevOps/SRE (YAML, analiza logów, retry)
|
||||
|
||||
## Zadania (`devops_ops.py`)
|
||||
1. `extract_k8s_images(manifest_yaml: str) -> list[str]` — wyciągnij nazwy obrazów (`image`) z kontenerów w manifeście Deployment YAML (`yaml.safe_load`).
|
||||
2. `count_error_lines(log_text: str) -> int` — policz linie zawierające poziom `ERROR` lub `CRITICAL`.
|
||||
3. `retry_call(fn: Callable[[], T], attempts: int) -> T` — ponawiaj wywołanie do `attempts` razy przy wyjątku `RuntimeError`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=03-ecosystems/03-devops-automation
|
||||
```
|
||||
@@ -0,0 +1,18 @@
|
||||
from collections.abc import Callable
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
def extract_k8s_images(manifest_yaml: str) -> list[str]:
|
||||
_ = yaml
|
||||
raise NotImplementedError(
|
||||
"Użyj yaml.safe_load i wyciągnij image z spec.template.spec.containers"
|
||||
)
|
||||
|
||||
|
||||
def count_error_lines(log_text: str) -> int:
|
||||
raise NotImplementedError("Policz linie zawierające 'ERROR' lub 'CRITICAL'")
|
||||
|
||||
|
||||
def retry_call[T](fn: Callable[[], T], attempts: int) -> T:
|
||||
raise NotImplementedError("Wywołuj fn() do attempts razy przy RuntimeError")
|
||||
@@ -0,0 +1,46 @@
|
||||
import pytest
|
||||
from devops_ops import count_error_lines, extract_k8s_images, retry_call
|
||||
|
||||
SAMPLE_DEPLOYMENT = """
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: web
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: app
|
||||
image: ghcr.io/org/web:1.2.0
|
||||
- name: sidecar
|
||||
image: envoyproxy/envoy:v1.31
|
||||
"""
|
||||
|
||||
|
||||
def test_extract_k8s_images() -> None:
|
||||
assert extract_k8s_images(SAMPLE_DEPLOYMENT) == [
|
||||
"ghcr.io/org/web:1.2.0",
|
||||
"envoyproxy/envoy:v1.31",
|
||||
]
|
||||
|
||||
|
||||
def test_count_error_lines() -> None:
|
||||
logs = "INFO start\nWARN retry\nERROR db timeout\nCRITICAL oom\n"
|
||||
assert count_error_lines(logs) == 2
|
||||
|
||||
|
||||
def test_retry_call() -> None:
|
||||
calls = 0
|
||||
|
||||
def flaky() -> str:
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
if calls < 3:
|
||||
raise RuntimeError("transient")
|
||||
return "ok"
|
||||
|
||||
assert retry_call(flaky, attempts=3) == "ok"
|
||||
assert calls == 3
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
retry_call(lambda: (_ for _ in ()).throw(RuntimeError("always")), attempts=2)
|
||||
Reference in new issue
Block a user