Files
python-learning/exercises/03-ecosystems/03-devops-automation/test_devops_ops.py
T

47 lines
1.0 KiB
Python

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)