blob: c3efb2d1ac4ef7df1b5dca608a1efe32dc2f0123 [file] [log] [blame]
Peter Pentchevd00519f2021-11-08 01:17:13 +02001"""Test the sp_rand_cleanup tool."""
2
3import collections
4import errno
5import pathlib
6import tempfile
7
Peter Pencheva8fc47e2022-06-24 12:14:41 +03008from typing import DefaultDict, Dict, List, NamedTuple # noqa: H301
Peter Pentchevd00519f2021-11-08 01:17:13 +02009
10import pytest
11
12from storpool import sptypes # type: ignore
13
14from sp_rand import cleanup as r_cleanup
15from sp_rand import defs as r_defs
16
17
Peter Pencheva8fc47e2022-06-24 12:14:41 +030018class VolSnapData(NamedTuple):
19 """Test data: what volumes and snapshots should be shown as attached."""
20
21 volumes: List[str]
22 snapshots: List[str]
23
24
25class ExportsData(NamedTuple):
26 """Test data: which volumes are exported to an initiator."""
27
28 portal_group: str
29 volume_name: str
30
31
32class TargetData(NamedTuple):
33 """Test data: an iSCSI target record for a StorPool volume."""
34
35 name: str
36 volume: str
37
38
39T_ATTACHED = VolSnapData(
40 volumes=["something-else", "testvol-42"],
41 snapshots=["testvol-9000", "another.snapshot", "testvol-616", "some-testvol-12"],
42)
Peter Pentchev2474e122022-06-23 20:34:43 +030043
44T_EXPORTED = {
45 "beleriand": [
Peter Pencheva8fc47e2022-06-24 12:14:41 +030046 ExportsData(portal_group="neighbors", volume_name="testvol-451"),
47 ExportsData(portal_group="ours", volume_name="something-else"),
48 ExportsData(portal_group="ours", volume_name="testvol-616"),
Peter Pentchev2474e122022-06-23 20:34:43 +030049 ],
Peter Pencheva8fc47e2022-06-24 12:14:41 +030050 "gondor": [ExportsData(portal_group="ours", volume_name="testvol-12")],
Peter Pentchev2474e122022-06-23 20:34:43 +030051}
52
53T_TARGETS = {
Peter Pencheva8fc47e2022-06-24 12:14:41 +030054 1: TargetData(name="ouriqn:target-something-else", volume="something-else"),
55 2: TargetData(name="ouriqn:target-testvol-12", volume="testvol-12"),
56 3: TargetData(name="ouriqn:target-testvol-616", volume="testvol-616"),
57 4: TargetData(name="neiqn:whee-testvol-451", volume="testvol-451"),
Peter Pentchev2474e122022-06-23 20:34:43 +030058}
59
Peter Pencheva8fc47e2022-06-24 12:14:41 +030060T_VOLS = VolSnapData(
61 volumes=["something-else", "testvol-42", "testvol-451"],
62 snapshots=[
Peter Pentchev2474e122022-06-23 20:34:43 +030063 "another.snapshot",
64 "testvol-616",
65 "testvol-9000",
66 "some-testvol-12",
67 "yet.another.thing",
68 ],
Peter Pencheva8fc47e2022-06-24 12:14:41 +030069)
Peter Pentchev2474e122022-06-23 20:34:43 +030070
71
Peter Pentchevd00519f2021-11-08 01:17:13 +020072def test_parse_file() -> None:
73 """Make sure the name prefix file will be parsed correctly."""
74 with tempfile.TemporaryDirectory() as tempd_obj:
75 tempd = pathlib.Path(tempd_obj)
76 cfg = r_cleanup.Config.default()._replace(confdir=tempd)
77
78 conffile = tempd / r_defs.FILENAME
79 conffile.write_text(f"{r_defs.PREFIX_VAR}=testvol-\n", encoding="UTF-8")
80
81 parsed_conffile, prefix = r_cleanup.parse_file(cfg)
82 assert (parsed_conffile, prefix) == (conffile, "testvol-")
83
84
85def do_test_remove_file(noop: bool) -> None:
86 """Make sure the name prefix file will be removed... or not."""
87 with tempfile.TemporaryDirectory() as tempd_obj:
88 tempd = pathlib.Path(tempd_obj)
89 cfg = r_cleanup.Config.default()._replace(confdir=tempd, noop=noop)
90
91 conffile = tempd / r_defs.FILENAME
92 otherfile = tempd / "something.conf"
93
94 otherfile.write_text("Can't touch this!\n", encoding="UTF-8")
95 assert sorted(tempd.iterdir()) == [otherfile]
96
97 # Make sure it does not complain if the file does not exist.
98 r_cleanup.remove_file(cfg, conffile)
99 assert sorted(tempd.iterdir()) == [otherfile]
100
101 conffile.write_text("hello\n", encoding="UTF-8")
102 assert sorted(tempd.iterdir()) == sorted([conffile, otherfile])
103
104 # Make sure it raises an error if it cannot remove the file.
105 tempd.chmod((tempd.stat().st_mode & 0o7777) & 0o7577)
106 try:
107 if noop:
108 r_cleanup.remove_file(cfg, conffile)
109 else:
110 with pytest.raises(OSError) as exc_info:
111 r_cleanup.remove_file(cfg, conffile)
112 assert exc_info.value.errno == errno.EACCES
113 finally:
114 tempd.chmod((tempd.stat().st_mode & 0o7777) | 0o0200)
115 assert sorted(tempd.iterdir()) == sorted([conffile, otherfile])
116
117 # Make sure it removes the file.
118 r_cleanup.remove_file(cfg, conffile)
119 if noop:
120 assert sorted(tempd.iterdir()) == sorted([conffile, otherfile])
121 else:
122 assert sorted(tempd.iterdir()) == [otherfile]
123
124
125def test_remove_file() -> None:
126 """Make sure the name prefix file will be removed."""
127 do_test_remove_file(False)
128
129
130def test_remove_file_noop() -> None:
131 """Make sure the name prefix file will not be removed."""
132 do_test_remove_file(True)
133
134
135class MockVolumeSummary(NamedTuple):
136 """Mock a volume or snapshot description."""
137
138 name: str
139
140
141class MockApi:
142 """Mock the StorPool API."""
143
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300144 attached: VolSnapData
145 exported: Dict[str, List[ExportsData]]
Peter Pentchevd00519f2021-11-08 01:17:13 +0200146 invoked: DefaultDict[str, int]
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300147 targets: Dict[int, TargetData]
148 vols: VolSnapData
Peter Pentchevd00519f2021-11-08 01:17:13 +0200149
150 def __init__(
Peter Pentchev2474e122022-06-23 20:34:43 +0300151 self,
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300152 attached: VolSnapData,
153 exported: Dict[str, List[ExportsData]],
154 targets: Dict[int, TargetData],
155 vols: VolSnapData,
Peter Pentchevd00519f2021-11-08 01:17:13 +0200156 ) -> None:
157 """Store deep copies of the passed definitions."""
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300158 self.attached = VolSnapData(
159 volumes=list(attached.volumes), snapshots=list(attached.snapshots)
160 )
Peter Pentchev2474e122022-06-23 20:34:43 +0300161 self.exported = {key: list(value) for key, value in exported.items()}
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300162 self.targets = dict(targets)
163 self.vols = VolSnapData(
164 volumes=list(vols.volumes), snapshots=list(vols.snapshots)
165 )
Peter Pentchevd00519f2021-11-08 01:17:13 +0200166 self.invoked = collections.defaultdict(lambda: 0)
167
168 # pylint: disable=invalid-name
169
170 def attachmentsList(self) -> List[sptypes.AttachmentDesc]:
171 """Return the current view of what is attached."""
172 self.invoked["attachmentsList"] += 1
173 return [
174 sptypes.AttachmentDesc(
175 volume=name, snapshot=False, client=11, rights="rw", pos=0
176 )
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300177 for name in self.attached.volumes
Peter Pentchevd00519f2021-11-08 01:17:13 +0200178 ] + [
179 sptypes.AttachmentDesc(
180 volume=name, snapshot=True, client=11, rights="ro", pos=0
181 )
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300182 for name in self.attached.snapshots
Peter Pentchevd00519f2021-11-08 01:17:13 +0200183 ]
184
Peter Pentchev2474e122022-06-23 20:34:43 +0300185 def iSCSIConfig(self) -> sptypes.iSCSIConfig:
186 """Return the current view of the iSCSI configuration."""
187 self.invoked["iSCSIConfig"] += 1
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300188 target_dict = {tgt.volume: tgt.name for tgt in self.targets.values()}
Peter Pentchev2474e122022-06-23 20:34:43 +0300189
190 return sptypes.iSCSIConfig(
191 iscsi=sptypes.iSCSIConfigData(
192 baseName="just-us-here",
193 initiators={
194 idx: sptypes.iSCSIInitiator(
195 name=name,
196 username="",
197 secret="",
198 nets=[],
199 exports=[
200 sptypes.iSCSIExport(
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300201 portalGroup=exp.portal_group,
202 target=target_dict[exp.volume_name],
Peter Pentchev2474e122022-06-23 20:34:43 +0300203 )
204 for exp in explist
205 ],
206 )
207 for idx, (name, explist) in enumerate(sorted(self.exported.items()))
208 },
209 portalGroups={},
210 targets={
211 tid: sptypes.iSCSITarget(
212 currentControllerId=65535,
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300213 name=tgt.name,
214 volume=tgt.volume,
Peter Pentchev2474e122022-06-23 20:34:43 +0300215 )
216 for tid, tgt in self.targets.items()
217 },
218 )
219 )
220
221 def iSCSIConfigChange(self, change: sptypes.iSCSIConfigChange) -> None:
222 """Apply the "delete export" and "remove target" commands."""
223 self.invoked["iSCSIConfigChange"] += 1
224
225 for cmd in change.commands:
226 expdel = cmd.exportDelete
227 tgtdel = cmd.deleteTarget
228 if expdel is not None:
229 initiator = self.exported[expdel.initiator]
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300230 data = ExportsData(
231 portal_group=expdel.portalGroup, volume_name=expdel.volumeName
232 )
Peter Pentchev2474e122022-06-23 20:34:43 +0300233 assert data in initiator
234 initiator.remove(data)
235 elif tgtdel is not None:
236 found = [
237 tid
238 for tid, tgt in self.targets.items()
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300239 if tgt.volume == tgtdel.volumeName
Peter Pentchev2474e122022-06-23 20:34:43 +0300240 ]
241 assert len(found) == 1
242 del self.targets[found[0]]
243 else:
244 raise ValueError(repr(cmd))
245
Peter Pentchevd00519f2021-11-08 01:17:13 +0200246 def volumesReassignWait(self, req: sptypes.VolumesReassignWaitDesc) -> None:
247 """Update the current view of what is attached."""
248 self.invoked["volumesReassignWait"] += 1
249
250 for item in req.reassign:
251 if isinstance(item, sptypes.VolumeReassignDesc):
252 assert item.volume.startswith("testvol-")
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300253 self.attached.volumes.remove(item.volume)
Peter Pentchevd00519f2021-11-08 01:17:13 +0200254 else:
255 assert isinstance(item, sptypes.SnapshotReassignDesc)
256 assert item.snapshot.startswith("testvol-")
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300257 self.attached.snapshots.remove(item.snapshot)
Peter Pentchevd00519f2021-11-08 01:17:13 +0200258
259 assert item.detach == [11]
260
261 def volumesList(self) -> List[MockVolumeSummary]:
262 """Return the current view of the available volumes."""
263 self.invoked["volumesList"] += 1
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300264 return [MockVolumeSummary(name=name) for name in self.vols.volumes]
Peter Pentchevd00519f2021-11-08 01:17:13 +0200265
266 def snapshotsList(self) -> List[MockVolumeSummary]:
267 """Return the current view of the available snapshots."""
268 self.invoked["snapshotsList"] += 1
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300269 return [MockVolumeSummary(name=name) for name in self.vols.snapshots]
Peter Pentchevd00519f2021-11-08 01:17:13 +0200270
271 def volumeDelete(self, name: str) -> None:
272 """Remove a volume from our view."""
273 self.invoked["volumeDelete"] += 1
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300274 assert name not in self.attached.volumes
275 self.vols.volumes.remove(name)
Peter Pentchevd00519f2021-11-08 01:17:13 +0200276
277 def snapshotDelete(self, name: str) -> None:
278 """Remove a snapshot from our view."""
279 self.invoked["snapshotDelete"] += 1
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300280 assert name not in self.attached.snapshots
281 self.vols.snapshots.remove(name)
Peter Pentchevd00519f2021-11-08 01:17:13 +0200282
283
284def test_remove_volumes() -> None:
285 """Make sure the StorPool volumes and snapshots will be removed."""
286 cfg = r_cleanup.Config.default()
Peter Pentchev2474e122022-06-23 20:34:43 +0300287 mock_api = MockApi(
288 attached=T_ATTACHED, exported=T_EXPORTED, targets=T_TARGETS, vols=T_VOLS
289 )
Peter Pentchevd00519f2021-11-08 01:17:13 +0200290
291 r_cleanup.remove_volumes(cfg, mock_api, "testvol-")
292
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300293 assert mock_api.attached == VolSnapData(
294 volumes=[
295 name for name in T_ATTACHED.volumes if not name.startswith("testvol-")
296 ],
297 snapshots=[
298 name for name in T_ATTACHED.snapshots if not name.startswith("testvol-")
299 ],
300 )
Peter Pentchev2474e122022-06-23 20:34:43 +0300301 assert mock_api.exported == {
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300302 initiator: [
303 item for item in exports if not item.volume_name.startswith("testvol-")
304 ]
Peter Pentchev2474e122022-06-23 20:34:43 +0300305 for initiator, exports in T_EXPORTED.items()
306 }
307 assert mock_api.targets == {
308 tid: tgt
309 for tid, tgt in T_TARGETS.items()
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300310 if not tgt.volume.startswith("testvol-")
Peter Pentchevd00519f2021-11-08 01:17:13 +0200311 }
Peter Pencheva8fc47e2022-06-24 12:14:41 +0300312 assert mock_api.vols == VolSnapData(
313 volumes=[name for name in T_VOLS.volumes if not name.startswith("testvol-")],
314 snapshots=[
315 name for name in T_VOLS.snapshots if not name.startswith("testvol-")
316 ],
317 )
Peter Pentchevd00519f2021-11-08 01:17:13 +0200318
319 assert mock_api.invoked == {
320 "attachmentsList": 1,
Peter Pentchev2474e122022-06-23 20:34:43 +0300321 "iSCSIConfig": 1,
322 "iSCSIConfigChange": 2,
Peter Pentchevd00519f2021-11-08 01:17:13 +0200323 "volumesReassignWait": 1,
324 "volumesList": 1,
325 "snapshotsList": 1,
326 "volumeDelete": len(
Peter Pentchev2474e122022-06-23 20:34:43 +0300327 [name for name in T_VOLS[False] if name.startswith("testvol-")]
Peter Pentchevd00519f2021-11-08 01:17:13 +0200328 ),
329 "snapshotDelete": len(
Peter Pentchev2474e122022-06-23 20:34:43 +0300330 [name for name in T_VOLS[True] if name.startswith("testvol-")]
Peter Pentchevd00519f2021-11-08 01:17:13 +0200331 ),
332 }
333
334
335def test_remove_volumes_noop() -> None:
336 """Make sure the StorPool volumes and snapshots will be removed."""
337 cfg = r_cleanup.Config.default()._replace(noop=True)
Peter Pentchev2474e122022-06-23 20:34:43 +0300338 mock_api = MockApi(
339 attached=T_ATTACHED, exported=T_EXPORTED, targets=T_TARGETS, vols=T_VOLS
340 )
Peter Pentchevd00519f2021-11-08 01:17:13 +0200341
342 r_cleanup.remove_volumes(cfg, mock_api, "testvol-")
343
Peter Pentchev2474e122022-06-23 20:34:43 +0300344 assert mock_api.attached == T_ATTACHED
345 assert mock_api.exported == T_EXPORTED
346 assert mock_api.targets == T_TARGETS
347 assert mock_api.vols == T_VOLS
Peter Pentchevd00519f2021-11-08 01:17:13 +0200348
349 assert mock_api.invoked == {
350 "attachmentsList": 1,
Peter Pentchev2474e122022-06-23 20:34:43 +0300351 "iSCSIConfig": 1,
Peter Pentchevd00519f2021-11-08 01:17:13 +0200352 "volumesList": 1,
353 "snapshotsList": 1,
354 }