def _register_test_datasource(self, name): args = helper.datasource_openstack_args() if name == 'nova': ds = nova_driver.NovaDriver('nova', args=args) if name == 'neutron': ds = neutronv2_driver.NeutronV2Driver('neutron', args=args) ds.update_from_datasource = mock.MagicMock() return ds
def setUp(self): super(TestNovaDriver, self).setUp() nova_client = mock.MagicMock() self.nova = fakes.NovaFakeClient() with mock.patch.object(novaclient.client.Client, '__init__', return_value=nova_client): self.driver = nova_driver.NovaDriver( name='nova', args=helper.datasource_openstack_args())
def test_datasource_pub(self, nova_mock): node = helper.make_dsenode_new_partition('testnode') nova = nova_driver.NovaDriver(name='nova', args=helper.datasource_openstack_args()) test = fake_datasource.FakeDataSource('test') node.register_service(nova) node.register_service(test) test.subscribe('nova', 'p') helper.retry_check_function_return_value( lambda: hasattr(test, 'last_msg'), True) nova.publish('p', 42, use_snapshot=True) helper.retry_check_function_return_value(lambda: test.last_msg['data'], 42) self.assertFalse(hasattr(nova, "last_msg")) node.stop()
def test_datasource_unsub(self, nova_mock): node = helper.make_dsenode_new_partition('testnode') nova = nova_driver.NovaDriver(name='nova', args=helper.datasource_openstack_args()) test = fake_datasource.FakeDataSource('test') node.register_service(nova) node.register_service(test) nova.subscribe('test', 'p') helper.retry_check_function_return_value( lambda: hasattr(nova, 'last_msg'), True) test.publish('p', 42, use_snapshot=True) helper.retry_check_function_return_value(lambda: nova.last_msg['data'], 42) self.assertFalse(hasattr(test, "last_msg")) nova.unsubscribe('test', 'p') test.publish('p', 43, use_snapshot=True) # hard to test that the message is never delivered time.sleep(0.2) self.assertEqual(nova.last_msg['data'], 42) node.stop()
def test_datasource_api_model_execute(self): def _execute_api(client, action, action_args): positional_args = action_args.get('positional', []) named_args = action_args.get('named', {}) method = reduce(getattr, action.split('.'), client) method(*positional_args, **named_args) class NovaClient(object): def __init__(self, testkey): self.testkey = testkey def _get_testkey(self): return self.testkey def disconnect(self, arg1, arg2, arg3): self.testkey = "arg1=%s arg2=%s arg3=%s" % (arg1, arg2, arg3) def disconnect_all(self): self.testkey = "action_has_no_args" nova_client = NovaClient("testing") args = helper.datasource_openstack_args() nova = nova_driver.NovaDriver('nova', args=args) nova.nova_client = nova_client nova.update_from_datasource = mock.MagicMock() nova._execute_api = _execute_api self.node.register_service(nova) execute_action = self.datasource_model.execute_action # Positive test: valid body args, ds_id context = {'ds_id': 'nova'} body = { 'name': 'disconnect', 'args': { 'positional': ['value1', 'value2'], 'named': { 'arg3': 'value3' } } } request = helper.FakeRequest(body) result = execute_action({}, context, request) self.assertEqual(result, {}) expected_result = "arg1=value1 arg2=value2 arg3=value3" f = nova.nova_client._get_testkey helper.retry_check_function_return_value(f, expected_result) # Positive test: no body args context = {'ds_id': 'nova'} body = {'name': 'disconnect_all'} request = helper.FakeRequest(body) result = execute_action({}, context, request) self.assertEqual(result, {}) expected_result = "action_has_no_args" f = nova.nova_client._get_testkey helper.retry_check_function_return_value(f, expected_result) # Negative test: invalid ds_id context = {'ds_id': 'unknown_ds'} self.assertRaises(webservice.DataModelException, execute_action, {}, context, request) # Negative test: no ds_id context = {} self.assertRaises(webservice.DataModelException, execute_action, {}, context, request) # Negative test: empty body context = {'ds_id': 'nova'} bad_request = helper.FakeRequest({}) self.assertRaises(webservice.DataModelException, execute_action, {}, context, bad_request) # Negative test: no body name/action context = {'ds_id': 'nova'} body = { 'args': { 'positional': ['value1', 'value2'], 'named': { 'arg3': 'value3' } } } bad_request = helper.FakeRequest(body) self.assertRaises(webservice.DataModelException, execute_action, {}, context, bad_request) # Positive test with retry: no body args cfg.CONF.dse.execute_action_retry = True context = {'ds_id': 'nova'} body = {'name': 'disconnect_all'} request = helper.FakeRequest(body) result = execute_action({}, context, request) self.assertEqual(result, {}) expected_result = "action_has_no_args" f = nova.nova_client._get_testkey helper.retry_check_function_return_value(f, expected_result)