Commit 18017716 authored by Alan Rominger's avatar Alan Rominger

Merge pull request #103 from AlanCoding/fixes231

Bug fixes revealed in pre-release testing
parents d536be58 ddee1a95
......@@ -93,7 +93,7 @@ class Resource(models.ExeResource):
# Add the runtime extra_vars to this list
if extra_vars:
extra_vars_list += extra_vars
extra_vars_list += list(extra_vars) # accept tuples
# If the job template requires prompting for extra variables,
# do so (unless --no-input is set).
......@@ -109,7 +109,12 @@ class Resource(models.ExeResource):
initial,
))
extra_vars = click.edit(initial) or ''
extra_vars_list = [extra_vars]
if extra_vars != initial:
extra_vars_list = [extra_vars]
# Data is starting out with JT variables, and we only want to
# include extra_vars that come from the algorithm here.
data.pop('extra_vars', None)
# Dump extra_vars into JSON string for launching job
if len(extra_vars_list) > 0:
......
......@@ -76,7 +76,7 @@ class Resource(models.Resource):
extra_vars, force_json=False
)
# Provide a default value for job_type, but only in creation of JT
if 'job_type' not in kwargs:
if not kwargs.get('job_type', False):
kwargs['job_type'] = 'run'
return super(Resource, self).create(
fail_on_found=fail_on_found, force_on_exists=force_on_exists,
......
......@@ -61,6 +61,26 @@ def standard_registration(t):
t.register_json('/job_templates/1/launch/', {'job': 42}, method='POST')
def jt_vars_registration(t, extra_vars):
""" Endpoints that are needed to get information from job template.
This particular combination also entails
1) version of Tower - 2.2.0
2) sucessful job launch, id=42
3) prompts user for variables on launch """
t.register_json('/job_templates/1/', {
'ask_variables_on_launch': True,
'extra_vars': extra_vars,
'id': 1,
'name': 'frobnicate',
'related': {'launch': '/job_templates/1/launch/'},
})
register_get(t)
t.register_json('/config/', {'version': '2.2.0'}, method='GET')
t.register_json('/job_templates/1/launch/', {}, method='GET')
t.register_json('/job_templates/1/launch/', {'job': 42},
method='POST')
class LaunchTests(unittest.TestCase):
"""A set of tests for ensuring that the job resource's launch command
works in the way we expect.
......@@ -101,6 +121,15 @@ class LaunchTests(unittest.TestCase):
json.loads(t.requests[2].body)['job_tags'], 'a, b, c',
)
def test_launch_w_tuple_extra_vars(self):
"""Establish that if the click library gives a tuple, than the job
will run normally.
"""
with client.test_mode as t:
standard_registration(t)
result = self.res.launch(1, extra_vars=())
self.assertDictContainsSubset({'changed': True, 'id': 42}, result)
def test_basic_launch_monitor_option(self):
"""Establish that we are able to create a job that doesn't require
any invocation-time input, and that monitor is called if requested.
......@@ -117,18 +146,7 @@ class LaunchTests(unittest.TestCase):
"""
with client.test_mode as t:
# test with JSON job template extra_vars
t.register_json('/job_templates/1/', {
'ask_variables_on_launch': True,
'extra_vars': '{"spam": "eggs"}',
'id': 1,
'name': 'frobnicate',
'related': {'launch': '/job_templates/1/launch/'},
})
register_get(t)
t.register_json('/config/', {'version': '2.2.0'}, method='GET')
t.register_json('/job_templates/1/launch/', {}, method='GET')
t.register_json('/job_templates/1/launch/', {'job': 42},
method='POST')
jt_vars_registration(t, '{"spam": "eggs"}')
with mock.patch.object(click, 'edit') as edit:
edit.return_value = '# Nothing.\nfoo: bar'
result = self.res.launch(1, no_input=False)
......@@ -142,42 +160,47 @@ class LaunchTests(unittest.TestCase):
)
self.assertDictContainsSubset({'changed': True, 'id': 42}, result)
def test_extra_vars_at_runtime_YAML_JT(self):
"""Establish that if we should be asking for extra variables at
runtime, that we do.
"""
with client.test_mode as t:
# test with YAML and comments
t.register_json('/job_templates/1/', {
'ask_variables_on_launch': True,
'extra_vars': 'spam: eggs\n# comment',
'id': 1,
'name': 'frobnicate',
'related': {'launch': '/job_templates/1/launch/'},
})
jt_vars_registration(t, 'spam: eggs\n# comment')
with mock.patch.object(click, 'edit') as edit:
edit.return_value = '# Nothing.\nfoo: bar'
result = self.res.launch(
1, no_input=False
)
self.res.launch(1, no_input=False)
self.assertIn('# comment', edit.mock_calls[0][1][0])
self.assertDictContainsSubset(
{"spam": "eggs"},
yaml.load(edit.mock_calls[0][1][0])
)
def test_extra_vars_at_runtime_no_user_data(self):
"""User launches a job that prompts for variables. User closes
editor without adding any text.
Establish that we launch the job as-is.
"""
with client.test_mode as t:
# No job template variables
jt_vars_registration(t, '')
initial = '\n'.join((
'# Specify extra variables (if any) here as YAML.',
'# Lines beginning with "#" denote comments.',
'',
))
with mock.patch.object(click, 'edit') as edit:
edit.return_value = initial
self.res.launch(1, no_input=False)
self.assertEqual(t.requests[2].method, 'POST')
self.assertEqual(t.requests[2].body, '{}')
def test_job_template_variables(self):
"""Establish that job template extra_vars are combined with local
extra vars, but only for older versions
"""
with client.test_mode as t:
t.register_json('/job_templates/1/', {
'ask_variables_on_launch': True,
'extra_vars': 'spam: eggs',
'id': 1,
'name': 'frobnicate',
'related': {'launch': '/job_templates/1/launch/'},
})
register_get(t)
t.register_json('/config/', {'version': '2.2.0'}, method='GET')
t.register_json('/job_templates/1/launch/', {}, method='GET')
t.register_json('/job_templates/1/launch/', {'job': 42},
method='POST')
jt_vars_registration(t, 'spam: eggs')
result = self.res.launch(1, extra_vars=['foo: bar'])
response_json = yaml.load(t.requests[3].body)
ev_json = yaml.load(response_json['extra_vars'])
......@@ -185,21 +208,12 @@ class LaunchTests(unittest.TestCase):
self.assertTrue('spam' in ev_json)
self.assertDictContainsSubset({'changed': True, 'id': 42}, result)
# check that in recent versions, it does not include job template
# variables along with the rest
def test_job_template_variables_post_24(self):
""" Check that in recent versions, it does not include job template
variables along with the rest """
with client.test_mode as t:
t.register_json('/job_templates/1/', {
'ask_variables_on_launch': True,
'extra_vars': 'spam: eggs',
'id': 1,
'name': 'frobnicate',
'related': {'launch': '/job_templates/1/launch/'},
})
register_get(t)
jt_vars_registration(t, 'spam: eggs')
t.register_json('/config/', {'version': '2.4'}, method='GET')
t.register_json('/job_templates/1/launch/', {}, method='GET')
t.register_json('/job_templates/1/launch/', {'job': 42},
method='POST')
result = self.res.launch(1, extra_vars=['foo: bar'])
response_json = yaml.load(t.requests[3].body)
ev_json = yaml.load(response_json['extra_vars'])
......
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