Gwendal Grignou | 4cf71ad | 2015-05-13 14:21:29 -0700 | [diff] [blame^] | 1 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Sequence extensions to server_job. |
| 6 | Adds ability to schedule jobs on given machines. |
| 7 | """ |
| 8 | |
| 9 | import common |
| 10 | from autotest_lib.client.common_lib import control_data |
| 11 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
| 12 | from autotest_lib.site_utils import job_directories |
| 13 | |
| 14 | MINUTE_IN_SECS = 60 |
| 15 | HOUR_IN_MINUTES = 60 |
| 16 | HOUR_IN_SECS = HOUR_IN_MINUTES * MINUTE_IN_SECS |
| 17 | DAY_IN_HOURS = 24 |
| 18 | DAY_IN_SECS = DAY_IN_HOURS*HOUR_IN_SECS |
| 19 | |
| 20 | DEFAULT_JOB_TIMEOUT_IN_MINS = 4 * HOUR_IN_MINUTES |
| 21 | |
| 22 | class SequenceJob(object): |
| 23 | """Define part of a sequence that will be scheduled by the sequence test.""" |
| 24 | |
| 25 | CONTROL_FILE = """ |
| 26 | def run(machine): |
| 27 | job.run_test('%s', client_ip=machine, %s) |
| 28 | |
| 29 | parallel_simple(run, machines) |
| 30 | """ |
| 31 | |
| 32 | def __init__(self, name, args, iteration=1, duration=None): |
| 33 | """ |
| 34 | Constructor |
| 35 | |
| 36 | @param name: name of the sever test to run. |
| 37 | @param args: arguments needed by the server test. |
| 38 | @param iteration: number of copy of this test to sechudle |
| 39 | @param duration: expected duration of the test (in seconds). |
| 40 | """ |
| 41 | self._name = name |
| 42 | self._args = args or {} |
| 43 | self._iteration = iteration |
| 44 | self._duration = duration |
| 45 | |
| 46 | |
| 47 | def child_job_name(self, machine, iteration_number): |
| 48 | """ |
| 49 | Return a name for a child job. |
| 50 | |
| 51 | @param machine: machine name on which the test will run. |
| 52 | @param iteration_number: number with 0 and self._iteration - 1. |
| 53 | |
| 54 | @returns a unique name based on the machine, the name and the iteration. |
| 55 | """ |
| 56 | name_parts = [machine, self._name] |
| 57 | tag = self._args.get('tag') |
| 58 | if tag: |
| 59 | name_parts.append(tag) |
| 60 | if self._iteration > 1: |
| 61 | name_parts.append(str(iteration_number)) |
| 62 | return '_'.join(name_parts) |
| 63 | |
| 64 | |
| 65 | def child_job_timeout(self): |
| 66 | """ |
| 67 | Get the child job timeout in minutes. |
| 68 | |
| 69 | @param args: arguments sent to the test. |
| 70 | |
| 71 | @returns a timeout value for the test, 4h by default. |
| 72 | """ |
| 73 | if self._duration: |
| 74 | return 2 * int(self._duration) / MINUTE_IN_SECS |
| 75 | # default value: |
| 76 | return DEFAULT_JOB_TIMEOUT_IN_MINS |
| 77 | |
| 78 | |
| 79 | def child_control_file(self): |
| 80 | """ |
| 81 | Populate the template control file. |
| 82 | |
| 83 | Populate it with the test name and expand the arguments |
| 84 | list. |
| 85 | |
| 86 | @param test: name of the test to run |
| 87 | @param args: dictionary of argument for this test. |
| 88 | |
| 89 | @returns a fully built control file to be use for the child job. |
| 90 | """ |
| 91 | child_args = [] |
| 92 | for arg, value in self._args.iteritems(): |
| 93 | child_args.append('%s=%s' % (arg, repr(value))) |
| 94 | if self._duration: |
| 95 | child_args.append('duration=%d' % self._duration) |
| 96 | return self.CONTROL_FILE % (self._name, ', '.join(child_args)) |
| 97 | |
| 98 | |
| 99 | def schedule(self, job, timeout_mins, machine): |
| 100 | """ |
| 101 | Sequence a job on the running AFE. |
| 102 | |
| 103 | Will schedule a given test on the job machine(s). |
| 104 | Support a subset of tests: |
| 105 | - server job |
| 106 | - no hostless. |
| 107 | - no cleanup around tests. |
| 108 | |
| 109 | @param job: server_job object that will server as parent. |
| 110 | @param timeout_mins: timeout to set up: if the test last more than |
| 111 | timeout_mins, the test will fail. |
| 112 | @param machine: machine to run the test on. |
| 113 | |
| 114 | @returns a maximal time in minutes that the sequence can take. |
| 115 | """ |
| 116 | afe = frontend_wrappers.RetryingAFE(timeout_min=30, delay_sec=10, |
| 117 | user=job.user, debug=False) |
| 118 | current_job_id = job_directories.get_job_id_or_task_id(job.resultdir) |
| 119 | runtime_mins = self.child_job_timeout() |
| 120 | |
| 121 | for i in xrange(0, self._iteration): |
| 122 | afe.create_job( |
| 123 | self.child_control_file(), |
| 124 | name=self.child_job_name(machine, i), |
| 125 | priority='Medium', |
| 126 | control_type=control_data.CONTROL_TYPE.SERVER, |
| 127 | hosts=[machine], meta_hosts=(), one_time_hosts=(), |
| 128 | atomic_group_name=None, synch_count=None, is_template=False, |
| 129 | timeout_mins=timeout_mins + (i + 1) * runtime_mins, |
| 130 | max_runtime_mins=runtime_mins, |
| 131 | run_verify=False, email_list='', dependencies=(), |
| 132 | reboot_before=None, reboot_after=None, |
| 133 | parse_failed_repair=None, |
| 134 | hostless=False, keyvals=None, |
| 135 | drone_set=None, image=None, |
| 136 | parent_job_id=current_job_id, test_retry=0, run_reset=False, |
| 137 | require_ssp=None) |
| 138 | return runtime_mins * self._iteration |
| 139 | |
| 140 | |
| 141 | def sequence_schedule(job, machines, server_tests): |
| 142 | """ |
| 143 | Schedule the tests to run |
| 144 | |
| 145 | Launch all the tests in the sequence on all machines. |
| 146 | Returns as soon as the jobs are launched. |
| 147 | |
| 148 | @param job: Job running. |
| 149 | @param machines: machine to run on. |
| 150 | @param server_tests: Array of sequence_test objects. |
| 151 | """ |
| 152 | for machine in machines: |
| 153 | timeout_mins = 0 |
| 154 | for test in server_tests: |
| 155 | timeout_mins += test.schedule(job, timeout_mins, machine) |