Skip to content

Commit 3a0626f

Browse files
committed
Fix teuthology.log file exists and add test cases
1 parent b48f79f commit 3a0626f

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

ceph_devstack/cli.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,20 @@ def teuthology_logs(data_path:str) -> int:
7878
logger.error("No jobs found!")
7979
return 1
8080
# check if only one job present, then display logs. Also check if the teuthology.log file exists in the latest run directory
81-
if len(latest_run_subdir) == 1 and os.path.exists(latest_run_subdir[0] + "/teuthology.log") and os.path.isfile(latest_run_subdir[0] + "/teuthology.log"):
82-
try:
83-
if config["args"].get("log_file", False):
84-
print(f"Log file path: {latest_run_subdir[0]}/teuthology.log")
81+
if len(latest_run_subdir) == 1 :
82+
if os.path.exists(latest_run_subdir[0] + "/teuthology.log") and os.path.isfile(latest_run_subdir[0] + "/teuthology.log"):
83+
try:
84+
if config["args"].get("log_file", False):
85+
print(f"Log file path: {latest_run_subdir[0]}/teuthology.log")
86+
return 0
87+
with open(latest_run_subdir[0] + "/teuthology.log", 'r') as f:
88+
ttypager(f.read())
8589
return 0
86-
with open(latest_run_subdir[0] + "/teuthology.log", 'r') as f:
87-
ttypager(f.read())
88-
return 0
89-
except :
90-
logger.error("No logs found!")
90+
except :
91+
logger.error("Error while reading teuthology.log!")
92+
return 1
93+
else:
94+
logger.error("teuthology.log file not found!")
9195
return 1
9296

9397
# Multiple jobs present, then display the job ids
@@ -110,6 +114,9 @@ def teuthology_logs(data_path:str) -> int:
110114
with open(latest_run +"/"+ job_id +"/teuthology.log", 'r') as f:
111115
ttypager(f.read())
112116
except :
113-
logger.error("Error reading the logs!")
117+
logger.error("Error while reading teuthology.log!!")
114118
return 1
119+
else:
120+
logger.error("teuthology.log file not found!")
121+
return 1
115122
return 0

ceph_devstack/resources/test/test_logs.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
'''
1212
Using pytest paramtrization to test logs command with different
1313
combinations of runs, jobs, selection and if view file path flag is set.
14-
Following parameter combination results in 5*2=10 test cases.
14+
Following parameter combination results in 7*2=14 test cases.
1515
'''
16-
@pytest.mark.parametrize("num_runs,num_jobs,selection",[(0,0,0),(2,0,0),(4,1,0),(4,3,2),(3, 2, 3)])
16+
@pytest.mark.parametrize("num_runs,num_jobs,selection,teuthology_file_present",[(0,0,0,True),(2,0,0,True),(4,1,0,True),(4,1,0,False),(4,3,2,True),((4,3,2,False)),(3, 2, 3,True)])
1717
@pytest.mark.parametrize("flag_set",[ True, False])
18-
def test_teuthology_logs(num_runs:int,num_jobs:int,selection:int, flag_set:bool,capsys:pytest.CaptureFixture,monkeypatch:pytest.MonkeyPatch,caplog:pytest.LogCaptureFixture) -> int:
18+
def test_teuthology_logs(num_runs:int,num_jobs:int,selection:int, flag_set:bool,teuthology_file_present:bool,capsys:pytest.CaptureFixture,monkeypatch:pytest.MonkeyPatch,caplog:pytest.LogCaptureFixture) -> int:
1919
""" This function tests the 'logs' command of ceph-devstack.
2020
2121
Creates a directory structure with random logs and runs the 'logs' command.
@@ -64,13 +64,14 @@ def test_teuthology_logs(num_runs:int,num_jobs:int,selection:int, flag_set:bool,
6464
except Exception as e:
6565
logger.error(f"Error creating directory: {e}")
6666
return 1
67-
try:
68-
with open(data_path+'/archive'+'/root-'+random_date+'-teuthology/'+str(j)+'/teuthology.log', 'w') as f:
69-
random_logs.append(''.join(random.choices(string.ascii_letters, k=200)))
70-
f.write(random_logs[-1])
71-
except Exception as e:
72-
logger.error(f"Error creating file: {e}")
73-
return 1
67+
if teuthology_file_present:
68+
try:
69+
with open(data_path+'/archive'+'/root-'+random_date+'-teuthology/'+str(j)+'/teuthology.log', 'w') as f:
70+
random_logs.append(''.join(random.choices(string.ascii_letters, k=200)))
71+
f.write(random_logs[-1])
72+
except Exception as e:
73+
logger.error(f"Error creating file: {e}")
74+
return 1
7475
runs_dir[data_path+'/archive'+'/root-'+random_date+'-teuthology']=random_logs
7576
try:
7677
with pytest.raises(SystemExit) as main_exit:
@@ -98,6 +99,9 @@ def test_teuthology_logs(num_runs:int,num_jobs:int,selection:int, flag_set:bool,
9899
elif selection not in range(num_jobs):
99100
assert main_exit.value.code == 1
100101
assert "Invalid job id!" in caplog.text
102+
elif not teuthology_file_present:
103+
assert main_exit.value.code == 1
104+
assert "teuthology.log file not found!" in caplog.text
101105
elif flag_set:
102106
assert main_exit.value.code == 0
103107
assert f"Log file path: {runs_dir_list[-1]}/{selection}/teuthology.log" in output

0 commit comments

Comments
 (0)