Commit 5140ec99 authored by Titouan Soulard's avatar Titouan Soulard

cli/request: allow enforcing a given serialisation

parent 09d2ca06
......@@ -113,6 +113,10 @@ class RequestCommand(ClientConfigCommand):
action='store_true',
help='Ask for a slave instance')
ap.add_argument('--force-serialisation',
choices=[s.value for s in SoftwareReleaseSerialisation],
help='Enforce serialisation of instance parameters when using --parameters-file')
parameter_args = ap.add_mutually_exclusive_group()
parameter_args.add_argument(
......@@ -151,8 +155,11 @@ def do_request(logger, conf, local):
software_schema = SoftwareReleaseSchema(conf.software_url, conf.type)
parameters = conf.parameters
if conf.parameters_file:
# getSerialisation must throw an exception if serialization cannot be found
parameters = getParametersFromFile(conf.parameters_file, software_schema.getSerialisation())
if conf.force_serialisation:
parameters = getParametersFromFile(conf.parameters_file, SoftwareReleaseSerialisation(conf.force_serialisation))
else:
# getSerialisation will throw an exception if serialization cannot be found
parameters = getParametersFromFile(conf.parameters_file, software_schema.getSerialisation())
try:
partition = local['slap'].registerOpenOrder().request(
software_release=conf.software_url,
......
......@@ -910,6 +910,7 @@ class TestCliRequest(CliMixin):
self.conf.type = None
self.conf.state = None
self.conf.slave = False
self.conf.force_serialisation = None
with patch.object(
slapos.slap.slap,
......@@ -961,6 +962,7 @@ class TestCliRequest(CliMixin):
self.conf.type = None
self.conf.state = None
self.conf.slave = False
self.conf.force_serialisation = None
cp = slapos.slap.ComputerPartition(
'computer_%s' % self.id(),
......@@ -1013,6 +1015,7 @@ class TestCliRequestParameterFile(CliMixin):
self.conf.type = None
self.conf.state = None
self.conf.slave = False
self.conf.force_serialisation = None
class TestCliRequestParameterFileUndefinedSerialization(TestCliRequestParameterFile):
......@@ -1118,3 +1121,40 @@ class TestCliRequestParametersFileXml(TestCliRequestParametersFileJson):
</instance>
'''
parameter_file_suffix = '.xml'
class TestCliRequestForceSerialisation(TestCliRequestParameterFile):
"""Request with --parameter-file, with a .json file, enforcing
serialisation with --force-serialisation.
"""
parameter_file_suffix = '.json'
parameter_file_content = '''\
{
"foo": ["bar"]
}
'''
expected_partition_parameter_kw = {"_": "{\"foo\": [\"bar\"]}"}
def test_request_parameters_file(self):
self._request_parameters_file_setup()
self.conf.force_serialisation = 'json-in-xml'
with patch.object(
slapos.slap.slap,
'registerOpenOrder',
return_value=mock.create_autospec(slapos.slap.OpenOrder)) as registerOpenOrder:
slapos.cli.request.do_request(self.logger, self.conf, self.local)
registerOpenOrder().request.assert_called_once_with(
software_release='software URL',
partition_reference='instance reference',
partition_parameter_kw=self.expected_partition_parameter_kw,
software_type=None,
filter_kw={'computer_guid': 'COMP-1234'},
state=None,
shared=False,
)
self.logger.info.assert_any_call(
'Requesting %s as instance of %s...',
'instance reference',
'software URL',
)
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