=== modified file 'lava_scheduler_app/models.py'
@@ -219,12 +219,18 @@
raise JSONDataError(
"Neither 'target' nor 'device_type' found in job data.")
job_name = job_data.get('job_name', '')
+ tags = []
+ for tag_name in job_data.get('device_tags', []):
+ try:
+ tags.append(Tag.objects.get(name=tag_name))
+ except Tag.DoesNotExist:
+ raise JSONDataError("tag %r does not exist" % tag_name)
job = TestJob(
definition=json_data, submitter=user, requested_device=target,
requested_device_type=device_type, description=job_name)
job.save()
- for tag_name in job_data.get('device_tags', []):
- job.tags.add(Tag.objects.get_or_create(name=tag_name)[0])
+ for tag in tags:
+ job.tags.add(tag)
return job
def can_cancel(self, user):
=== modified file 'lava_scheduler_app/tests.py'
@@ -11,7 +11,12 @@
from linaro_django_xmlrpc.models import AuthToken
-from lava_scheduler_app.models import Device, DeviceType, Tag, TestJob
+from lava_scheduler_app.models import (
+ Device,
+ DeviceType,
+ JSONDataError,
+ Tag,
+ TestJob)
from lava_scheduler_daemon.dbjobsource import DatabaseJobSource
@@ -59,6 +64,9 @@
name = self.getUniqueString('name')
return DeviceType.objects.get_or_create(name=name)[0]
+ def ensure_tag(self, name):
+ return Tag.objects.get_or_create(name=name)[0]
+
def make_device(self, device_type=None, hostname=None):
if device_type is None:
device_type = self.ensure_device_type()
@@ -141,8 +149,16 @@
self.factory.make_user())
self.assertEqual(set(job.tags.all()), set([]))
+ def test_from_json_and_user_errors_on_unknown_tags(self):
+ self.factory.ensure_device_type(name='panda')
+ self.assertRaises(
+ JSONDataError, TestJob.from_json_and_user,
+ json.dumps({'device_type':'panda', 'device_tags':['unknown']}),
+ self.factory.make_user())
+
def test_from_json_and_user_sets_tag_from_device_tags(self):
self.factory.ensure_device_type(name='panda')
+ self.factory.ensure_tag('tag')
job = TestJob.from_json_and_user(
json.dumps({'device_type':'panda', 'device_tags':['tag']}),
self.factory.make_user())
@@ -151,6 +167,8 @@
def test_from_json_and_user_sets_multiple_tag_from_device_tags(self):
self.factory.ensure_device_type(name='panda')
+ self.factory.ensure_tag('tag1')
+ self.factory.ensure_tag('tag2')
job = TestJob.from_json_and_user(
json.dumps({'device_type':'panda', 'device_tags':['tag1', 'tag2']}),
self.factory.make_user())
@@ -159,6 +177,7 @@
def test_from_json_and_user_reuses_tag_objects(self):
self.factory.ensure_device_type(name='panda')
+ self.factory.ensure_tag('tag')
job1 = TestJob.from_json_and_user(
json.dumps({'device_type':'panda', 'device_tags':['tag']}),
self.factory.make_user())