added unit tests
This commit is contained in:
parent
a4988b051b
commit
c0c2512df6
0
hooks/__init__.py
Normal file
0
hooks/__init__.py
Normal file
@ -23,16 +23,21 @@ def process_requests(reqs):
|
|||||||
# setup to use them for these operations.
|
# setup to use them for these operations.
|
||||||
svc = 'admin'
|
svc = 'admin'
|
||||||
if op == "create_pool":
|
if op == "create_pool":
|
||||||
pool = req.get('pool')
|
pool = req.get('name')
|
||||||
replicas = req.get('replicas')
|
replicas = req.get('replicas')
|
||||||
if not all([pool, replicas]):
|
if not all([pool, replicas]):
|
||||||
log("Missing parameter(s)", level=ERROR)
|
log("Missing parameter(s)", level=ERROR)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if not pool_exists(service=svc, name=pool):
|
if not pool_exists(service=svc, name=pool):
|
||||||
log("Creating pool '%s'" % (pool), level=INFO)
|
log("Creating pool '%s' (replicas=%s)" % (pool, replicas),
|
||||||
|
level=INFO)
|
||||||
create_pool(service=svc, name=pool, replicas=replicas)
|
create_pool(service=svc, name=pool, replicas=replicas)
|
||||||
else:
|
else:
|
||||||
log("Pool '%s' already exists" % (pool), level=INFO)
|
log("Pool '%s' already exists - skipping create" % (pool),
|
||||||
|
level=INFO)
|
||||||
|
else:
|
||||||
|
log("Unknown operation '%s'" % (op))
|
||||||
|
return 1
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
@ -296,15 +296,16 @@ def client_relation_joined(relid=None):
|
|||||||
|
|
||||||
@hooks.hook('client-relation-changed')
|
@hooks.hook('client-relation-changed')
|
||||||
def client_relation_changed(relid=None):
|
def client_relation_changed(relid=None):
|
||||||
if ceph.is_quorum():
|
"""Process broker requests from ceph client relations."""
|
||||||
resp = None
|
if ceph.is_quorum() and ceph.is_leader():
|
||||||
settings = relation_get(rid=relid)
|
settings = relation_get(rid=relid)
|
||||||
if 'broker_req' in settings:
|
if 'broker_req' in settings:
|
||||||
req = settings['broker_req']
|
req = settings['broker_req']
|
||||||
log("Broker request received")
|
log("Broker request received from ceph client")
|
||||||
resp = process_requests(json.loads(req))
|
exit_code = process_requests(json.loads(req))
|
||||||
|
# Construct JSON response dict allowing other data to be added as
|
||||||
if resp is not None:
|
# and when we need it.
|
||||||
|
resp = json.dumps({'exit_code': exit_code})
|
||||||
relation_set(relation_id=relid,
|
relation_set(relation_id=relid,
|
||||||
relation_settings={'broker_rsp': resp})
|
relation_settings={'broker_rsp': resp})
|
||||||
else:
|
else:
|
||||||
|
2
unit_tests/__init__.py
Normal file
2
unit_tests/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
import sys
|
||||||
|
sys.path.append('hooks')
|
@ -1,11 +1,46 @@
|
|||||||
#import mock
|
import mock
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import ceph_broker
|
||||||
|
|
||||||
|
|
||||||
class CephBrokerTestCase(unittest.TestCase):
|
class CephBrokerTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(CephBrokerTestCase, self).setUp()
|
super(CephBrokerTestCase, self).setUp()
|
||||||
|
|
||||||
def test_process_requests(self):
|
@mock.patch('ceph_broker.log')
|
||||||
pass
|
def test_process_requests_noop(self, mock_log):
|
||||||
|
rc = ceph_broker.process_requests([{}])
|
||||||
|
self.assertEqual(rc, 1)
|
||||||
|
|
||||||
|
@mock.patch('ceph_broker.log')
|
||||||
|
def test_process_requests_invalid(self, mock_log):
|
||||||
|
rc = ceph_broker.process_requests([{'op': 'invalid_op'}])
|
||||||
|
self.assertEqual(rc, 1)
|
||||||
|
|
||||||
|
@mock.patch('ceph_broker.create_pool')
|
||||||
|
@mock.patch('ceph_broker.pool_exists')
|
||||||
|
@mock.patch('ceph_broker.log')
|
||||||
|
def test_process_requests_create_pool(self, mock_log, mock_pool_exists,
|
||||||
|
mock_create_pool):
|
||||||
|
mock_pool_exists.return_value = False
|
||||||
|
rc = ceph_broker.process_requests([{'op': 'create_pool', 'name': 'foo',
|
||||||
|
'replicas': 3}])
|
||||||
|
mock_pool_exists.assert_called_with(service='admin', name='foo')
|
||||||
|
mock_create_pool.assert_called_with(service='admin', name='foo',
|
||||||
|
replicas=3)
|
||||||
|
self.assertEqual(rc, 0)
|
||||||
|
|
||||||
|
@mock.patch('ceph_broker.create_pool')
|
||||||
|
@mock.patch('ceph_broker.pool_exists')
|
||||||
|
@mock.patch('ceph_broker.log')
|
||||||
|
def test_process_requests_create_pool_exists(self, mock_log,
|
||||||
|
mock_pool_exists,
|
||||||
|
mock_create_pool):
|
||||||
|
mock_pool_exists.return_value = True
|
||||||
|
rc = ceph_broker.process_requests([{'op': 'create_pool', 'name': 'foo',
|
||||||
|
'replicas': 3}])
|
||||||
|
mock_pool_exists.assert_called_with(service='admin', name='foo')
|
||||||
|
self.assertFalse(mock_create_pool.called)
|
||||||
|
self.assertEqual(rc, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user