Debian Codegraph @

taskflow/tests/unit/persistence/test_dir_persistence.py

raw
1 # Copyright (C) 2013 Rackspace Hosting All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import contextlib
16 import os
17 import shutil
18 import tempfile
19
20 from oslo_utils import uuidutils
21 import testscenarios
22
23 from taskflow import exceptions as exc
24 from taskflow.persistence import backends
25 from taskflow.persistence.backends import impl_dir
26 from taskflow.persistence import models
27 from taskflow import test
28 from taskflow.tests.unit.persistence import base
29
30
31 class DirPersistenceTest(testscenarios.TestWithScenarios,
32 test.TestCase, base.PersistenceTestMixin):
33
34 scenarios = [
35 ('no_cache', {'max_cache_size': None}),
36 ('one', {'max_cache_size': 1}),
37 ('tiny', {'max_cache_size': 256}),
38 ('medimum', {'max_cache_size': 512}),
39 ('large', {'max_cache_size': 1024}),
40 ]
41
42 def _get_connection(self):
43 return self.backend.get_connection()
44
45 def setUp(self):
46 super().setUp()
47 self.path = tempfile.mkdtemp()
48 self.backend = impl_dir.DirBackend({
49 'path': self.path,
50 'max_cache_size': self.max_cache_size,
51 })
52 with contextlib.closing(self._get_connection()) as conn:
53 conn.upgrade()
54
55 def tearDown(self):
56 super().tearDown()
57 if self.path and os.path.isdir(self.path):
58 shutil.rmtree(self.path)
59 self.path = None
60 self.backend = None
61
62 def _check_backend(self, conf):
63 with contextlib.closing(backends.fetch(conf)) as be:
64 self.assertIsInstance(be, impl_dir.DirBackend)
65
66 def test_dir_backend_invalid_cache_size(self):
67 for invalid_size in [-1024, 0, -1]:
68 conf = {
69 'path': self.path,
70 'max_cache_size': invalid_size,
71 }
72 self.assertRaises(ValueError, impl_dir.DirBackend, conf)
73
74 def test_dir_backend_cache_overfill(self):
75 if self.max_cache_size is not None:
76 # Ensure cache never goes past the desired max size...
77 books_ids_made = []
78 with contextlib.closing(self._get_connection()) as conn:
79 for i in range(0, int(1.5 * self.max_cache_size)):
80 lb_name = 'book-%s' % (i)
81 lb_id = uuidutils.generate_uuid()
82 lb = models.LogBook(name=lb_name, uuid=lb_id)
83 self.assertRaises(exc.NotFound, conn.get_logbook, lb_id)
84 conn.save_logbook(lb)
85 books_ids_made.append(lb_id)
86 self.assertLessEqual(self.backend.file_cache.currsize,
87 self.max_cache_size)
88 # Also ensure that we can still read all created books...
89 with contextlib.closing(self._get_connection()) as conn:
90 for lb_id in books_ids_made:
91 lb = conn.get_logbook(lb_id)
92 self.assertIsNotNone(lb)
93
94 def test_dir_backend_entry_point(self):
95 self._check_backend(dict(connection='dir:', path=self.path))
96
97 def test_dir_backend_name(self):
98 self._check_backend(dict(connection='dir', # no colon
99 path=self.path))
100
101 def test_file_backend_entry_point(self):
102 self._check_backend(dict(connection='file:', path=self.path))