Merge "Begin low-level API for Network v2"
This commit is contained in:
commit
09357009c1
@ -64,16 +64,16 @@ List networks
|
|||||||
|
|
||||||
os network list
|
os network list
|
||||||
[--external]
|
[--external]
|
||||||
[--dhcp]
|
[--dhcp <dhcp-id>]
|
||||||
[--long]
|
[--long]
|
||||||
|
|
||||||
.. option:: --external
|
.. option:: --external
|
||||||
|
|
||||||
List external networks
|
List external networks
|
||||||
|
|
||||||
.. option:: --dhcp
|
.. option:: --dhcp <dhcp-id>
|
||||||
|
|
||||||
ID of the DHCP agent
|
DHCP agent ID
|
||||||
|
|
||||||
.. option:: --long
|
.. option:: --long
|
||||||
|
|
||||||
|
59
openstackclient/api/network_v2.py
Normal file
59
openstackclient/api/network_v2.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
"""Network v2 API Library"""
|
||||||
|
|
||||||
|
from openstackclient.api import api
|
||||||
|
|
||||||
|
|
||||||
|
class APIv2(api.BaseAPI):
|
||||||
|
"""Network v2 API"""
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super(APIv2, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
def dhcp_agent_list(
|
||||||
|
self,
|
||||||
|
dhcp_id=None,
|
||||||
|
**filter
|
||||||
|
):
|
||||||
|
"""List DHCP agents
|
||||||
|
|
||||||
|
:param string dhcp_id:
|
||||||
|
DHCP Agent ID
|
||||||
|
:param filter:
|
||||||
|
used to create the query string filters
|
||||||
|
http://docs.openstack.org/api/openstack-network/2.0/content/filtering.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
return self.list('dhcp-networks', **filter)['dhcp-networks']
|
||||||
|
|
||||||
|
def network_list(
|
||||||
|
self,
|
||||||
|
external=False,
|
||||||
|
**filter
|
||||||
|
):
|
||||||
|
"""List external networks
|
||||||
|
|
||||||
|
:param string dhcp_id:
|
||||||
|
DHCP agent ID
|
||||||
|
:param bool external:
|
||||||
|
Return external networks if True
|
||||||
|
:param filter:
|
||||||
|
used to create the query string filters
|
||||||
|
http://docs.openstack.org/api/openstack-network/2.0/content/filtering.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
if external:
|
||||||
|
filter = {'router:external': True}
|
||||||
|
return self.list('networks', **filter)['networks']
|
@ -24,21 +24,54 @@ API_NAME = "network"
|
|||||||
API_VERSIONS = {
|
API_VERSIONS = {
|
||||||
"2": "neutronclient.v2_0.client.Client",
|
"2": "neutronclient.v2_0.client.Client",
|
||||||
}
|
}
|
||||||
|
# Translate our API version to auth plugin version prefix
|
||||||
|
API_VERSION_MAP = {
|
||||||
|
'2.0': 'v2.0',
|
||||||
|
'2': 'v2.0',
|
||||||
|
}
|
||||||
|
|
||||||
|
NETWORK_API_TYPE = 'network'
|
||||||
|
NETWORK_API_VERSIONS = {
|
||||||
|
'2': 'openstackclient.api.network_v2.APIv2',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def make_client(instance):
|
def make_client(instance):
|
||||||
"""Returns an network service client."""
|
"""Returns an network service client"""
|
||||||
network_client = utils.get_client_class(
|
network_client = utils.get_client_class(
|
||||||
API_NAME,
|
API_NAME,
|
||||||
instance._api_version[API_NAME],
|
instance._api_version[API_NAME],
|
||||||
API_VERSIONS)
|
API_VERSIONS)
|
||||||
LOG.debug('Instantiating network client: %s', network_client)
|
LOG.debug('Instantiating network client: %s', network_client)
|
||||||
|
|
||||||
return network_client(
|
endpoint = instance.get_endpoint_for_service_type(
|
||||||
|
API_NAME,
|
||||||
|
region_name=instance._region_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
client = network_client(
|
||||||
session=instance.session,
|
session=instance.session,
|
||||||
region_name=instance._region_name,
|
region_name=instance._region_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
network_api = utils.get_client_class(
|
||||||
|
API_NAME,
|
||||||
|
instance._api_version[API_NAME],
|
||||||
|
NETWORK_API_VERSIONS)
|
||||||
|
LOG.debug('Instantiating network api: %s', network_client)
|
||||||
|
|
||||||
|
# v2 is hard-coded until discovery is completed, neutron only has one atm
|
||||||
|
client.api = network_api(
|
||||||
|
session=instance.session,
|
||||||
|
service_type=NETWORK_API_TYPE,
|
||||||
|
endpoint='/'.join([
|
||||||
|
endpoint,
|
||||||
|
API_VERSION_MAP[instance._api_version[API_NAME]],
|
||||||
|
])
|
||||||
|
)
|
||||||
|
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
def build_option_parser(parser):
|
def build_option_parser(parser):
|
||||||
"""Hook to add global options"""
|
"""Hook to add global options"""
|
||||||
|
@ -25,10 +25,21 @@ from openstackclient.common import utils
|
|||||||
from openstackclient.network import common
|
from openstackclient.network import common
|
||||||
|
|
||||||
|
|
||||||
def filters(data):
|
def _prep_network_detail(net):
|
||||||
if 'subnets' in data:
|
"""Prepare network object for output"""
|
||||||
data['subnets'] = utils.format_list(data['subnets'])
|
|
||||||
return data
|
if 'subnets' in net:
|
||||||
|
net['subnets'] = utils.format_list(net['subnets'])
|
||||||
|
if 'admin_state_up' in net:
|
||||||
|
net['state'] = 'UP' if net['admin_state_up'] else 'DOWN'
|
||||||
|
net.pop('admin_state_up')
|
||||||
|
if 'router:external' in net:
|
||||||
|
net['router_type'] = 'External' if net['router:external'] \
|
||||||
|
else 'Internal'
|
||||||
|
net.pop('router:external')
|
||||||
|
if 'tenant_id' in net:
|
||||||
|
net['project_id'] = net.pop('tenant_id')
|
||||||
|
return net
|
||||||
|
|
||||||
|
|
||||||
class CreateNetwork(show.ShowOne):
|
class CreateNetwork(show.ShowOne):
|
||||||
@ -80,7 +91,7 @@ class CreateNetwork(show.ShowOne):
|
|||||||
create_method = getattr(client, "create_network")
|
create_method = getattr(client, "create_network")
|
||||||
data = create_method(body)['network']
|
data = create_method(body)['network']
|
||||||
if data:
|
if data:
|
||||||
data = filters(data)
|
data = _prep_network_detail(data)
|
||||||
else:
|
else:
|
||||||
data = {'': ''}
|
data = {'': ''}
|
||||||
return zip(*sorted(six.iteritems(data)))
|
return zip(*sorted(six.iteritems(data)))
|
||||||
@ -133,40 +144,63 @@ class ListNetwork(lister.Lister):
|
|||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--dhcp',
|
'--dhcp',
|
||||||
help='ID of the DHCP agent')
|
metavar='<dhcp-id>',
|
||||||
|
help='DHCP agent ID')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--long',
|
'--long',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
default=False,
|
default=False,
|
||||||
help='Long listing',
|
help='List additional fields in output',
|
||||||
)
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
self.log.debug('take_action(%s)' % parsed_args)
|
self.log.debug('take_action(%s)' % parsed_args)
|
||||||
client = self.app.client_manager.network
|
client = self.app.client_manager.network
|
||||||
|
|
||||||
if parsed_args.dhcp:
|
if parsed_args.dhcp:
|
||||||
list_method = getattr(client, 'list_networks_on_dhcp_agent')
|
data = client.api.dhcp_agent_list(dhcp_id=parsed_args.dhcp)
|
||||||
resources = 'networks_on_dhcp_agent'
|
|
||||||
report_filter = {'dhcp_agent': parsed_args.dhcp}
|
columns = ('ID',)
|
||||||
data = list_method(**report_filter)[resources]
|
column_headers = columns
|
||||||
else:
|
else:
|
||||||
list_method = getattr(client, "list_networks")
|
data = client.api.network_list(external=parsed_args.external)
|
||||||
report_filter = {}
|
|
||||||
if parsed_args.external:
|
if parsed_args.long:
|
||||||
report_filter = {'router:external': True}
|
columns = (
|
||||||
data = list_method(**report_filter)['networks']
|
'ID',
|
||||||
columns = len(data) > 0 and sorted(data[0].keys()) or []
|
'Name',
|
||||||
if parsed_args.columns:
|
'Status',
|
||||||
list_columns = parsed_args.columns
|
'project_id',
|
||||||
else:
|
'state',
|
||||||
list_columns = ['id', 'name', 'subnets']
|
'Shared',
|
||||||
if not parsed_args.long and not parsed_args.dhcp:
|
'Subnets',
|
||||||
columns = [x for x in list_columns if x in columns]
|
'provider:network_type',
|
||||||
formatters = {'subnets': utils.format_list}
|
'router_type',
|
||||||
return (columns,
|
)
|
||||||
(utils.get_dict_properties(s, columns, formatters=formatters)
|
column_headers = (
|
||||||
for s in data))
|
'ID',
|
||||||
|
'Name',
|
||||||
|
'Status',
|
||||||
|
'Project',
|
||||||
|
'State',
|
||||||
|
'Shared',
|
||||||
|
'Subnets',
|
||||||
|
'Network Type',
|
||||||
|
'Router Type',
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
columns = ('ID', 'Name', 'Subnets')
|
||||||
|
column_headers = columns
|
||||||
|
|
||||||
|
for d in data:
|
||||||
|
d = _prep_network_detail(d)
|
||||||
|
|
||||||
|
return (column_headers,
|
||||||
|
(utils.get_dict_properties(
|
||||||
|
s, columns,
|
||||||
|
formatters={'subnets': utils.format_list},
|
||||||
|
) for s in data))
|
||||||
|
|
||||||
|
|
||||||
class SetNetwork(command.Command):
|
class SetNetwork(command.Command):
|
||||||
@ -253,9 +287,9 @@ class ShowNetwork(show.ShowOne):
|
|||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
self.log.debug('take_action(%s)' % parsed_args)
|
self.log.debug('take_action(%s)' % parsed_args)
|
||||||
client = self.app.client_manager.network
|
client = self.app.client_manager.network
|
||||||
_id = common.find(client, 'network', 'networks',
|
net = client.api.find_attr(
|
||||||
parsed_args.identifier)
|
'networks',
|
||||||
show_method = getattr(client, "show_network")
|
parsed_args.identifier,
|
||||||
data = show_method(_id)['network']
|
)
|
||||||
data = filters(data)
|
data = _prep_network_detail(net)
|
||||||
return zip(*sorted(six.iteritems(data)))
|
return zip(*sorted(six.iteritems(data)))
|
||||||
|
52
openstackclient/tests/api/test_network_v2.py
Normal file
52
openstackclient/tests/api/test_network_v2.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
"""Network v2 API Library Tests"""
|
||||||
|
|
||||||
|
from requests_mock.contrib import fixture
|
||||||
|
|
||||||
|
from keystoneclient import session
|
||||||
|
from openstackclient.api import network_v2 as network
|
||||||
|
from openstackclient.tests import utils
|
||||||
|
|
||||||
|
|
||||||
|
FAKE_PROJECT = 'xyzpdq'
|
||||||
|
FAKE_URL = 'http://gopher.com/v2/' + FAKE_PROJECT
|
||||||
|
|
||||||
|
|
||||||
|
class TestNetworkAPIv2(utils.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestNetworkAPIv2, self).setUp()
|
||||||
|
sess = session.Session()
|
||||||
|
self.api = network.APIv2(session=sess, endpoint=FAKE_URL)
|
||||||
|
self.requests_mock = self.useFixture(fixture.Fixture())
|
||||||
|
|
||||||
|
|
||||||
|
class TestNetwork(TestNetworkAPIv2):
|
||||||
|
|
||||||
|
LIST_NETWORK_RESP = [
|
||||||
|
{'id': '1', 'name': 'p1', 'description': 'none', 'enabled': True},
|
||||||
|
{'id': '2', 'name': 'p2', 'description': 'none', 'enabled': False},
|
||||||
|
{'id': '3', 'name': 'p3', 'description': 'none', 'enabled': True},
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_network_list_no_options(self):
|
||||||
|
self.requests_mock.register_uri(
|
||||||
|
'GET',
|
||||||
|
FAKE_URL + '/networks',
|
||||||
|
json={'networks': self.LIST_NETWORK_RESP},
|
||||||
|
status_code=200,
|
||||||
|
)
|
||||||
|
ret = self.api.network_list()
|
||||||
|
self.assertEqual(self.LIST_NETWORK_RESP, ret)
|
@ -14,16 +14,26 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
|
from openstackclient.api import network_v2
|
||||||
from openstackclient.tests import utils
|
from openstackclient.tests import utils
|
||||||
|
|
||||||
|
|
||||||
|
class FakeNetworkClient(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TestNetworkBase(utils.TestCommand):
|
class TestNetworkBase(utils.TestCommand):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestNetworkBase, self).setUp()
|
super(TestNetworkBase, self).setUp()
|
||||||
self.app = mock.Mock(name='app')
|
|
||||||
self.app.client_manager = mock.Mock(name='client_manager')
|
|
||||||
self.namespace = argparse.Namespace()
|
self.namespace = argparse.Namespace()
|
||||||
|
|
||||||
|
self.app.client_manager.network = FakeNetworkClient()
|
||||||
|
self.app.client_manager.network.api = network_v2.APIv2(
|
||||||
|
session=mock.Mock(),
|
||||||
|
service_type="network",
|
||||||
|
)
|
||||||
|
self.api = self.app.client_manager.network.api
|
||||||
|
|
||||||
given_show_options = [
|
given_show_options = [
|
||||||
'-f',
|
'-f',
|
||||||
'shell',
|
'shell',
|
||||||
|
@ -22,16 +22,38 @@ RESOURCE = 'network'
|
|||||||
RESOURCES = 'networks'
|
RESOURCES = 'networks'
|
||||||
FAKE_ID = 'iditty'
|
FAKE_ID = 'iditty'
|
||||||
FAKE_NAME = 'noo'
|
FAKE_NAME = 'noo'
|
||||||
|
FAKE_PROJECT = 'yaa'
|
||||||
RECORD = {
|
RECORD = {
|
||||||
'id': FAKE_ID,
|
'id': FAKE_ID,
|
||||||
'name': FAKE_NAME,
|
'name': FAKE_NAME,
|
||||||
|
'admin_state_up': True,
|
||||||
'router:external': True,
|
'router:external': True,
|
||||||
|
'status': 'ACTIVE',
|
||||||
'subnets': ['a', 'b'],
|
'subnets': ['a', 'b'],
|
||||||
|
'tenant_id': FAKE_PROJECT,
|
||||||
}
|
}
|
||||||
COLUMNS = ['id', 'name', 'subnets']
|
COLUMNS = ['ID', 'Name', 'Subnets']
|
||||||
RESPONSE = {RESOURCE: RECORD}
|
RESPONSE = {RESOURCE: copy.deepcopy(RECORD)}
|
||||||
FILTERED = [('id', 'name', 'router:external', 'subnets'),
|
FILTERED = [
|
||||||
(FAKE_ID, FAKE_NAME, True, 'a, b')]
|
(
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'project_id',
|
||||||
|
'router_type',
|
||||||
|
'state',
|
||||||
|
'status',
|
||||||
|
'subnets',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
FAKE_ID,
|
||||||
|
FAKE_NAME,
|
||||||
|
FAKE_PROJECT,
|
||||||
|
'External',
|
||||||
|
'UP',
|
||||||
|
'ACTIVE',
|
||||||
|
'a, b',
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class TestCreateNetwork(common.TestNetworkBase):
|
class TestCreateNetwork(common.TestNetworkBase):
|
||||||
@ -122,7 +144,7 @@ class TestDeleteNetwork(common.TestNetworkBase):
|
|||||||
verifylist = [
|
verifylist = [
|
||||||
('networks', [FAKE_NAME]),
|
('networks', [FAKE_NAME]),
|
||||||
]
|
]
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
lister = mock.Mock(return_value={RESOURCES: [copy.deepcopy(RECORD)]})
|
||||||
self.app.client_manager.network.list_networks = lister
|
self.app.client_manager.network.list_networks = lister
|
||||||
mocker = mock.Mock(return_value=None)
|
mocker = mock.Mock(return_value=None)
|
||||||
self.app.client_manager.network.delete_network = mocker
|
self.app.client_manager.network.delete_network = mocker
|
||||||
@ -135,85 +157,170 @@ class TestDeleteNetwork(common.TestNetworkBase):
|
|||||||
self.assertEqual(None, result)
|
self.assertEqual(None, result)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch(
|
||||||
|
'openstackclient.api.network_v2.APIv2.network_list'
|
||||||
|
)
|
||||||
class TestListNetwork(common.TestNetworkBase):
|
class TestListNetwork(common.TestNetworkBase):
|
||||||
def test_list_no_options(self):
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestListNetwork, self).setUp()
|
||||||
|
|
||||||
|
# Get the command object to test
|
||||||
|
self.cmd = network.ListNetwork(self.app, self.namespace)
|
||||||
|
|
||||||
|
self.NETWORK_LIST = [
|
||||||
|
copy.deepcopy(RECORD),
|
||||||
|
copy.deepcopy(RECORD),
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_network_list_no_options(self, n_mock):
|
||||||
|
n_mock.return_value = self.NETWORK_LIST
|
||||||
|
|
||||||
arglist = []
|
arglist = []
|
||||||
verifylist = [
|
verifylist = [
|
||||||
('long', False),
|
|
||||||
('dhcp', None),
|
|
||||||
('external', False),
|
('external', False),
|
||||||
|
('dhcp', None),
|
||||||
|
('long', False),
|
||||||
]
|
]
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
self.app.client_manager.network.list_networks = lister
|
|
||||||
cmd = network.ListNetwork(self.app, self.namespace)
|
|
||||||
|
|
||||||
parsed_args = self.check_parser(cmd, arglist, verifylist)
|
# DisplayCommandBase.take_action() returns two tuples
|
||||||
result = cmd.take_action(parsed_args)
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
lister.assert_called_with()
|
# Set expected values
|
||||||
self.assertEqual(COLUMNS, result[0])
|
n_mock.assert_called_with(
|
||||||
self.assertEqual((FAKE_ID, FAKE_NAME, 'a, b'), next(result[1]))
|
external=False,
|
||||||
self.assertRaises(StopIteration, next, result[1])
|
)
|
||||||
|
|
||||||
def test_list_long(self):
|
self.assertEqual(tuple(COLUMNS), columns)
|
||||||
arglist = ['--long']
|
datalist = [
|
||||||
|
(FAKE_ID, FAKE_NAME, 'a, b'),
|
||||||
|
(FAKE_ID, FAKE_NAME, 'a, b'),
|
||||||
|
]
|
||||||
|
self.assertEqual(datalist, list(data))
|
||||||
|
|
||||||
|
def test_list_external(self, n_mock):
|
||||||
|
n_mock.return_value = self.NETWORK_LIST
|
||||||
|
|
||||||
|
arglist = [
|
||||||
|
'--external',
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('external', True),
|
||||||
|
('dhcp', None),
|
||||||
|
('long', False),
|
||||||
|
]
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
|
||||||
|
# DisplayCommandBase.take_action() returns two tuples
|
||||||
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
# Set expected values
|
||||||
|
n_mock.assert_called_with(
|
||||||
|
external=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(tuple(COLUMNS), columns)
|
||||||
|
datalist = [
|
||||||
|
(FAKE_ID, FAKE_NAME, 'a, b'),
|
||||||
|
(FAKE_ID, FAKE_NAME, 'a, b'),
|
||||||
|
]
|
||||||
|
self.assertEqual(datalist, list(data))
|
||||||
|
|
||||||
|
def test_network_list_long(self, n_mock):
|
||||||
|
n_mock.return_value = self.NETWORK_LIST
|
||||||
|
|
||||||
|
arglist = [
|
||||||
|
'--long',
|
||||||
|
]
|
||||||
verifylist = [
|
verifylist = [
|
||||||
('long', True),
|
('long', True),
|
||||||
('dhcp', None),
|
('dhcp', None),
|
||||||
('external', False),
|
('external', False),
|
||||||
]
|
]
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
self.app.client_manager.network.list_networks = lister
|
|
||||||
cmd = network.ListNetwork(self.app, self.namespace)
|
|
||||||
|
|
||||||
parsed_args = self.check_parser(cmd, arglist, verifylist)
|
# DisplayCommandBase.take_action() returns two tuples
|
||||||
result = cmd.take_action(parsed_args)
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
lister.assert_called_with()
|
# Set expected values
|
||||||
headings = ['id', 'name', 'router:external', 'subnets']
|
n_mock.assert_called_with(
|
||||||
self.assertEqual(headings, result[0])
|
external=False,
|
||||||
data = (FAKE_ID, FAKE_NAME, True, 'a, b')
|
)
|
||||||
self.assertEqual(data, next(result[1]))
|
|
||||||
self.assertRaises(StopIteration, next, result[1])
|
collist = (
|
||||||
|
'ID',
|
||||||
|
'Name',
|
||||||
|
'Status',
|
||||||
|
'Project',
|
||||||
|
'State',
|
||||||
|
'Shared',
|
||||||
|
'Subnets',
|
||||||
|
'Network Type',
|
||||||
|
'Router Type',
|
||||||
|
)
|
||||||
|
self.assertEqual(columns, collist)
|
||||||
|
dataitem = (
|
||||||
|
FAKE_ID,
|
||||||
|
FAKE_NAME,
|
||||||
|
'ACTIVE',
|
||||||
|
FAKE_PROJECT,
|
||||||
|
'UP',
|
||||||
|
'',
|
||||||
|
'a, b',
|
||||||
|
'',
|
||||||
|
'External',
|
||||||
|
)
|
||||||
|
datalist = [
|
||||||
|
dataitem,
|
||||||
|
dataitem,
|
||||||
|
]
|
||||||
|
self.assertEqual(list(data), datalist)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch(
|
||||||
|
'openstackclient.api.network_v2.APIv2.dhcp_agent_list'
|
||||||
|
)
|
||||||
|
class TestListDhcpAgent(common.TestNetworkBase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestListDhcpAgent, self).setUp()
|
||||||
|
|
||||||
|
# Get the command object to test
|
||||||
|
self.cmd = network.ListNetwork(self.app, self.namespace)
|
||||||
|
|
||||||
|
self.DHCP_LIST = [
|
||||||
|
{'id': '1'},
|
||||||
|
{'id': '2'},
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_list_dhcp(self, n_mock):
|
||||||
|
n_mock.return_value = self.DHCP_LIST
|
||||||
|
|
||||||
def test_list_dhcp(self):
|
|
||||||
arglist = [
|
arglist = [
|
||||||
'--dhcp',
|
'--dhcp', 'dhcpid',
|
||||||
'dhcpid',
|
]
|
||||||
] + self.given_list_options
|
|
||||||
verifylist = [
|
verifylist = [
|
||||||
|
('external', False),
|
||||||
('dhcp', 'dhcpid'),
|
('dhcp', 'dhcpid'),
|
||||||
] + self.then_list_options
|
('long', False),
|
||||||
fake_dhcp_data = [{'id': '1'}, {'id': '2'}]
|
]
|
||||||
fake_dhcp_response = {'networks_on_dhcp_agent': fake_dhcp_data}
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
lister = mock.Mock(return_value=fake_dhcp_response)
|
|
||||||
netty = self.app.client_manager.network
|
|
||||||
netty.list_networks_on_dhcp_agent = lister
|
|
||||||
cmd = network.ListNetwork(self.app, self.namespace)
|
|
||||||
|
|
||||||
parsed_args = self.check_parser(cmd, arglist, verifylist)
|
# DisplayCommandBase.take_action() returns two tuples
|
||||||
result = cmd.take_action(parsed_args)
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
lister.assert_called_with(dhcp_agent='dhcpid')
|
# Set expected values
|
||||||
self.assertEqual(['id'], result[0])
|
n_mock.assert_called_with(
|
||||||
self.assertEqual(('1',), next(result[1]))
|
dhcp_id='dhcpid',
|
||||||
self.assertEqual(('2',), next(result[1]))
|
)
|
||||||
self.assertRaises(StopIteration, next, result[1])
|
|
||||||
|
|
||||||
def test_list_external(self):
|
self.assertEqual(('ID',), columns)
|
||||||
arglist = ['--external', '-c', 'id']
|
datalist = [
|
||||||
verifylist = [('external', True)]
|
('1',),
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
('2',),
|
||||||
self.app.client_manager.network.list_networks = lister
|
]
|
||||||
cmd = network.ListNetwork(self.app, self.namespace)
|
self.assertEqual(datalist, list(data))
|
||||||
|
|
||||||
parsed_args = self.check_parser(cmd, arglist, verifylist)
|
|
||||||
result = cmd.take_action(parsed_args)
|
|
||||||
|
|
||||||
lister.assert_called_with(**{'router:external': True})
|
|
||||||
self.assertEqual(['id'], result[0])
|
|
||||||
self.assertEqual((FAKE_ID,), next(result[1]))
|
|
||||||
self.assertRaises(StopIteration, next, result[1])
|
|
||||||
|
|
||||||
|
|
||||||
class TestSetNetwork(common.TestNetworkBase):
|
class TestSetNetwork(common.TestNetworkBase):
|
||||||
@ -230,7 +337,7 @@ class TestSetNetwork(common.TestNetworkBase):
|
|||||||
('name', 'noob'),
|
('name', 'noob'),
|
||||||
('shared', True),
|
('shared', True),
|
||||||
]
|
]
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
lister = mock.Mock(return_value={RESOURCES: [copy.deepcopy(RECORD)]})
|
||||||
self.app.client_manager.network.list_networks = lister
|
self.app.client_manager.network.list_networks = lister
|
||||||
mocker = mock.Mock(return_value=None)
|
mocker = mock.Mock(return_value=None)
|
||||||
self.app.client_manager.network.update_network = mocker
|
self.app.client_manager.network.update_network = mocker
|
||||||
@ -255,7 +362,7 @@ class TestSetNetwork(common.TestNetworkBase):
|
|||||||
('admin_state', False),
|
('admin_state', False),
|
||||||
('shared', False),
|
('shared', False),
|
||||||
]
|
]
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
lister = mock.Mock(return_value={RESOURCES: [copy.deepcopy(RECORD)]})
|
||||||
self.app.client_manager.network.list_networks = lister
|
self.app.client_manager.network.list_networks = lister
|
||||||
mocker = mock.Mock(return_value=None)
|
mocker = mock.Mock(return_value=None)
|
||||||
self.app.client_manager.network.update_network = mocker
|
self.app.client_manager.network.update_network = mocker
|
||||||
@ -272,7 +379,7 @@ class TestSetNetwork(common.TestNetworkBase):
|
|||||||
def test_set_nothing(self):
|
def test_set_nothing(self):
|
||||||
arglist = [FAKE_NAME, ]
|
arglist = [FAKE_NAME, ]
|
||||||
verifylist = [('identifier', FAKE_NAME), ]
|
verifylist = [('identifier', FAKE_NAME), ]
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
lister = mock.Mock(return_value={RESOURCES: [copy.deepcopy(RECORD)]})
|
||||||
self.app.client_manager.network.list_networks = lister
|
self.app.client_manager.network.list_networks = lister
|
||||||
mocker = mock.Mock(return_value=None)
|
mocker = mock.Mock(return_value=None)
|
||||||
self.app.client_manager.network.update_network = mocker
|
self.app.client_manager.network.update_network = mocker
|
||||||
@ -283,37 +390,43 @@ class TestSetNetwork(common.TestNetworkBase):
|
|||||||
parsed_args)
|
parsed_args)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch(
|
||||||
|
'openstackclient.api.network_v2.APIv2.find_attr'
|
||||||
|
)
|
||||||
class TestShowNetwork(common.TestNetworkBase):
|
class TestShowNetwork(common.TestNetworkBase):
|
||||||
def test_show_no_options(self):
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestShowNetwork, self).setUp()
|
||||||
|
|
||||||
|
# Get the command object to test
|
||||||
|
self.cmd = network.ShowNetwork(self.app, self.namespace)
|
||||||
|
|
||||||
|
self.NETWORK_ITEM = copy.deepcopy(RECORD)
|
||||||
|
|
||||||
|
def test_show_no_options(self, n_mock):
|
||||||
arglist = [
|
arglist = [
|
||||||
FAKE_NAME,
|
FAKE_NAME,
|
||||||
]
|
]
|
||||||
verifylist = [
|
verifylist = [
|
||||||
('identifier', FAKE_NAME),
|
('identifier', FAKE_NAME),
|
||||||
]
|
]
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
n_mock.return_value = copy.deepcopy(RECORD)
|
||||||
self.app.client_manager.network.list_networks = lister
|
self.cmd = network.ShowNetwork(self.app, self.namespace)
|
||||||
mocker = mock.Mock(return_value=copy.deepcopy(RESPONSE))
|
|
||||||
self.app.client_manager.network.show_network = mocker
|
|
||||||
cmd = network.ShowNetwork(self.app, self.namespace)
|
|
||||||
|
|
||||||
parsed_args = self.check_parser(cmd, arglist, verifylist)
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
result = list(cmd.take_action(parsed_args))
|
result = list(self.cmd.take_action(parsed_args))
|
||||||
|
|
||||||
mocker.assert_called_with(FAKE_ID)
|
n_mock.assert_called_with('networks', FAKE_NAME)
|
||||||
self.assertEqual(FILTERED, result)
|
self.assertEqual(FILTERED, result)
|
||||||
|
|
||||||
def test_show_all_options(self):
|
def test_show_all_options(self, n_mock):
|
||||||
arglist = [FAKE_NAME] + self.given_show_options
|
arglist = [FAKE_NAME] + self.given_show_options
|
||||||
verifylist = [('identifier', FAKE_NAME)] + self.then_show_options
|
verifylist = [('identifier', FAKE_NAME)] + self.then_show_options
|
||||||
lister = mock.Mock(return_value={RESOURCES: [RECORD]})
|
n_mock.return_value = copy.deepcopy(RECORD)
|
||||||
self.app.client_manager.network.list_networks = lister
|
self.cmd = network.ShowNetwork(self.app, self.namespace)
|
||||||
mocker = mock.Mock(return_value=copy.deepcopy(RESPONSE))
|
|
||||||
self.app.client_manager.network.show_network = mocker
|
|
||||||
cmd = network.ShowNetwork(self.app, self.namespace)
|
|
||||||
|
|
||||||
parsed_args = self.check_parser(cmd, arglist, verifylist)
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
result = list(cmd.take_action(parsed_args))
|
result = list(self.cmd.take_action(parsed_args))
|
||||||
|
|
||||||
mocker.assert_called_with(FAKE_ID)
|
n_mock.assert_called_with('networks', FAKE_NAME)
|
||||||
self.assertEqual(FILTERED, result)
|
self.assertEqual(FILTERED, result)
|
||||||
|
Loading…
Reference in New Issue
Block a user