lockss-turtles 0.6.0.dev23__py3-none-any.whl → 0.6.0.dev25__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,411 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # Copyright (c) 2000-2025, Board of Trustees of Leland Stanford Jr. University
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice,
9
+ # this list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # 3. Neither the name of the copyright holder nor the names of its contributors
16
+ # may be used to endorse or promote products derived from this software without
17
+ # specific prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ # POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ # Remove in Python 3.14
32
+ # See https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class/33533514#33533514
33
+ from __future__ import annotations
34
+
35
+ from abc import ABC, abstractmethod
36
+ from pathlib import Path
37
+
38
+ from lockss.turtles.plugin_registry import BasePluginRegistryLayout, DirectoryPluginRegistryLayout, PluginRegistry, PluginRegistryCatalog, PluginRegistryLayer, PluginRegistryLayoutFileNamingConvention, RcsPluginRegistryLayout
39
+
40
+ from . import PydanticTestCase, ROOT
41
+
42
+
43
+ class TestPluginRegistryCatalog(PydanticTestCase):
44
+
45
+ def setUp(self) -> None:
46
+ super().setUp()
47
+ self.valid_absolute = PluginRegistryCatalog(**{'kind': 'PluginRegistryCatalog',
48
+ 'plugin-registry-files': [
49
+ '/tmp/one.yaml',
50
+ ]}).initialize(ROOT)
51
+ self.valid_relative = PluginRegistryCatalog(**{'kind': 'PluginRegistryCatalog',
52
+ 'plugin-registry-files': [
53
+ 'one.yaml',
54
+ ]}).initialize(ROOT)
55
+
56
+ def test_missing_kind(self) -> None:
57
+ self.assertPydanticMissing(lambda: PluginRegistryCatalog(),
58
+ 'kind')
59
+
60
+ def test_null_kind(self) -> None:
61
+ self.assertPydanticLiteralError(lambda: PluginRegistryCatalog(kind=None),
62
+ 'kind',
63
+ 'PluginRegistryCatalog')
64
+
65
+ def test_wrong_kind(self) -> None:
66
+ self.assertPydanticLiteralError(lambda: PluginRegistryCatalog(kind='WrongKind'),
67
+ 'kind',
68
+ 'PluginRegistryCatalog')
69
+
70
+ def test_missing_plugin_registry_files(self) -> None:
71
+ self.assertPydanticMissing(lambda: PluginRegistryCatalog(),
72
+ 'plugin-registry-files')
73
+
74
+ def test_null_plugin_registry_files(self) -> None:
75
+ self.assertPydanticListType(lambda: PluginRegistryCatalog(**{'plugin-registry-files': None}),
76
+ 'plugin-registry-files')
77
+
78
+ def test_empty_plugin_registry_files(self) -> None:
79
+ self.assertPydanticTooShort(lambda: PluginRegistryCatalog(**{'plugin-registry-files': []}),
80
+ 'plugin-registry-files')
81
+
82
+ def test_uninitialized(self) -> None:
83
+ prc = PluginRegistryCatalog(**{'kind': 'PluginRegistryCatalog',
84
+ 'plugin-registry-files': ['whatever']})
85
+ self.assertRaises(ValueError, lambda: prc.get_root())
86
+ self.assertRaises(ValueError, lambda: prc.get_plugin_registry_files())
87
+
88
+ def test_kind(self) -> None:
89
+ self.assertEqual(self.valid_absolute.kind, 'PluginRegistryCatalog')
90
+ self.assertEqual(self.valid_relative.kind, 'PluginRegistryCatalog')
91
+
92
+ def test_get_plugin_registry_files(self) -> None:
93
+ self.assertListEqual(self.valid_absolute.get_plugin_registry_files(),
94
+ [Path('/tmp/one.yaml')])
95
+ self.assertListEqual(self.valid_relative.get_plugin_registry_files(),
96
+ [ROOT.joinpath('one.yaml')])
97
+
98
+
99
+ # Important: see "del _BasePluginRegistryLayoutTestCase" at the end
100
+ class _BasePluginRegistryLayoutTestCase(ABC, PydanticTestCase):
101
+
102
+ def setUp(self) -> None:
103
+ class _FakePluginRegistry:
104
+ def get_root(self) -> Path:
105
+ return ROOT
106
+
107
+ super().setUp()
108
+ self.fake_plugin_registry = _FakePluginRegistry()
109
+ self.valid_identifier = self.instance(**{'type': self.type(),
110
+ 'file-naming-convention': 'identifier'}).initialize(self.fake_plugin_registry)
111
+ self.valid_abbreviated = self.instance(**{'type': self.type(),
112
+ 'file-naming-convention': 'abbreviated'}).initialize(self.fake_plugin_registry)
113
+ self.valid_underscore = self.instance(**{'type': self.type(),
114
+ 'file-naming-convention': 'underscore'}).initialize(self.fake_plugin_registry)
115
+ self.valid = [self.valid_identifier, self.valid_abbreviated, self.valid_underscore]
116
+
117
+ @abstractmethod
118
+ def instance(self, **kwargs) -> BasePluginRegistryLayout:
119
+ pass
120
+
121
+ @abstractmethod
122
+ def type(self) -> str:
123
+ pass
124
+
125
+ def test_missing_type(self) -> None:
126
+ self.assertPydanticMissing(lambda: self.instance(),
127
+ 'type')
128
+
129
+ def test_null_type(self) -> None:
130
+ self.assertPydanticLiteralError(lambda: self.instance(type=None),
131
+ 'type',
132
+ self.type())
133
+
134
+ def test_invalid_type(self) -> None:
135
+ self.assertPydanticLiteralError(lambda: self.instance(type='invalid'),
136
+ 'type',
137
+ self.type())
138
+
139
+ def test_file_naming_conventions(self) -> None:
140
+ self.assertTupleEqual(('abbreviated', 'identifier', 'underscore'),
141
+ PluginRegistryLayoutFileNamingConvention.__args__)
142
+
143
+ def test_default_file_naming_convention(self) -> None:
144
+ self.assertEqual(self.instance(type=self.type()).get_file_naming_convention(),
145
+ 'identifier')
146
+
147
+ def test_null_file_naming_convention(self) -> None:
148
+ self.assertPydanticLiteralError(lambda: self.instance(**{'file-naming-convention': None}),
149
+ 'file-naming-convention',
150
+ PluginRegistryLayoutFileNamingConvention.__args__)
151
+
152
+ def test_invalid_file_naming_convention(self) -> None:
153
+ self.assertPydanticLiteralError(lambda: self.instance(**{'file-naming-convention': 'invalid'}),
154
+ 'file-naming-convention',
155
+ PluginRegistryLayoutFileNamingConvention.__args__)
156
+
157
+ def test_get_type(self) -> None:
158
+ for valid in self.valid:
159
+ self.assertEqual(valid.get_type(), self.type())
160
+
161
+ def test_get_file_naming_convention(self) -> None:
162
+ self.assertEqual(self.valid_identifier.get_file_naming_convention(), 'identifier')
163
+ self.assertEqual(self.valid_abbreviated.get_file_naming_convention(), 'abbreviated')
164
+ self.assertEqual(self.valid_underscore.get_file_naming_convention(), 'underscore')
165
+
166
+ def test_get_dstfile(self) -> None:
167
+ plugid = 'org.myproject.plugin.MyPlugin'
168
+ self.assertEqual(getattr(self.valid_identifier, '_get_dstfile')(plugid),
169
+ 'org.myproject.plugin.MyPlugin.jar')
170
+ self.assertEqual(getattr(self.valid_abbreviated, '_get_dstfile')(plugid),
171
+ 'MyPlugin.jar')
172
+ self.assertEqual(getattr(self.valid_underscore, '_get_dstfile')(plugid),
173
+ 'org_myproject_plugin_MyPlugin.jar')
174
+
175
+ def test_get_plugin_registry(self) -> None:
176
+ for valid in self.valid:
177
+ self.assertEqual(valid.get_plugin_registry(), self.fake_plugin_registry)
178
+
179
+
180
+ class TestDirectoryPluginRegistryLayout(_BasePluginRegistryLayoutTestCase):
181
+
182
+ def instance(self, **kwargs) -> DirectoryPluginRegistryLayout:
183
+ return DirectoryPluginRegistryLayout(**kwargs)
184
+
185
+ def type(self) -> str:
186
+ return 'directory'
187
+
188
+
189
+ class TestRcsPluginRegistryLayout(_BasePluginRegistryLayoutTestCase):
190
+
191
+ def instance(self, **kwargs) -> RcsPluginRegistryLayout:
192
+ return RcsPluginRegistryLayout(**kwargs)
193
+
194
+ def type(self) -> str:
195
+ return 'rcs'
196
+
197
+
198
+ class TestPluginRegistryLayer(PydanticTestCase):
199
+
200
+ def setUp(self) -> None:
201
+ class _FakePluginRegistry:
202
+ def get_root(self) -> Path:
203
+ return ROOT
204
+
205
+ super().setUp()
206
+ self.fake_plugin_registry = _FakePluginRegistry()
207
+ self.valid_absolute = PluginRegistryLayer(id='mylayer',
208
+ name='My Layer',
209
+ path='/tmp/layerdir').initialize(self.fake_plugin_registry)
210
+ self.valid_relative = PluginRegistryLayer(id='mylayer',
211
+ name='My Layer',
212
+ path='layerdir').initialize(self.fake_plugin_registry)
213
+ self.valid = [self.valid_absolute, self.valid_relative]
214
+
215
+ def test_missing_identifier(self) -> None:
216
+ self.assertPydanticMissing(lambda: PluginRegistryLayer(),
217
+ 'id')
218
+
219
+ def test_null_identifier(self) -> None:
220
+ self.assertPydanticStringType(lambda: PluginRegistryLayer(id=None),
221
+ 'id')
222
+
223
+ def test_missing_name(self) -> None:
224
+ self.assertPydanticMissing(lambda: PluginRegistryLayer(),
225
+ 'name')
226
+
227
+ def test_null_name(self) -> None:
228
+ self.assertPydanticStringType(lambda: PluginRegistryLayer(name=None),
229
+ 'name')
230
+
231
+ def test_missing_path(self) -> None:
232
+ self.assertPydanticMissing(lambda: PluginRegistryLayer(),
233
+ 'path')
234
+
235
+ def test_null_path(self) -> None:
236
+ self.assertPydanticStringType(lambda: PluginRegistryLayer(path=None),
237
+ 'path')
238
+
239
+ def test_uninitialized(self) -> None:
240
+ prl = PluginRegistryLayer(id='myid',
241
+ name='My Name',
242
+ path='whatever')
243
+ self.assertRaises(ValueError, lambda: prl.get_plugin_registry())
244
+ self.assertRaises(ValueError, lambda: prl.get_path())
245
+
246
+ def test_get_id(self) -> None:
247
+ for valid in self.valid:
248
+ self.assertEqual(valid.get_id(), 'mylayer')
249
+
250
+ def test_get_name(self) -> None:
251
+ for valid in self.valid:
252
+ self.assertEqual(valid.get_name(), 'My Layer')
253
+
254
+ def test_get_path(self) -> None:
255
+ self.assertEqual(self.valid_absolute.get_path(), Path('/tmp/layerdir'))
256
+ self.assertEqual(self.valid_relative.get_path(), ROOT.joinpath('layerdir'))
257
+
258
+ def test_get_plugin_registry(self) -> None:
259
+ for valid in self.valid:
260
+ self.assertEqual(valid.get_plugin_registry(), self.fake_plugin_registry)
261
+
262
+
263
+ class TestPluginRegistry(PydanticTestCase):
264
+
265
+ def setUp(self) -> None:
266
+ super().setUp()
267
+ self.valid_layout = DirectoryPluginRegistryLayout(type='directory')
268
+ self.valid_layer = PluginRegistryLayer(id='mylayer',
269
+ name='My Layer',
270
+ path='layerpath')
271
+ self.valid = PluginRegistry(**{'kind': 'PluginRegistry',
272
+ 'id': 'myid',
273
+ 'name': 'My Name',
274
+ 'layout': self.valid_layout,
275
+ 'layers': [
276
+ self.valid_layer
277
+ ],
278
+ 'plugin-identifiers': [
279
+ 'org.myproject.plugin.MyPlugin'
280
+ ],
281
+ 'suppressed-plugin-identifiers': [
282
+ 'org.myproject.plugin.BadPlugin'
283
+ ]}).initialize(ROOT)
284
+
285
+ def test_missing_kind(self) -> None:
286
+ self.assertPydanticMissing(lambda: PluginRegistry(),
287
+ 'kind')
288
+
289
+ def test_null_kind(self) -> None:
290
+ self.assertPydanticLiteralError(lambda: PluginRegistry(kind=None),
291
+ 'kind',
292
+ 'PluginRegistry')
293
+
294
+ def test_wrong_kind(self) -> None:
295
+ self.assertPydanticLiteralError(lambda: PluginRegistry(kind='WrongKind'),
296
+ 'kind',
297
+ 'PluginRegistry')
298
+
299
+ def test_missing_identifier(self) -> None:
300
+ self.assertPydanticMissing(lambda: PluginRegistry(),
301
+ 'id')
302
+
303
+ def test_null_identifier(self) -> None:
304
+ self.assertPydanticStringType(lambda: PluginRegistry(id=None),
305
+ 'id')
306
+
307
+ def test_missing_name(self) -> None:
308
+ self.assertPydanticMissing(lambda: PluginRegistry(id='myid'),
309
+ 'name')
310
+
311
+ def test_null_name(self) -> None:
312
+ self.assertPydanticStringType(lambda: PluginRegistry(name=None),
313
+ 'name')
314
+
315
+ def test_missing_layout(self) -> None:
316
+ self.assertPydanticMissing(lambda: PluginRegistry(),
317
+ 'layout')
318
+
319
+ def test_null_layout(self) -> None:
320
+ self.assertPydanticModelAttributesType(lambda: PluginRegistry(layout=None),
321
+ 'layout')
322
+
323
+ def test_missing_layers(self) -> None:
324
+ self.assertPydanticMissing(lambda: PluginRegistry(),
325
+ 'layers')
326
+
327
+ def test_null_layers(self) -> None:
328
+ self.assertPydanticListType(lambda: PluginRegistry(layers=None),
329
+ 'layers')
330
+
331
+ def test_empty_layers(self) -> None:
332
+ self.assertPydanticTooShort(lambda: PluginRegistry(layers=[]),
333
+ 'layers')
334
+
335
+ def test_missing_plugin_identifiers(self) -> None:
336
+ self.assertPydanticMissing(lambda: PluginRegistry(),
337
+ 'plugin-identifiers')
338
+
339
+ def test_null_plugin_identifiers(self) -> None:
340
+ self.assertPydanticListType(lambda: PluginRegistry(**{'plugin-identifiers': None}),
341
+ 'plugin-identifiers')
342
+
343
+ def test_empty_plugin_identifiers(self) -> None:
344
+ self.assertPydanticTooShort(lambda: PluginRegistry(**{'plugin-identifiers': []}),
345
+ 'plugin-identifiers')
346
+
347
+ def test_missing_suppressed_plugin_identifiers(self) -> None:
348
+ PluginRegistry(**{'kind': 'PluginRegistry',
349
+ 'id': 'myid',
350
+ 'name': 'My Name',
351
+ 'layout': self.valid_layout,
352
+ 'layers': [
353
+ self.valid_layer,
354
+ ],
355
+ 'plugin-identifiers': [
356
+ 'whatever',
357
+ ]})
358
+
359
+ def test_null_suppressed_plugin_identifiers(self) -> None:
360
+ self.assertPydanticListType(lambda: PluginRegistry(**{'suppressed-plugin-identifiers': None}),
361
+ 'suppressed-plugin-identifiers')
362
+
363
+ def test_empty_plugin_identifiers(self) -> None:
364
+ PluginRegistry(**{'kind': 'PluginRegistry',
365
+ 'id': 'myid',
366
+ 'name': 'My Name',
367
+ 'layout': self.valid_layout,
368
+ 'layers': [
369
+ self.valid_layer,
370
+ ],
371
+ 'plugin-identifiers': [
372
+ 'whatever',
373
+ ],
374
+ 'suppressed-plugin-identifiers': []
375
+ })
376
+
377
+ def test_kind(self) -> None:
378
+ self.assertEqual(self.valid.kind, 'PluginRegistry')
379
+
380
+ def test_get_id(self) -> None:
381
+ self.assertEqual(self.valid.get_id(), 'myid')
382
+
383
+ def test_get_name(self) -> None:
384
+ self.assertEqual(self.valid.get_name(), 'My Name')
385
+
386
+ def test_get_layout(self) -> None:
387
+ self.assertEqual(self.valid.get_layout(), self.valid_layout)
388
+
389
+ def test_get_layers(self) -> None:
390
+ self.assertListEqual(self.valid.get_layers(), [self.valid_layer])
391
+
392
+ def test_get_layer(self) -> None:
393
+ self.assertEqual(self.valid.get_layer('mylayer'), self.valid_layer)
394
+ self.assertIsNone(self.valid.get_layer('invalidlayer'))
395
+
396
+ def test_get_layer_ids(self) -> None:
397
+ self.assertListEqual(self.valid.get_layer_ids(), ['mylayer'])
398
+
399
+ def test_get_plugin_identifiers(self) -> None:
400
+ self.assertListEqual(self.valid.get_plugin_identifiers(),
401
+ ['org.myproject.plugin.MyPlugin'])
402
+
403
+ def test_get_suppressed_plugin_identifiers(self) -> None:
404
+ self.assertListEqual(self.valid.get_suppressed_plugin_identifiers(),
405
+ ['org.myproject.plugin.BadPlugin'])
406
+
407
+
408
+ #
409
+ # See https://stackoverflow.com/a/43353680
410
+ #
411
+ del _BasePluginRegistryLayoutTestCase