-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathtest_storage.py
542 lines (478 loc) · 18.8 KB
/
test_storage.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# Copyright 2019 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import abc
import gc
import io
import os
import pathlib
import stat
import sys
import tempfile
import typing
import unittest
import unittest.mock
from textwrap import dedent
import pytest
import yaml
import ops
import ops.storage
from test.test_helpers import FakeScript
@pytest.fixture
def fake_script(request: pytest.FixtureRequest):
return FakeScript(request)
class StoragePermutations(abc.ABC):
assertEqual = unittest.TestCase.assertEqual # noqa
assertRaises = unittest.TestCase.assertRaises # noqa
def create_framework(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
) -> ops.Framework:
"""Create a Framework that we can use to test the backend storage."""
storage = self.create_storage(request, fake_script)
return ops.Framework(
storage,
None, # type: ignore
None, # type: ignore
None, # type: ignore
juju_debug_at=set(),
)
@abc.abstractmethod
def create_storage(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
) -> ops.storage.SQLiteStorage:
"""Create a Storage backend that we can interact with."""
return NotImplemented
def test_save_and_load_snapshot(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
):
f = self.create_framework(request, fake_script)
class Sample(ops.StoredStateData):
def __init__(
self,
parent: ops.Object,
key: str,
content: typing.Dict[str, typing.Any],
):
super().__init__(parent, key)
self.content = content
def snapshot(self):
return {'content': self.content}
def restore(self, snapshot: typing.Dict[str, typing.Any]):
self.__dict__.update(snapshot)
f.register_type(Sample, None, Sample.handle_kind)
data = {
'str': 'string',
'bytes': b'bytes',
'int': 1,
'float': 3.0,
'dict': {'a': 'b'},
'set': {'a', 'b'},
'list': [1, 2],
}
s = Sample(f, 'test', data)
handle = s.handle
f.save_snapshot(s)
del s
gc.collect()
res = f.load_snapshot(handle)
assert data == res.content # type: ignore
def test_emit_event(self, request: pytest.FixtureRequest, fake_script: FakeScript):
f = self.create_framework(request, fake_script)
class Evt(ops.EventBase):
def __init__(self, handle: ops.Handle, content: typing.Any):
super().__init__(handle)
self.content = content
def snapshot(self):
return self.content
def restore(self, content: typing.Any):
self.content = content
class Events(ops.ObjectEvents):
event = ops.EventSource(Evt)
class Sample(ops.Object):
on = Events() # type: ignore
def __init__(self, parent: ops.Object, key: str):
super().__init__(parent, key)
self.observed_content = None
self.framework.observe(self.on.event, self._on_event)
def _on_event(self, event: Evt):
self.observed_content = event.content
def snapshot(self) -> typing.Dict[str, typing.Any]:
raise NotImplementedError()
def restore(self, snapshot: typing.Dict[str, typing.Any]) -> None:
raise NotImplementedError()
s = Sample(f, 'key')
f.register_type(Sample, None, Sample.handle_kind)
s.on.event.emit('foo')
assert s.observed_content == 'foo'
s.on.event.emit(1)
assert s.observed_content == 1
s.on.event.emit(None)
assert s.observed_content is None
def test_save_and_overwrite_snapshot(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
):
store = self.create_storage(request, fake_script)
store.save_snapshot('foo', {1: 2})
assert store.load_snapshot('foo') == {1: 2}
store.save_snapshot('foo', {'three': 4})
assert store.load_snapshot('foo') == {'three': 4}
def test_drop_snapshot(self, request: pytest.FixtureRequest, fake_script: FakeScript):
store = self.create_storage(request, fake_script)
store.save_snapshot('foo', {1: 2})
assert store.load_snapshot('foo') == {1: 2}
store.drop_snapshot('foo')
with pytest.raises(ops.storage.NoSnapshotError):
store.load_snapshot('foo')
def test_save_snapshot_empty_string(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
):
store = self.create_storage(request, fake_script)
with pytest.raises(ops.storage.NoSnapshotError):
store.load_snapshot('foo')
store.save_snapshot('foo', '')
assert store.load_snapshot('foo') == ''
store.drop_snapshot('foo')
with pytest.raises(ops.storage.NoSnapshotError):
store.load_snapshot('foo')
def test_save_snapshot_none(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
):
store = self.create_storage(request, fake_script)
with pytest.raises(ops.storage.NoSnapshotError):
store.load_snapshot('bar')
store.save_snapshot('bar', None)
assert store.load_snapshot('bar') is None
store.drop_snapshot('bar')
with pytest.raises(ops.storage.NoSnapshotError):
store.load_snapshot('bar')
def test_save_snapshot_zero(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
):
store = self.create_storage(request, fake_script)
with pytest.raises(ops.storage.NoSnapshotError):
store.load_snapshot('zero')
store.save_snapshot('zero', 0)
assert store.load_snapshot('zero') == 0
store.drop_snapshot('zero')
with pytest.raises(ops.storage.NoSnapshotError):
store.load_snapshot('zero')
def test_save_notice(self, request: pytest.FixtureRequest, fake_script: FakeScript):
store = self.create_storage(request, fake_script)
store.save_notice('event', 'observer', 'method')
assert list(store.notices('event')) == [('event', 'observer', 'method')]
def test_all_notices(self, request: pytest.FixtureRequest, fake_script: FakeScript):
notices = [('e1', 'o1', 'm1'), ('e1', 'o2', 'm2'), ('e2', 'o3', 'm3')]
store = self.create_storage(request, fake_script)
for notice in notices:
store.save_notice(*notice)
# passing in the arg, you get the ones that match
assert list(store.notices('e1')) == notices[:2]
assert list(store.notices('e2')) == notices[2:]
# the match is exact
assert list(store.notices('e%')) == []
assert list(store.notices('e*')) == []
assert list(store.notices('e.')) == []
assert list(store.notices('e')) == []
# no arg, or non-arg, means all
assert list(store.notices()) == notices
assert list(store.notices(None)) == notices
assert list(store.notices('')) == notices
def test_load_notices(self, request: pytest.FixtureRequest, fake_script: FakeScript):
store = self.create_storage(request, fake_script)
assert list(store.notices('path')) == []
def test_save_one_load_another_notice(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
):
store = self.create_storage(request, fake_script)
store.save_notice('event', 'observer', 'method')
assert list(store.notices('other')) == []
def test_save_load_drop_load_notices(
self,
request: pytest.FixtureRequest,
fake_script: FakeScript,
):
store = self.create_storage(request, fake_script)
store.save_notice('event', 'observer', 'method')
store.save_notice('event', 'observer', 'method2')
assert list(store.notices('event')) == [
('event', 'observer', 'method'),
('event', 'observer', 'method2'),
]
class TestSQLiteStorage(StoragePermutations):
def create_storage(self, request: pytest.FixtureRequest, fake_script: FakeScript):
return ops.storage.SQLiteStorage(':memory:')
def test_permissions_new(self):
with tempfile.TemporaryDirectory() as temp_dir:
filename = os.path.join(temp_dir, '.unit-state.db')
storage = ops.storage.SQLiteStorage(filename)
assert stat.S_IMODE(os.stat(filename).st_mode) == stat.S_IRUSR | stat.S_IWUSR
storage.close()
def test_permissions_existing(self):
with tempfile.TemporaryDirectory() as temp_dir:
filename = os.path.join(temp_dir, '.unit-state.db')
ops.storage.SQLiteStorage(filename).close()
# Set the file to access that will need fixing for user, group, and other.
os.chmod(filename, 0o744)
storage = ops.storage.SQLiteStorage(filename)
assert stat.S_IMODE(os.stat(filename).st_mode) == stat.S_IRUSR | stat.S_IWUSR
storage.close()
@unittest.mock.patch('os.path.exists')
def test_permissions_race(self, exists: unittest.mock.MagicMock):
exists.return_value = False
with tempfile.TemporaryDirectory() as temp_dir:
filename = os.path.join(temp_dir, '.unit-state.db')
# Create an existing file, but the mock will simulate a race condition saying that it
# does not exist.
open(filename, 'w').close()
pytest.raises(RuntimeError, ops.storage.SQLiteStorage, filename)
@unittest.mock.patch('os.chmod')
def test_permissions_failure(self, chmod: unittest.mock.MagicMock):
chmod.side_effect = OSError
with tempfile.TemporaryDirectory() as temp_dir:
filename = os.path.join(temp_dir, '.unit-state.db')
open(filename, 'w').close()
pytest.raises(RuntimeError, ops.storage.SQLiteStorage, filename)
def setup_juju_backend(fake_script: FakeScript, state_file: pathlib.Path):
"""Create fake scripts for pretending to be state-set and state-get."""
template_args = {
'executable': str(pathlib.Path(sys.executable).as_posix()),
'pthpth': repr(os.path.dirname(pathlib.__file__))[1:-1],
'state_file': str(state_file.as_posix()),
}
fake_script.write(
'state-set',
dedent("""\
{executable} -c '
import sys
if "{pthpth}" not in sys.path:
sys.path.append("{pthpth}")
import sys, yaml, pathlib, pickle
assert sys.argv[1:] == ["--file", "-"]
request = yaml.load(sys.stdin, Loader=getattr(yaml, "CSafeLoader", yaml.SafeLoader))
state_file = pathlib.Path("{state_file}")
if state_file.exists() and state_file.stat().st_size > 0:
with state_file.open("rb") as f:
state = pickle.load(f)
else:
state = {{}}
for k, v in request.items():
state[k] = v
with state_file.open("wb") as f:
pickle.dump(state, f)
' "$@"
""").format(**template_args),
)
fake_script.write(
'state-get',
dedent("""\
{executable} -Sc '
import sys
if "{pthpth}" not in sys.path:
sys.path.append("{pthpth}")
import sys, pathlib, pickle
assert len(sys.argv) == 2
state_file = pathlib.Path("{state_file}")
if state_file.exists() and state_file.stat().st_size > 0:
with state_file.open("rb") as f:
state = pickle.load(f)
else:
state = {{}}
result = state.get(sys.argv[1], "\\n")
sys.stdout.write(result)
' "$@"
""").format(**template_args),
)
fake_script.write(
'state-delete',
dedent("""\
{executable} -Sc '
import sys
if "{pthpth}" not in sys.path:
sys.path.append("{pthpth}")
import sys, pathlib, pickle
assert len(sys.argv) == 2
state_file = pathlib.Path("{state_file}")
if state_file.exists() and state_file.stat().st_size > 0:
with state_file.open("rb") as f:
state = pickle.load(f)
else:
state = {{}}
state.pop(sys.argv[1], None)
with state_file.open("wb") as f:
pickle.dump(state, f)
' "$@"
""").format(**template_args),
)
class TestJujuStorage(StoragePermutations):
def create_storage(self, request: pytest.FixtureRequest, fake_script: FakeScript):
fd, fn = tempfile.mkstemp(prefix='tmp-ops-test-state-')
os.close(fd)
state_file = pathlib.Path(fn)
request.addfinalizer(state_file.unlink)
setup_juju_backend(fake_script, state_file)
return ops.storage.JujuStorage()
class TestSimpleLoader:
def test_is_c_loader(self):
loader = ops.storage._SimpleLoader(io.StringIO(''))
if getattr(yaml, 'CSafeLoader', None) is not None:
assert isinstance(loader, yaml.CSafeLoader)
else:
assert isinstance(loader, yaml.SafeLoader)
def test_is_c_dumper(self):
dumper = ops.storage._SimpleDumper(io.StringIO(''))
if getattr(yaml, 'CSafeDumper', None) is not None:
assert isinstance(dumper, yaml.CSafeDumper)
else:
assert isinstance(dumper, yaml.SafeDumper)
def test_handles_tuples(self):
raw = yaml.dump((1, 'tuple'), Dumper=ops.storage._SimpleDumper)
parsed = yaml.load(raw, Loader=ops.storage._SimpleLoader) # noqa: S506
assert parsed == (1, 'tuple')
def assertRefused(self, obj: typing.Any): # noqa: N802
# We shouldn't allow them to be written
with pytest.raises(yaml.representer.RepresenterError):
yaml.dump(obj, Dumper=ops.storage._SimpleDumper)
# If they did somehow end up written, we shouldn't be able to load them
raw = yaml.dump(obj, Dumper=yaml.Dumper)
with pytest.raises(yaml.constructor.ConstructorError):
yaml.load(raw, Loader=ops.storage._SimpleLoader) # noqa: S506
def test_forbids_some_types(self):
self.assertRefused(1 + 2j)
self.assertRefused({'foo': 1 + 2j})
self.assertRefused(frozenset(['foo', 'bar']))
self.assertRefused(bytearray(b'foo'))
self.assertRefused(object())
class Foo:
pass
f = Foo()
self.assertRefused(f)
class TestJujuStateBackend:
def test_is_not_available(self):
assert not ops.storage.juju_backend_available()
def test_is_available(self, fake_script: FakeScript):
fake_script.write('state-get', 'echo ""')
assert ops.storage.juju_backend_available()
assert fake_script.calls(clear=True) == []
def test_set_encodes_args(self, fake_script: FakeScript):
t = tempfile.NamedTemporaryFile() # noqa: SIM115
try:
fake_script.write(
'state-set',
dedent("""
cat >> {}
""").format(pathlib.Path(t.name).as_posix()),
)
backend = ops.storage._JujuStorageBackend()
backend.set('key', {'foo': 2})
assert fake_script.calls(clear=True) == [
['state-set', '--file', '-'],
]
t.seek(0)
content = t.read()
finally:
t.close()
assert content.decode('utf-8') == dedent("""\
"key": |
{foo: 2}
""")
def test_get(self, fake_script: FakeScript):
fake_script.write(
'state-get',
dedent("""
echo 'foo: "bar"'
"""),
)
backend = ops.storage._JujuStorageBackend()
value = backend.get('key')
assert value == {'foo': 'bar'}
assert fake_script.calls(clear=True) == [
['state-get', 'key'],
]
def test_set_and_get_complex_value(self, fake_script: FakeScript):
t = tempfile.NamedTemporaryFile() # noqa: SIM115
try:
fake_script.write(
'state-set',
dedent("""
cat >> {}
""").format(pathlib.Path(t.name).as_posix()),
)
backend = ops.storage._JujuStorageBackend()
complex_val = {
'foo': 2,
3: [1, 2, '3'],
'four': {2, 3},
'five': {'a': 2, 'b': 3.0},
'six': ('a', 'b'),
'seven': b'1234',
}
backend.set('Class[foo]/_stored', complex_val)
assert fake_script.calls(clear=True) == [
['state-set', '--file', '-'],
]
t.seek(0)
content = t.read()
finally:
t.close()
outer = yaml.safe_load(content)
key = 'Class[foo]/_stored'
assert list(outer.keys()) == [key]
inner = yaml.load(outer[key], Loader=ops.storage._SimpleLoader) # noqa: S506
assert complex_val == inner
assert content.decode('utf-8') == dedent("""\
"Class[foo]/_stored": |
foo: 2
3: [1, 2, '3']
four: !!set {2: null, 3: null}
five: {a: 2, b: 3.0}
six: !!python/tuple [a, b]
seven: !!binary |
MTIzNA==
""")
# Note that the content is yaml in a string, embedded inside YAML to declare the Key:
# Value of where to store the entry.
fake_script.write(
'state-get',
dedent("""
echo "foo: 2
3: [1, 2, '3']
four: !!set {2: null, 3: null}
five: {a: 2, b: 3.0}
six: !!python/tuple [a, b]
seven: !!binary |
MTIzNA==
"
"""),
)
out = backend.get('Class[foo]/_stored')
assert out == complex_val
# TODO: Add tests for things we don't want to support. eg, YAML that has custom types should
# be properly forbidden.
# TODO: Tests for state-set/get/delete and how they handle if you ask to delete something
# that doesn't exist, or get something that doesn't exist, etc.