diff mbox

[Branch,~linaro-validation/lava-scheduler/trunk] Rev 123: reject unknown jobs at submit time

Message ID 20120201030812.15931.32163.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Michael-Doyle Hudson Feb. 1, 2012, 3:08 a.m. UTC
Merge authors:
  Michael Hudson-Doyle (mwhudson)
Related merge proposals:
  https://code.launchpad.net/~mwhudson/lava-scheduler/reject-unknown-tags-bug-921250/+merge/90801
  proposed by: Michael Hudson-Doyle (mwhudson)
  review: Approve - Zygmunt Krynicki (zkrynicki)
------------------------------------------------------------
revno: 123 [merge]
committer: Michael Hudson-Doyle <michael.hudson@linaro.org>
branch nick: trunk
timestamp: Wed 2012-02-01 16:06:38 +1300
message:
  reject unknown jobs at submit time
modified:
  lava_scheduler_app/models.py
  lava_scheduler_app/tests.py


--
lp:lava-scheduler
https://code.launchpad.net/~linaro-validation/lava-scheduler/trunk

You are subscribed to branch lp:lava-scheduler.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-scheduler/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'lava_scheduler_app/models.py'
--- lava_scheduler_app/models.py	2012-01-20 01:15:16 +0000
+++ lava_scheduler_app/models.py	2012-01-30 22:26:03 +0000
@@ -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'
--- lava_scheduler_app/tests.py	2012-01-24 01:43:43 +0000
+++ lava_scheduler_app/tests.py	2012-01-30 22:26:03 +0000
@@ -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())