Skip to content

Commit a2d41b1

Browse files
committed
make create_log_file method pytest fixture
1 parent 31bd3a9 commit a2d41b1

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

ceph_devstack/resources/ceph/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ async def logs(
237237
except FileNotFoundError:
238238
logger.error("No log file found")
239239
except TooManyJobsFound as e:
240-
msg = "Found too many jobs: {jobs} for target run. Please pick a job id with -j flag.".format(
240+
msg = "Found too many jobs ({jobs}) for target run. Please pick a job id with -j flag.".format(
241241
jobs=", ".join(e.jobs)
242242
)
243243
logger.error(msg)

ceph_devstack/resources/test/test_devstack.py

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def test_get_job_id_throws_toomanyjobsfound_on_more_than_one_job(self):
5050
get_job_id(jobs)
5151
assert exc.value.jobs == jobs
5252

53-
async def test_logs_command_display_log_file_of_latest_run(self, tmp_path):
53+
async def test_logs_command_display_log_file_of_latest_run(
54+
self, tmp_path, create_log_file
55+
):
5456
data_dir = str(tmp_path)
5557
config["data_dir"] = data_dir
5658
f = io.StringIO()
@@ -60,15 +62,17 @@ async def test_logs_command_display_log_file_of_latest_run(self, tmp_path):
6062
"%Y-%m-%d_%H:%M:%S"
6163
)
6264

63-
self.create_log_file(data_dir, timestamp=now, content=content)
64-
self.create_log_file(data_dir, timestamp=forty_days_ago)
65+
create_log_file(data_dir, timestamp=now, content=content)
66+
create_log_file(data_dir, timestamp=forty_days_ago)
6567

6668
with contextlib.redirect_stdout(f):
6769
devstack = CephDevStack()
6870
await devstack.logs()
6971
assert content in f.getvalue()
7072

71-
async def test_logs_display_roughly_contents_of_log_file(self, tmp_path):
73+
async def test_logs_display_roughly_contents_of_log_file(
74+
self, tmp_path, create_log_file
75+
):
7276
data_dir = str(tmp_path)
7377
config["data_dir"] = data_dir
7478
f = io.StringIO()
@@ -77,28 +81,30 @@ async def test_logs_display_roughly_contents_of_log_file(self, tmp_path):
7781
for _ in range(6 * 8 * 1024)
7882
)
7983
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
80-
self.create_log_file(data_dir, timestamp=now, content=content)
84+
create_log_file(data_dir, timestamp=now, content=content)
8185

8286
with contextlib.redirect_stdout(f):
8387
devstack = CephDevStack()
8488
await devstack.logs()
8589
assert content == f.getvalue()
8690

87-
async def test_logs_command_display_log_file_of_given_job_id(self, tmp_path):
91+
async def test_logs_command_display_log_file_of_given_job_id(
92+
self, tmp_path, create_log_file
93+
):
8894
data_dir = str(tmp_path)
8995
config["data_dir"] = data_dir
9096
f = io.StringIO()
9197
content = "custom log message"
9298
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
9399

94-
self.create_log_file(
100+
create_log_file(
95101
data_dir,
96102
timestamp=now,
97103
test_type="ceph",
98104
job_id="1",
99105
content="another log",
100106
)
101-
self.create_log_file(
107+
create_log_file(
102108
data_dir, timestamp=now, test_type="ceph", job_id="2", content=content
103109
)
104110

@@ -107,7 +113,9 @@ async def test_logs_command_display_log_file_of_given_job_id(self, tmp_path):
107113
await devstack.logs(job_id="2")
108114
assert content in f.getvalue()
109115

110-
async def test_logs_display_content_of_provided_run_name(self, tmp_path):
116+
async def test_logs_display_content_of_provided_run_name(
117+
self, tmp_path, create_log_file
118+
):
111119
data_dir = str(tmp_path)
112120
config["data_dir"] = data_dir
113121
f = io.StringIO()
@@ -117,11 +125,11 @@ async def test_logs_display_content_of_provided_run_name(self, tmp_path):
117125
"%Y-%m-%d_%H:%M:%S"
118126
)
119127

120-
self.create_log_file(
128+
create_log_file(
121129
data_dir,
122130
timestamp=now,
123131
)
124-
run_name = self.create_log_file(
132+
run_name = create_log_file(
125133
data_dir,
126134
timestamp=three_days_ago,
127135
content=content,
@@ -132,37 +140,43 @@ async def test_logs_display_content_of_provided_run_name(self, tmp_path):
132140
await devstack.logs(run_name=run_name)
133141
assert content in f.getvalue()
134142

135-
async def test_logs_locate_display_file_path_instead_of_config(self, tmp_path):
143+
async def test_logs_locate_display_file_path_instead_of_config(
144+
self, tmp_path, create_log_file
145+
):
136146
data_dir = str(tmp_path)
137147

138148
config["data_dir"] = data_dir
139149
f = io.StringIO()
140-
log_file = self.create_log_file(data_dir)
150+
log_file = create_log_file(data_dir)
141151
with contextlib.redirect_stdout(f):
142152
devstack = CephDevStack()
143153
await devstack.logs(locate=True)
144154
assert log_file in f.getvalue()
145155

146-
def create_log_file(self, data_dir: str, **kwargs):
147-
parts = {
148-
"timestamp": (datetime.now() - timedelta(days=rd.randint(1, 100))).strftime(
149-
"%Y-%m-%d_%H:%M:%S"
150-
),
151-
"test_type": rd.choice(["ceph", "rgw", "rbd", "mds"]),
152-
"job_id": rd.randint(1, 100),
153-
"content": "some log data",
154-
**kwargs,
155-
}
156-
timestamp = parts["timestamp"]
157-
test_type = parts["test_type"]
158-
job_id = parts["job_id"]
159-
content = parts["content"]
160-
161-
run_name = f"root-{timestamp}-orch:cephadm:{test_type}-small-main-distro-default-testnode"
162-
log_dir = f"{data_dir}/archive/{run_name}/{job_id}"
163-
164-
os.makedirs(log_dir, exist_ok=True)
165-
log_file = f"{log_dir}/teuthology.log"
166-
with open(log_file, "w") as f:
167-
f.write(content)
168-
return log_file
156+
@pytest.fixture(scope="class")
157+
def create_log_file(self):
158+
def _create_log_file(data_dir: str, **kwargs):
159+
parts = {
160+
"timestamp": (
161+
datetime.now() - timedelta(days=rd.randint(1, 100))
162+
).strftime("%Y-%m-%d_%H:%M:%S"),
163+
"test_type": rd.choice(["ceph", "rgw", "rbd", "mds"]),
164+
"job_id": rd.randint(1, 100),
165+
"content": "some log data",
166+
**kwargs,
167+
}
168+
timestamp = parts["timestamp"]
169+
test_type = parts["test_type"]
170+
job_id = parts["job_id"]
171+
content = parts["content"]
172+
173+
run_name = f"root-{timestamp}-orch:cephadm:{test_type}-small-main-distro-default-testnode"
174+
log_dir = f"{data_dir}/archive/{run_name}/{job_id}"
175+
176+
os.makedirs(log_dir, exist_ok=True)
177+
log_file = f"{log_dir}/teuthology.log"
178+
with open(log_file, "w") as f:
179+
f.write(content)
180+
return log_file
181+
182+
return _create_log_file

0 commit comments

Comments
 (0)