-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nfs.py
More file actions
203 lines (184 loc) · 9.29 KB
/
Copy pathtest_nfs.py
File metadata and controls
203 lines (184 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import asyncio
import os
from nasty.context import TestContext
from nasty.output import info
from nasty.shell import run
N = 5 # subvolumes per run
S = 2 # snapshots per subvolume
async def test_nfs(ctx: TestContext):
"""Create N NFS shares, mount all, write/read, take S snapshots each, cleanup."""
from nasty.output import header
header(f"NFS Tests (x{N} concurrent)")
sv_names = [f"test-nfs{i}-{ctx.tag}" for i in range(1, N + 1)]
mount_points = [f"/tmp/nasty-test-nfs{i}-{ctx.tag}" for i in range(1, N + 1)]
share_ids = [None] * N
svs = [None] * N
mounted = [False] * N
clone_names = [f"test-nfs{i+1}-clone-{ctx.tag}" for i in range(N)]
clone_share_ids = [None] * N
clone_mounts = [f"/tmp/nasty-test-nfs{i+1}-clone-{ctx.tag}" for i in range(N)]
clone_mounted = [False] * N
try:
if ctx.remount:
# ── Remount: look up existing subvolumes by name ──────
all_svs = await ctx.client.call("subvolume.list", {"filesystem": ctx.pool})
for i in range(N):
sv = next((s for s in all_svs if s["name"] == sv_names[i]), None)
if sv:
svs[i] = sv
else:
ctx.record(f"NFS[{i+1}]: subvolume found", False, f"'{sv_names[i]}' not found")
else:
# ── Create subvolumes + shares ────────────────────────
for i in range(N):
label = f"NFS[{i+1}]"
info(f"Creating filesystem subvolume '{sv_names[i]}'...")
svs[i] = await ctx.client.call("subvolume.create", {
"filesystem": ctx.pool,
"name": sv_names[i],
"subvolume_type": "filesystem",
"volsize_bytes": 524288000,
})
ctx.record(f"{label}: subvolume created", True)
info(f"Creating NFS share for {sv_names[i]}...")
share = await ctx.client.call("share.nfs.create", {
"path": svs[i]["path"],
"clients": [{"host": "*", "options": "rw,sync,no_subtree_check,no_root_squash"}],
})
share_ids[i] = share["id"]
ctx.record(f"{label}: share created", True)
# ── Mount ─────────────────────────────────────────────────
for i in range(N):
if svs[i] is None:
continue
label = f"NFS[{i+1}]"
info(f"Mounting NFS share at {mount_points[i]}...")
os.makedirs(mount_points[i], exist_ok=True)
r = run(["mount", "-t", "nfs4", f"{ctx.host}:{svs[i]['path']}", mount_points[i]], check=False)
if r.returncode != 0:
ctx.record(f"{label}: mount", False, r.stderr.strip())
else:
mounted[i] = True
ctx.record(f"{label}: mount", True)
# ── Write ─────────────────────────────────────────────────
if not ctx.remount:
for i in range(N):
if not mounted[i]:
continue
test_data = f"nasty-nfs-test{i+1}-{ctx.tag}"
with open(os.path.join(mount_points[i], "testfile.txt"), "w") as f:
f.write(test_data)
ctx.record(f"NFS[{i+1}]: write", True)
# ── Read/verify ───────────────────────────────────────────
for i in range(N):
if not mounted[i]:
continue
expected = f"nasty-nfs-test{i+1}-{ctx.tag}"
with open(os.path.join(mount_points[i], "testfile.txt")) as f:
got = f.read()
ctx.record(f"NFS[{i+1}]: read/verify", got == expected,
"" if got == expected else f"expected '{expected}', got '{got}'")
if ctx.remount:
return
# ── Snapshots ─────────────────────────────────────────────
snap_names = [[f"snap-nfs{i+1}-s{j+1}-{ctx.tag}" for j in range(S)] for i in range(N)]
for i in range(N):
for j in range(S):
label = f"NFS[{i+1}]"
info(f"Creating snapshot '{snap_names[i][j]}' of '{sv_names[i]}'...")
try:
await ctx.client.call("snapshot.create", {
"filesystem": ctx.pool,
"subvolume": sv_names[i],
"name": snap_names[i][j],
"read_only": True,
})
ctx.record(f"{label}: snapshot {j+1} created", True)
except Exception as e:
ctx.record(f"{label}: snapshot {j+1} created", False, str(e))
snapshots = await ctx.client.call("snapshot.list", {"filesystem": ctx.pool})
for i in range(N):
for j in range(S):
found = any(s["name"] == snap_names[i][j] and s["subvolume"] == sv_names[i]
for s in snapshots)
ctx.record(f"NFS[{i+1}]: snapshot {j+1} listed", found,
"" if found else f"'{snap_names[i][j]}' not found")
# ── Clone ─────────────────────────────────────────────────
for i in range(N):
label = f"NFS[{i+1}] clone"
info(f"Cloning '{snap_names[i][0]}' → '{clone_names[i]}'...")
try:
clone = await ctx.client.call("snapshot.clone", {
"filesystem": ctx.pool,
"subvolume": sv_names[i],
"snapshot": snap_names[i][0],
"new_name": clone_names[i],
})
ctx.record(f"{label}: created", True)
except Exception as e:
ctx.record(f"{label}: created", False, str(e))
continue
try:
share = await ctx.client.call("share.nfs.create", {
"path": clone["path"],
"clients": [{"host": "*", "options": "rw,sync,no_subtree_check,no_root_squash"}],
})
clone_share_ids[i] = share["id"]
except Exception as e:
ctx.record(f"{label}: read/verify", False, f"share create: {e}")
continue
os.makedirs(clone_mounts[i], exist_ok=True)
r = run(["mount", "-t", "nfs4", f"{ctx.host}:{clone['path']}", clone_mounts[i]], check=False)
if r.returncode != 0:
ctx.record(f"{label}: read/verify", False, f"mount: {r.stderr.strip()}")
continue
clone_mounted[i] = True
expected = f"nasty-nfs-test{i+1}-{ctx.tag}"
try:
with open(os.path.join(clone_mounts[i], "testfile.txt")) as f:
got = f.read()
ctx.record(f"{label}: read/verify", got == expected,
"" if got == expected else f"expected '{expected}', got '{got}'")
except Exception as e:
ctx.record(f"{label}: read/verify", False, str(e))
if not ctx.skip_delete:
for i in range(N):
for j in range(S):
try:
await ctx.client.call("snapshot.delete", {
"filesystem": ctx.pool, "subvolume": sv_names[i], "name": snap_names[i][j],
})
ctx.record(f"NFS[{i+1}]: snapshot {j+1} deleted", True)
except Exception as e:
ctx.record(f"NFS[{i+1}]: snapshot {j+1} deleted", False, str(e))
except Exception as e:
ctx.record("NFS: test", False, str(e))
finally:
for i in range(N):
if clone_mounted[i]:
run(["umount", clone_mounts[i]], check=False)
if os.path.isdir(clone_mounts[i]):
os.rmdir(clone_mounts[i])
if mounted[i]:
run(["umount", mount_points[i]], check=False)
if os.path.isdir(mount_points[i]):
os.rmdir(mount_points[i])
if not ctx.skip_delete:
if clone_share_ids[i]:
try:
await ctx.client.call("share.nfs.delete", {"id": clone_share_ids[i]})
except Exception:
pass
try:
await ctx.client.call("subvolume.delete", {"filesystem": ctx.pool, "name": clone_names[i]})
except Exception:
pass
if share_ids[i]:
try:
await ctx.client.call("share.nfs.delete", {"id": share_ids[i]})
except Exception:
pass
try:
await ctx.client.call("subvolume.delete", {"filesystem": ctx.pool, "name": sv_names[i]})
except Exception:
pass