Commit 530f422b authored by Jérome Perrin's avatar Jérome Perrin Committed by Rafael Monnerat

proxy show: output on stdout

It was set to output on stderr, which is not natural and inconvenient
parent 5aec3d03
......@@ -30,6 +30,8 @@
import collections
import hashlib
import json
import logging
import sys
import lxml.etree
import prettytable
......@@ -198,7 +200,17 @@ def log_network(logger, conn):
def do_show(conf):
conf.logger.debug('Using database: %s', conf.database_uri)
# Because this command uses logging facility to output,
# setup a logger which displays on stdout.
proxy_show_logger = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
proxy_show_logger.addHandler(handler)
proxy_show_logger.propagate = False
proxy_show_logger.debug('Using database: %s', conf.database_uri)
conn = sqlite_connect(conf.database_uri)
conn.row_factory = sqlite3.Row
......@@ -219,6 +231,6 @@ def do_show(conf):
to_call = [func for flag, func in call_table if flag]
for idx, func in enumerate(to_call):
func(conf.logger, conn)
func(proxy_show_logger, conn)
if idx < len(to_call) - 1:
conf.logger.info(' ')
proxy_show_logger.info(' ')
......@@ -104,14 +104,7 @@ class TestCliProxyShow(CliMixin):
db.cursor().executescript(schema.read())
db.commit()
def tearDown(self):
super(TestCliProxyShow, self).tearDown()
os.remove(self.db_file.name)
def test_proxy_show(self):
# simulate "show all" arguments
# by default we simulate being invoked with "show all" arguments
self.conf.computers = True
self.conf.software = True
self.conf.partitions = True
......@@ -119,30 +112,59 @@ class TestCliProxyShow(CliMixin):
self.conf.params = True
self.conf.network = True
slapos.cli.proxy_show.do_show(self.conf)
def tearDown(self):
super(TestCliProxyShow, self).tearDown()
os.remove(self.db_file.name)
def test_proxy_show(self):
logger = create_autospec(logging.Logger)
with mock.patch(
'slapos.cli.proxy_show.logging.getLogger',
return_value=logger):
slapos.cli.proxy_show.do_show(self.conf)
# installed softwares are listed
self.logger.info.assert_any_call(
logger.info.assert_any_call(
' /srv/slapgrid/slappart8/srv/runner/project/slapos/software/erp5/software.cfg slaprunner 287375f0cba269902ba1bc50242839d7 ' )
# instance parameters are listed
# _ parameter is json formatted
self.logger.info.assert_any_call(
logger.info.assert_any_call(
' %s = %s',
'_',
'{\n "url": "memcached://10.0.30.235:2003/", \n "monitor-base-url": ""\n}')
# other parameters are displayed as simple string
self.logger.info.assert_any_call(
logger.info.assert_any_call(
' %s = %s',
'url',
'http://10.0.30.235:4444/wd/hub')
# if _ cannot be decoded as json, it is displayed "as is"
self.logger.info.assert_any_call(
logger.info.assert_any_call(
' %s = %s',
'_',
u'Ahah this is not json \U0001f61c ')
# Nothing was output on application logger, because this command uses
# its own logger.
self.logger.info.assert_not_called()
def test_proxy_show_displays_on_stdout(self):
saved_stderr = sys.stderr
saved_stdout = sys.stdout
sys.stderr = stderr = StringIO.StringIO()
sys.stdout = stdout = StringIO.StringIO()
try:
slapos.cli.proxy_show.do_show(self.conf)
finally:
sys.stderr = saved_stderr
sys.stdout = saved_stdout
# 287375f0cba269902ba1bc50242839d7 is the hash of an installed software
# in our setup database
# pytest users, be sure to use --log-level=DEBUG
self.assertIn('287375f0cba269902ba1bc50242839d7', stdout.getvalue())
self.assertEqual('', stderr.getvalue())
class TestCliNode(CliMixin):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment