[autotest] pull _autoserv_command_line functionality into utility module
This CL pulls the logic used by monitor_db.py to create an autoserv
command line from a job into its own function, in a separate new module
-- autoserv_utils.py. This will allow for code reuse with test_that.
monitor_db still contains a number of other functions which create
autoserv command lines for other tasks like log collection. These have
not yet been pulled out to shared utility functions, because those parts
of the scheduler didn't seem to have any unit test coverage. A future CL
may pull some of these out as well.
BUG=chromium:236471
TEST=unit tests pass. Ran a smoke suite in local autotest.
DEPLOY=scheduler
Change-Id: I6317b70aa9fb7e9968739582b9379112baa4507b
Reviewed-on: https://gerrit.chromium.org/gerrit/56136
Reviewed-by: Aviv Keshet <[email protected]>
Tested-by: Aviv Keshet <[email protected]>
Commit-Queue: Aviv Keshet <[email protected]>
diff --git a/scheduler/monitor_db.py b/scheduler/monitor_db.py
index d86ed3c..a50668a 100755
--- a/scheduler/monitor_db.py
+++ b/scheduler/monitor_db.py
@@ -25,6 +25,7 @@
from autotest_lib.scheduler import scheduler_logging_config
from autotest_lib.scheduler import scheduler_models
from autotest_lib.scheduler import status_server, scheduler_config
+from autotest_lib.server import autoserv_utils
from autotest_lib.site_utils.graphite import stats
BABYSITTER_PID_FILE_PREFIX = 'monitor_db_babysitter'
@@ -52,7 +53,8 @@
_db = None
_shutdown = False
-_autoserv_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'server', 'autoserv')
+_autoserv_directory = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'server')
+_autoserv_path = os.path.join(_autoserv_directory, 'autoserv')
_testing_mode = False
_drone_manager = None
@@ -234,24 +236,15 @@
@param machines - string - A machine or comma separated list of machines
for the (-m) flag.
@param extra_args - list - Additional arguments to pass to autoserv.
- @param job - Job object - If supplied, -u owner, -l name, and --test-retry
- parameters will be added.
+ @param job - Job object - If supplied, -u owner, -l name, --test-retry,
+ and client -c or server -s parameters will be added.
@param queue_entry - A HostQueueEntry object - If supplied and no Job
object was supplied, this will be used to lookup the Job object.
"""
- autoserv_argv = [_autoserv_path, '-p',
- '-r', drone_manager.WORKING_DIRECTORY]
- if machines:
- autoserv_argv += ['-m', machines]
- if job or queue_entry:
- if not job:
- job = queue_entry.job
- autoserv_argv += ['-u', job.owner, '-l', job.name]
- if job.test_retry:
- autoserv_argv += ['--test-retry='+str(job.test_retry)]
- if verbose:
- autoserv_argv.append('--verbose')
- return autoserv_argv + extra_args
+ return autoserv_utils.autoserv_run_job_command(_autoserv_directory,
+ machines, results_directory=drone_manager.WORKING_DIRECTORY,
+ extra_args=extra_args, job=job, queue_entry=queue_entry,
+ verbose=verbose)
class BaseDispatcher(object):
@@ -1788,6 +1781,7 @@
return control_path
+ # TODO: Refactor into autoserv_utils. crbug.com/243090
def _command_line(self):
execution_path = self.queue_entries[0].execution_path()
control_path = self._write_control_file(execution_path)
@@ -1802,9 +1796,6 @@
_drone_manager.absolute_path(control_path)],
job=self.job, verbose=False)
- if not self.job.is_server_job():
- params.append('-c')
-
if self.job.is_image_update_job():
params += ['--image', self.job.update_image_path]
@@ -2045,6 +2036,7 @@
self._set_ids(queue_entries=queue_entries)
+ # TODO: Refactor into autoserv_utils. crbug.com/243090
def _generate_command(self, results_dir):
host_list = ','.join(queue_entry.host.hostname
for queue_entry in self.queue_entries)
@@ -2232,6 +2224,7 @@
return drone_manager.ARCHIVER_PID_FILE
+ # TODO: Refactor into autoserv_utils. crbug.com/243090
def _generate_command(self, results_dir):
return [_autoserv_path , '-p',
'--pidfile-label=%s' % self._pidfile_label(), '-r', results_dir,
diff --git a/server/autoserv_utils.py b/server/autoserv_utils.py
new file mode 100644
index 0000000..7d30dc3
--- /dev/null
+++ b/server/autoserv_utils.py
@@ -0,0 +1,75 @@
+#!/usr/bin/python
+# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os
+
+import common
+from autotest_lib.client.common_lib import control_data
+
+
+def autoserv_run_job_command(autoserv_directory, machines,
+ results_directory=None, extra_args=[], job=None,
+ queue_entry=None, verbose=True,
+ write_pidfile=True):
+ """
+ Construct an autoserv command from a job or host queue entry.
+
+ @param autoserv_directory: Absolute path to directory containing the
+ autoserv executable.
+ @param machines: A machine or comma separated list of machines to run
+ job on. Leave as None or empty string for hostless job
+ (String).
+ @param results_directory: Absolute path to directory in which to deposit
+ results.
+ @param extra_args: Additional arguments to pass to autoserv
+ (List of Strings).
+ @param job: Job object. If supplied, -u owner, -l name, and --test-retry,
+ and -c or -s (client or server) parameters will be added.
+ @param queue_entry: HostQueueEntry object. If supplied and no job
+ was supplied, this will be used to lookup the job.
+ @param verbose: Boolean (default: True) for autoserv verbosity.
+ @param write_pidfile: Boolean (default: True) for whether autoserv should
+ write a pidfile.
+ @returns The autoserv command line as a list of executable + parameters.
+ """
+ command = [os.path.join(autoserv_directory, 'autoserv')]
+
+ if write_pidfile:
+ command.append('-p')
+
+ if results_directory:
+ command += ['-r', results_directory]
+
+ if machines:
+ command += ['-m', machines]
+
+ if job or queue_entry:
+ if not job:
+ job = queue_entry.job
+
+ owner = getattr(job, 'owner', None)
+ name = getattr(job, 'name', None)
+ test_retry = getattr(job, 'test_retry', None)
+ control_type = getattr(job, 'control_type', None)
+
+
+ if owner:
+ command += ['-u', owner]
+ if name:
+ command += ['-l', name]
+ if test_retry:
+ command += ['--test-retry='+str(test_retry)]
+ if control_type is not None: # still want to enter if control_type==0
+ control_type_value = control_data.CONTROL_TYPE.get_value(
+ control_type)
+ if control_type_value == control_data.CONTROL_TYPE.CLIENT:
+ command.append('-c')
+ elif control_type_value == control_data.CONTROL_TYPE.SERVER:
+ command.append('-s')
+
+ if verbose:
+ command.append('--verbose')
+
+ return command + extra_args