diff mbox

[Branch,~linaro-validation/lava-dispatcher/trunk] Rev 160: Add a --validate switch for using the dispatcher to validate the schema

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

Commit Message

Paul Larson Nov. 17, 2011, 5:34 p.m. UTC
Merge authors:
  Michael Hudson-Doyle (mwhudson)
Related merge proposals:
  https://code.launchpad.net/~mwhudson/lava-dispatcher/validate-job-files/+merge/82237
  proposed by: Michael Hudson-Doyle (mwhudson)
------------------------------------------------------------
revno: 160 [merge]
committer: Paul Larson <paul.larson@canonical.com>
branch nick: lava-dispatcher
timestamp: Thu 2011-11-17 11:29:55 -0600
message:
  Add a --validate switch for using the dispatcher to validate the schema
  before running a job.
modified:
  lava-dispatch
  lava_dispatcher/job.py
  requirements.txt
  setup.py


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

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

Patch

=== modified file 'lava-dispatch'
--- lava-dispatch	2011-11-14 00:52:45 +0000
+++ lava-dispatch	2011-11-14 04:45:54 +0000
@@ -24,12 +24,17 @@ 
 import sys
 import logging.config
 
+from linaro_json.schema import ValidationError
+
 from lava_dispatcher.job import LavaTestJob
 from lava_dispatcher.config import get_config
 
 parser = optparse.OptionParser('%prog: lava-dispatch <json job file>')
 parser.add_option(
     "--oob-fd", default=None, type=int, help="Write OOB data to this fd.")
+parser.add_option(
+    "--validate", action='store_true',
+    help="Just validate the job file, do not execute any steps.")
 
 (options, args) = parser.parse_args()
 
@@ -59,4 +64,10 @@ 
 job = LavaTestJob(jobdata, oob_file)
 
 #FIXME Return status
-job.run()
+if options.validate:
+    try:
+        job.validate()
+    except ValidationError as e:
+        print e
+else:
+    job.run()

=== modified file 'lava_dispatcher/job.py'
--- lava_dispatcher/job.py	2011-11-11 14:47:22 +0000
+++ lava_dispatcher/job.py	2011-11-15 01:44:45 +0000
@@ -23,12 +23,53 @@ 
 import pexpect
 import traceback
 
+from linaro_json.schema import Schema, Validator
+
 from lava_dispatcher.actions import get_all_cmds
 from lava_dispatcher.client import CriticalError, GeneralError
 from lava_dispatcher.config import get_config
 from lava_dispatcher.context import LavaContext 
 
 
+job_schema = {
+    'type': 'object',
+    'additionalProperties': False,
+    'properties': {
+        'actions': {
+            'items': {
+                'properties': {
+                    'command': {
+                        'optional': False,
+                        },
+                    'parameters': {
+                        'optional': True,
+                        },
+                    'metadata': {
+                        'optional': True,
+                        },
+                    },
+                'additionalProperties': False,
+                },
+            },
+        'device_type': {
+            'optional': True,
+            },
+        'job_name': {
+            'optional': True,
+            },
+        'image_type': {
+            'optional': True,
+            },
+        'target': {
+            'optional': True,
+            },
+        'timeout': {
+            'type': 'integer',
+            'optional': False,
+            },
+        },
+    }
+
 class LavaTestJob(object):
     def __init__(self, job_json, oob_file):
         self.job_status = 'pass'
@@ -49,7 +90,19 @@ 
     def image_type(self):
         return self.job_data.get('image_type')
 
+    def validate(self):
+        schema = Schema(job_schema)
+        validator = Validator()
+        validator.validate(schema, self.job_data)
+
+        lava_commands = get_all_cmds()
+        for action in self.job_data['actions']:
+            command_name = action['command']
+            if command_name not in lava_commands:
+                raise CriticalError("action %r not known" % command_name)
+
     def run(self):
+        self.validate()
         lava_commands = get_all_cmds()
 
         if self.job_data['actions'][-1]['command'].startswith("submit_results"):

=== modified file 'requirements.txt'
--- requirements.txt	2011-04-20 22:45:21 +0000
+++ requirements.txt	2011-11-14 04:24:37 +0000
@@ -4,5 +4,3 @@ 
 python-openid
 lockfile
 python-daemon
-
-

=== modified file 'setup.py'
--- setup.py	2011-11-11 14:38:21 +0000
+++ setup.py	2011-11-14 04:24:37 +0000
@@ -22,6 +22,7 @@ 
         },
     install_requires=[
         "pexpect >= 2.3",
+        "json-schema-validator",
     ],
     setup_requires=[
         'versiontools >= 1.8',