-
Notifications
You must be signed in to change notification settings - Fork 9
Implements logs command #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
exkson
wants to merge
13
commits into
zmc:main
Choose a base branch
from
exkson:gsoc-2/implements-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f4028dd
wip: add some behavior tests
exkson 00570f0
separate concerns to add more tests
exkson 2726844
fix conflicts with rebase
exkson 182ece3
add short flag for parser args
exkson 4c44e5f
add small improvements
exkson 4a7b2a3
wip: fix break line display in logs dump
exkson 7cb3b5e
replace os.path usage by Path methods
exkson a014e78
provide more descriptive message on multiple jobs found
exkson 1474b7b
correct test
exkson 77b8416
replace tempfile usage with pytest fixture
exkson 31bd3a9
replace logfile uses with log_file
exkson a2d41b1
make create_log_file method pytest fixture
exkson 7c8b43b
improve toomanyjobsfound error message
exkson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class TooManyJobsFound(Exception): | ||
def __init__(self, jobs: list[str]): | ||
self.jobs = jobs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import re | ||
from datetime import datetime | ||
|
||
from ceph_devstack.resources.ceph.exceptions import TooManyJobsFound | ||
|
||
RUN_DIRNAME_PATTERN = re.compile( | ||
r"^(?P<username>^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}))-(?P<timestamp>\d{4}-\d{2}-\d{2}_\d{2}:\d{2}:\d{2})" | ||
) | ||
|
||
|
||
def get_logtimestamp(dirname: str) -> datetime: | ||
match_ = RUN_DIRNAME_PATTERN.search(dirname) | ||
return datetime.strptime(match_.group("timestamp"), "%Y-%m-%d_%H:%M:%S") | ||
|
||
|
||
def get_most_recent_run(runs: list[str]) -> str: | ||
try: | ||
run_name = next( | ||
iter( | ||
sorted( | ||
( | ||
dirname | ||
for dirname in runs | ||
if RUN_DIRNAME_PATTERN.search(dirname) | ||
), | ||
key=lambda dirname: get_logtimestamp(dirname), | ||
reverse=True, | ||
) | ||
) | ||
) | ||
return run_name | ||
except StopIteration: | ||
raise FileNotFoundError | ||
|
||
|
||
def get_job_id(jobs: list[str]): | ||
job_dir_pattern = re.compile(r"^\d+$") | ||
dirs = [d for d in jobs if job_dir_pattern.match(d)] | ||
|
||
if len(dirs) == 0: | ||
raise FileNotFoundError | ||
elif len(dirs) > 1: | ||
raise TooManyJobsFound(dirs) | ||
return dirs[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import os | ||
import io | ||
import contextlib | ||
import random as rd | ||
from datetime import datetime, timedelta | ||
import secrets | ||
import string | ||
|
||
import pytest | ||
|
||
from ceph_devstack import config | ||
from ceph_devstack.resources.ceph.utils import ( | ||
get_logtimestamp, | ||
get_most_recent_run, | ||
get_job_id, | ||
) | ||
from ceph_devstack.resources.ceph.exceptions import TooManyJobsFound | ||
from ceph_devstack.resources.ceph import CephDevStack | ||
|
||
|
||
class TestDevStack: | ||
def test_get_logtimestamp(self): | ||
dirname = "root-2025-03-20_18:34:43-orch:cephadm:smoke-small-main-distro-default-testnode" | ||
assert get_logtimestamp(dirname) == datetime(2025, 3, 20, 18, 34, 43) | ||
|
||
def test_get_most_recent_run_returns_most_recent_run(self): | ||
runs = [ | ||
"root-2024-02-07_12:23:43-orch:cephadm:smoke-small-devlop-distro-smithi-testnode", | ||
"root-2025-02-20_11:23:43-orch:cephadm:smoke-small-devlop-distro-smithi-testnode", | ||
"root-2025-03-20_18:34:43-orch:cephadm:smoke-small-main-distro-default-testnode", | ||
"root-2025-01-18_18:34:43-orch:cephadm:smoke-small-main-distro-default-testnode", | ||
] | ||
assert ( | ||
get_most_recent_run(runs) | ||
== "root-2025-03-20_18:34:43-orch:cephadm:smoke-small-main-distro-default-testnode" | ||
) | ||
|
||
def test_get_job_id_returns_job_on_unique_job(self): | ||
jobs = ["97"] | ||
assert get_job_id(jobs) == "97" | ||
|
||
def test_get_job_id_throws_filenotfound_on_missing_job(self): | ||
jobs = [] | ||
with pytest.raises(FileNotFoundError): | ||
get_job_id(jobs) | ||
|
||
def test_get_job_id_throws_toomanyjobsfound_on_more_than_one_job(self): | ||
jobs = ["1", "2"] | ||
with pytest.raises(TooManyJobsFound) as exc: | ||
get_job_id(jobs) | ||
assert exc.value.jobs == jobs | ||
|
||
async def test_logs_command_display_log_file_of_latest_run( | ||
self, tmp_path, create_log_file | ||
): | ||
data_dir = str(tmp_path) | ||
config["data_dir"] = data_dir | ||
f = io.StringIO() | ||
content = "custom log content" | ||
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S") | ||
forty_days_ago = (datetime.now() - timedelta(days=40)).strftime( | ||
"%Y-%m-%d_%H:%M:%S" | ||
) | ||
|
||
create_log_file(data_dir, timestamp=now, content=content) | ||
create_log_file(data_dir, timestamp=forty_days_ago) | ||
|
||
with contextlib.redirect_stdout(f): | ||
devstack = CephDevStack() | ||
await devstack.logs() | ||
assert content in f.getvalue() | ||
|
||
async def test_logs_display_roughly_contents_of_log_file( | ||
self, tmp_path, create_log_file | ||
): | ||
data_dir = str(tmp_path) | ||
config["data_dir"] = data_dir | ||
f = io.StringIO() | ||
content = "".join( | ||
secrets.choice(string.ascii_letters + string.digits) | ||
for _ in range(6 * 8 * 1024) | ||
) | ||
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S") | ||
create_log_file(data_dir, timestamp=now, content=content) | ||
|
||
with contextlib.redirect_stdout(f): | ||
devstack = CephDevStack() | ||
await devstack.logs() | ||
assert content == f.getvalue() | ||
|
||
async def test_logs_command_display_log_file_of_given_job_id( | ||
self, tmp_path, create_log_file | ||
): | ||
data_dir = str(tmp_path) | ||
config["data_dir"] = data_dir | ||
f = io.StringIO() | ||
content = "custom log message" | ||
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S") | ||
|
||
create_log_file( | ||
data_dir, | ||
timestamp=now, | ||
test_type="ceph", | ||
job_id="1", | ||
content="another log", | ||
) | ||
create_log_file( | ||
data_dir, timestamp=now, test_type="ceph", job_id="2", content=content | ||
) | ||
|
||
with contextlib.redirect_stdout(f): | ||
devstack = CephDevStack() | ||
await devstack.logs(job_id="2") | ||
assert content in f.getvalue() | ||
|
||
async def test_logs_display_content_of_provided_run_name( | ||
self, tmp_path, create_log_file | ||
): | ||
data_dir = str(tmp_path) | ||
config["data_dir"] = data_dir | ||
f = io.StringIO() | ||
content = "custom content" | ||
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S") | ||
three_days_ago = (datetime.now() - timedelta(days=3)).strftime( | ||
"%Y-%m-%d_%H:%M:%S" | ||
) | ||
|
||
create_log_file( | ||
data_dir, | ||
timestamp=now, | ||
) | ||
run_name = create_log_file( | ||
data_dir, | ||
timestamp=three_days_ago, | ||
content=content, | ||
).split("/")[-3] | ||
|
||
with contextlib.redirect_stdout(f): | ||
devstack = CephDevStack() | ||
await devstack.logs(run_name=run_name) | ||
assert content in f.getvalue() | ||
|
||
async def test_logs_locate_display_file_path_instead_of_config( | ||
self, tmp_path, create_log_file | ||
): | ||
data_dir = str(tmp_path) | ||
|
||
config["data_dir"] = data_dir | ||
f = io.StringIO() | ||
log_file = create_log_file(data_dir) | ||
with contextlib.redirect_stdout(f): | ||
devstack = CephDevStack() | ||
await devstack.logs(locate=True) | ||
assert log_file in f.getvalue() | ||
|
||
@pytest.fixture(scope="class") | ||
def create_log_file(self): | ||
def _create_log_file(data_dir: str, **kwargs): | ||
parts = { | ||
"timestamp": ( | ||
datetime.now() - timedelta(days=rd.randint(1, 100)) | ||
).strftime("%Y-%m-%d_%H:%M:%S"), | ||
"test_type": rd.choice(["ceph", "rgw", "rbd", "mds"]), | ||
"job_id": rd.randint(1, 100), | ||
"content": "some log data", | ||
**kwargs, | ||
} | ||
timestamp = parts["timestamp"] | ||
test_type = parts["test_type"] | ||
job_id = parts["job_id"] | ||
content = parts["content"] | ||
|
||
run_name = f"root-{timestamp}-orch:cephadm:{test_type}-small-main-distro-default-testnode" | ||
log_dir = f"{data_dir}/archive/{run_name}/{job_id}" | ||
|
||
os.makedirs(log_dir, exist_ok=True) | ||
log_file = f"{log_dir}/teuthology.log" | ||
with open(log_file, "w") as f: | ||
f.write(content) | ||
return log_file | ||
|
||
return _create_log_file |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.