diff --git a/heat/tests/openstack/nova/test_floatingip.py b/heat/tests/openstack/nova/test_floatingip.py index 45d9d600a9..b0cecc4c7a 100644 --- a/heat/tests/openstack/nova/test_floatingip.py +++ b/heat/tests/openstack/nova/test_floatingip.py @@ -66,12 +66,12 @@ class NovaFloatingIPTest(common.HeatTestCase): self.novaclient = fakes_nova.FakeClient() self.patchobject(nova.NovaClientPlugin, '_create', return_value=self.novaclient) - self.m.StubOutWithMock(neutronclient.Client, - 'create_floatingip') - self.m.StubOutWithMock(neutronclient.Client, - 'update_floatingip') - self.m.StubOutWithMock(neutronclient.Client, - 'delete_floatingip') + self.mock_create_fip = self.patchobject(neutronclient.Client, + 'create_floatingip') + self.mock_upd_fip = self.patchobject(neutronclient.Client, + 'update_floatingip') + self.mock_del_fip = self.patchobject(neutronclient.Client, + 'delete_floatingip') self.patchobject(neutron.NeutronClientPlugin, 'find_resourceid_by_name_or_id', return_value='eeee') @@ -85,41 +85,12 @@ class NovaFloatingIPTest(common.HeatTestCase): return MockIface(port, ip) def mock_create_floatingip(self): - neutronclient.Client.create_floatingip({ - 'floatingip': {'floating_network_id': u'eeee'} - }).AndReturn({'floatingip': { + self.mock_create_fip.return_value = {'floatingip': { "status": "ACTIVE", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", 'floating_network_id': 'eeee', "floating_ip_address": "11.0.0.1" - }}) - - def mock_update_floatingip(self, - fip='fc68ea2c-b60b-4b4f-bd82-94ec81110766', - ex=None, fip_request=None, - delete_assc=False): - if fip_request: - request_body = fip_request - elif delete_assc: - request_body = { - 'floatingip': { - 'port_id': None, - 'fixed_ip_address': None}} - else: - request_body = { - 'floatingip': { - 'port_id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', - 'fixed_ip_address': '1.2.3.4'}} - if ex: - neutronclient.Client.update_floatingip( - fip, request_body).AndRaise(ex) - else: - neutronclient.Client.update_floatingip( - fip, request_body).AndReturn(None) - - def mock_delete_floatingip(self): - id = 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' - neutronclient.Client.delete_floatingip(id).AndReturn(None) + }} def prepare_floating_ip(self): self.mock_create_floatingip() @@ -138,6 +109,7 @@ class NovaFloatingIPTest(common.HeatTestCase): iface = self.mock_interface('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '1.2.3.4') self.patchobject(return_server, 'interface_list', return_value=[iface]) + template = template_format.parse(floating_ip_template_with_assoc) self.stack = utils.parse_stack(template) resource_defns = self.stack.t.resource_definitions(self.stack) @@ -148,36 +120,30 @@ class NovaFloatingIPTest(common.HeatTestCase): def test_floating_ip_create(self): rsrc = self.prepare_floating_ip() - self.m.ReplayAll() - rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) + self.mock_create_fip.assert_called_with( + {'floatingip': {'floating_network_id': 'eeee'}}) self.assertEqual('fc68ea2c-b60b-4b4f-bd82-94ec81110766', rsrc.FnGetRefId()) self.assertEqual('11.0.0.1', rsrc.FnGetAtt('ip')) self.assertEqual('eeee', rsrc.FnGetAtt('pool')) - self.m.VerifyAll() - def test_floating_ip_delete(self): rsrc = self.prepare_floating_ip() - self.mock_delete_floatingip() - self.m.ReplayAll() - rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) scheduler.TaskRunner(rsrc.delete)() self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) - - self.m.VerifyAll() + self.mock_del_fip.assert_called_with( + 'fc68ea2c-b60b-4b4f-bd82-94ec81110766') def test_delete_floating_ip_assoc_successful_if_create_failed(self): rsrc = self.prepare_floating_ip_assoc() - self.mock_update_floatingip(fakes_nova.fake_exception(400)) - self.m.ReplayAll() + self.mock_upd_fip.side_effect = [fakes_nova.fake_exception(400)] rsrc.validate() self.assertRaises(heat_ex.ResourceFailure, @@ -186,62 +152,51 @@ class NovaFloatingIPTest(common.HeatTestCase): scheduler.TaskRunner(rsrc.delete)() self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) - self.m.VerifyAll() - def test_floating_ip_assoc_create(self): rsrc = self.prepare_floating_ip_assoc() - self.mock_update_floatingip() - self.m.ReplayAll() - + fip_request = {'floatingip': { + 'fixed_ip_address': '1.2.3.4', + 'port_id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'} + } rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) + self.mock_upd_fip.assert_called_with( + 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', fip_request) self.assertIsNotNone(rsrc.id) self.assertEqual(rsrc.id, rsrc.resource_id) - self.m.VerifyAll() - def test_floating_ip_assoc_delete(self): rsrc = self.prepare_floating_ip_assoc() - self.mock_update_floatingip() - self.mock_update_floatingip(delete_assc=True) - self.m.ReplayAll() - rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) scheduler.TaskRunner(rsrc.delete)() self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) + fip_request = {'floatingip': { + 'fixed_ip_address': None, 'port_id': None}} + self.mock_upd_fip.assert_called_with( + 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', fip_request) - self.m.VerifyAll() - - def test_floating_ip_assoc_delete_not_found(self): + def create_delete_assoc_with_exc(self, exc): rsrc = self.prepare_floating_ip_assoc() - self.mock_update_floatingip() - self.mock_update_floatingip(ex=fakes_nova.fake_exception(404), - delete_assc=True) - self.m.ReplayAll() - rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) + self.mock_upd_fip.side_effect = [exc] scheduler.TaskRunner(rsrc.delete)() self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) - self.m.VerifyAll() + def test_floating_ip_assoc_delete_neutron_not_found(self): + self.create_delete_assoc_with_exc(neutronclient.exceptions.NotFound()) + + def test_floating_ip_assoc_delete_nova_not_found(self): + self.create_delete_assoc_with_exc(fakes_nova.fake_exception(404)) def test_floating_ip_assoc_update_server_id(self): rsrc = self.prepare_floating_ip_assoc() - self.mock_update_floatingip() - fip_request = {'floatingip': { - 'fixed_ip_address': '4.5.6.7', - 'port_id': 'bbbbb-bbbb-bbbb-bbbbbbbbb'} - } - self.mock_update_floatingip(fip_request=fip_request) - self.m.ReplayAll() - rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) @@ -263,67 +218,88 @@ class NovaFloatingIPTest(common.HeatTestCase): scheduler.TaskRunner(rsrc.update, update_snippet)() self.assertEqual((rsrc.UPDATE, rsrc.COMPLETE), rsrc.state) - self.m.VerifyAll() + fip_request = {'floatingip': { + 'fixed_ip_address': '1.2.3.4', + 'port_id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'} + } + fip_request_update = {'floatingip': { + 'fixed_ip_address': '4.5.6.7', + 'port_id': 'bbbbb-bbbb-bbbb-bbbbbbbbb'} + } + calls = [mock.call('fc68ea2c-b60b-4b4f-bd82-94ec81110766', + fip_request), + mock.call('fc68ea2c-b60b-4b4f-bd82-94ec81110766', + fip_request_update)] + self.mock_upd_fip.assert_has_calls(calls) + self.assertEqual(2, self.mock_upd_fip.call_count) def test_floating_ip_assoc_update_fl_ip(self): rsrc = self.prepare_floating_ip_assoc() - # for create - self.mock_update_floatingip() - # mock for delete the old association - self.mock_update_floatingip(delete_assc=True) - # mock for new association - self.mock_update_floatingip(fip='fc68ea2c-dddd-4b4f-bd82-94ec81110766') - self.m.ReplayAll() - rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) + # update with the new floatingip props = copy.deepcopy(rsrc.properties.data) - props['floating_ip'] = 'fc68ea2c-dddd-4b4f-bd82-94ec81110766' + props['floating_ip'] = 'fc68ea2c-cccc-4b4f-bd82-94ec81110766' update_snippet = rsrc_defn.ResourceDefinition(rsrc.name, rsrc.type(), props) scheduler.TaskRunner(rsrc.update, update_snippet)() self.assertEqual((rsrc.UPDATE, rsrc.COMPLETE), rsrc.state) - self.m.VerifyAll() + fip_request = {'floatingip': { + 'fixed_ip_address': '1.2.3.4', + 'port_id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'} + } + fip_request_none = {'floatingip': { + 'fixed_ip_address': None, 'port_id': None}} + calls = [mock.call('fc68ea2c-b60b-4b4f-bd82-94ec81110766', + fip_request), + mock.call('fc68ea2c-b60b-4b4f-bd82-94ec81110766', + fip_request_none), + mock.call('fc68ea2c-cccc-4b4f-bd82-94ec81110766', + fip_request)] + self.mock_upd_fip.assert_has_calls(calls) + self.assertEqual(3, self.mock_upd_fip.call_count) def test_floating_ip_assoc_update_both(self): rsrc = self.prepare_floating_ip_assoc() - # for create - self.mock_update_floatingip() - # mock for delete the old association - self.mock_update_floatingip(delete_assc=True) - # mock for new association - fip_request = {'floatingip': { - 'fixed_ip_address': '4.5.6.7', - 'port_id': 'bbbbb-bbbb-bbbb-bbbbbbbbb'} - } - self.mock_update_floatingip(fip='fc68ea2c-dddd-4b4f-bd82-94ec81110766', - fip_request=fip_request) - self.m.ReplayAll() - rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) - # update with the new floatingip and server + # update with the new floatingip return_server = self.novaclient.servers.list()[2] self.patchobject(self.novaclient.servers, 'get', return_value=return_server) iface = self.mock_interface('bbbbb-bbbb-bbbb-bbbbbbbbb', '4.5.6.7') self.patchobject(return_server, 'interface_list', return_value=[iface]) - props = copy.deepcopy(rsrc.properties.data) update_server_id = '2146dfbf-ba77-4083-8e86-d052f671ece5' props['server_id'] = update_server_id - props['floating_ip'] = 'fc68ea2c-dddd-4b4f-bd82-94ec81110766' + props['floating_ip'] = 'fc68ea2c-cccc-4b4f-bd82-94ec81110766' update_snippet = rsrc_defn.ResourceDefinition(rsrc.name, rsrc.type(), props) scheduler.TaskRunner(rsrc.update, update_snippet)() self.assertEqual((rsrc.UPDATE, rsrc.COMPLETE), rsrc.state) - - self.m.VerifyAll() + fip_request = {'floatingip': { + 'port_id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', + 'fixed_ip_address': '1.2.3.4'} + } + fip_request_none = {'floatingip': { + 'port_id': None, 'fixed_ip_address': None}} + fip_request_update = {'floatingip': { + 'port_id': 'bbbbb-bbbb-bbbb-bbbbbbbbb', + 'fixed_ip_address': '4.5.6.7'} + } + calls = [mock.call('fc68ea2c-b60b-4b4f-bd82-94ec81110766', + fip_request), + mock.call('fc68ea2c-b60b-4b4f-bd82-94ec81110766', + fip_request_none), + mock.call('fc68ea2c-cccc-4b4f-bd82-94ec81110766', + fip_request_update)] + self.mock_upd_fip.assert_has_calls(calls) + self.assertEqual(3, self.mock_upd_fip.call_count) def test_floating_ip_assoc_refid_rsrc_name(self): t = template_format.parse(floating_ip_template_with_assoc)