class TestShowNetworkTrunk(test_fakes.TestNeutronClientOSCV2):

    # The trunk to set.
    _trunk = fakes.FakeTrunk.create_one_trunk()

    columns = (
        'admin_state_up',
        'description',
        'id',
        'name',
        'port_id',
        'project_id',
        'status',
        'sub_ports',
    )
    data = (
        v2_utils.AdminStateColumn(_trunk['admin_state_up']),
        _trunk['description'],
        _trunk['id'],
        _trunk['name'],
        _trunk['port_id'],
        _trunk['project_id'],
        _trunk['status'],
        format_columns.ListDictColumn(_trunk['sub_ports']),
    )

    def setUp(self):
        super(TestShowNetworkTrunk, self).setUp()

        mock.patch('neutronclient.osc.v2.trunk.network_trunk._get_id',
                   new=_get_id).start()
        self.neutronclient.show_trunk = mock.Mock(
            return_value={trunk.TRUNK: self._trunk})

        # Get the command object to test
        self.cmd = trunk.ShowNetworkTrunk(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._trunk['id'],
        ]
        verifylist = [
            ('trunk', self._trunk['id']),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        columns, data = self.cmd.take_action(parsed_args)

        self.neutronclient.show_trunk.assert_called_once_with(
            self._trunk['id'])
        self.assertEqual(self.columns, columns)
        self.assertItemEqual(self.data, data)
示例#2
0
    def setUp(self):
        super(TestFirewallGroup, self).setUp()

        def _find_resource(*args, **kwargs):
            return {'id': args[1], 'ports': _fwg['ports']}

        self.neutronclient.find_resource = mock.Mock(
            side_effect=_find_resource)
        osc_utils.find_project = mock.Mock()
        osc_utils.find_project.id = _fwg['tenant_id']
        self.res = 'firewall_group'
        self.res_plural = 'firewall_groups'
        self.resource = _fwg
        self.list_headers = (
            'ID',
            'Name',
            'Ingress Policy ID',
            'Egress Policy ID',
        )
        self.list_data = (
            _fwg['id'],
            _fwg['name'],
            _fwg['ingress_firewall_policy_id'],
            _fwg['egress_firewall_policy_id'],
        )
        self.headers = tuple(self.list_headers + (
            'Description',
            'Status',
            'Ports',
            'State',
            'Shared',
            'Project',
        ))
        self.data = _generate_response()
        self.ordered_headers = copy.deepcopy(tuple(sorted(self.headers)))
        self.ordered_data = (
            _fwg['description'],
            _fwg['egress_firewall_policy_id'],
            _fwg['id'],
            _fwg['ingress_firewall_policy_id'],
            _fwg['name'],
            _fwg['ports'],
            _fwg['tenant_id'],
            _fwg['shared'],
            v2_utils.AdminStateColumn(_fwg['admin_state_up']),
            _fwg['status'],
        )
        self.ordered_columns = (
            'description',
            'egress_firewall_policy_id',
            'id',
            'ingress_firewall_policy_id',
            'name',
            'ports',
            'tenant_id',
            'shared',
            'admin_state_up',
            'status',
        )
示例#3
0
def _generate_response(ordered_dict=None, data=None):
    source = ordered_dict if ordered_dict else _fwg
    up = {'admin_state_up':
          v2_utils.AdminStateColumn(source['admin_state_up'])}
    if data:
        up.append(data)
    source.update(up)
    return tuple(source[key] for key in source)
 def get_data(self):
     return (
         v2_utils.AdminStateColumn(self._trunk['admin_state_up']),
         self._trunk['description'],
         self._trunk['id'],
         self._trunk['name'],
         self._trunk['port_id'],
         self._trunk['project_id'],
         self._trunk['status'],
         format_columns.ListDictColumn(self._trunk['sub_ports']),
     )
class TestUnsetNetworkTrunk(test_fakes.TestNeutronClientOSCV2):

    _trunk = fakes.FakeTrunk.create_one_trunk()

    columns = (
        'admin_state_up',
        'id',
        'name',
        'port_id',
        'project_id',
        'status',
        'sub_ports',
    )
    data = (
        v2_utils.AdminStateColumn(_trunk['admin_state_up']),
        _trunk['id'],
        _trunk['name'],
        _trunk['port_id'],
        _trunk['project_id'],
        _trunk['status'],
        format_columns.ListDictColumn(_trunk['sub_ports']),
    )

    def setUp(self):
        super(TestUnsetNetworkTrunk, self).setUp()

        mock.patch('neutronclient.osc.v2.trunk.network_trunk._get_id',
                   new=_get_id).start()
        self.neutronclient.trunk_remove_subports = mock.Mock(
            return_value=None)

        # Get the command object to test
        self.cmd = trunk.UnsetNetworkTrunk(self.app, self.namespace)

    def test_unset_network_trunk_subport(self):
        subport = self._trunk['sub_ports'][0]
        arglist = [
            "--subport", subport['port_id'],
            self._trunk['name'],
        ]
        verifylist = [
            ('trunk', self._trunk['name']),
            ('unset_subports', [subport['port_id']]),
        ]

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        result = self.cmd.take_action(parsed_args)

        self.neutronclient.trunk_remove_subports.assert_called_once_with(
            self._trunk['name'],
            {trunk.SUB_PORTS: [{'port_id': subport['port_id']}]}
        )
        self.assertIsNone(result)

    def test_unset_subport_no_arguments_fail(self):
        arglist = [
            self._trunk['name'],
        ]
        verifylist = [
            ('trunk', self._trunk['name']),
        ]
        self.assertRaises(tests_utils.ParserException,
                          self.check_parser, self.cmd, arglist, verifylist)
class TestSetNetworkTrunk(test_fakes.TestNeutronClientOSCV2):
    # Create trunks to be listed.
    _trunk = fakes.FakeTrunk.create_one_trunk()

    columns = (
        'admin_state_up',
        'id',
        'name',
        'description',
        'port_id',
        'project_id',
        'status',
        'sub_ports',
    )
    data = (
        v2_utils.AdminStateColumn(_trunk['admin_state_up']),
        _trunk['id'],
        _trunk['name'],
        _trunk['description'],
        _trunk['port_id'],
        _trunk['project_id'],
        _trunk['status'],
        format_columns.ListDictColumn(_trunk['sub_ports']),
    )

    def setUp(self):
        super(TestSetNetworkTrunk, self).setUp()
        mock.patch('neutronclient.osc.v2.trunk.network_trunk._get_id',
                   new=_get_id).start()
        self.neutronclient.update_trunk = mock.Mock(
            return_value={trunk.TRUNK: self._trunk})
        self.neutronclient.trunk_add_subports = mock.Mock(
            return_value=self._trunk)

        # Get the command object to test
        self.cmd = trunk.SetNetworkTrunk(self.app, self.namespace)

    def _test_set_network_trunk_attr(self, attr, value):
        arglist = [
            '--%s' % attr, value,
            self._trunk[attr],
        ]
        verifylist = [
            (attr, value),
            ('trunk', self._trunk[attr]),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        result = self.cmd.take_action(parsed_args)

        attrs = {
            attr: value,
        }
        self.neutronclient.update_trunk.assert_called_once_with(
            self._trunk[attr], {trunk.TRUNK: attrs})
        self.assertIsNone(result)

    def test_set_network_trunk_name(self):
        self._test_set_network_trunk_attr('name', 'trunky')

    def test_test_set_network_trunk_description(self):
        self._test_set_network_trunk_attr('description', 'description')

    def test_set_network_trunk_admin_state_up_disable(self):
        arglist = [
            '--disable',
            self._trunk['name'],
        ]
        verifylist = [
            ('disable', True),
            ('trunk', self._trunk['name']),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        result = self.cmd.take_action(parsed_args)

        attrs = {
            'admin_state_up': False,
        }
        self.neutronclient.update_trunk.assert_called_once_with(
            self._trunk['name'], {trunk.TRUNK: attrs})
        self.assertIsNone(result)

    def test_set_network_trunk_admin_state_up_enable(self):
        arglist = [
            '--enable',
            self._trunk['name'],
        ]
        verifylist = [
            ('enable', True),
            ('trunk', self._trunk['name']),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        result = self.cmd.take_action(parsed_args)

        attrs = {
            'admin_state_up': True,
        }
        self.neutronclient.update_trunk.assert_called_once_with(
            self._trunk['name'], {trunk.TRUNK: attrs})
        self.assertIsNone(result)

    def test_set_network_trunk_nothing(self):
        arglist = [self._trunk['name'], ]
        verifylist = [('trunk', self._trunk['name']), ]

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        result = self.cmd.take_action(parsed_args)

        attrs = {}
        self.neutronclient.update_trunk.assert_called_once_with(
            self._trunk['name'], {trunk.TRUNK: attrs})
        self.assertIsNone(result)

    def test_set_network_trunk_subports(self):
        subport = self._trunk['sub_ports'][0]
        arglist = [
            '--subport', 'port=%(port)s,segmentation-type=%(seg_type)s,'
            'segmentation-id=%(seg_id)s' % {
                'seg_id': subport['segmentation_id'],
                'seg_type': subport['segmentation_type'],
                'port': subport['port_id']},
            self._trunk['name'],
        ]
        verifylist = [
            ('trunk', self._trunk['name']),
            ('set_subports', [{
                'port': subport['port_id'],
                'segmentation-id': str(subport['segmentation_id']),
                'segmentation-type': subport['segmentation_type']}]),
        ]

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        result = self.cmd.take_action(parsed_args)

        self.neutronclient.trunk_add_subports.assert_called_once_with(
            self._trunk['name'], {'sub_ports': [subport]}
        )
        self.assertIsNone(result)

    def test_set_network_trunk_subports_without_optional_keys(self):
        subport = copy.copy(self._trunk['sub_ports'][0])
        # Pop out the segmentation-id and segmentation-type
        subport.pop('segmentation_type')
        subport.pop('segmentation_id')
        arglist = [
            '--subport', 'port=%(port)s' % {'port': subport['port_id']},
            self._trunk['name'],
        ]
        verifylist = [
            ('trunk', self._trunk['name']),
            ('set_subports', [{
                'port': subport['port_id']}]),
        ]

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        result = self.cmd.take_action(parsed_args)

        self.neutronclient.trunk_add_subports.assert_called_once_with(
            self._trunk['name'], {'sub_ports': [subport]}
        )
        self.assertIsNone(result)

    def test_set_network_trunk_subports_without_required_key_fail(self):
        subport = self._trunk['sub_ports'][0]
        arglist = [
            '--subport', 'segmentation-type=%(seg_type)s,'
            'segmentation-id=%(seg_id)s' % {
                'seg_id': subport['segmentation_id'],
                'seg_type': subport['segmentation_type']},
            self._trunk['name'],
        ]
        verifylist = [
            ('trunk', self._trunk['name']),
            ('set_subports', [{
                'segmentation-id': str(subport['segmentation_id']),
                'segmentation-type': subport['segmentation_type']}]),
        ]

        with testtools.ExpectedException(argparse.ArgumentTypeError):
            self.check_parser(self.cmd, arglist, verifylist)

        self.neutronclient.trunk_add_subports.assert_not_called()

    def test_set_trunk_attrs_with_exception(self):
        arglist = [
            '--name', 'reallylongname',
            self._trunk['name'],
        ]
        verifylist = [
            ('trunk', self._trunk['name']),
            ('name', 'reallylongname'),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        self.neutronclient.update_trunk = (
            mock.Mock(side_effect=exceptions.CommandError)
        )
        with testtools.ExpectedException(exceptions.CommandError) as e:
            self.cmd.take_action(parsed_args)
            self.assertEqual(
                "Failed to set trunk '%s': " % self._trunk['name'],
                str(e))
        attrs = {'name': 'reallylongname'}
        self.neutronclient.update_trunk.assert_called_once_with(
            self._trunk['name'], {trunk.TRUNK: attrs})
        self.neutronclient.trunk_add_subports.assert_not_called()

    def test_set_trunk_add_subport_with_exception(self):
        arglist = [
            '--subport', 'port=invalid_subport',
            self._trunk['name'],
        ]
        verifylist = [
            ('trunk', self._trunk['name']),
            ('set_subports', [{'port': 'invalid_subport'}]),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        self.neutronclient.trunk_add_subports = (
            mock.Mock(side_effect=exceptions.CommandError)
        )
        with testtools.ExpectedException(exceptions.CommandError) as e:
            self.cmd.take_action(parsed_args)
            self.assertEqual(
                "Failed to add subports to trunk '%s': " % self._trunk['name'],
                str(e))
        self.neutronclient.update_trunk.assert_called_once_with(
            self._trunk['name'], {trunk.TRUNK: {}})
        self.neutronclient.trunk_add_subports.assert_called_once_with(
            self._trunk['name'],
            {'sub_ports': [{'port_id': 'invalid_subport'}]}
        )
class TestListNetworkTrunk(test_fakes.TestNeutronClientOSCV2):
    # Create trunks to be listed.
    _trunks = fakes.FakeTrunk.create_trunks(
        {'created_at': '2001-01-01 00:00:00',
         'updated_at': '2001-01-01 00:00:00'}, count=3)

    columns = (
        'ID',
        'Name',
        'Parent Port',
        'Description'
    )
    columns_long = columns + (
        'Status',
        'State',
        'Created At',
        'Updated At'
    )
    data = []
    for t in _trunks:
        data.append((
            t['id'],
            t['name'],
            t['port_id'],
            t['description']
        ))
    data_long = []
    for t in _trunks:
        data_long.append((
            t['id'],
            t['name'],
            t['port_id'],
            t['description'],
            t['status'],
            v2_utils.AdminStateColumn(t['admin_state_up']),
            '2001-01-01 00:00:00',
            '2001-01-01 00:00:00',
        ))

    def setUp(self):
        super(TestListNetworkTrunk, self).setUp()
        mock.patch('neutronclient.osc.v2.trunk.network_trunk._get_id',
                   new=_get_id).start()
        self.neutronclient.list_trunks = mock.Mock(
            return_value={trunk.TRUNKS: self._trunks})

        # Get the command object to test
        self.cmd = trunk.ListNetworkTrunk(self.app, self.namespace)

    def test_trunk_list_no_option(self):
        arglist = []
        verifylist = []
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        columns, data = self.cmd.take_action(parsed_args)

        self.neutronclient.list_trunks.assert_called_once_with()
        self.assertEqual(self.columns, columns)
        self.assertListItemEqual(self.data, list(data))

    def test_trunk_list_long(self):
        arglist = [
            '--long',
        ]
        verifylist = [
            ('long', True),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        columns, data = self.cmd.take_action(parsed_args)

        self.neutronclient.list_trunks.assert_called_once_with()
        self.assertEqual(self.columns_long, columns)
        self.assertListItemEqual(self.data_long, list(data))