Commit b623b78b authored by Jérome Perrin's avatar Jérome Perrin

cli/info,list: output in json format

because json output is different on python2 and python3 and cli is only
used on python2, we don't test the exact output in python2.
parent 0642e884
Changes
=======
1.8.4 (unreleased)
------------------
* service list, service info: output json
1.8.3 (2022-10-17)
------------------
* format: use correct IPv4 for tun interfaces
......
......@@ -91,9 +91,14 @@ def do_info(logger, conf, local):
if '_' in connection_parameter_dict:
connection_parameter_dict = json.loads(connection_parameter_dict['_'])
logger.info('Software Release URL: %s', instance._software_release_url)
logger.info('Instance state: %s', instance._requested_state)
logger.info('Instance parameters:')
logger.info(StrPrettyPrinter().pformat(instance._parameter_dict))
logger.info('Connection parameters:')
logger.info(StrPrettyPrinter().pformat(connection_parameter_dict))
logger.info(
json.dumps(
{
'software-url': instance._software_release_url,
'instance-state': instance._requested_state,
'instance-parameters': instance._parameter_dict,
'connection-parameters': connection_parameter_dict,
},
indent=2,
)
)
......@@ -27,6 +27,7 @@
#
##############################################################################
import json
import sys
import six
......@@ -54,9 +55,9 @@ def do_list(logger, conf, local):
resetLogger(logger)
# XXX catch exception
instance_dict = local['slap'].getOpenOrderDict()
if instance_dict == {}:
logger.info('No existing service.')
return
logger.info('List of services:')
for title, instance in six.iteritems(instance_dict):
logger.info('%s %s', title, instance._software_release_url)
logger.info(
json.dumps(
{title: instance._software_release_url
for (title, instance) in six.iteritems(instance_dict)},
sort_keys=True,
indent=2))
......@@ -62,6 +62,7 @@ import slapos.grid.svcbackend
import slapos.proxy
import slapos.slap
import six
import supervisor.supervisorctl
signature_certificate_list = """-----BEGIN CERTIFICATE-----
......@@ -602,16 +603,15 @@ class TestCliList(CliMixin):
'instance2': slapos.slap.SoftwareInstance(_title='instance2', _software_release_url='SR2'),
}
with patch.object(slapos.slap.slap, 'getOpenOrderDict', return_value=return_value) as _:
slapos.cli.list.do_list(self.logger, None, self.local)
self.logger.info.assert_any_call('%s %s', 'instance1', 'SR1')
self.logger.info.assert_any_call('%s %s', 'instance2', 'SR2')
slapos.cli.list.do_list(self.logger, self.conf, self.local)
if six.PY3:
self.logger.info.assert_called_once_with('{\n "instance1": "SR1",\n "instance2": "SR2"\n}')
def test_emptyList(self):
with patch.object(slapos.slap.slap, 'getOpenOrderDict', return_value={}) as _:
slapos.cli.list.do_list(self.logger, None, self.local)
slapos.cli.list.do_list(self.logger, self.conf, self.local)
self.logger.info.assert_called_once_with('No existing service.')
self.logger.info.assert_called_once_with('{}')
@patch.object(slapos.slap.slap, 'registerOpenOrder', return_value=slapos.slap.OpenOrder())
class TestCliInfo(CliMixin):
......@@ -643,13 +643,19 @@ class TestCliInfo(CliMixin):
return_value=instance):
slapos.cli.info.do_info(self.logger, self.conf, self.local)
self.logger.info.assert_any_call(pprint.pformat(instance._parameter_dict))
self.logger.info.assert_any_call(
'Software Release URL: %s', instance._software_release_url)
self.logger.info.assert_any_call(
'Instance state: %s', instance._requested_state)
self.logger.info.assert_any_call("{'myinstanceparameter': 'value2'}", )
self.logger.info.assert_any_call("{'myconnectionparameter': 'value1'}")
if six.PY3:
self.logger.info.assert_called_once_with(
textwrap.dedent('''\
{
"software-url": "SR1",
"instance-state": "mystate",
"instance-parameters": {
"myinstanceparameter": "value2"
},
"connection-parameters": {
"myconnectionparameter": "value1"
}
}'''))
def test_unknownReference(self, _):
"""
......
......@@ -1283,13 +1283,10 @@ class TestCliInformation(CliMasterMixin):
self.request('http://sr1//', None, 'MyInstance1', None)
self.request('http://sr2//', None, 'MyInstance2', None)
self.request('http://sr3//', None, 'MyInstance3', 'slappart0')
output = self.cliDoSlapos(('service', 'list'), stderr=subprocess.DEVNULL).splitlines()
self.assertEqual(len(output), 4)
self.assertEqual(output[0], 'List of services:')
output = self.cliDoSlapos(('service', 'list'), stderr=subprocess.DEVNULL)
self.assertEqual(
sorted(output[1:]),
['MyInstance0 http://sr0//', 'MyInstance1 http://sr1//', 'MyInstance2 http://sr2//'],
)
json.loads(output),
{'MyInstance0': 'http://sr0//', 'MyInstance1': 'http://sr1//', 'MyInstance2': 'http://sr2//'})
def test_service_info(self):
self.format_for_number_of_partitions(3)
......@@ -1298,27 +1295,23 @@ class TestCliInformation(CliMasterMixin):
self.request('http://sr2//', None, 'MyInstance2', 'slappart0')
output0 = self.cliDoSlapos(('service', 'info', 'MyInstance0'), stderr=subprocess.DEVNULL)
self.assertEqual(
output0.splitlines(),
[
'Software Release URL: http://sr0//',
'Instance state: busy',
'Instance parameters:',
'{}',
'Connection parameters:',
'{}'
],
json.loads(output0),
{
"software-url": "http://sr0//",
"instance-state": "busy",
"instance-parameters": {},
"connection-parameters": {},
},
)
output1 = self.cliDoSlapos(('service', 'info', 'MyInstance1'), stderr=subprocess.DEVNULL)
self.assertEqual(
output1.splitlines(),
[
'Software Release URL: http://sr1//',
'Instance state: busy',
'Instance parameters:',
"{'couscous': 'hello'}",
'Connection parameters:',
'{}'
],
json.loads(output1),
{
"software-url": "http://sr1//",
"instance-state": "busy",
"instance-parameters": {"couscous": "hello"},
"connection-parameters": {},
},
)
try:
self.cliDoSlapos(('service', 'info', 'MyInstance2'), stderr=subprocess.STDOUT)
......
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