Refactor for get_xenapi_facts

This commit contains the following changes:
1. remove it from cmd; as there is no need to expose this as a
   separate command. Making this as an util which will be invoked
   by xenapi_bootstrap.
2. rename the general function of get_hostname to be get_remote_hostname
   and move it to common_functions.
3. change the keywords for the facts items to make it more clear.

Change-Id: Ie786b3085c29f9a1faf2c51a1a704ded1010af7b
This commit is contained in:
Jianghua Wang 2018-01-05 06:42:06 +00:00
parent e961b45553
commit 870e502850
6 changed files with 71 additions and 161 deletions

View File

@ -1,75 +0,0 @@
# Copyright 2017 Citrix Systems
#
# 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.
"""Command for getting facts about XenAPI.
This command will return the facts about XenAPI in a json formatted dict.
e.g.
'
{
"DOM0_HOST_NAME": u"traya",
"HIMN_LOCAL_IP": u"169.254.0.2",
"HIMN_LOCAL_ETH": u"eth3"
...}
'
"""
import getopt
import sys
import os_xenapi.utils.xenapi_facts as xenapi_facts
USAGE_MSG = "Run the following command to get facts for XenAPI:\n"
USAGE_MSG += sys.argv[0]
USAGE_MSG += " [-i|--himn-ip=] <XenServer's HIMN IP>"
USAGE_MSG += " [-u|--user-name=] <user-name>"
USAGE_MSG += " [-p|--passwd=] <passwd>\n\n"
VALID_OPS_SHORT_STR = "i:p:u:"
VALID_OPS_LONG_LST = ["himn-ip=", "passwd=", "user-name="]
def exit_with_usage():
sys.stderr.write(USAGE_MSG)
sys.exit(1)
def main(argv):
try:
opts, args = getopt.getopt(argv,
VALID_OPS_SHORT_STR,
VALID_OPS_LONG_LST)
except getopt.GetoptError:
return exit_with_usage()
if len(opts) != len(VALID_OPS_LONG_LST):
return exit_with_usage()
# Get the values from input parameters.
for opt, arg in opts:
if opt in ("-i", "--himn-ip"):
himn_ip = arg
elif opt in ("-p", "--passwd"):
passwd = arg
elif opt in ("-u", "--user-name"):
user_name = arg
return xenapi_facts.get_facts(himn_ip, user_name, passwd)
if __name__ == "__main__":
if len(sys.argv) < 2:
exit_with_usage()
sys.exit(main(sys.argv[1:]))

View File

@ -1,51 +0,0 @@
# 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 mock
from os_xenapi.cmd import get_xenapi_facts
from os_xenapi.tests import base
from os_xenapi.utils import xenapi_facts
class GetXenapiFactsTestCase(base.TestCase):
@mock.patch.object(xenapi_facts, 'get_facts')
def test_get_xenapi_facts(self, mock_get):
argv = ['-i', '169.254.0.1', '-u', 'root', '-p', 'passwd']
get_xenapi_facts.main(argv)
mock_get.assert_called_with('169.254.0.1', 'root', 'passwd')
@mock.patch.object(get_xenapi_facts, 'exit_with_usage')
@mock.patch.object(xenapi_facts, 'get_facts')
def test_get_xenapi_facts_wrong_opt(self, mock_get, mock_usage):
# Verify if it will exit with prompting usage if pass in
# wrong opts.
argv = ['-i', '169.254.0.1', '-u', 'root', '-v', 'invalid_opt']
get_xenapi_facts.main(argv)
mock_usage.assert_called_with()
mock_get.assert_not_called()
@mock.patch.object(get_xenapi_facts, 'exit_with_usage')
@mock.patch.object(xenapi_facts, 'get_facts')
def test_get_xenapi_facts_lack_opts(self, mock_get, mock_usage):
# Verify if it will exit with prompting usage if not giving
# all required opts.
argv = ['-i', '169.254.0.1']
get_xenapi_facts.main(argv)
mock_usage.assert_called_with()
mock_get.assert_not_called()

View File

@ -0,0 +1,29 @@
# 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 mock
from os_xenapi.tests import base
from os_xenapi.utils import common_function
class CommonUtilFuncTestCase(base.TestCase):
def test_get_remote_hostname(self):
mock_client = mock.Mock()
out = ' \nFake_host_name\n '
err = ''
mock_client.ssh.return_value = (out, err)
hostname = common_function.get_remote_hostname(mock_client)
self.assertEqual(hostname, 'Fake_host_name')
mock_client.ssh.assert_called_with('hostname')

View File

@ -10,40 +10,31 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import mock
import netifaces
from os_xenapi.tests import base
from os_xenapi.utils import common_function
from os_xenapi.utils import himn
from os_xenapi.utils import xenapi_facts
class XenapiFactsTestCase(base.TestCase):
def test_hostname(self):
mock_client = mock.Mock()
out = 'fake_hostname\r\n'
err = ''
mock_client.ssh.return_value = (out, err)
hostname = xenapi_facts.get_hostname(mock_client)
mock_client.ssh.assert_called_with('hostname')
self.assertEqual(hostname, 'fake_hostname')
@mock.patch.object(xenapi_facts, 'get_hostname')
@mock.patch.object(common_function, 'get_remote_hostname')
@mock.patch.object(himn, 'get_local_himn_eth')
@mock.patch.object(netifaces, 'ifaddresses')
def test_get_facts(self, mock_ifaddr, mock_eth, mock_hostname):
xenapi_facts.sshclient.SSHClient = mock.Mock
mock_client = mock.Mock()
mock_client.ip = mock.sentinel.dom0_himn_ip
mock_eth.return_value = 'eth3'
mock_ifaddr.return_value = {2: [{'netmask': u'255.255.0.0',
'addr': u'169.254.0.2'}]}
mock_hostname.return_value = 'traya'
facts_json = xenapi_facts.get_facts('169.254.0.1', 'root', 'passwd')
ret_facts = xenapi_facts.get_xenapi_facts(mock_client)
expect_facts = {"local_himn_ip": "169.254.0.2",
"local_himn_eth": "eth3",
"hostname": "traya"}
self.assertEqual(json.loads(facts_json), expect_facts)
expect_facts = {"domu_himn_ip": "169.254.0.2",
"domu_himn_eth": "eth3",
"dom0_hostname": "traya"}
self.assertEqual(ret_facts, expect_facts)
mock_eth.assert_called_with(mock.sentinel.dom0_himn_ip)

View File

@ -80,3 +80,10 @@ def get_eth_ipaddr(eth):
def get_eth_mac(eth):
# Get eth's mac address.
return netifaces.ifaddresses(eth).get(netifaces.AF_LINK)[0]['addr']
def get_remote_hostname(host_client):
# Get remote host's hostname via the host_client connected to the host.
out, _ = host_client.ssh('hostname')
hostname = out.strip()
return hostname

View File

@ -25,28 +25,37 @@ from os_xenapi.utils import himn
from os_xenapi.utils import sshclient
def get_hostname(host_client):
out, _ = host_client.ssh('hostname')
hostname = out.strip()
return hostname
def get_xenapi_facts(dom0_client):
"""Get XenAPI facts
This function will get XenAPI relative facts on the compute node:
dom0_hostname: dom0's hostname.
domu_himn_eth: domU's network interface which is connected to HIMN
domu_himn_ip: domU's ip which belong to the subnt reserved for HIMN
:param dom0_client: the remote access client connected to dom0
:returns: a dict which contains all facts gathered.
"""
def get_facts(dom0_himn_ip, user_name, passwd):
facts = {}
dom0_client = sshclient.SSHClient(dom0_himn_ip, user_name, passwd)
# get dom0's hostname
facts['dom0_hostname'] = common_function.get_remote_hostname(dom0_client)
facts['hostname'] = get_hostname(dom0_client)
# get local HIMN info
eth = himn.get_local_himn_eth(dom0_himn_ip)
# get domU's eth and ip which are connected to HIMN.
eth = himn.get_local_himn_eth(dom0_client.ip)
ip_addr = common_function.get_eth_ipaddr(eth)
facts['local_himn_eth'] = eth
facts['local_himn_ip'] = ip_addr
facts['domu_himn_eth'] = eth
facts['domu_himn_ip'] = ip_addr
return facts
return json.dumps(facts)
if __name__ == '__main__':
dom0_himn_ip, user_name, passwd = sys.argv[1:]
facts_json = get_facts(dom0_himn_ip, user_name, passwd)
print('get_facts returns:\n %s' % facts_json)
# Run in domU which has an interface connected to HIMN
# argv[1]: dom0's IP address
# argv[2]: user name
# argv[3]: user passwd
ssh_client = sshclient.SSHClient(sys.argv[1], sys.argv[2], sys.argv[3])
print('Got XenAPI facts as:\n%s' % json.dumps(get_xenapi_facts(ssh_client),
indent=4, sort_keys=True))