def test_format_list_of_dicts(self): expected = "a='b', c='d'\ne='f'" sorted_data = [{"a": "b", "c": "d"}, {"e": "f"}] unsorted_data = [{"c": "d", "a": "b"}, {"e": "f"}] self.assertEqual(expected, utils.format_list_of_dicts(sorted_data)) self.assertEqual(expected, utils.format_list_of_dicts(unsorted_data)) self.assertEqual("", utils.format_list_of_dicts([])) self.assertEqual("", utils.format_list_of_dicts([{}]))
def test_format_list_of_dicts(self): expected = "a='b', c='d'\ne='f'" sorted_data = [{'a': 'b', 'c': 'd'}, {'e': 'f'}] unsorted_data = [{'c': 'd', 'a': 'b'}, {'e': 'f'}] self.assertEqual(expected, utils.format_list_of_dicts(sorted_data)) self.assertEqual(expected, utils.format_list_of_dicts(unsorted_data)) self.assertEqual('', utils.format_list_of_dicts([])) self.assertEqual('', utils.format_list_of_dicts([{}]))
def test_format_list_of_dicts(self): expected = "a='b', c='d'\ne='f'" sorted_data = [{'a': 'b', 'c': 'd'}, {'e': 'f'}] unsorted_data = [{'c': 'd', 'a': 'b'}, {'e': 'f'}] self.assertEqual(expected, utils.format_list_of_dicts(sorted_data)) self.assertEqual(expected, utils.format_list_of_dicts(unsorted_data)) self.assertEqual('', utils.format_list_of_dicts([])) self.assertEqual('', utils.format_list_of_dicts([{}]))
def _get_common_cols_data(self, fake_port): columns = ( 'admin_state_up', 'allowed_address_pairs', 'binding_host_id', 'binding_profile', 'binding_vif_details', 'binding_vif_type', 'binding_vnic_type', 'device_id', 'device_owner', 'dns_assignment', 'dns_name', 'extra_dhcp_opts', 'fixed_ips', 'id', 'mac_address', 'name', 'network_id', 'port_security_enabled', 'project_id', 'security_groups', 'status', ) data = ( port._format_admin_state(fake_port.admin_state_up), utils.format_list_of_dicts(fake_port.allowed_address_pairs), fake_port.binding_host_id, utils.format_dict(fake_port.binding_profile), utils.format_dict(fake_port.binding_vif_details), fake_port.binding_vif_type, fake_port.binding_vnic_type, fake_port.device_id, fake_port.device_owner, utils.format_list_of_dicts(fake_port.dns_assignment), fake_port.dns_name, utils.format_list_of_dicts(fake_port.extra_dhcp_opts), utils.format_list_of_dicts(fake_port.fixed_ips), fake_port.id, fake_port.mac_address, fake_port.name, fake_port.network_id, fake_port.port_security_enabled, fake_port.project_id, utils.format_list(fake_port.security_groups), fake_port.status, ) return columns, data
def _get_common_cols_data(self, fake_port): columns = ( 'admin_state_up', 'allowed_address_pairs', 'binding_host_id', 'binding_profile', 'binding_vif_details', 'binding_vif_type', 'binding_vnic_type', 'device_id', 'device_owner', 'dns_assignment', 'dns_name', 'extra_dhcp_opts', 'fixed_ips', 'id', 'mac_address', 'name', 'network_id', 'port_security_enabled', 'project_id', 'security_groups', 'status', ) data = ( port._format_admin_state(fake_port.admin_state_up), utils.format_list_of_dicts(fake_port.allowed_address_pairs), fake_port.binding_host_id, utils.format_dict(fake_port.binding_profile), utils.format_dict(fake_port.binding_vif_details), fake_port.binding_vif_type, fake_port.binding_vnic_type, fake_port.device_id, fake_port.device_owner, utils.format_list_of_dicts(fake_port.dns_assignment), fake_port.dns_name, utils.format_list_of_dicts(fake_port.extra_dhcp_opts), utils.format_list_of_dicts(fake_port.fixed_ips), fake_port.id, fake_port.mac_address, fake_port.name, fake_port.network_id, fake_port.port_security_enabled, fake_port.project_id, utils.format_list(fake_port.security_groups), fake_port.status, ) return columns, data
class TestListPort(TestPort): _ports = network_fakes.FakePort.create_ports(count=3) columns = ( 'ID', 'Name', 'MAC Address', 'Fixed IP Addresses', ) data = [] for prt in _ports: data.append(( prt.id, prt.name, prt.mac_address, utils.format_list_of_dicts(prt.fixed_ips), )) def setUp(self): super(TestListPort, self).setUp() # Get the command object to test self.cmd = port.ListPort(self.app, self.namespace) self.network.ports = mock.Mock(return_value=self._ports) fake_router = network_fakes.FakeRouter.create_one_router({ 'id': 'fake-router-id', }) self.network.find_router = mock.Mock(return_value=fake_router) def test_port_list_no_options(self): arglist = [] verifylist = [] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.network.ports.assert_called_once_with() self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) def test_port_list_router_opt(self): arglist = [ '--router', 'fake-router-name', ] verifylist = [('router', 'fake-router-name')] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.network.ports.assert_called_once_with( **{'device_id': 'fake-router-id'}) self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data))
def _format_network_security_group_rules(sg_rules): # For readability and to align with formatting compute security group # rules, trim keys with caller known (e.g. security group and tenant ID) # or empty values. for sg_rule in sg_rules: empty_keys = [k for k, v in six.iteritems(sg_rule) if not v] for key in empty_keys: sg_rule.pop(key) sg_rule.pop('security_group_id', None) sg_rule.pop('tenant_id', None) return utils.format_list_of_dicts(sg_rules)
def _format_network_security_group_rules(sg_rules): # For readability and to align with formatting compute security group # rules, trim keys with caller known (e.g. security group and tenant ID) # or empty values. for sg_rule in sg_rules: empty_keys = [k for k, v in six.iteritems(sg_rule) if not v] for key in empty_keys: sg_rule.pop(key) sg_rule.pop('security_group_id', None) sg_rule.pop('tenant_id', None) return utils.format_list_of_dicts(sg_rules)
def _format_host_routes(data): # Map the host route keys to match --host-route option. return utils.format_list_of_dicts(convert_entries_to_gateway(data))
def _format_routes(routes): # Map the route keys to match --route option. for route in routes: if 'nexthop' in route: route['gateway'] = route.pop('nexthop') return utils.format_list_of_dicts(routes)
def _format_host_routes(data): # Map the host route keys to match --host-route option. return utils.format_list_of_dicts(convert_entries_to_gateway(data))
def _format_routes(routes): # Map the route keys to match --route option. for route in routes: if 'nexthop' in route: route['gateway'] = route.pop('nexthop') return utils.format_list_of_dicts(routes)
class TestShowPort(TestPort): # The port to show. _port = network_fakes.FakePort.create_one_port() columns = ( 'admin_state_up', 'allowed_address_pairs', 'binding_host_id', 'binding_profile', 'binding_vif_details', 'binding_vif_type', 'binding_vnic_type', 'device_id', 'device_owner', 'dns_assignment', 'dns_name', 'extra_dhcp_opts', 'fixed_ips', 'id', 'mac_address', 'name', 'network_id', 'port_security_enabled', 'project_id', 'security_groups', 'status', ) data = ( port._format_admin_state(_port.admin_state_up), utils.format_list_of_dicts(_port.allowed_address_pairs), _port.binding_host_id, utils.format_dict(_port.binding_profile), utils.format_dict(_port.binding_vif_details), _port.binding_vif_type, _port.binding_vnic_type, _port.device_id, _port.device_owner, utils.format_list_of_dicts(_port.dns_assignment), _port.dns_name, utils.format_list_of_dicts(_port.extra_dhcp_opts), utils.format_list_of_dicts(_port.fixed_ips), _port.id, _port.mac_address, _port.name, _port.network_id, _port.port_security_enabled, _port.project_id, utils.format_list(_port.security_groups), _port.status, ) def setUp(self): super(TestShowPort, self).setUp() self.network.find_port = mock.Mock(return_value=self._port) # Get the command object to test self.cmd = port.ShowPort(self.app, self.namespace) def test_show_no_options(self): arglist = [] verifylist = [] self.assertRaises(tests_utils.ParserException, self.check_parser, self.cmd, arglist, verifylist) def test_show_all_options(self): arglist = [ self._port.name, ] verifylist = [ ('port', self._port.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.network.find_port.assert_called_with(self._port.name, ignore_missing=False) self.assertEqual(tuple(self.columns), columns) self.assertEqual(self.data, data)