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