Skip to content

Commit 77b8416

Browse files
committed
replace tempfile usage with pytest fixture
1 parent 1474b7b commit 77b8416

File tree

1 file changed

+92
-93
lines changed

1 file changed

+92
-93
lines changed

ceph_devstack/resources/test/test_devstack.py

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import io
33
import contextlib
4-
import tempfile
54
import random as rd
65
from datetime import datetime, timedelta
76
import secrets
@@ -51,98 +50,98 @@ def test_get_job_id_throws_toomanyjobsfound_on_more_than_one_job(self):
5150
get_job_id(jobs)
5251
assert exc.value.jobs == jobs
5352

54-
async def test_logs_command_display_log_file_of_latest_run(self):
55-
with tempfile.TemporaryDirectory() as data_dir:
56-
config["data_dir"] = data_dir
57-
f = io.StringIO()
58-
content = "custom log content"
59-
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
60-
forty_days_ago = (datetime.now() - timedelta(days=40)).strftime(
61-
"%Y-%m-%d_%H:%M:%S"
62-
)
63-
64-
self.create_logfile(data_dir, timestamp=now, content=content)
65-
self.create_logfile(data_dir, timestamp=forty_days_ago)
66-
67-
with contextlib.redirect_stdout(f):
68-
devstack = CephDevStack()
69-
await devstack.logs()
70-
assert content in f.getvalue()
71-
72-
async def test_logs_display_roughly_contents_of_log_file(self):
73-
with tempfile.TemporaryDirectory() as data_dir:
74-
config["data_dir"] = data_dir
75-
f = io.StringIO()
76-
content = "".join(
77-
secrets.choice(string.ascii_letters + string.digits)
78-
for _ in range(6 * 8 * 1024)
79-
)
80-
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
81-
self.create_logfile(data_dir, timestamp=now, content=content)
82-
83-
with contextlib.redirect_stdout(f):
84-
devstack = CephDevStack()
85-
await devstack.logs()
86-
assert content == f.getvalue()
87-
88-
async def test_logs_command_display_log_file_of_given_job_id(self):
89-
with tempfile.TemporaryDirectory() as data_dir:
90-
config["data_dir"] = data_dir
91-
f = io.StringIO()
92-
content = "custom log message"
93-
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
94-
95-
self.create_logfile(
96-
data_dir,
97-
timestamp=now,
98-
test_type="ceph",
99-
job_id="1",
100-
content="another log",
101-
)
102-
self.create_logfile(
103-
data_dir, timestamp=now, test_type="ceph", job_id="2", content=content
104-
)
105-
106-
with contextlib.redirect_stdout(f):
107-
devstack = CephDevStack()
108-
await devstack.logs(job_id="2")
109-
assert content in f.getvalue()
110-
111-
async def test_logs_display_content_of_provided_run_name(self):
112-
with tempfile.TemporaryDirectory() as data_dir:
113-
config["data_dir"] = data_dir
114-
f = io.StringIO()
115-
content = "custom content"
116-
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
117-
three_days_ago = (datetime.now() - timedelta(days=3)).strftime(
118-
"%Y-%m-%d_%H:%M:%S"
119-
)
120-
121-
self.create_logfile(
122-
data_dir,
123-
timestamp=now,
124-
)
125-
run_name = self.create_logfile(
126-
data_dir,
127-
timestamp=three_days_ago,
128-
content=content,
129-
).split("/")[-3]
130-
131-
with contextlib.redirect_stdout(f):
132-
devstack = CephDevStack()
133-
await devstack.logs(run_name=run_name)
134-
assert content in f.getvalue()
135-
136-
async def test_logs_locate_display_file_path_instead_of_config(self):
137-
with tempfile.TemporaryDirectory() as data_dir:
138-
config["data_dir"] = data_dir
139-
f = io.StringIO()
140-
141-
logfile = self.create_logfile(data_dir)
142-
with contextlib.redirect_stdout(f):
143-
devstack = CephDevStack()
144-
await devstack.logs(locate=True)
145-
assert logfile in f.getvalue()
53+
async def test_logs_command_display_log_file_of_latest_run(self, tmp_path):
54+
data_dir = str(tmp_path)
55+
config["data_dir"] = data_dir
56+
f = io.StringIO()
57+
content = "custom log content"
58+
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
59+
forty_days_ago = (datetime.now() - timedelta(days=40)).strftime(
60+
"%Y-%m-%d_%H:%M:%S"
61+
)
62+
63+
self.create_logfile(data_dir, timestamp=now, content=content)
64+
self.create_logfile(data_dir, timestamp=forty_days_ago)
65+
66+
with contextlib.redirect_stdout(f):
67+
devstack = CephDevStack()
68+
await devstack.logs()
69+
assert content in f.getvalue()
70+
71+
async def test_logs_display_roughly_contents_of_log_file(self, tmp_path):
72+
data_dir = str(tmp_path)
73+
config["data_dir"] = data_dir
74+
f = io.StringIO()
75+
content = "".join(
76+
secrets.choice(string.ascii_letters + string.digits)
77+
for _ in range(6 * 8 * 1024)
78+
)
79+
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
80+
self.create_logfile(data_dir, timestamp=now, content=content)
81+
82+
with contextlib.redirect_stdout(f):
83+
devstack = CephDevStack()
84+
await devstack.logs()
85+
assert content == f.getvalue()
86+
87+
async def test_logs_command_display_log_file_of_given_job_id(self, tmp_path):
88+
data_dir = str(tmp_path)
89+
config["data_dir"] = data_dir
90+
f = io.StringIO()
91+
content = "custom log message"
92+
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
93+
94+
self.create_logfile(
95+
data_dir,
96+
timestamp=now,
97+
test_type="ceph",
98+
job_id="1",
99+
content="another log",
100+
)
101+
self.create_logfile(
102+
data_dir, timestamp=now, test_type="ceph", job_id="2", content=content
103+
)
104+
105+
with contextlib.redirect_stdout(f):
106+
devstack = CephDevStack()
107+
await devstack.logs(job_id="2")
108+
assert content in f.getvalue()
109+
110+
async def test_logs_display_content_of_provided_run_name(self, tmp_path):
111+
data_dir = str(tmp_path)
112+
config["data_dir"] = data_dir
113+
f = io.StringIO()
114+
content = "custom content"
115+
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
116+
three_days_ago = (datetime.now() - timedelta(days=3)).strftime(
117+
"%Y-%m-%d_%H:%M:%S"
118+
)
119+
120+
self.create_logfile(
121+
data_dir,
122+
timestamp=now,
123+
)
124+
run_name = self.create_logfile(
125+
data_dir,
126+
timestamp=three_days_ago,
127+
content=content,
128+
).split("/")[-3]
129+
130+
with contextlib.redirect_stdout(f):
131+
devstack = CephDevStack()
132+
await devstack.logs(run_name=run_name)
133+
assert content in f.getvalue()
134+
135+
async def test_logs_locate_display_file_path_instead_of_config(self, tmp_path):
136+
data_dir = str(tmp_path)
137+
138+
config["data_dir"] = data_dir
139+
f = io.StringIO()
140+
logfile = self.create_logfile(data_dir)
141+
with contextlib.redirect_stdout(f):
142+
devstack = CephDevStack()
143+
await devstack.logs(locate=True)
144+
assert logfile in f.getvalue()
146145

147146
def create_logfile(self, data_dir: str, **kwargs):
148147
parts = {

0 commit comments

Comments
 (0)