Controller: check DHCP on compute node

Change-Id: Ide36d3898d21a1a001f7f3d2abc4e356633a994b
This commit is contained in:
changzhi1990 2016-03-04 10:04:35 +08:00
parent f4325cfc85
commit 56a48feb9a
5 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,15 @@
#!/usr/bin/python
# Copyright 2015 UnitedStack, Inc.
# All Rights Reserved.
#
# 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.

View File

@ -0,0 +1,50 @@
#!/usr/bin/python
# Copyright 2015 UnitedStack, Inc.
# All Rights Reserved.
#
# 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.
import sys
from oslo_config import cfg
try:
from neutronclient.v2_0 import client
from neutronclient.common import exceptions
except ImportError:
print "Import neutronclient error. Please check it out."
sys.exit()
def get_neutronclient():
neutroncli = client.Client(
username=cfg.CONF.neutron_client.username,
password=cfg.CONF.neutron_client.password,
tenant_name=cfg.CONF.neutron_client.tenant_name,
auth_url=cfg.CONF.neutron_client.auth_url)
return neutroncli
def get_port_attr(port_id, attr):
client = get_neutronclient()
try:
res = client.show_port(port_id)
except exceptions.NeutronClientException:
print 'Port %s Not Found.' % port_id
return
except KeyError:
print 'Port attr: %s Not Found.' % attr
return
return res['port'][attr]

View File

@ -0,0 +1,95 @@
#!/usr/bin/python
# Copyright 2015 UnitedStack, Inc.
# All Rights Reserved.
#
# 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.
import logging
import sys
from cliff.lister import Lister
from steth.stethclient.clients import neutron
from steth.stethclient import utils
class CheckDHCPonComputeNodes(Lister):
"Check DHCP on compute nodes."
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(CheckDHCPonComputeNodes, self).get_parser(prog_name)
parser.add_argument('port_id', default='bad',
help='ID of port to look up.')
parser.add_argument('physical_interface', default='bad',
help=('Name of physical interface.'
'We catch packets at this interface'))
parser.add_argument('network_type', default='vlan',
help='Network type, you want to check.')
return parser
def take_action(self, parsed_args):
self.log.debug('Get parsed_args: %s' % parsed_args)
if not utils.is_uuid_like(parsed_args.port_id):
utils.Logger.log_fail("Port id: %s is not like"
"uuid." % parsed_args.port_id)
sys.exit()
# get network_type
network_type = parsed_args.network_type
if network_type != 'vlan' and network_type != 'vxlan':
utils.Logger.log_fail("Network type %s not support!"
"Please choose from 'vlan' and 'vxlan'."
% network_type)
sys.exit()
self.log.debug("network_type is %s" % network_type)
# get port's address
port_mac_address = neutron.get_port_attr(parsed_args.port_id,
'mac_address')
if not port_mac_address:
utils.Logger.log_fail("Get port mac_address fails."
"Please check this port.")
sys.exit()
self.log.debug("port mac address is %s" % port_mac_address)
# get port's host info
host_id = neutron.get_port_attr(parsed_args.port_id, 'binding:host_id')
if not host_id:
utils.Logger.log_fail("Port %s doesn't attach to any vms."
% parsed_args.port_id)
sys.exit()
self.log.debug("port host id is %s" % host_id)
# setup steth server
try:
server = utils.setup_server(host_id)
self.log.debug("setup server: %s" % host_id)
except:
utils.Logger.log_fail("Setup server fail in: %s." % server)
sys.exit()
# get physical interface name
physical_interface = parsed_args.physical_interface
self.log.debug("Physical interface is %s" % physical_interface)
res = server.check_dhcp_on_comp(port_id=parsed_args.port_id,
port_mac=port_mac_address,
phy_iface=physical_interface,
net_type=network_type)
self.log.debug("Response is %s" % res)
data = res['data']
return (['Device Name', 'Result'],
(['qvo', data['qvo']],
['qvb', data['qvb']],
['qbr', data['qbr']],
['tap', data['tap']]))

View File

@ -21,6 +21,7 @@ import sys
from cliff import app
from cliff import commandmanager
from steth.stethclient import agent_api
from steth.stethclient.drivers import dhcp
from steth.stethclient.drivers import iperf_api
from steth.stethclient import utils
@ -37,6 +38,7 @@ COMMAND_V1 = {
'get-interface': agent_api.GetInterface,
'check-vlan-interface': agent_api.CheckVlanInterface,
'check-iperf': iperf_api.CheckIperf,
'check-dhcp-on-comp': dhcp.CheckDHCPonComputeNodes,
}
COMMANDS = {'0.1': COMMAND_V1}

View File

@ -2,6 +2,7 @@ import mock
import unittest
from steth.stethclient import shell
from steth.stethclient import agent_api
from steth.stethclient.drivers import dhcp
from steth.stethclient.drivers import iperf_api
@ -31,6 +32,9 @@ class Server(object):
def teardown_iperf_server(self):
pass
def check_dhcp_on_comp(self):
pass
class TestStethClientMethods(unittest.TestCase):
@ -38,6 +42,7 @@ class TestStethClientMethods(unittest.TestCase):
self.server = Server()
agent_api.setup_server = mock.Mock(return_value=self.server)
iperf_api.setup_server = mock.Mock(return_value=self.server)
dhcp.setup_server = mock.Mock(return_value=self.server)
def test_stethclient_get_interface(self):
r = {'message': '', 'code': 0, 'data': {'name': 'eth0'}}
@ -96,3 +101,25 @@ class TestStethClientMethods(unittest.TestCase):
self.assertEqual(self.server.setup_iperf_server.called, True)
self.assertEqual(self.server.start_iperf_client.called, True)
self.assertEqual(self.server.teardown_iperf_server.called, True)
@mock.patch('neutronclient.v2_0.client.Client.show_port')
def test_check_dhcp_on_comp(self, show_port):
show_port.return_value = {
'port': {
'mac_address': 'aa:bb:cc:dd:ee:ff',
'binding:host_id': 'server-9'
}
}
device = "tapaaaaaaaa-aa"
msg = device + "No such device exists (SIOCGIFHWADDR: No such device)"
check_dhcp_on_comp_r = {
'message': msg,
'code': 1,
'data': {}
}
self.server.check_dhcp_on_comp = mock.Mock(
return_value=check_dhcp_on_comp_r)
shell.main(['check-dhcp-on-comp',
'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
'eth0', 'vlan'])
self.assertEqual(self.server.check_dhcp_on_comp.called, True)