def test_create_provider_net(self): resource_type = 'networks' rsrc = self.create_provider_net() self.mockclient.show_network.side_effect = [ stpnb, stpna, qe.NetworkNotFoundClient(status_code=404), stpna, qe.NetworkNotFoundClient(status_code=404), ] self.mockclient.delete_network.side_effect = [ None, qe.NetworkNotFoundClient(status_code=404), ] rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) ref_id = rsrc.FnGetRefId() self.assertEqual('fc68ea2c-b60b-4b4f-bd82-94ec81110766', ref_id) self.assertIsNone(rsrc.FnGetAtt('status')) self.assertEqual('ACTIVE', rsrc.FnGetAtt('status')) self.assertRaises( exception.InvalidTemplateAttribute, rsrc.FnGetAtt, 'Foo') self.assertIsNone(scheduler.TaskRunner(rsrc.delete)()) self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) rsrc.state_set(rsrc.CREATE, rsrc.COMPLETE, 'to delete again') scheduler.TaskRunner(rsrc.delete)() self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) self.mockclient.create_network.assert_called_once_with({ 'network': { 'name': u'the_provider_network', 'admin_state_up': True, 'provider:network_type': 'vlan', 'provider:physical_network': 'physnet_1', 'provider:segmentation_id': '101', 'router:external': False, 'shared': True } }) self.mockclient.replace_tag.assert_called_with( resource_type, 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'tags': ['tag1', 'tag2']} ) self.mockclient.show_network.assert_called_with( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766') self.assertEqual(5, self.mockclient.show_network.call_count) self.mockclient.delete_network.assert_called_with( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766') self.assertEqual(2, self.mockclient.delete_network.call_count)
def test_create_provider_net(self): rsrc = self.create_provider_net() neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndRaise(qe.NetworkNotFoundClient(status_code=404)) # Delete script neutronclient.Client.delete_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn(None) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn(stpna) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndRaise(qe.NetworkNotFoundClient(status_code=404)) neutronclient.Client.delete_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndRaise(qe.NetworkNotFoundClient(status_code=404)) self.m.ReplayAll() rsrc.validate() scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) ref_id = rsrc.FnGetRefId() self.assertEqual('fc68ea2c-b60b-4b4f-bd82-94ec81110766', ref_id) self.assertIsNone(rsrc.FnGetAtt('status')) self.assertEqual('ACTIVE', rsrc.FnGetAtt('status')) self.assertRaises( exception.InvalidTemplateAttribute, rsrc.FnGetAtt, 'Foo') self.assertIsNone(scheduler.TaskRunner(rsrc.delete)()) self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) rsrc.state_set(rsrc.CREATE, rsrc.COMPLETE, 'to delete again') scheduler.TaskRunner(rsrc.delete)() self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) self.m.VerifyAll()
def create_port(self, body=None): """Mock'd version of neutronclient...create_port(). Create a Neutron network port. :param body: Dictionary with port information. :return: An updated copy of the 'body' that was passed in, with other information populated. """ if body and body['port'] and body['port']['network_id']: network_id = body['port']['network_id'] if network_id not in self._network_list: raise exceptions.NetworkNotFoundClient( "network %s not found" % network_id ) else: body = {'port': {}} port_id = str(uuid.uuid4()) body['port']['id'] = port_id body['port']['fixed_ips'] = [] body['port']['fixed_ips'].append({'ip_address': '0.0.0.0'}) self._port_list[port_id] = body['port'].copy() return body
class TestIsNotFound(common.HeatTestCase): scenarios = [ ('ceilometer_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='ceilometer', exception=lambda: ceil_exc.HTTPNotFound(details='gone'), )), ('ceilometer_not_found_apiclient', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='ceilometer', exception=lambda: c_a_exc.NotFound(details='gone'), )), ('ceilometer_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='ceilometer', exception=lambda: Exception() )), ('ceilometer_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='ceilometer', exception=lambda: ceil_exc.HTTPOverLimit(details='over'), )), ('ceilometer_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='ceilometer', exception=lambda: ceil_exc.HTTPConflict(), )), ('cinder_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='cinder', exception=lambda: cinder_exc.NotFound(code=404), )), ('cinder_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='cinder', exception=lambda: Exception() )), ('cinder_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='cinder', exception=lambda: cinder_exc.OverLimit(code=413), )), ('cinder_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='cinder', exception=lambda: cinder_exc.ClientException(code=409), )), ('glance_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='glance', exception=lambda: glance_exc.HTTPNotFound(details='gone'), )), ('glance_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='glance', exception=lambda: Exception() )), ('glance_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='glance', exception=lambda: glance_exc.HTTPOverLimit(details='over'), )), ('glance_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='glance', exception=lambda: glance_exc.HTTPConflict(), )), ('heat_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='heat', exception=lambda: heat_exc.HTTPNotFound(message='gone'), )), ('heat_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='heat', exception=lambda: Exception() )), ('heat_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='heat', exception=lambda: heat_exc.HTTPOverLimit(message='over'), )), ('heat_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='heat', exception=lambda: heat_exc.HTTPConflict(), )), ('keystone_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='keystone', exception=lambda: keystone_exc.NotFound(details='gone'), )), ('keystone_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='keystone', exception=lambda: Exception() )), ('keystone_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='keystone', exception=lambda: keystone_exc.RequestEntityTooLarge( details='over'), )), ('keystone_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='keystone', exception=lambda: keystone_exc.Conflict( message='Conflict'), )), ('neutron_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='neutron', exception=lambda: neutron_exc.NotFound, )), ('neutron_network_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='neutron', exception=lambda: neutron_exc.NetworkNotFoundClient(), )), ('neutron_port_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='neutron', exception=lambda: neutron_exc.PortNotFoundClient(), )), ('neutron_status_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='neutron', exception=lambda: neutron_exc.NeutronClientException( status_code=404), )), ('neutron_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='neutron', exception=lambda: Exception() )), ('neutron_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='neutron', exception=lambda: neutron_exc.NeutronClientException( status_code=413), )), ('neutron_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='neutron', exception=lambda: neutron_exc.Conflict(), )), ('nova_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, is_unprocessable_entity=False, plugin='nova', exception=lambda: fakes_nova.fake_exception(), )), ('nova_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, is_unprocessable_entity=False, plugin='nova', exception=lambda: Exception() )), ('nova_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, is_unprocessable_entity=False, plugin='nova', exception=lambda: fakes_nova.fake_exception(413), )), ('nova_unprocessable_entity', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=False, is_unprocessable_entity=True, plugin='nova', exception=lambda: fakes_nova.fake_exception(422), )), ('nova_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, is_unprocessable_entity=False, plugin='nova', exception=lambda: fakes_nova.fake_exception(409), )), ('swift_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='swift', exception=lambda: swift_exc.ClientException( msg='gone', http_status=404), )), ('swift_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='swift', exception=lambda: Exception() )), ('swift_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='swift', exception=lambda: swift_exc.ClientException( msg='ouch', http_status=413), )), ('swift_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='swift', exception=lambda: swift_exc.ClientException( msg='conflict', http_status=409), )), ('trove_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='trove', exception=lambda: troveclient.exceptions.NotFound(message='gone'), )), ('trove_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='trove', exception=lambda: Exception() )), ('trove_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='trove', exception=lambda: troveclient.exceptions.RequestEntityTooLarge( message='over'), )), ('trove_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='trove', exception=lambda: troveclient.exceptions.Conflict( message='Conflict'), )), ('sahara_not_found', dict( is_not_found=True, is_over_limit=False, is_client_exception=True, is_conflict=False, plugin='sahara', exception=lambda: sahara_base.APIException( error_message='gone1', error_code=404), )), ('sahara_exception', dict( is_not_found=False, is_over_limit=False, is_client_exception=False, is_conflict=False, plugin='sahara', exception=lambda: Exception() )), ('sahara_overlimit', dict( is_not_found=False, is_over_limit=True, is_client_exception=True, is_conflict=False, plugin='sahara', exception=lambda: sahara_base.APIException( error_message='over1', error_code=413), )), ('sahara_conflict', dict( is_not_found=False, is_over_limit=False, is_client_exception=True, is_conflict=True, plugin='sahara', exception=lambda: sahara_base.APIException( error_message='conflict1', error_code=409), )), ] def test_is_not_found(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) try: raise self.exception() except Exception as e: if self.is_not_found != client_plugin.is_not_found(e): raise def test_ignore_not_found(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) try: exp = self.exception() exp_class = exp.__class__ raise exp except Exception as e: if self.is_not_found: client_plugin.ignore_not_found(e) else: self.assertRaises(exp_class, client_plugin.ignore_not_found, e) def test_ignore_conflict_and_not_found(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) try: exp = self.exception() exp_class = exp.__class__ raise exp except Exception as e: if self.is_conflict or self.is_not_found: client_plugin.ignore_conflict_and_not_found(e) else: self.assertRaises(exp_class, client_plugin.ignore_conflict_and_not_found, e) def test_is_over_limit(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) try: raise self.exception() except Exception as e: if self.is_over_limit != client_plugin.is_over_limit(e): raise def test_is_client_exception(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) try: raise self.exception() except Exception as e: ice = self.is_client_exception actual = client_plugin.is_client_exception(e) if ice != actual: raise def test_is_conflict(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) try: raise self.exception() except Exception as e: if self.is_conflict != client_plugin.is_conflict(e): raise def test_is_unprocessable_entity(self): con = mock.Mock() c = clients.Clients(con) # only 'nova' client plugin need to check this exception if self.plugin == 'nova': client_plugin = c.client_plugin(self.plugin) try: raise self.exception() except Exception as e: iue = self.is_unprocessable_entity if iue != client_plugin.is_unprocessable_entity(e): raise
def test_net(self): neutronV20.find_resourceid_by_name_or_id( mox.IsA(neutronclient.Client), 'router', '792ff887-6c85-4a56-b518-23f24fa65581', cmd_resource=None, ).MultipleTimes().AndReturn('792ff887-6c85-4a56-b518-23f24fa65581') # Create script neutronclient.Client.create_network({ 'network': { 'name': u'the_network', 'admin_state_up': True, 'tenant_id': 'c1210485b2424d48804aad5d39c61b8f', 'port_security_enabled': False, 'shared': True } }).AndReturn({ "network": { "status": "BUILD", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 } }) neutronclient.Client.list_dhcp_agent_hosting_networks( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn({"agents": []}) neutronclient.Client.add_network_to_dhcp_agent( '28c25a04-3f73-45a7-a2b4-59e183943ddc', { 'network_id': u'fc68ea2c-b60b-4b4f-bd82-94ec81110766' }).AndReturn(None) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn({ "network": { "status": "BUILD", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 } }) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn({ "network": { "status": "ACTIVE", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 } }) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndRaise( qe.NetworkNotFoundClient(status_code=404)) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn({ "network": { "status": "ACTIVE", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 } }) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn({ "network": { "status": "ACTIVE", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 } }) # Update script neutronclient.Client.list_dhcp_agent_hosting_networks( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn({ "agents": [{ "admin_state_up": True, "agent_type": "DHCP agent", "alive": True, "binary": "neutron-dhcp-agent", "configurations": { "dhcp_driver": "DummyDriver", "dhcp_lease_duration": 86400, "networks": 0, "ports": 0, "subnets": 0, "use_namespaces": True }, "created_at": "2014-03-20 05:12:34", "description": None, "heartbeat_timestamp": "2014-03-20 05:12:34", "host": "hostname", "id": "28c25a04-3f73-45a7-a2b4-59e183943ddc", "started_at": "2014-03-20 05:12:34", "topic": "dhcp_agent" }] }) neutronclient.Client.add_network_to_dhcp_agent( 'bb09cfcd-5277-473d-8336-d4ed8628ae68', { 'network_id': u'fc68ea2c-b60b-4b4f-bd82-94ec81110766' }).AndReturn(None) neutronclient.Client.remove_network_from_dhcp_agent( '28c25a04-3f73-45a7-a2b4-59e183943ddc', 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn(None) # Update script neutronclient.Client.update_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', { 'network': { 'name': 'mynet', 'qos_policy_id': '0389f747-7785-4757-b7bb-2ab07e4b09c3' } }).AndReturn(None) # update again to detach qos_policy neutronclient.Client.update_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', { 'network': { 'name': 'mynet', 'qos_policy_id': None } }).AndReturn(None) neutronclient.Client.update_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', { 'network': { 'name': 'mynet', 'port_security_enabled': True, 'qos_policy_id': None } }).AndReturn(None) # update with name = None neutronclient.Client.update_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', { 'network': { 'name': utils.PhysName('test_stack', 'test_net'), } }).AndReturn(None) # Delete script neutronclient.Client.delete_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndReturn(None) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndRaise( qe.NetworkNotFoundClient(status_code=404)) neutronclient.Client.delete_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766').AndRaise( qe.NetworkNotFoundClient(status_code=404)) self.patchobject(neutron.NeutronClientPlugin, 'get_qos_policy_id', return_value='0389f747-7785-4757-b7bb-2ab07e4b09c3') self.m.ReplayAll() t = template_format.parse(neutron_template) stack = utils.parse_stack(t) self.patchobject(stack['router'], 'FnGetRefId', return_value='792ff887-6c85-4a56-b518-23f24fa65581') rsrc = self.create_net(t, stack, 'network') # assert the implicit dependency between the gateway and the interface deps = stack.dependencies[stack['router_interface']] self.assertIn(stack['gateway'], deps) # assert the implicit dependency between the gateway and the subnet deps = stack.dependencies[stack['subnet']] self.assertIn(stack['gateway'], deps) rsrc.validate() ref_id = rsrc.FnGetRefId() self.assertEqual('fc68ea2c-b60b-4b4f-bd82-94ec81110766', ref_id) self.assertIsNone(rsrc.FnGetAtt('status')) self.assertEqual('ACTIVE', rsrc.FnGetAtt('status')) self.assertEqual(0, rsrc.FnGetAtt('mtu')) self.assertRaises(exception.InvalidTemplateAttribute, rsrc.FnGetAtt, 'Foo') prop_diff = { "name": "mynet", "dhcp_agent_ids": ["bb09cfcd-5277-473d-8336-d4ed8628ae68"], 'qos_policy': '0389f747-7785-4757-b7bb-2ab07e4b09c3' } update_snippet = rsrc_defn.ResourceDefinition(rsrc.name, rsrc.type(), prop_diff) rsrc.handle_update(update_snippet, {}, prop_diff) # Update with None qos_policy prop_diff['qos_policy'] = None rsrc.handle_update(update_snippet, {}, prop_diff) # Update with value_specs prop_diff['value_specs'] = {"port_security_enabled": True} rsrc.handle_update(update_snippet, {}, prop_diff) # Update with name = None rsrc.handle_update(update_snippet, {}, {'name': None}) # Update with empty prop_diff rsrc.handle_update(update_snippet, {}, {}) scheduler.TaskRunner(rsrc.delete)() rsrc.state_set(rsrc.CREATE, rsrc.COMPLETE, 'to delete again') scheduler.TaskRunner(rsrc.delete)() self.m.VerifyAll()
def test_net(self): t = template_format.parse(neutron_template) stack = utils.parse_stack(t) resource_type = 'networks' net_info_build = {"network": { "status": "BUILD", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 }} net_info_active = copy.deepcopy(net_info_build) net_info_active['network'].update({'status': 'ACTIVE'}) agent_info = { "agents": [{ "admin_state_up": True, "agent_type": "DHCP agent", "alive": True, "binary": "neutron-dhcp-agent", "configurations": { "dhcp_driver": "DummyDriver", "dhcp_lease_duration": 86400, "networks": 0, "ports": 0, "subnets": 0, "use_namespaces": True}, "created_at": "2014-03-20 05:12:34", "description": None, "heartbeat_timestamp": "2014-03-20 05:12:34", "host": "hostname", "id": "28c25a04-3f73-45a7-a2b4-59e183943ddc", "started_at": "2014-03-20 05:12:34", "topic": "dhcp_agent" }] } create_mock = self.patchobject(neutronclient.Client, 'create_network') create_mock.return_value = net_info_build list_dhcp_agent_mock = self.patchobject( neutronclient.Client, 'list_dhcp_agent_hosting_networks') list_dhcp_agent_mock.side_effect = [{"agents": []}, agent_info] add_dhcp_agent_mock = self.patchobject( neutronclient.Client, 'add_network_to_dhcp_agent') remove_dhcp_agent_mock = self.patchobject( neutronclient.Client, 'remove_network_from_dhcp_agent') replace_tag_mock = self.patchobject( neutronclient.Client, 'replace_tag') show_network_mock = self.patchobject( neutronclient.Client, 'show_network') show_network_mock.side_effect = [ net_info_build, net_info_active, qe.NetworkNotFoundClient(status_code=404), net_info_active, net_info_active, qe.NetworkNotFoundClient(status_code=404)] update_net_mock = self.patchobject( neutronclient.Client, 'update_network') del_net_mock = self.patchobject( neutronclient.Client, 'delete_network') del_net_mock.side_effect = [None, qe.NetworkNotFoundClient(status_code=404)] self.patchobject(neutron.NeutronClientPlugin, 'get_qos_policy_id', return_value='0389f747-7785-4757-b7bb-2ab07e4b09c3') self.patchobject(stack['router'], 'FnGetRefId', return_value='792ff887-6c85-4a56-b518-23f24fa65581') # network create rsrc = self.create_net(t, stack, 'network') create_mock.assert_called_with( {'network': {'name': u'the_network', 'admin_state_up': True, 'tenant_id': u'c1210485b2424d48804aad5d39c61b8f', 'dns_domain': u'openstack.org.', 'shared': True, 'port_security_enabled': False} } ) add_dhcp_agent_mock.assert_called_with( '28c25a04-3f73-45a7-a2b4-59e183943ddc', {'network_id': u'fc68ea2c-b60b-4b4f-bd82-94ec81110766'}) replace_tag_mock.assert_called_with( resource_type, 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'tags': ['tag1', 'tag2']} ) # assert the implicit dependency between the gateway and the interface deps = stack.dependencies[stack['router_interface']] self.assertIn(stack['gateway'], deps) # assert the implicit dependency between the gateway and the subnet deps = stack.dependencies[stack['subnet']] self.assertIn(stack['gateway'], deps) rsrc.validate() ref_id = rsrc.FnGetRefId() self.assertEqual('fc68ea2c-b60b-4b4f-bd82-94ec81110766', ref_id) self.assertIsNone(rsrc.FnGetAtt('status')) self.assertEqual('ACTIVE', rsrc.FnGetAtt('status')) self.assertEqual(0, rsrc.FnGetAtt('mtu')) self.assertRaises( exception.InvalidTemplateAttribute, rsrc.FnGetAtt, 'Foo') # update tests prop_diff = { "name": "mynet", "dhcp_agent_ids": [ "bb09cfcd-5277-473d-8336-d4ed8628ae68" ], 'qos_policy': '0389f747-7785-4757-b7bb-2ab07e4b09c3' } update_snippet = rsrc_defn.ResourceDefinition(rsrc.name, rsrc.type(), prop_diff) rsrc.handle_update(update_snippet, {}, prop_diff) update_net_mock.assert_called_with( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'network': { 'name': 'mynet', 'qos_policy_id': '0389f747-7785-4757-b7bb-2ab07e4b09c3' }}) add_dhcp_agent_mock.assert_called_with( 'bb09cfcd-5277-473d-8336-d4ed8628ae68', {'network_id': 'fc68ea2c-b60b-4b4f-bd82-94ec81110766'}) remove_dhcp_agent_mock.assert_called_with( '28c25a04-3f73-45a7-a2b4-59e183943ddc', 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ) # Update with None qos_policy prop_diff['qos_policy'] = None rsrc.handle_update(update_snippet, {}, prop_diff) update_net_mock.assert_called_with( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'network': { 'name': 'mynet', 'qos_policy_id': None }} ) # Update with value_specs prop_diff['value_specs'] = {"port_security_enabled": True} rsrc.handle_update(update_snippet, {}, prop_diff) update_net_mock.assert_called_with( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'network': { 'name': 'mynet', 'port_security_enabled': True, 'qos_policy_id': None }} ) # Update with name = None rsrc.handle_update(update_snippet, {}, {'name': None}) update_net_mock.assert_called_with( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'network': { 'name': utils.PhysName(stack.name, 'test_net'), }} ) # Update with tags=[] rsrc.handle_update(update_snippet, {}, {'tags': []}) replace_tag_mock.assert_called_with( resource_type, 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'tags': []} ) # Update with empty prop_diff rsrc.handle_update(update_snippet, {}, {}) # update delete scheduler.TaskRunner(rsrc.delete)() del_net_mock.assert_called_with('fc68ea2c-b60b-4b4f-bd82-94ec81110766') # delete raise not found rsrc.state_set(rsrc.CREATE, rsrc.COMPLETE, 'to delete again') scheduler.TaskRunner(rsrc.delete)() self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)
def show_network(self, network_id, **_params): if network_id not in self._networks: raise neutron_client_exc.NetworkNotFoundClient() return {'network': copy.deepcopy(self._networks[network_id])}
def test_net(self): # Create script neutronclient.Client.create_network({ 'network': { 'name': u'the_network', 'admin_state_up': True, 'tenant_id': 'c1210485b2424d48804aad5d39c61b8f', 'port_security_enabled': False, 'shared': True} }).AndReturn({"network": { "status": "BUILD", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 }}) neutronclient.Client.list_dhcp_agent_hosting_networks( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn({"agents": []}) neutronclient.Client.add_network_to_dhcp_agent( '28c25a04-3f73-45a7-a2b4-59e183943ddc', {'network_id': u'fc68ea2c-b60b-4b4f-bd82-94ec81110766'} ).AndReturn(None) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn({"network": { "status": "BUILD", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 }}) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn({"network": { "status": "ACTIVE", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 }}) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndRaise(qe.NetworkNotFoundClient(status_code=404)) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn({"network": { "status": "ACTIVE", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 }}) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn({"network": { "status": "ACTIVE", "subnets": [], "name": "name", "admin_state_up": True, "shared": True, "tenant_id": "c1210485b2424d48804aad5d39c61b8f", "id": "fc68ea2c-b60b-4b4f-bd82-94ec81110766", "mtu": 0 }}) # Update script neutronclient.Client.list_dhcp_agent_hosting_networks( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn({ "agents": [{ "admin_state_up": True, "agent_type": "DHCP agent", "alive": True, "binary": "neutron-dhcp-agent", "configurations": { "dhcp_driver": "DummyDriver", "dhcp_lease_duration": 86400, "networks": 0, "ports": 0, "subnets": 0, "use_namespaces": True}, "created_at": "2014-03-20 05:12:34", "description": None, "heartbeat_timestamp": "2014-03-20 05:12:34", "host": "hostname", "id": "28c25a04-3f73-45a7-a2b4-59e183943ddc", "started_at": "2014-03-20 05:12:34", "topic": "dhcp_agent" }] }) neutronclient.Client.add_network_to_dhcp_agent( 'bb09cfcd-5277-473d-8336-d4ed8628ae68', {'network_id': u'fc68ea2c-b60b-4b4f-bd82-94ec81110766'} ).AndReturn(None) neutronclient.Client.remove_network_from_dhcp_agent( '28c25a04-3f73-45a7-a2b4-59e183943ddc', 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn(None) neutronclient.Client.update_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766', {'network': { 'shared': True, 'name': 'mynet', 'admin_state_up': True, 'port_security_enabled': False }}).AndReturn(None) # Delete script neutronclient.Client.delete_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndReturn(None) neutronclient.Client.show_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndRaise(qe.NetworkNotFoundClient(status_code=404)) neutronclient.Client.delete_network( 'fc68ea2c-b60b-4b4f-bd82-94ec81110766' ).AndRaise(qe.NetworkNotFoundClient(status_code=404)) self.m.ReplayAll() t = template_format.parse(neutron_template) stack = utils.parse_stack(t) rsrc = self.create_net(t, stack, 'network') # assert the implicit dependency between the gateway and the interface deps = stack.dependencies[stack['router_interface']] self.assertIn(stack['gateway'], deps) # assert the implicit dependency between the gateway and the subnet deps = stack.dependencies[stack['subnet']] self.assertIn(stack['gateway'], deps) rsrc.validate() ref_id = rsrc.FnGetRefId() self.assertEqual('fc68ea2c-b60b-4b4f-bd82-94ec81110766', ref_id) self.assertIsNone(rsrc.FnGetAtt('status')) self.assertEqual('ACTIVE', rsrc.FnGetAtt('status')) self.assertEqual(0, rsrc.FnGetAtt('mtu')) self.assertRaises( exception.InvalidTemplateAttribute, rsrc.FnGetAtt, 'Foo') prop_diff = { "name": "mynet", "dhcp_agent_ids": [ "bb09cfcd-5277-473d-8336-d4ed8628ae68" ] } props = copy.copy(rsrc.properties.data) props.update(prop_diff) update_snippet = rsrc_defn.ResourceDefinition(rsrc.name, rsrc.type(), props) rsrc.handle_update(update_snippet, {}, prop_diff) scheduler.TaskRunner(rsrc.delete)() rsrc.state_set(rsrc.CREATE, rsrc.COMPLETE, 'to delete again') scheduler.TaskRunner(rsrc.delete)() self.m.VerifyAll()