From patchwork Mon Jun 13 23:43:12 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael-Doyle Hudson X-Patchwork-Id: 1886 Return-Path: Delivered-To: unknown Received: from imap.gmail.com (74.125.47.109) by localhost6.localdomain6 with IMAP4-SSL; 14 Jun 2011 16:46:21 -0000 Delivered-To: patches@linaro.org Received: by 10.52.181.10 with SMTP id ds10cs492080vdc; Mon, 13 Jun 2011 16:43:14 -0700 (PDT) Received: by 10.217.6.79 with SMTP id x57mr64694wes.10.1308008593291; Mon, 13 Jun 2011 16:43:13 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id r10si14221999wec.106.2011.06.13.16.43.12; Mon, 13 Jun 2011 16:43:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) client-ip=91.189.90.139; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) smtp.mail=bounces@canonical.com Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QWGmm-0001hq-MT for ; Mon, 13 Jun 2011 23:43:12 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id A60F12E8067 for ; Mon, 13 Jun 2011 23:43:12 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: lava-scheduler X-Launchpad-Branch: ~linaro-validation/lava-scheduler/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 4 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~linaro-validation/lava-scheduler/trunk] Rev 4: add a submit_job api frontend to job creation Message-Id: <20110613234312.7776.45324.launchpad@loganberry.canonical.com> Date: Mon, 13 Jun 2011 23:43:12 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13175"; Instance="initZopeless config overlay" X-Launchpad-Hash: 484bee6c8fbd200fe605b1d74a7ec05f034c6d62 Merge authors: Michael Hudson-Doyle (mwhudson) Related merge proposals: https://code.launchpad.net/~mwhudson/lava-scheduler/expose-submit_job/+merge/64354 proposed by: Michael Hudson-Doyle (mwhudson) review: Approve - Zygmunt Krynicki (zkrynicki) ------------------------------------------------------------ revno: 4 [merge] committer: Michael-Doyle Hudson branch nick: trunk timestamp: Tue 2011-06-14 11:41:33 +1200 message: add a submit_job api frontend to job creation added: lava_scheduler_app/api.py modified: lava_scheduler_app/extension.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 === added file 'lava_scheduler_app/api.py' --- lava_scheduler_app/api.py 1970-01-01 00:00:00 +0000 +++ lava_scheduler_app/api.py 2011-06-13 23:37:25 +0000 @@ -0,0 +1,15 @@ +import xmlrpclib + +from linaro_django_xmlrpc.models import ExposedAPI + +from lava_scheduler_app.models import TestJob + + +class SchedulerAPI(ExposedAPI): + + def submit_job(self, job_data): + if not self.user: + raise xmlrpclib.Fault(401, "Authentication required.") + if not self.user.has_perm('lava_scheduler_app.add_testjob'): + raise xmlrpclib.Fault(403, "Permission denied.") + return TestJob.from_json_and_user(job_data, self.user).id === modified file 'lava_scheduler_app/extension.py' --- lava_scheduler_app/extension.py 2011-06-12 22:41:42 +0000 +++ lava_scheduler_app/extension.py 2011-06-12 23:17:32 +0000 @@ -33,6 +33,11 @@ return "lava_scheduler_app" @property + def api_class(self): + from lava_scheduler_app.api import SchedulerAPI + return SchedulerAPI + + @property def name(self): return "Scheduler" === modified file 'lava_scheduler_app/tests.py' --- lava_scheduler_app/tests.py 2011-06-13 04:25:04 +0000 +++ lava_scheduler_app/tests.py 2011-06-13 23:39:37 +0000 @@ -1,11 +1,38 @@ import datetime import json +import xmlrpclib -from django.contrib.auth.models import User +from django.contrib.auth.models import Permission, User from django.test import TestCase from lava_scheduler_app.models import Device, DeviceType, TestJob +import cStringIO + +from xmlrpclib import ServerProxy, Transport + +from django.test.client import Client + +# Based on http://www.technobabble.dk/2008/apr/02/xml-rpc-dispatching-through-django-test-client/ +class TestTransport(Transport): + """Handles connections to XML-RPC server through Django test client.""" + + def __init__(self, user=None, password=None): + self.client = Client() + if user: + success = self.client.login(username=user, password=password) + if not success: + raise AssertionError("Login attempt failed!") + self._use_datetime = True + + def request(self, host, handler, request_body, verbose=0): + self.verbose = verbose + response = self.client.post( + handler, request_body, content_type="text/xml") + res = cStringIO.StringIO(response.content) + res.seek(0) + return self.parse_response(res) + class TestTestJob(TestCase): @@ -60,3 +87,42 @@ job = TestJob.from_json_and_user( json.dumps({'device_type':'panda'}), self.make_user()) self.assertEqual(job.status, TestJob.SUBMITTED) + + +class TestSchedulerAPI(TestCase): + + def server_proxy(self, user=None, password=None): + return ServerProxy( + 'http://localhost/RPC2/', + transport=TestTransport(user=user, password=password)) + + def test_api_rejects_anonymous(self): + server = self.server_proxy() + try: + server.scheduler.submit_job("{}") + except xmlrpclib.Fault as f: + self.assertEqual(401, f.faultCode) + else: + self.fail("fault not raised") + + def test_api_rejects_unpriv_user(self): + User.objects.create_user('test', 'e@mail.invalid', 'test').save() + server = self.server_proxy('test', 'test') + try: + server.scheduler.submit_job("{}") + except xmlrpclib.Fault as f: + self.assertEqual(403, f.faultCode) + else: + self.fail("fault not raised") + + def test_sets_definition(self): + user = User.objects.create_user('test', 'e@mail.invalid', 'test') + user.user_permissions.add( + Permission.objects.get(codename='add_testjob')) + user.save() + server = self.server_proxy('test', 'test') + DeviceType.objects.get_or_create(name='panda') + definition = json.dumps({'device_type':'panda'}) + job_id = server.scheduler.submit_job(definition) + job = TestJob.objects.get(id=job_id) + self.assertEqual(definition, job.definition)