Skip to content

Commit aa8a0ca

Browse files
committed
added chaosk8s.pod.probes.should_be_found_in_logs
Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent 20c8a17 commit aa8a0ca

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

CHANGELOG.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,56 @@
22

33
## [Unreleased][]
44

5-
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-kubernetes/compare/0.38.2...HEAD
5+
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-kubernetes/compare/0.39.0...HEAD
6+
7+
## [0.39.0][] - 2024-05-06
8+
9+
[0.39.0]: https://github.com/chaostoolkit/chaostoolkit-kubernetes/compare/0.39.0...0.39.0
10+
11+
### Added
12+
13+
* Added the `chaosk8s.pod.probes.should_be_found_in_logs` probe to search
14+
into container logs:
15+
16+
```json
17+
{
18+
"title": "Check pod logs",
19+
"description": "n/a",
20+
"steady-state-hypothesis": {
21+
"title": "extract-logs-with-regex",
22+
"probes": [
23+
{
24+
"name": "at-least-one-pod-should-have-restarted",
25+
"type": "probe",
26+
"tolerance": {
27+
"name": "search-probe",
28+
"type": "probe",
29+
"provider": {
30+
"type": "python",
31+
"module": "chaosk8s.pod.probes",
32+
"func": "should_be_found_in_logs",
33+
"arguments": {
34+
"pattern": "startup",
35+
"all_containers": false
36+
}
37+
}
38+
},
39+
"provider": {
40+
"type": "python",
41+
"module": "chaosk8s.pod.probes",
42+
"func": "read_pod_logs",
43+
"arguments": {
44+
"last": "60s",
45+
"label_selector": "app=producer",
46+
"container_name": "producer"
47+
}
48+
}
49+
}
50+
]
51+
},
52+
"method": []
53+
}
54+
```
655

756
## [0.38.2][] - 2024-04-18
857

chaosk8s/pod/probes.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23
from datetime import datetime
34
from typing import Dict, List, Union
45

@@ -17,6 +18,7 @@
1718
"count_pods",
1819
"pod_is_not_available",
1920
"count_min_pods",
21+
"should_be_found_in_logs",
2022
]
2123
logger = logging.getLogger("chaostoolkit")
2224

@@ -370,3 +372,43 @@ def count_min_pods(
370372
label_selector=label_selector, phase=phase, ns=ns, secrets=secrets
371373
)
372374
return count >= min_count
375+
376+
377+
def should_be_found_in_logs(
378+
pattern: str,
379+
all_containers: bool = True,
380+
value: Dict[str, str] = None,
381+
secrets: Secrets = None,
382+
) -> bool:
383+
"""
384+
Lookup for the first occurence of `pattern` in the logs of each container
385+
fetched by the `read_pod_logs` probe.
386+
387+
If `all_containers` is set the match must occur on all continers. Otherwise,
388+
allow for only a subset of containers to match the search.
389+
"""
390+
if not value:
391+
raise ActivityFailed("no logs to search from")
392+
393+
c_pattern = re.compile(pattern)
394+
395+
matched: list[re.Match] = []
396+
397+
for container_name in value:
398+
logs = value[container_name]
399+
m = c_pattern.search(logs)
400+
if m:
401+
logger.debug(
402+
f"Container '{container_name}' matched at position {m.span()}"
403+
)
404+
matched.append(m)
405+
continue
406+
407+
logger.debug(f"Container '{container_name}' did not match")
408+
409+
if all_containers and (len(matched) != len(value)):
410+
return False
411+
elif not matched:
412+
return False
413+
414+
return True

0 commit comments

Comments
 (0)