os-xenapi: Add setting function to support logging

Add function to support logging

Change-Id: I7b77089b38219467a983c78548787d6dfdafade4
This commit is contained in:
naichuans 2018-01-10 08:43:00 +00:00
parent b1bf53ccd6
commit 5ae81fe3fb
4 changed files with 62 additions and 3 deletions

View File

@ -19,9 +19,11 @@ a good state to proceed for further OpenStack deployment."""
import getopt
import json
import logging
import sys
from os_xenapi.utils.common_conf import enable_linux_bridge
from os_xenapi.utils.common_function import setup_logging
from os_xenapi.utils.himn import config_himn
from os_xenapi.utils.iptables import config_iptables
from os_xenapi.utils.sshclient import SSHClient
@ -37,6 +39,8 @@ USAGE_MSG += " [-p|--passwd] <passwd>\n\n"
DEF_XENAPI_FACTS_FILE = '/etc/xenapi_facts.json'
LOG = logging.getLogger(__name__)
def exit_with_usage():
sys.stderr.write(USAGE_MSG)
@ -86,6 +90,8 @@ def _parse_args(argv):
def main():
setup_logging(log_level=logging.DEBUG)
opt_values = _parse_args(sys.argv)
himn_ip = opt_values['himn-ip']
@ -96,6 +102,7 @@ def main():
dom0_client = SSHClient(himn_ip, user_name, passwd)
# Invoke functions to do needed boostrap tasks.
LOG.info("Launch bootstrap task")
config_himn(himn_ip)
config_iptables(dom0_client)
install_plugins_to_dom0(dom0_client)

View File

@ -9,7 +9,7 @@
# 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 mock
from os_xenapi.cmd import bootstrap
@ -76,8 +76,10 @@ class GetXenapiFactsTestCase(base.TestCase):
@mock.patch.object(bootstrap, 'install_plugins_to_dom0')
@mock.patch.object(bootstrap, 'get_and_store_facts')
@mock.patch.object(bootstrap, 'enable_linux_bridge')
def test_bootstrap(self, mock_enable_lbr, mock_facts, mock_plugin,
mock_iptables, mock_himn, mock_client, mock_parse):
@mock.patch.object(bootstrap, 'setup_logging')
def test_bootstrap(self, mock_setup_logging, mock_enable_lbr, mock_facts,
mock_plugin, mock_iptables, mock_himn, mock_client,
mock_parse):
fake_opts = {'himn-ip': '169.254.0.1',
'passwd': 'passwd',
'user-name': 'root'}
@ -93,3 +95,4 @@ class GetXenapiFactsTestCase(base.TestCase):
mock_facts.assert_called_with(mock.sentinel.sshclient,
bootstrap.DEF_XENAPI_FACTS_FILE)
mock_enable_lbr.assert_called_with(mock.sentinel.sshclient)
mock_setup_logging.assert_called_once_with(log_level=logging.DEBUG)

View File

@ -10,7 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import mock
import os
from os_xenapi.tests import base
from os_xenapi.utils import common_function
@ -118,3 +120,36 @@ class CommonUtilFuncTestCase(base.TestCase):
mock_if.assert_called_with()
self.assertEqual(mock_ifaddr.call_args_list,
[mock.call('eth0'), mock.call('eth1')])
@mock.patch.object(logging, 'basicConfig')
@mock.patch.object(os.path, 'exists')
@mock.patch.object(os, 'mkdir')
def test_setup_logging(self, mock_mkdir, mock_exists, fake_log_conf):
expect_log_file = 'fake_folder/fake_file'
mock_exists.return_value = True
common_function.setup_logging('fake_file', 'fake_folder/',
'fake_debug_level')
fake_log_conf.assert_called_once_with(
filename=expect_log_file, level='fake_debug_level',
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
mock_mkdir.assert_not_called()
mock_exists.assert_called_once_with('fake_folder/')
@mock.patch.object(logging, 'basicConfig')
@mock.patch.object(os.path, 'exists')
@mock.patch.object(os, 'mkdir')
def test_setup_logging_create_path(self, mock_mkdir, mock_exists,
fake_log_conf):
expect_log_file = 'fake_folder/fake_file'
mock_exists.return_value = False
common_function.setup_logging('fake_file', 'fake_folder/',
'fake_debug_level')
fake_log_conf.assert_called_once_with(
filename=expect_log_file, level='fake_debug_level',
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
mock_mkdir.assert_called_once_with('fake_folder/')
mock_exists.assert_called_once_with('fake_folder/')

View File

@ -30,6 +30,8 @@ from os_xenapi.client import exception
PATTERN_XENSTORE_VIFS_IN_VM = '/xapi/%s/private/vif'
LOG_ROOT = '/var/log/os-xenapi'
DEFAULT_LOG_FILE = 'xenapi.log'
LOG = logging.getLogger('XenAPI_utils')
@ -191,3 +193,15 @@ def scp_and_execute(dom0_client, script_name):
dom0_client.ssh(TMP_SH_PATH)
finally:
dom0_client.ssh("rm -rf " + TMP_SH_DIR)
def setup_logging(filename=DEFAULT_LOG_FILE, folder=LOG_ROOT,
log_level=logging.WARNING):
log_file = os.path.join(folder, filename)
if not os.path.exists(folder):
os.mkdir(folder)
logging.basicConfig(
filename=log_file, level=log_level,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')