Debian Codegraph @

taskflow/tests/unit/persistence/test_zk_persistence.py

raw
1 # Copyright (C) 2014 AT&T Labs 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
17 from kazoo import exceptions as kazoo_exceptions
18 from oslo_utils import uuidutils
19 import testtools
20
21 from taskflow import exceptions as exc
22 from taskflow.persistence import backends
23 from taskflow.persistence.backends import impl_zookeeper
24 from taskflow import test
25 from taskflow.tests.unit.persistence import base
26 from taskflow.tests import utils as test_utils
27 from taskflow.utils import kazoo_utils
28
29 TEST_PATH_TPL = '/taskflow/persistence-test/%s'
30 _ZOOKEEPER_AVAILABLE = test_utils.zookeeper_available(
31 impl_zookeeper.MIN_ZK_VERSION)
32
33
34 def clean_backend(backend, conf):
35 with contextlib.closing(backend.get_connection()) as conn:
36 try:
37 conn.clear_all()
38 except exc.NotFound:
39 pass
40 client = kazoo_utils.make_client(conf)
41 client.start()
42 try:
43 client.delete(conf['path'], recursive=True)
44 except kazoo_exceptions.NoNodeError:
45 pass
46 finally:
47 kazoo_utils.finalize_client(client)
48
49
50 @testtools.skipIf(not _ZOOKEEPER_AVAILABLE, 'zookeeper is not available')
51 class ZkPersistenceTest(test.TestCase, base.PersistenceTestMixin):
52 def _get_connection(self):
53 return self.backend.get_connection()
54
55 def setUp(self):
56 super().setUp()
57 conf = test_utils.ZK_TEST_CONFIG.copy()
58 # Create a unique path just for this test (so that we don't overwrite
59 # what other tests are doing).
60 conf['path'] = TEST_PATH_TPL % (uuidutils.generate_uuid())
61 self.backend = impl_zookeeper.ZkBackend(conf)
62 self.addCleanup(self.backend.close)
63 self.addCleanup(clean_backend, self.backend, conf)
64 with contextlib.closing(self.backend.get_connection()) as conn:
65 conn.upgrade()
66
67 def test_zk_persistence_entry_point(self):
68 conf = {'connection': 'zookeeper:'}
69 with contextlib.closing(backends.fetch(conf)) as be:
70 self.assertIsInstance(be, impl_zookeeper.ZkBackend)