gate-issue: Update tests to use fake clients

IndexError: list index out of range errors are seen while
executing py27 jobs as tests try to connect to real client
instead of fake ones while polling. This commit fixes the same.

Closes-Bug:#1648320
Change-Id: Ieb42e8265a6ada95a3430a3f78a01806f714be0e
This commit is contained in:
Anusha Ramineni 2016-12-08 12:17:01 +05:30
parent 24486809e5
commit c4ef12cb38
3 changed files with 78 additions and 60 deletions

View File

@ -43,7 +43,6 @@ class TestDatasourceModel(base.SqlTestCase):
def tearDown(self):
super(TestDatasourceModel, self).tearDown()
self.node.stop()
self.node.start()
def _get_datasource_request(self):
# leave ID out--generated during creation
@ -129,10 +128,10 @@ class TestDatasourceModel(base.SqlTestCase):
nova_client = NovaClient("testing")
args = helper.datasource_openstack_args()
nova = nova_driver.NovaDriver('nova', args=args)
self.node.register_service(nova)
nova.nova_client = nova_client
nova.update_from_datasource = mock.MagicMock()
nova._execute_api = _execute_api
nova.nova_client = nova_client
self.node.register_service(nova)
execute_action = self.datasource_model.execute_action

View File

@ -14,13 +14,13 @@
#
import mock
import novaclient
import tenacity
import time
from oslo_config import cfg
# Note(ekcs): this is needed for direct unit test because Dse2Runtime import,
# which takes place before the confFixture is setup, fails w/o it
from novaclient import client as nova_client
from oslo_config import cfg
cfg.CONF.datasource_sync_period = 0
from oslo_messaging import conffixture
@ -57,6 +57,7 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: test1.last_msg['data'], 42)
self.assertFalse(hasattr(test2, "last_msg"))
node.stop()
def test_intranode_pubsub2(self):
# same as test_intranode_pubsub but with opposite ordering.
@ -74,6 +75,7 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: test2.last_msg['data'], 42)
self.assertFalse(hasattr(test1, "last_msg"))
node.stop()
def test_intranode_partial_unsub(self):
node = helper.make_dsenode_new_partition('testnode')
@ -91,6 +93,7 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: test1.last_msg['data'], 42)
self.assertFalse(hasattr(test2, "last_msg"))
node.stop()
def test_internode_pubsub(self):
node1 = helper.make_dsenode_new_partition('testnode1')
@ -107,6 +110,8 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: test1.last_msg['data'], 42)
self.assertFalse(hasattr(test2, "last_msg"))
node1.stop()
node2.stop()
def test_internode_partial_unsub(self):
node1 = helper.make_dsenode_new_partition('testnode1')
@ -125,6 +130,8 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: test1.last_msg['data'], 42)
self.assertFalse(hasattr(test2, "last_msg"))
node1.stop()
node2.stop()
def test_multiservice_pubsub(self):
node1 = helper.make_dsenode_new_partition('testnode1')
@ -144,6 +151,8 @@ class TestDSE(base.TestCase):
lambda: test1.last_msg['data'], 42)
self.assertFalse(hasattr(test2, "last_msg"))
self.assertFalse(hasattr(test3, "last_msg"))
node1.stop()
node2.stop()
def test_subscribe_snapshot(self):
node = helper.make_dsenode_new_partition('testnode')
@ -156,68 +165,66 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: hasattr(test1, 'last_msg'), True)
self.assertEqual(test1.last_msg['data'], test2.state['fake_table'])
node.stop()
def test_datasource_sub(self):
@mock.patch.object(nova_client, 'Client', spec_set=True, autospec=True)
def test_datasource_sub(self, nova_mock):
node = helper.make_dsenode_new_partition('testnode')
nova_client = mock.MagicMock()
with mock.patch.object(novaclient.client.Client, '__init__',
return_value=nova_client):
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 = 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)
helper.retry_check_function_return_value(
lambda: nova.last_msg['data'], 42)
self.assertFalse(hasattr(test, "last_msg"))
nova.subscribe('test', 'p')
helper.retry_check_function_return_value(
lambda: hasattr(nova, 'last_msg'), True)
test.publish('p', 42)
helper.retry_check_function_return_value(
lambda: nova.last_msg['data'], 42)
self.assertFalse(hasattr(test, "last_msg"))
node.stop()
def test_datasource_unsub(self):
@mock.patch.object(nova_client, 'Client', spec_set=True, autospec=True)
def test_datasource_unsub(self, nova_mock):
node = helper.make_dsenode_new_partition('testnode')
nova_client = mock.MagicMock()
with mock.patch.object(novaclient.client.Client, '__init__',
return_value=nova_client):
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 = 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)
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)
# hard to test that the message is never delivered
time.sleep(0.2)
self.assertEqual(nova.last_msg['data'], 42)
nova.subscribe('test', 'p')
helper.retry_check_function_return_value(
lambda: hasattr(nova, 'last_msg'), True)
test.publish('p', 42)
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)
# 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_pub(self):
@mock.patch.object(nova_client, 'Client', spec_set=True, autospec=True)
def test_datasource_pub(self, nova_mock):
node = helper.make_dsenode_new_partition('testnode')
nova_client = mock.MagicMock()
with mock.patch.object(novaclient.client.Client, '__init__',
return_value=nova_client):
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 = 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)
helper.retry_check_function_return_value(
lambda: test.last_msg['data'], 42)
self.assertFalse(hasattr(nova, "last_msg"))
test.subscribe('nova', 'p')
helper.retry_check_function_return_value(
lambda: hasattr(test, 'last_msg'), True)
nova.publish('p', 42)
helper.retry_check_function_return_value(
lambda: test.last_msg['data'], 42)
self.assertFalse(hasattr(nova, "last_msg"))
node.stop()
def test_auto_resub(self):
node = helper.make_dsenode_new_partition('testnode')
@ -248,6 +255,7 @@ class TestDSE(base.TestCase):
# check that resub takes place, setting data to initial state
helper.retry_check_function_return_value(
lambda: sub.last_msg['data'], set([]))
node.stop()
def test_datasource_poll(self):
node = helper.make_dsenode_new_partition('testnode')
@ -263,6 +271,7 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: sub.last_msg['data'], set(pub.state['fake_table']))
self.assertFalse(hasattr(pub, "last_msg"))
node.stop()
def test_policy_data(self):
"""Test policy correctly processes initial data snapshot."""
@ -281,6 +290,7 @@ class TestDSE(base.TestCase):
helper.retry_check_db_equal(
engine, 'p(x)', 'p(1) p(2)', target='policy1')
self.assertFalse(hasattr(engine, "last_msg"))
node.stop()
def test_policy_data_update(self):
"""Test policy correctly processes initial data snapshot and update."""
@ -303,6 +313,7 @@ class TestDSE(base.TestCase):
helper.retry_check_db_equal(
engine, 'p(x)', 'p(1) p(2) p(3)', target='policy1')
self.assertFalse(hasattr(engine, "last_msg"))
node.stop()
def test_policy_data_late_sub(self):
"""Test policy correctly processes data on late subscribe."""
@ -325,6 +336,7 @@ class TestDSE(base.TestCase):
helper.retry_check_db_equal(
engine, 'p(x)', 'p(1) p(2) p(3)', target='policy1')
self.assertFalse(hasattr(engine, "last_msg"))
node.stop()
def insert_rule(self, engine, statement, target=None):
statement = compile.parse1(statement)
@ -346,6 +358,7 @@ class TestDSE(base.TestCase):
congressException.NotFound,
lambda: node.invoke_service_rpc(
'test1', 'get_status', {'source_id': None, 'params': None}))
node.stop()
def _create_node_with_services(self, nodes, services, num, partition_id):
nid = 'cbd_node%s' % num
@ -488,6 +501,7 @@ class TestDSE(base.TestCase):
policy.insert('q(4)', target='data')
# TODO(ekcs): test that no publish triggered (because no subscribers)
node.stop()
def test_replicated_pe_exec(self):
"""Test correct local leader behavior with 2 PEs requesting exec"""
@ -540,3 +554,5 @@ class TestDSE(base.TestCase):
helper.retry_check_function_return_value(
lambda: len(dsd.exec_history), 3)
self.assertEqual(dsd._leader_node_id, 'testnode1')
node1.stop()
node2.stop()

View File

@ -59,7 +59,6 @@ class BaseTestPolicyCongress(base.SqlTestCase):
# FIXME(ekcs): this is a hack to prevent the synchronizer from
# attempting to delete this DSD because it's not in DB
neutronv2.type = 'no_sync_datasource_driver'
self.node.register_service(neutronv2)
neutron_mock = mock.MagicMock(spec=neutronclient.v2_0.client.Client)
neutronv2.neutron = neutron_mock
@ -72,6 +71,7 @@ class BaseTestPolicyCongress(base.SqlTestCase):
neutron_mock.list_ports.return_value = port_response
neutron_mock.list_routers.return_value = router_response
neutron_mock.list_security_groups.return_value = sg_group_response
self.node.register_service(neutronv2)
return neutronv2
@ -248,7 +248,6 @@ class TestPolicyExecute(BaseTestPolicyCongress):
ds = nova_driver.NovaDriver('nova', args=args)
if name == 'neutron':
ds = neutronv2_driver.NeutronV2Driver('neutron', args=args)
self.node.register_service(ds)
ds.update_from_datasource = mock.MagicMock()
return ds
@ -264,6 +263,7 @@ class TestPolicyExecute(BaseTestPolicyCongress):
nova_client = NovaClient("testing")
nova = self.nova
nova.nova_client = nova_client
self.node.register_service(nova)
# insert rule and data
self.api['api-policy'].add_item({'name': 'alice'}, {})
@ -313,6 +313,7 @@ class TestPolicyExecute(BaseTestPolicyCongress):
nova_client = NovaClient(None)
nova = self.nova
nova.nova_client = nova_client
self.node.register_service(nova)
# insert rule and data
self.api['api-policy'].add_item({'name': 'alice'}, {})
@ -347,6 +348,7 @@ class TestPolicyExecute(BaseTestPolicyCongress):
nova_client = NovaClient(None)
nova = self.nova
nova.nova_client = nova_client
self.node.register_service(nova)
self.api['api-policy'].add_item({'name': 'alice'}, {})
self.api['api-rule'].add_item(
@ -372,6 +374,7 @@ class TestPolicyExecute(BaseTestPolicyCongress):
nova_client = NovaClient(None)
nova = self.nova
nova.nova_client = nova_client
self.node.register_service(nova)
# Note: this probably isn't the behavior we really want.
# But at least we have a test documenting that behavior.