Add an 'owner' param to the Test Planner execution engine and the REST 
client

Signed-off-by: James Ren <jamesren@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@4431 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/afe/resources.py b/frontend/afe/resources.py
index 9527acb..b049be0 100644
--- a/frontend/afe/resources.py
+++ b/frontend/afe/resources.py
@@ -639,6 +639,10 @@
 
     @classmethod
     def create_instance(cls, input_dict, containing_collection):
+        owner = input_dict.get('owner')
+        if not owner:
+            owner = models.User.current_user().login
+
         cls._check_for_required_fields(input_dict, ('name', 'execution_info',
                                                     'queue_entries'))
         execution_info = input_dict['execution_info']
@@ -686,7 +690,7 @@
                     atomic_group = atomic_group_entry.instance
 
         job_id = rpc_utils.create_new_job(
-                owner=models.User.current_user().login,
+                owner=owner,
                 options=options,
                 host_objects=host_objects,
                 metahost_objects=metahost_label_objects,
diff --git a/frontend/afe/resources_test.py b/frontend/afe/resources_test.py
index 427b8c9..1489362 100644
--- a/frontend/afe/resources_test.py
+++ b/frontend/afe/resources_test.py
@@ -442,10 +442,11 @@
         self.check_collection(entries, ['host', 'hostname'], ['host1', 'host2'])
 
 
-    def test_post(self):
+    def _test_post_helper(self, owner):
         data = {'name': 'myjob',
                 'execution_info': {'control_file': self.CONTROL_FILE_CONTENTS,
                                    'is_server': True},
+                'owner': owner,
                 'queue_entries':
                 [{'host': {'href': self.URI_PREFIX + '/hosts/host1'}},
                  {'host': {'href': self.URI_PREFIX + '/hosts/host2'}}]}
@@ -459,6 +460,19 @@
         self.assertEquals(entries[0].host.hostname, 'host1')
         self.assertEquals(entries[1].host.hostname, 'host2')
 
+        owner_test = owner
+        if not owner_test:
+            owner_test = models.User.current_user().login
+        self.assertEquals(job.owner, owner_test)
+
+
+    def test_post_no_owner(self):
+        self._test_post_helper(None)
+
+
+    def test_post_with_owner(self):
+        self._test_post_helper('job_owner')
+
 
 class DirectoryTest(ResourceTestCase):
     def test_get(self):
diff --git a/frontend/planner/execution_engine.py b/frontend/planner/execution_engine.py
index 9eea6ad..ab7d508 100644
--- a/frontend/planner/execution_engine.py
+++ b/frontend/planner/execution_engine.py
@@ -15,12 +15,13 @@
     _planner_rpc = frontend.Planner()
     _tko_rpc = frontend.TKO()
 
-    def __init__(self, plan_id, server, label_name):
+    def __init__(self, plan_id, server, label_name, owner):
         self._plan_id = plan_id
         self._server = server
+        self._label_name = label_name
+        self._owner = owner
         self._afe_rest = rest_client.Resource.load(
                 'http://%s/afe/server/resources' % server)
-        self._label_name = label_name
 
 
     def start(self):
@@ -80,6 +81,7 @@
                        'plan_id': self._plan_id}
 
             job_req = {'name' : name,
+                       'owner': self._owner,
                        'execution_info' : info,
                        'queue_entries' : entries,
                        'keyvals' : keyvals}
@@ -217,6 +219,7 @@
             prefix = plan['name']
         job_req = {'name' : '%s_%s_%s' % (prefix, test_config['alias'],
                                           hostname),
+                   'owner': self._owner,
                    'execution_info' : info,
                    'queue_entries' : entries}
 
diff --git a/frontend/planner/execution_engine_control.srv b/frontend/planner/execution_engine_control.srv
index 38b0e6c..28fd33d 100644
--- a/frontend/planner/execution_engine_control.srv
+++ b/frontend/planner/execution_engine_control.srv
@@ -2,5 +2,6 @@
 
 engine = execution_engine.ExecutionEngine(plan_id=%(plan_id)d,
                                           server='%(server)s',
-                                          label_name='%(label_name)s')
+                                          label_name='%(label_name)s',
+                                          owner='%(owner)s')
 engine.start()
diff --git a/frontend/planner/execution_engine_unittest.py b/frontend/planner/execution_engine_unittest.py
index 2e13180..7c69128 100644
--- a/frontend/planner/execution_engine_unittest.py
+++ b/frontend/planner/execution_engine_unittest.py
@@ -44,6 +44,7 @@
     _server = object()
     _afe_rest = MockAfeRest()
     _label_name = object()
+    _owner = object()
 
 
     def __init__(self, *args, **kwargs):
@@ -136,6 +137,7 @@
                    'plan_id': self.engine._plan_id}
 
         job_req = {'name': name,
+                   'owner': self.engine._owner,
                    'execution_info': DUMMY_EXECUTION_INFO.execution_info,
                    'queue_entries': DUMMY_QUEUE_ENTRIES_REQUEST.queue_entries,
                    'keyvals': keyvals}
diff --git a/frontend/planner/rpc_utils.py b/frontend/planner/rpc_utils.py
index 4c93444..7195921 100644
--- a/frontend/planner/rpc_utils.py
+++ b/frontend/planner/rpc_utils.py
@@ -38,9 +38,11 @@
     """
     timeout = global_config.global_config.get_config_value(
             'PLANNER', 'execution_engine_timeout')
-    control = _get_execution_engine_control(server=SERVER,
-                                            plan_id=plan.id,
-                                            label_name=label.name)
+    control = _get_execution_engine_control(
+            server=SERVER,
+            plan_id=plan.id,
+            label_name=label.name,
+            owner=afe_models.User.current_user().login)
     options = {'name': plan.name + '_execution_engine',
                'priority': afe_models.Job.Priority.MEDIUM,
                'control_file': control,
@@ -57,13 +59,14 @@
     job.queue(hosts=())
 
 
-def _get_execution_engine_control(server, plan_id, label_name):
+def _get_execution_engine_control(server, plan_id, label_name, owner):
     """
     Gets the control file to run the execution engine
     """
     control = lazy_load(os.path.join(os.path.dirname(__file__),
                                      'execution_engine_control.srv'))
-    return control % dict(server=server, plan_id=plan_id, label_name=label_name)
+    return control % dict(server=server, plan_id=plan_id,
+                          label_name=label_name, owner=owner)
 
 
 def lazy_load(path):