Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 5 | import __builtin__ |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 6 | import Queue |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 7 | import base64 |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 8 | import datetime |
| 9 | import logging |
| 10 | import os |
| 11 | import shutil |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 12 | import signal |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 13 | import sys |
| 14 | import tempfile |
| 15 | import time |
| 16 | import unittest |
| 17 | |
| 18 | import mox |
| 19 | |
| 20 | import common |
Michael Tang | 328073b | 2016-11-01 15:33:38 -0700 | [diff] [blame] | 21 | from autotest_lib.client.common_lib import global_config, site_utils |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 22 | from autotest_lib.client.common_lib import time_utils |
| 23 | from autotest_lib.client.common_lib import utils |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 24 | from autotest_lib.scheduler import email_manager |
Prathmesh Prabhu | beb9e01 | 2017-01-30 16:18:39 -0800 | [diff] [blame^] | 25 | from autotest_lib.site_utils import gs_offloader |
| 26 | from autotest_lib.site_utils import job_directories |
Ningning Xia | 2d981ee | 2016-07-06 17:59:54 -0700 | [diff] [blame] | 27 | from autotest_lib.tko import models |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 28 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 29 | # Test value to use for `days_old`, if nothing else is required. |
| 30 | _TEST_EXPIRATION_AGE = 7 |
| 31 | |
| 32 | # When constructing sample time values for testing expiration, |
| 33 | # allow this many seconds between the expiration time and the |
| 34 | # current time. |
| 35 | _MARGIN_SECS = 10.0 |
| 36 | |
| 37 | |
| 38 | def _get_options(argv): |
| 39 | """Helper function to exercise command line parsing. |
| 40 | |
| 41 | @param argv Value of sys.argv to be parsed. |
| 42 | |
| 43 | """ |
| 44 | sys.argv = ['bogus.py'] + argv |
| 45 | return gs_offloader.parse_options() |
| 46 | |
| 47 | |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 48 | class OffloaderOptionsTests(mox.MoxTestBase): |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 49 | """Tests for the `Offloader` constructor. |
| 50 | |
| 51 | Tests that offloader instance fields are set as expected |
| 52 | for given command line options. |
| 53 | |
| 54 | """ |
| 55 | |
| 56 | _REGULAR_ONLY = set([job_directories.RegularJobDirectory]) |
| 57 | _SPECIAL_ONLY = set([job_directories.SpecialJobDirectory]) |
| 58 | _BOTH = _REGULAR_ONLY | _SPECIAL_ONLY |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 59 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 60 | |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 61 | def setUp(self): |
| 62 | super(OffloaderOptionsTests, self).setUp() |
| 63 | self.mox.StubOutWithMock(utils, 'get_offload_gsuri') |
Simran Basi | f3e305f | 2014-10-03 14:43:53 -0700 | [diff] [blame] | 64 | gs_offloader.GS_OFFLOADING_ENABLED = True |
Michael Tang | 0df2eb4 | 2016-05-13 19:06:54 -0700 | [diff] [blame] | 65 | gs_offloader.GS_OFFLOADER_MULTIPROCESSING = False |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 66 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 67 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 68 | def _mock_get_offload_func(self, is_moblab, multiprocessing=False, |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 69 | pubsub_topic=None, delete_age=0): |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 70 | """Mock the process of getting the offload_dir function.""" |
| 71 | if is_moblab: |
| 72 | expected_gsuri = '%sresults/%s/%s/' % ( |
| 73 | global_config.global_config.get_config_value( |
| 74 | 'CROS', 'image_storage_server'), |
| 75 | 'Fa:ke:ma:c0:12:34', 'rand0m-uu1d') |
| 76 | else: |
| 77 | expected_gsuri = utils.DEFAULT_OFFLOAD_GSURI |
| 78 | utils.get_offload_gsuri().AndReturn(expected_gsuri) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 79 | offload_func = gs_offloader.get_offload_dir_func(expected_gsuri, |
| 80 | multiprocessing, delete_age, pubsub_topic) |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 81 | self.mox.StubOutWithMock(gs_offloader, 'get_offload_dir_func') |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 82 | gs_offloader.get_offload_dir_func(expected_gsuri, multiprocessing, |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 83 | delete_age, pubsub_topic).AndReturn(offload_func) |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 84 | self.mox.ReplayAll() |
| 85 | return offload_func |
| 86 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 87 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 88 | def test_process_no_options(self): |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 89 | """Test default offloader options.""" |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 90 | offload_func = self._mock_get_offload_func(False) |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 91 | offloader = gs_offloader.Offloader(_get_options([])) |
| 92 | self.assertEqual(set(offloader._jobdir_classes), |
| 93 | self._REGULAR_ONLY) |
| 94 | self.assertEqual(offloader._processes, 1) |
| 95 | self.assertEqual(offloader._offload_func, |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 96 | offload_func) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 97 | self.assertEqual(offloader._upload_age_limit, 0) |
| 98 | self.assertEqual(offloader._delete_age_limit, 0) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 99 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 100 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 101 | def test_process_all_option(self): |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 102 | """Test offloader handling for the --all option.""" |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 103 | offload_func = self._mock_get_offload_func(False) |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 104 | offloader = gs_offloader.Offloader(_get_options(['--all'])) |
| 105 | self.assertEqual(set(offloader._jobdir_classes), self._BOTH) |
| 106 | self.assertEqual(offloader._processes, 1) |
| 107 | self.assertEqual(offloader._offload_func, |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 108 | offload_func) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 109 | self.assertEqual(offloader._upload_age_limit, 0) |
| 110 | self.assertEqual(offloader._delete_age_limit, 0) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 111 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 112 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 113 | def test_process_hosts_option(self): |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 114 | """Test offloader handling for the --hosts option.""" |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 115 | offload_func = self._mock_get_offload_func(False) |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 116 | offloader = gs_offloader.Offloader( |
| 117 | _get_options(['--hosts'])) |
| 118 | self.assertEqual(set(offloader._jobdir_classes), |
| 119 | self._SPECIAL_ONLY) |
| 120 | self.assertEqual(offloader._processes, 1) |
| 121 | self.assertEqual(offloader._offload_func, |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 122 | offload_func) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 123 | self.assertEqual(offloader._upload_age_limit, 0) |
| 124 | self.assertEqual(offloader._delete_age_limit, 0) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 125 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 126 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 127 | def test_parallelism_option(self): |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 128 | """Test offloader handling for the --parallelism option.""" |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 129 | offload_func = self._mock_get_offload_func(False) |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 130 | offloader = gs_offloader.Offloader( |
| 131 | _get_options(['--parallelism', '2'])) |
| 132 | self.assertEqual(set(offloader._jobdir_classes), |
| 133 | self._REGULAR_ONLY) |
| 134 | self.assertEqual(offloader._processes, 2) |
| 135 | self.assertEqual(offloader._offload_func, |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 136 | offload_func) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 137 | self.assertEqual(offloader._upload_age_limit, 0) |
| 138 | self.assertEqual(offloader._delete_age_limit, 0) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 139 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 140 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 141 | def test_delete_only_option(self): |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 142 | """Test offloader handling for the --delete_only option.""" |
| 143 | offloader = gs_offloader.Offloader( |
| 144 | _get_options(['--delete_only'])) |
| 145 | self.assertEqual(set(offloader._jobdir_classes), |
| 146 | self._REGULAR_ONLY) |
| 147 | self.assertEqual(offloader._processes, 1) |
| 148 | self.assertEqual(offloader._offload_func, |
| 149 | gs_offloader.delete_files) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 150 | self.assertEqual(offloader._upload_age_limit, 0) |
| 151 | self.assertEqual(offloader._delete_age_limit, 0) |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 152 | self.assertIsNone(offloader._pubsub_topic) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 153 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 154 | |
Simran Basi | df4751e | 2014-10-10 14:19:22 -0700 | [diff] [blame] | 155 | def test_days_old_option(self): |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 156 | """Test offloader handling for the --days_old option.""" |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 157 | offload_func = self._mock_get_offload_func(False, delete_age=7) |
J. Richard Barnette | ef4b47d | 2014-05-19 07:52:08 -0700 | [diff] [blame] | 158 | offloader = gs_offloader.Offloader( |
| 159 | _get_options(['--days_old', '7'])) |
| 160 | self.assertEqual(set(offloader._jobdir_classes), |
| 161 | self._REGULAR_ONLY) |
| 162 | self.assertEqual(offloader._processes, 1) |
| 163 | self.assertEqual(offloader._offload_func, |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 164 | offload_func) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 165 | self.assertEqual(offloader._upload_age_limit, 7) |
| 166 | self.assertEqual(offloader._delete_age_limit, 7) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 167 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 168 | |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 169 | def test_moblab_gsuri_generation(self): |
| 170 | """Test offloader construction for Moblab.""" |
| 171 | offload_func = self._mock_get_offload_func(True) |
| 172 | offloader = gs_offloader.Offloader(_get_options([])) |
| 173 | self.assertEqual(set(offloader._jobdir_classes), |
| 174 | self._REGULAR_ONLY) |
| 175 | self.assertEqual(offloader._processes, 1) |
| 176 | self.assertEqual(offloader._offload_func, |
| 177 | offload_func) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 178 | self.assertEqual(offloader._upload_age_limit, 0) |
| 179 | self.assertEqual(offloader._delete_age_limit, 0) |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 180 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 181 | |
Simran Basi | f3e305f | 2014-10-03 14:43:53 -0700 | [diff] [blame] | 182 | def test_globalconfig_offloading_flag(self): |
| 183 | """Test enabling of --delete_only via global_config.""" |
| 184 | gs_offloader.GS_OFFLOADING_ENABLED = False |
| 185 | offloader = gs_offloader.Offloader( |
| 186 | _get_options([])) |
| 187 | self.assertEqual(offloader._offload_func, |
| 188 | gs_offloader.delete_files) |
| 189 | |
Michael Tang | 0df2eb4 | 2016-05-13 19:06:54 -0700 | [diff] [blame] | 190 | def test_offloader_multiprocessing_flag_set(self): |
| 191 | """Test multiprocessing is set.""" |
| 192 | offload_func = self._mock_get_offload_func(True, True) |
| 193 | offloader = gs_offloader.Offloader(_get_options(['-m'])) |
| 194 | self.assertEqual(offloader._offload_func, |
| 195 | offload_func) |
| 196 | self.mox.VerifyAll() |
| 197 | |
| 198 | def test_offloader_multiprocessing_flag_not_set_default_false(self): |
| 199 | """Test multiprocessing is set.""" |
| 200 | gs_offloader.GS_OFFLOADER_MULTIPROCESSING = False |
| 201 | offload_func = self._mock_get_offload_func(True, False) |
| 202 | offloader = gs_offloader.Offloader(_get_options([])) |
| 203 | self.assertEqual(offloader._offload_func, |
| 204 | offload_func) |
| 205 | self.mox.VerifyAll() |
| 206 | |
| 207 | def test_offloader_multiprocessing_flag_not_set_default_true(self): |
| 208 | """Test multiprocessing is set.""" |
| 209 | gs_offloader.GS_OFFLOADER_MULTIPROCESSING = True |
| 210 | offload_func = self._mock_get_offload_func(True, True) |
| 211 | offloader = gs_offloader.Offloader(_get_options([])) |
| 212 | self.assertEqual(offloader._offload_func, |
| 213 | offload_func) |
| 214 | self.mox.VerifyAll() |
| 215 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 216 | def test_offloader_pubsub_topic_not_set(self): |
| 217 | """Test multiprocessing is set.""" |
| 218 | offload_func = self._mock_get_offload_func(True, False) |
| 219 | offloader = gs_offloader.Offloader(_get_options([])) |
| 220 | self.assertEqual(offloader._offload_func, |
| 221 | offload_func) |
| 222 | self.mox.VerifyAll() |
| 223 | |
| 224 | def test_offloader_pubsub_topic_set(self): |
| 225 | """Test multiprocessing is set.""" |
| 226 | offload_func = self._mock_get_offload_func(True, False, 'test-topic') |
| 227 | offloader = gs_offloader.Offloader(_get_options(['-t', 'test-topic'])) |
| 228 | self.assertEqual(offloader._offload_func, |
| 229 | offload_func) |
| 230 | self.mox.VerifyAll() |
| 231 | |
Simran Basi | f3e305f | 2014-10-03 14:43:53 -0700 | [diff] [blame] | 232 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 233 | def _make_timestamp(age_limit, is_expired): |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 234 | """Create a timestamp for use by `job_directories.is_job_expired()`. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 235 | |
| 236 | The timestamp will meet the syntactic requirements for |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 237 | timestamps used as input to `is_job_expired()`. If |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 238 | `is_expired` is true, the timestamp will be older than |
| 239 | `age_limit` days before the current time; otherwise, the |
| 240 | date will be younger. |
| 241 | |
| 242 | @param age_limit The number of days before expiration of the |
| 243 | target timestamp. |
| 244 | @param is_expired Whether the timestamp should be expired |
| 245 | relative to `age_limit`. |
| 246 | |
| 247 | """ |
| 248 | seconds = -_MARGIN_SECS |
| 249 | if is_expired: |
| 250 | seconds = -seconds |
| 251 | delta = datetime.timedelta(days=age_limit, seconds=seconds) |
| 252 | reference_time = datetime.datetime.now() - delta |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 253 | return reference_time.strftime(time_utils.TIME_FMT) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 254 | |
| 255 | |
| 256 | class JobExpirationTests(unittest.TestCase): |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 257 | """Tests to exercise `job_directories.is_job_expired()`.""" |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 258 | |
| 259 | def test_expired(self): |
| 260 | """Test detection of an expired job.""" |
| 261 | timestamp = _make_timestamp(_TEST_EXPIRATION_AGE, True) |
| 262 | self.assertTrue( |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 263 | job_directories.is_job_expired( |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 264 | _TEST_EXPIRATION_AGE, timestamp)) |
| 265 | |
| 266 | |
| 267 | def test_alive(self): |
| 268 | """Test detection of a job that's not expired.""" |
| 269 | # N.B. This test may fail if its run time exceeds more than |
| 270 | # about _MARGIN_SECS seconds. |
| 271 | timestamp = _make_timestamp(_TEST_EXPIRATION_AGE, False) |
| 272 | self.assertFalse( |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 273 | job_directories.is_job_expired( |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 274 | _TEST_EXPIRATION_AGE, timestamp)) |
| 275 | |
| 276 | |
| 277 | class _MockJobDirectory(job_directories._JobDirectory): |
| 278 | """Subclass of `_JobDirectory` used as a helper for tests.""" |
| 279 | |
| 280 | GLOB_PATTERN = '[0-9]*-*' |
| 281 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 282 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 283 | def __init__(self, resultsdir): |
| 284 | """Create new job in initial state.""" |
| 285 | super(_MockJobDirectory, self).__init__(resultsdir) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 286 | self._timestamp = None |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 287 | self.queue_args = [resultsdir, os.path.dirname(resultsdir), self._timestamp] |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 288 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 289 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 290 | def get_timestamp_if_finished(self): |
| 291 | return self._timestamp |
| 292 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 293 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 294 | def set_finished(self, days_old): |
| 295 | """Make this job appear to be finished. |
| 296 | |
| 297 | After calling this function, calls to `enqueue_offload()` |
| 298 | will find this job as finished, but not expired and ready |
| 299 | for offload. Note that when `days_old` is 0, |
| 300 | `enqueue_offload()` will treat a finished job as eligible |
| 301 | for offload. |
| 302 | |
| 303 | @param days_old The value of the `days_old` parameter that |
| 304 | will be passed to `enqueue_offload()` for |
| 305 | testing. |
| 306 | |
| 307 | """ |
| 308 | self._timestamp = _make_timestamp(days_old, False) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 309 | self.queue_args[2] = self._timestamp |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 310 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 311 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 312 | def set_expired(self, days_old): |
| 313 | """Make this job eligible to be offloaded. |
| 314 | |
| 315 | After calling this function, calls to `offload` will attempt |
| 316 | to offload this job. |
| 317 | |
| 318 | @param days_old The value of the `days_old` parameter that |
| 319 | will be passed to `enqueue_offload()` for |
| 320 | testing. |
| 321 | |
| 322 | """ |
| 323 | self._timestamp = _make_timestamp(days_old, True) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 324 | self.queue_args[2] = self._timestamp |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 325 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 326 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 327 | def set_incomplete(self): |
| 328 | """Make this job appear to have failed offload just once.""" |
| 329 | self._offload_count += 1 |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 330 | self._first_offload_start = time.time() |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 331 | if not os.path.isdir(self._dirname): |
| 332 | os.mkdir(self._dirname) |
| 333 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 334 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 335 | def set_reportable(self): |
| 336 | """Make this job be reportable.""" |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 337 | self.set_incomplete() |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 338 | self._offload_count += 1 |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 339 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 340 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 341 | def set_complete(self): |
| 342 | """Make this job be completed.""" |
| 343 | self._offload_count += 1 |
| 344 | if os.path.isdir(self._dirname): |
| 345 | os.rmdir(self._dirname) |
| 346 | |
| 347 | |
Simran Basi | 1e10e92 | 2015-04-16 15:09:56 -0700 | [diff] [blame] | 348 | def process_gs_instructions(self): |
| 349 | """Always still offload the job directory.""" |
| 350 | return True |
| 351 | |
| 352 | |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 353 | class CommandListTests(unittest.TestCase): |
| 354 | """Tests for `get_cmd_list()`.""" |
| 355 | |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 356 | def _command_list_assertions(self, job, use_rsync=True, multi=False): |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 357 | """Call `get_cmd_list()` and check the return value. |
| 358 | |
| 359 | Check the following assertions: |
| 360 | * The command name (argv[0]) is 'gsutil'. |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 361 | * '-m' option (argv[1]) is on when the argument, multi, is True. |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 362 | * The arguments contain the 'cp' subcommand. |
| 363 | * The next-to-last argument (the source directory) is the |
| 364 | job's `queue_args[0]`. |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 365 | * The last argument (the destination URL) is the job's |
| 366 | 'queue_args[1]'. |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 367 | |
| 368 | @param job A job with properly calculated arguments to |
| 369 | `get_cmd_list()` |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 370 | @param use_rsync True when using 'rsync'. False when using 'cp'. |
| 371 | @param multi True when using '-m' option for gsutil. |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 372 | |
| 373 | """ |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 374 | test_bucket_uri = 'gs://a-test-bucket' |
| 375 | |
| 376 | gs_offloader.USE_RSYNC_ENABLED = use_rsync |
| 377 | |
| 378 | command = gs_offloader.get_cmd_list( |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 379 | multi, job.queue_args[0], |
| 380 | os.path.join(test_bucket_uri, job.queue_args[1])) |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 381 | |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 382 | self.assertEqual(command[0], 'gsutil') |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 383 | if multi: |
| 384 | self.assertEqual(command[1], '-m') |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 385 | self.assertEqual(command[-2], job.queue_args[0]) |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 386 | |
| 387 | if use_rsync: |
| 388 | self.assertTrue('rsync' in command) |
| 389 | self.assertEqual(command[-1], |
| 390 | os.path.join(test_bucket_uri, job.queue_args[0])) |
| 391 | else: |
| 392 | self.assertTrue('cp' in command) |
| 393 | self.assertEqual(command[-1], |
| 394 | os.path.join(test_bucket_uri, job.queue_args[1])) |
| 395 | |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 396 | |
| 397 | def test_get_cmd_list_regular(self): |
| 398 | """Test `get_cmd_list()` as for a regular job.""" |
| 399 | job = _MockJobDirectory('118-debug') |
| 400 | self._command_list_assertions(job) |
| 401 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 402 | |
J. Richard Barnette | 9f4be0d | 2014-05-20 12:28:31 -0700 | [diff] [blame] | 403 | def test_get_cmd_list_special(self): |
| 404 | """Test `get_cmd_list()` as for a special job.""" |
| 405 | job = _MockJobDirectory('hosts/host1/118-reset') |
| 406 | self._command_list_assertions(job) |
| 407 | |
| 408 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 409 | def test_get_cmd_list_regular_no_rsync(self): |
| 410 | """Test `get_cmd_list()` as for a regular job.""" |
| 411 | job = _MockJobDirectory('118-debug') |
| 412 | self._command_list_assertions(job, use_rsync=False) |
| 413 | |
| 414 | |
| 415 | def test_get_cmd_list_special_no_rsync(self): |
| 416 | """Test `get_cmd_list()` as for a special job.""" |
| 417 | job = _MockJobDirectory('hosts/host1/118-reset') |
| 418 | self._command_list_assertions(job, use_rsync=False) |
| 419 | |
| 420 | |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 421 | def test_get_cmd_list_regular_multi(self): |
| 422 | """Test `get_cmd_list()` as for a regular job with True multi.""" |
| 423 | job = _MockJobDirectory('118-debug') |
| 424 | self._command_list_assertions(job, multi=True) |
| 425 | |
| 426 | |
| 427 | def test_get_cmd_list_special_multi(self): |
| 428 | """Test `get_cmd_list()` as for a special job with True multi.""" |
| 429 | job = _MockJobDirectory('hosts/host1/118-reset') |
| 430 | self._command_list_assertions(job, multi=True) |
| 431 | |
| 432 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 433 | # Below is partial sample of e-mail notification text. This text is |
| 434 | # deliberately hard-coded and then parsed to create the test data; |
| 435 | # the idea is to make sure the actual text format will be reviewed |
| 436 | # by a human being. |
| 437 | # |
| 438 | # first offload count directory |
| 439 | # --+----1----+---- ----+ ----+----1----+----2----+----3 |
| 440 | _SAMPLE_DIRECTORIES_REPORT = '''\ |
| 441 | =================== ====== ============================== |
| 442 | 2014-03-14 15:09:26 1 118-fubar |
| 443 | 2014-03-14 15:19:23 2 117-fubar |
| 444 | 2014-03-14 15:29:20 6 116-fubar |
| 445 | 2014-03-14 15:39:17 24 115-fubar |
| 446 | 2014-03-14 15:49:14 120 114-fubar |
| 447 | 2014-03-14 15:59:11 720 113-fubar |
| 448 | 2014-03-14 16:09:08 5040 112-fubar |
| 449 | 2014-03-14 16:19:05 40320 111-fubar |
| 450 | ''' |
| 451 | |
| 452 | |
| 453 | class EmailTemplateTests(mox.MoxTestBase): |
| 454 | """Test the formatting of e-mail notifications.""" |
| 455 | |
| 456 | def setUp(self): |
| 457 | super(EmailTemplateTests, self).setUp() |
| 458 | self.mox.StubOutWithMock(email_manager.manager, |
| 459 | 'send_email') |
| 460 | self._joblist = [] |
| 461 | for line in _SAMPLE_DIRECTORIES_REPORT.split('\n')[1 : -1]: |
| 462 | date_, time_, count, dir_ = line.split() |
| 463 | job = _MockJobDirectory(dir_) |
| 464 | job._offload_count = int(count) |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 465 | timestruct = time.strptime("%s %s" % (date_, time_), |
| 466 | gs_offloader.ERROR_EMAIL_TIME_FORMAT) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 467 | job._first_offload_start = time.mktime(timestruct) |
| 468 | # enter the jobs in reverse order, to make sure we |
| 469 | # test that the output will be sorted. |
| 470 | self._joblist.insert(0, job) |
| 471 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 472 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 473 | def test_email_template(self): |
| 474 | """Trigger an e-mail report and check its contents.""" |
| 475 | # The last line of the report is a separator that we |
| 476 | # repeat in the first line of our expected result data. |
| 477 | # So, we remove that separator from the end of the of |
| 478 | # the e-mail report message. |
| 479 | # |
| 480 | # The last element in the list returned by split('\n') |
| 481 | # will be an empty string, so to remove the separator, |
| 482 | # we remove the next-to-last entry in the list. |
| 483 | report_lines = gs_offloader.ERROR_EMAIL_REPORT_FORMAT.split('\n') |
| 484 | expected_message = ('\n'.join(report_lines[: -2] + |
| 485 | report_lines[-1 :]) + |
| 486 | _SAMPLE_DIRECTORIES_REPORT) |
| 487 | email_manager.manager.send_email( |
| 488 | mox.IgnoreArg(), mox.IgnoreArg(), expected_message) |
| 489 | self.mox.ReplayAll() |
| 490 | gs_offloader.report_offload_failures(self._joblist) |
| 491 | |
| 492 | |
Kevin Cheng | 686ae8c | 2015-09-09 11:56:38 -0700 | [diff] [blame] | 493 | def test_email_url(self): |
| 494 | """Check that the expected helper url is in the email header.""" |
| 495 | self.assertIn(gs_offloader.ERROR_EMAIL_HELPER_URL, |
| 496 | gs_offloader.ERROR_EMAIL_REPORT_FORMAT) |
| 497 | |
| 498 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 499 | class PubSubTest(mox.MoxTestBase): |
| 500 | """Test the test result notifcation data structure.""" |
| 501 | |
| 502 | def test_create_test_result_notification(self): |
| 503 | """Tests the test result notification message.""" |
Michael Tang | 328073b | 2016-11-01 15:33:38 -0700 | [diff] [blame] | 504 | self.mox.StubOutWithMock(site_utils, 'get_moblab_id') |
| 505 | self.mox.StubOutWithMock(site_utils, |
| 506 | 'get_default_interface_mac_address') |
| 507 | site_utils.get_default_interface_mac_address().AndReturn( |
| 508 | '1c:dc:d1:11:01:e1') |
| 509 | site_utils.get_moblab_id().AndReturn( |
| 510 | 'c8386d92-9ad1-11e6-80f5-111111111111') |
| 511 | self.mox.ReplayAll() |
Michael Tang | b45c723 | 2016-11-14 21:40:43 -0800 | [diff] [blame] | 512 | msg = gs_offloader._create_test_result_notification( |
| 513 | 'gs://test_bucket', '123-moblab') |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 514 | self.assertEquals(base64.b64encode( |
| 515 | gs_offloader.NEW_TEST_RESULT_MESSAGE), msg['data']) |
Michael Tang | 328073b | 2016-11-01 15:33:38 -0700 | [diff] [blame] | 516 | self.assertEquals( |
| 517 | gs_offloader.NOTIFICATION_VERSION, |
| 518 | msg['attributes'][gs_offloader.NOTIFICATION_ATTR_VERSION]) |
| 519 | self.assertEquals( |
| 520 | '1c:dc:d1:11:01:e1', |
| 521 | msg['attributes'][gs_offloader.NOTIFICATION_ATTR_MOBLAB_MAC]) |
| 522 | self.assertEquals( |
| 523 | 'c8386d92-9ad1-11e6-80f5-111111111111', |
| 524 | msg['attributes'][gs_offloader.NOTIFICATION_ATTR_MOBLAB_ID]) |
| 525 | self.assertEquals( |
Michael Tang | b45c723 | 2016-11-14 21:40:43 -0800 | [diff] [blame] | 526 | 'gs://test_bucket/123-moblab', |
Michael Tang | 328073b | 2016-11-01 15:33:38 -0700 | [diff] [blame] | 527 | msg['attributes'][gs_offloader.NOTIFICATION_ATTR_GCS_URI]) |
| 528 | self.mox.VerifyAll() |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 529 | |
| 530 | |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 531 | class _MockJob(object): |
| 532 | """Class to mock the return value of `AFE.get_jobs()`.""" |
| 533 | def __init__(self, created): |
| 534 | self.created_on = created |
| 535 | |
| 536 | |
| 537 | class _MockHostQueueEntry(object): |
| 538 | """Class to mock the return value of `AFE.get_host_queue_entries()`.""" |
| 539 | def __init__(self, finished): |
| 540 | self.finished_on = finished |
| 541 | |
| 542 | |
| 543 | class _MockSpecialTask(object): |
| 544 | """Class to mock the return value of `AFE.get_special_tasks()`.""" |
| 545 | def __init__(self, finished): |
| 546 | self.time_finished = finished |
| 547 | |
| 548 | |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 549 | class JobDirectorySubclassTests(mox.MoxTestBase): |
| 550 | """Test specific to RegularJobDirectory and SpecialJobDirectory. |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 551 | |
| 552 | This provides coverage for the implementation in both |
| 553 | RegularJobDirectory and SpecialJobDirectory. |
| 554 | |
| 555 | """ |
| 556 | |
| 557 | def setUp(self): |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 558 | super(JobDirectorySubclassTests, self).setUp() |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 559 | self.mox.StubOutWithMock(job_directories._AFE, 'get_jobs') |
| 560 | self.mox.StubOutWithMock(job_directories._AFE, |
| 561 | 'get_host_queue_entries') |
| 562 | self.mox.StubOutWithMock(job_directories._AFE, |
| 563 | 'get_special_tasks') |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 564 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 565 | |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 566 | def test_regular_job_fields(self): |
| 567 | """Test the constructor for `RegularJobDirectory`. |
| 568 | |
| 569 | Construct a regular job, and assert that the `_dirname` |
| 570 | and `_id` attributes are set as expected. |
| 571 | |
| 572 | """ |
| 573 | resultsdir = '118-fubar' |
| 574 | job = job_directories.RegularJobDirectory(resultsdir) |
| 575 | self.assertEqual(job._dirname, resultsdir) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 576 | self.assertEqual(job._id, 118) |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 577 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 578 | |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 579 | def test_special_job_fields(self): |
| 580 | """Test the constructor for `SpecialJobDirectory`. |
| 581 | |
| 582 | Construct a special job, and assert that the `_dirname` |
| 583 | and `_id` attributes are set as expected. |
| 584 | |
| 585 | """ |
| 586 | destdir = 'hosts/host1' |
| 587 | resultsdir = destdir + '/118-reset' |
| 588 | job = job_directories.SpecialJobDirectory(resultsdir) |
| 589 | self.assertEqual(job._dirname, resultsdir) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 590 | self.assertEqual(job._id, 118) |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 591 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 592 | |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 593 | def _check_finished_job(self, jobtime, hqetimes, expected): |
| 594 | """Mock and test behavior of a finished job. |
| 595 | |
| 596 | Initialize the mocks for a call to |
| 597 | `get_timestamp_if_finished()`, then simulate one call. |
| 598 | Assert that the returned timestamp matches the passed |
| 599 | in expected value. |
| 600 | |
| 601 | @param jobtime Time used to construct a _MockJob object. |
| 602 | @param hqetimes List of times used to construct |
| 603 | _MockHostQueueEntry objects. |
| 604 | @param expected Expected time to be returned by |
| 605 | get_timestamp_if_finished |
| 606 | |
| 607 | """ |
| 608 | job = job_directories.RegularJobDirectory('118-fubar') |
| 609 | job_directories._AFE.get_jobs( |
| 610 | id=job._id, finished=True).AndReturn( |
| 611 | [_MockJob(jobtime)]) |
| 612 | job_directories._AFE.get_host_queue_entries( |
| 613 | finished_on__isnull=False, |
| 614 | job_id=job._id).AndReturn( |
| 615 | [_MockHostQueueEntry(t) for t in hqetimes]) |
| 616 | self.mox.ReplayAll() |
| 617 | self.assertEqual(expected, job.get_timestamp_if_finished()) |
| 618 | self.mox.VerifyAll() |
| 619 | |
| 620 | |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 621 | def test_finished_regular_job(self): |
| 622 | """Test getting the timestamp for a finished regular job. |
| 623 | |
| 624 | Tests the return value for |
| 625 | `RegularJobDirectory.get_timestamp_if_finished()` when |
| 626 | the AFE indicates the job is finished. |
| 627 | |
| 628 | """ |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 629 | created_timestamp = _make_timestamp(1, True) |
| 630 | hqe_timestamp = _make_timestamp(0, True) |
| 631 | self._check_finished_job(created_timestamp, |
| 632 | [hqe_timestamp], |
| 633 | hqe_timestamp) |
Simran Basi | fb98e46 | 2014-08-18 12:35:44 -0700 | [diff] [blame] | 634 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 635 | |
Simran Basi | fb98e46 | 2014-08-18 12:35:44 -0700 | [diff] [blame] | 636 | def test_finished_regular_job_multiple_hqes(self): |
| 637 | """Test getting the timestamp for a regular job with multiple hqes. |
| 638 | |
| 639 | Tests the return value for |
| 640 | `RegularJobDirectory.get_timestamp_if_finished()` when |
| 641 | the AFE indicates the job is finished and the job has multiple host |
| 642 | queue entries. |
| 643 | |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 644 | Tests that the returned timestamp is the latest timestamp in |
| 645 | the list of HQEs, regardless of the returned order. |
| 646 | |
Simran Basi | fb98e46 | 2014-08-18 12:35:44 -0700 | [diff] [blame] | 647 | """ |
Simran Basi | fb98e46 | 2014-08-18 12:35:44 -0700 | [diff] [blame] | 648 | created_timestamp = _make_timestamp(2, True) |
| 649 | older_hqe_timestamp = _make_timestamp(1, True) |
| 650 | newer_hqe_timestamp = _make_timestamp(0, True) |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 651 | hqe_list = [older_hqe_timestamp, |
| 652 | newer_hqe_timestamp] |
| 653 | self._check_finished_job(created_timestamp, |
| 654 | hqe_list, |
| 655 | newer_hqe_timestamp) |
| 656 | self.mox.ResetAll() |
| 657 | hqe_list.reverse() |
| 658 | self._check_finished_job(created_timestamp, |
| 659 | hqe_list, |
| 660 | newer_hqe_timestamp) |
Simran Basi | fb98e46 | 2014-08-18 12:35:44 -0700 | [diff] [blame] | 661 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 662 | |
Simran Basi | fb98e46 | 2014-08-18 12:35:44 -0700 | [diff] [blame] | 663 | def test_finished_regular_job_null_finished_times(self): |
| 664 | """Test getting the timestamp for an aborted regular job. |
| 665 | |
| 666 | Tests the return value for |
| 667 | `RegularJobDirectory.get_timestamp_if_finished()` when |
| 668 | the AFE indicates the job is finished and the job has aborted host |
| 669 | queue entries. |
| 670 | |
| 671 | """ |
Simran Basi | fb98e46 | 2014-08-18 12:35:44 -0700 | [diff] [blame] | 672 | timestamp = _make_timestamp(0, True) |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 673 | self._check_finished_job(timestamp, [], timestamp) |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 674 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 675 | |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 676 | def test_unfinished_regular_job(self): |
| 677 | """Test getting the timestamp for an unfinished regular job. |
| 678 | |
| 679 | Tests the return value for |
| 680 | `RegularJobDirectory.get_timestamp_if_finished()` when |
| 681 | the AFE indicates the job is not finished. |
| 682 | |
| 683 | """ |
| 684 | job = job_directories.RegularJobDirectory('118-fubar') |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 685 | job_directories._AFE.get_jobs( |
| 686 | id=job._id, finished=True).AndReturn([]) |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 687 | self.mox.ReplayAll() |
| 688 | self.assertIsNone(job.get_timestamp_if_finished()) |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 689 | self.mox.VerifyAll() |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 690 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 691 | |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 692 | def test_finished_special_job(self): |
| 693 | """Test getting the timestamp for a finished special job. |
| 694 | |
| 695 | Tests the return value for |
| 696 | `SpecialJobDirectory.get_timestamp_if_finished()` when |
| 697 | the AFE indicates the job is finished. |
| 698 | |
| 699 | """ |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 700 | job = job_directories.SpecialJobDirectory( |
| 701 | 'hosts/host1/118-reset') |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 702 | timestamp = _make_timestamp(0, True) |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 703 | job_directories._AFE.get_special_tasks( |
| 704 | id=job._id, is_complete=True).AndReturn( |
| 705 | [_MockSpecialTask(timestamp)]) |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 706 | self.mox.ReplayAll() |
| 707 | self.assertEqual(timestamp, |
| 708 | job.get_timestamp_if_finished()) |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 709 | self.mox.VerifyAll() |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 710 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 711 | |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 712 | def test_unfinished_special_job(self): |
| 713 | """Test getting the timestamp for an unfinished special job. |
| 714 | |
| 715 | Tests the return value for |
| 716 | `SpecialJobDirectory.get_timestamp_if_finished()` when |
| 717 | the AFE indicates the job is not finished. |
| 718 | |
| 719 | """ |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 720 | job = job_directories.SpecialJobDirectory( |
| 721 | 'hosts/host1/118-reset') |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 722 | job_directories._AFE.get_special_tasks( |
| 723 | id=job._id, is_complete=True).AndReturn([]) |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 724 | self.mox.ReplayAll() |
| 725 | self.assertIsNone(job.get_timestamp_if_finished()) |
J. Richard Barnette | dd0227d | 2015-04-10 15:18:48 -0700 | [diff] [blame] | 726 | self.mox.VerifyAll() |
J. Richard Barnette | 3e3ed6a | 2014-05-19 07:59:00 -0700 | [diff] [blame] | 727 | |
| 728 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 729 | class _TempResultsDirTestBase(mox.MoxTestBase): |
| 730 | """Base class for tests using a temporary results directory.""" |
| 731 | |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 732 | REGULAR_JOBLIST = [ |
| 733 | '111-fubar', '112-fubar', '113-fubar', '114-snafu'] |
| 734 | HOST_LIST = ['host1', 'host2', 'host3'] |
| 735 | SPECIAL_JOBLIST = [ |
| 736 | 'hosts/host1/333-reset', 'hosts/host1/334-reset', |
| 737 | 'hosts/host2/444-reset', 'hosts/host3/555-reset'] |
| 738 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 739 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 740 | def setUp(self): |
| 741 | super(_TempResultsDirTestBase, self).setUp() |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 742 | self._resultsroot = tempfile.mkdtemp() |
| 743 | self._cwd = os.getcwd() |
| 744 | os.chdir(self._resultsroot) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 745 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 746 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 747 | def tearDown(self): |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 748 | os.chdir(self._cwd) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 749 | shutil.rmtree(self._resultsroot) |
| 750 | super(_TempResultsDirTestBase, self).tearDown() |
| 751 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 752 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 753 | def make_job(self, jobdir): |
| 754 | """Create a job with results in `self._resultsroot`. |
| 755 | |
| 756 | @param jobdir Name of the subdirectory to be created in |
| 757 | `self._resultsroot`. |
| 758 | |
| 759 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 760 | os.mkdir(jobdir) |
| 761 | return _MockJobDirectory(jobdir) |
| 762 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 763 | |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 764 | def make_job_hierarchy(self): |
| 765 | """Create a sample hierarchy of job directories. |
| 766 | |
| 767 | `self.REGULAR_JOBLIST` is a list of directories for regular |
| 768 | jobs to be created; `self.SPECIAL_JOBLIST` is a list of |
| 769 | directories for special jobs to be created. |
| 770 | |
| 771 | """ |
| 772 | for d in self.REGULAR_JOBLIST: |
| 773 | os.mkdir(d) |
| 774 | hostsdir = 'hosts' |
| 775 | os.mkdir(hostsdir) |
| 776 | for host in self.HOST_LIST: |
| 777 | os.mkdir(os.path.join(hostsdir, host)) |
| 778 | for d in self.SPECIAL_JOBLIST: |
| 779 | os.mkdir(d) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 780 | |
| 781 | |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 782 | class OffloadDirectoryTests(_TempResultsDirTestBase): |
| 783 | """Tests for `offload_dir()`.""" |
| 784 | |
| 785 | def setUp(self): |
| 786 | super(OffloadDirectoryTests, self).setUp() |
| 787 | # offload_dir() logs messages; silence them. |
| 788 | self._saved_loglevel = logging.getLogger().getEffectiveLevel() |
| 789 | logging.getLogger().setLevel(logging.CRITICAL+1) |
| 790 | self._job = self.make_job(self.REGULAR_JOBLIST[0]) |
| 791 | self.mox.StubOutWithMock(gs_offloader, 'get_cmd_list') |
| 792 | self.mox.StubOutWithMock(signal, 'alarm') |
Ningning Xia | 2d981ee | 2016-07-06 17:59:54 -0700 | [diff] [blame] | 793 | self.mox.StubOutWithMock(models.test, 'parse_job_keyval') |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 794 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 795 | |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 796 | def tearDown(self): |
| 797 | logging.getLogger().setLevel(self._saved_loglevel) |
| 798 | super(OffloadDirectoryTests, self).tearDown() |
| 799 | |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 800 | def _mock_upload_testresult_files(self): |
| 801 | self.mox.StubOutWithMock(gs_offloader, 'upload_testresult_files') |
| 802 | gs_offloader.upload_testresult_files( |
| 803 | mox.IgnoreArg(),mox.IgnoreArg()).AndReturn(None) |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 804 | |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 805 | def _mock_create_marker_file(self): |
| 806 | self.mox.StubOutWithMock(__builtin__, 'open') |
| 807 | mock_marker_file = self.mox.CreateMock(file) |
| 808 | open(mox.IgnoreArg(), 'a').AndReturn(mock_marker_file) |
| 809 | mock_marker_file.close() |
| 810 | |
| 811 | |
| 812 | def _mock_offload_dir_calls(self, command, queue_args, |
| 813 | marker_initially_exists=False, |
| 814 | marker_eventually_exists=True): |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 815 | """Mock out the calls needed by `offload_dir()`. |
| 816 | |
| 817 | This covers only the calls made when there is no timeout. |
| 818 | |
| 819 | @param command Command list to be returned by the mocked |
| 820 | call to `get_cmd_list()`. |
| 821 | |
| 822 | """ |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 823 | self.mox.StubOutWithMock(os.path, 'isfile') |
| 824 | os.path.isfile(mox.IgnoreArg()).AndReturn(marker_initially_exists) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 825 | signal.alarm(gs_offloader.OFFLOAD_TIMEOUT_SECS) |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 826 | command.append(queue_args[0]) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 827 | gs_offloader.get_cmd_list( |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 828 | False, queue_args[0], |
| 829 | '%s%s' % (utils.DEFAULT_OFFLOAD_GSURI, |
| 830 | queue_args[1])).AndReturn(command) |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 831 | self._mock_upload_testresult_files() |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 832 | signal.alarm(0) |
| 833 | signal.alarm(0) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 834 | os.path.isfile(mox.IgnoreArg()).AndReturn(marker_eventually_exists) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 835 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 836 | |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 837 | def _run_offload_dir(self, should_succeed, delete_age): |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 838 | """Make one call to `offload_dir()`. |
| 839 | |
| 840 | The caller ensures all mocks are set up already. |
| 841 | |
| 842 | @param should_succeed True iff the call to `offload_dir()` |
| 843 | is expected to succeed and remove the |
| 844 | offloaded job directory. |
| 845 | |
| 846 | """ |
| 847 | self.mox.ReplayAll() |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 848 | gs_offloader.get_offload_dir_func( |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 849 | utils.DEFAULT_OFFLOAD_GSURI, False, delete_age)( |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 850 | self._job.queue_args[0], |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 851 | self._job.queue_args[1], |
| 852 | self._job.queue_args[2]) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 853 | self.mox.VerifyAll() |
| 854 | self.assertEqual(not should_succeed, |
| 855 | os.path.isdir(self._job.queue_args[0])) |
| 856 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 857 | |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 858 | def test_offload_success(self): |
| 859 | """Test that `offload_dir()` can succeed correctly.""" |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 860 | self._mock_offload_dir_calls(['test', '-d'], |
| 861 | self._job.queue_args) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 862 | self._mock_create_marker_file() |
| 863 | self._run_offload_dir(True, 0) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 864 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 865 | |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 866 | def test_offload_failure(self): |
| 867 | """Test that `offload_dir()` can fail correctly.""" |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 868 | self._mock_offload_dir_calls(['test', '!', '-d'], |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 869 | self._job.queue_args, |
| 870 | marker_eventually_exists=False) |
| 871 | self._run_offload_dir(False, 0) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 872 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 873 | |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 874 | def test_offload_timeout_early(self): |
| 875 | """Test that `offload_dir()` times out correctly. |
| 876 | |
| 877 | This test triggers timeout at the earliest possible moment, |
| 878 | at the first call to set the timeout alarm. |
| 879 | |
| 880 | """ |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 881 | self._mock_upload_testresult_files() |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 882 | signal.alarm(gs_offloader.OFFLOAD_TIMEOUT_SECS).AndRaise( |
| 883 | gs_offloader.TimeoutException('fubar')) |
| 884 | signal.alarm(0) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 885 | self._run_offload_dir(False, 0) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 886 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 887 | |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 888 | def test_offload_timeout_late(self): |
| 889 | """Test that `offload_dir()` times out correctly. |
| 890 | |
| 891 | This test triggers timeout at the latest possible moment, at |
| 892 | the call to clear the timeout alarm. |
| 893 | |
| 894 | """ |
| 895 | signal.alarm(gs_offloader.OFFLOAD_TIMEOUT_SECS) |
| 896 | gs_offloader.get_cmd_list( |
MK Ryu | e93c857 | 2015-08-11 11:53:00 -0700 | [diff] [blame] | 897 | False, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 898 | ['test', '-d', self._job.queue_args[0]]) |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 899 | self._mock_upload_testresult_files() |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 900 | signal.alarm(0).AndRaise( |
| 901 | gs_offloader.TimeoutException('fubar')) |
| 902 | signal.alarm(0) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 903 | self._run_offload_dir(False, 0) |
J. Richard Barnette | 2e443ef | 2014-05-20 12:31:35 -0700 | [diff] [blame] | 904 | |
| 905 | |
Dan Shi | affb922 | 2015-04-15 17:05:47 -0700 | [diff] [blame] | 906 | def test_sanitize_dir(self): |
| 907 | """Test that folder/file name with invalid character can be corrected. |
| 908 | """ |
| 909 | results_folder = tempfile.mkdtemp() |
| 910 | invalid_chars = '_'.join(gs_offloader.INVALID_GS_CHARS) |
| 911 | invalid_files = [] |
| 912 | invalid_folder = os.path.join( |
| 913 | results_folder, |
| 914 | 'invalid_name_folder_%s' % invalid_chars) |
| 915 | invalid_files.append(os.path.join( |
| 916 | invalid_folder, |
| 917 | 'invalid_name_file_%s' % invalid_chars)) |
| 918 | for r in gs_offloader.INVALID_GS_CHAR_RANGE: |
| 919 | for c in range(r[0], r[1]+1): |
| 920 | # NULL cannot be in file name. |
| 921 | if c != 0: |
| 922 | invalid_files.append(os.path.join( |
| 923 | invalid_folder, |
| 924 | 'invalid_name_file_%s' % chr(c))) |
| 925 | good_folder = os.path.join(results_folder, 'valid_name_folder') |
| 926 | good_file = os.path.join(good_folder, 'valid_name_file') |
| 927 | for folder in [invalid_folder, good_folder]: |
| 928 | os.makedirs(folder) |
| 929 | for f in invalid_files + [good_file]: |
| 930 | with open(f, 'w'): |
| 931 | pass |
| 932 | gs_offloader.sanitize_dir(results_folder) |
| 933 | for _, dirs, files in os.walk(results_folder): |
| 934 | for name in dirs + files: |
| 935 | self.assertEqual(name, gs_offloader.get_sanitized_name(name)) |
| 936 | for c in name: |
| 937 | self.assertFalse(c in gs_offloader.INVALID_GS_CHARS) |
| 938 | for r in gs_offloader.INVALID_GS_CHAR_RANGE: |
| 939 | self.assertFalse(ord(c) >= r[0] and ord(c) <= r[1]) |
| 940 | self.assertTrue(os.path.exists(good_file)) |
| 941 | shutil.rmtree(results_folder) |
| 942 | |
| 943 | |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 944 | def check_limit_file_count(self, is_test_job=True): |
| 945 | """Test that folder with too many files can be compressed. |
| 946 | |
| 947 | @param is_test_job: True to check the method with test job result |
| 948 | folder. Set to False for special task folder. |
| 949 | """ |
| 950 | results_folder = tempfile.mkdtemp() |
| 951 | host_folder = os.path.join( |
| 952 | results_folder, |
| 953 | 'lab1-host1' if is_test_job else 'hosts/lab1-host1/1-repair') |
| 954 | debug_folder = os.path.join(host_folder, 'debug') |
| 955 | sysinfo_folder = os.path.join(host_folder, 'sysinfo') |
| 956 | for folder in [debug_folder, sysinfo_folder]: |
| 957 | os.makedirs(folder) |
| 958 | for i in range(10): |
| 959 | with open(os.path.join(folder, str(i)), 'w') as f: |
| 960 | f.write('test') |
| 961 | |
| 962 | gs_offloader.MAX_FILE_COUNT = 100 |
| 963 | gs_offloader.limit_file_count( |
| 964 | results_folder if is_test_job else host_folder) |
| 965 | self.assertTrue(os.path.exists(sysinfo_folder)) |
| 966 | |
| 967 | gs_offloader.MAX_FILE_COUNT = 10 |
| 968 | gs_offloader.limit_file_count( |
| 969 | results_folder if is_test_job else host_folder) |
| 970 | self.assertFalse(os.path.exists(sysinfo_folder)) |
| 971 | self.assertTrue(os.path.exists(sysinfo_folder + '.tgz')) |
| 972 | self.assertTrue(os.path.exists(debug_folder)) |
| 973 | |
| 974 | shutil.rmtree(results_folder) |
| 975 | |
| 976 | |
| 977 | def test_limit_file_count(self): |
| 978 | """Test that folder with too many files can be compressed. |
| 979 | """ |
| 980 | self.check_limit_file_count(is_test_job=True) |
| 981 | self.check_limit_file_count(is_test_job=False) |
| 982 | |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 983 | |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 984 | def test_is_valid_result(self): |
| 985 | """Test _is_valid_result.""" |
Ningning Xia | 21922c8 | 2016-07-29 11:03:15 -0700 | [diff] [blame] | 986 | release_build = 'veyron_minnie-cheets-release/R52-8248.0.0' |
| 987 | pfq_build = 'cyan-cheets-android-pfq/R54-8623.0.0-rc1' |
| 988 | trybot_build = 'trybot-samus-release/R54-8640.0.0-b5092' |
| 989 | trybot_2_build = 'trybot-samus-pfq/R54-8640.0.0-b5092' |
| 990 | release_2_build = 'test-trybot-release/R54-8640.0.0-b5092' |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 991 | self.assertTrue(gs_offloader._is_valid_result( |
| 992 | release_build, gs_offloader.CTS_RESULT_PATTERN, 'arc-cts')) |
Rohit Makasana | 6384c10 | 2016-10-21 17:09:47 -0700 | [diff] [blame] | 993 | self.assertTrue(gs_offloader._is_valid_result( |
| 994 | release_build, gs_offloader.CTS_RESULT_PATTERN, 'test_that_wrapper')) |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 995 | self.assertFalse(gs_offloader._is_valid_result( |
| 996 | release_build, gs_offloader.CTS_RESULT_PATTERN, 'arc-bvt-cq')) |
| 997 | self.assertTrue(gs_offloader._is_valid_result( |
| 998 | release_build, gs_offloader.GTS_RESULT_PATTERN, 'arc-gts-tot')) |
| 999 | self.assertFalse(gs_offloader._is_valid_result( |
| 1000 | None, gs_offloader.CTS_RESULT_PATTERN, 'arc-cts')) |
| 1001 | self.assertFalse(gs_offloader._is_valid_result( |
| 1002 | release_build, gs_offloader.CTS_RESULT_PATTERN, None)) |
| 1003 | self.assertFalse(gs_offloader._is_valid_result( |
| 1004 | pfq_build, gs_offloader.CTS_RESULT_PATTERN, 'arc-cts')) |
| 1005 | self.assertFalse(gs_offloader._is_valid_result( |
| 1006 | trybot_build, gs_offloader.CTS_RESULT_PATTERN, 'arc-cts')) |
| 1007 | self.assertFalse(gs_offloader._is_valid_result( |
| 1008 | trybot_2_build, gs_offloader.CTS_RESULT_PATTERN, 'arc-cts')) |
| 1009 | self.assertTrue(gs_offloader._is_valid_result( |
| 1010 | release_2_build, gs_offloader.CTS_RESULT_PATTERN, 'arc-cts')) |
Ningning Xia | 21922c8 | 2016-07-29 11:03:15 -0700 | [diff] [blame] | 1011 | |
| 1012 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1013 | def create_results_folder(self): |
| 1014 | """Create CTS/GTS results folders.""" |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 1015 | results_folder = tempfile.mkdtemp() |
| 1016 | host_folder = os.path.join(results_folder, 'chromeos4-row9-rack11-host22') |
| 1017 | debug_folder = os.path.join(host_folder, 'debug') |
| 1018 | sysinfo_folder = os.path.join(host_folder, 'sysinfo') |
| 1019 | cts_result_folder = os.path.join( |
| 1020 | host_folder, 'cheets_CTS.android.dpi', 'results', 'cts-results') |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1021 | gts_result_folder = os.path.join( |
Ilja H. Friedel | bfa6314 | 2017-01-26 00:56:29 -0800 | [diff] [blame] | 1022 | host_folder, 'cheets_GTS.google.admin', 'results', 'android-gts') |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 1023 | timestamp_str = '2016.04.28_01.41.44' |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1024 | timestamp_cts_folder = os.path.join(cts_result_folder, timestamp_str) |
| 1025 | timestamp_gts_folder = os.path.join(gts_result_folder, timestamp_str) |
| 1026 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1027 | # Test results in cts_result_folder with a different time-stamp. |
| 1028 | timestamp_str_2 = '2016.04.28_10.41.44' |
| 1029 | timestamp_cts_folder_2 = os.path.join(cts_result_folder, timestamp_str_2) |
| 1030 | |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1031 | for folder in [debug_folder, sysinfo_folder, cts_result_folder, |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1032 | timestamp_cts_folder, timestamp_gts_folder, timestamp_cts_folder_2]: |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 1033 | os.makedirs(folder) |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1034 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1035 | path_pattern_pair = [(timestamp_cts_folder, gs_offloader.CTS_RESULT_PATTERN), |
| 1036 | (timestamp_cts_folder_2, gs_offloader.CTS_RESULT_PATTERN), |
| 1037 | (timestamp_gts_folder, gs_offloader.GTS_RESULT_PATTERN)] |
| 1038 | |
| 1039 | # Create timestamp.zip file_path. |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1040 | cts_zip_file = os.path.join(cts_result_folder, timestamp_str + '.zip') |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1041 | cts_zip_file_2 = os.path.join(cts_result_folder, timestamp_str_2 + '.zip') |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1042 | gts_zip_file = os.path.join(gts_result_folder, timestamp_str + '.zip') |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1043 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1044 | # Create xml file_path. |
| 1045 | cts_result_file = os.path.join(timestamp_cts_folder, 'testResult.xml') |
| 1046 | cts_result_file_2 = os.path.join(timestamp_cts_folder_2, 'testResult.xml') |
Ilja H. Friedel | bfa6314 | 2017-01-26 00:56:29 -0800 | [diff] [blame] | 1047 | gts_result_file = os.path.join(timestamp_gts_folder, 'test_result.xml') |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1048 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1049 | for file_path in [cts_zip_file, cts_zip_file_2, gts_zip_file, |
| 1050 | cts_result_file, cts_result_file_2, gts_result_file]: |
| 1051 | with open(file_path, 'w') as f: |
| 1052 | f.write('test') |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 1053 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1054 | return (results_folder, host_folder, path_pattern_pair) |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1055 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1056 | |
| 1057 | def test_upload_testresult_files(self): |
| 1058 | """Test upload_testresult_files.""" |
| 1059 | results_folder, host_folder, path_pattern_pair = self.create_results_folder() |
| 1060 | |
| 1061 | self.mox.StubOutWithMock(gs_offloader, '_upload_files') |
| 1062 | gs_offloader._upload_files( |
| 1063 | mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), False).AndReturn( |
| 1064 | ['test', '-d', host_folder]) |
| 1065 | gs_offloader._upload_files( |
| 1066 | mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), False).AndReturn( |
| 1067 | ['test', '-d', host_folder]) |
| 1068 | gs_offloader._upload_files( |
| 1069 | mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), False).AndReturn( |
| 1070 | ['test', '-d', host_folder]) |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 1071 | |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 1072 | self.mox.ReplayAll() |
| 1073 | gs_offloader.upload_testresult_files(results_folder, False) |
| 1074 | self.mox.VerifyAll() |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1075 | shutil.rmtree(results_folder) |
| 1076 | |
| 1077 | |
| 1078 | def test_upload_files(self): |
| 1079 | """Test upload_files""" |
| 1080 | results_folder, host_folder, path_pattern_pair = self.create_results_folder() |
| 1081 | |
| 1082 | for path, pattern in path_pattern_pair: |
| 1083 | models.test.parse_job_keyval(mox.IgnoreArg()).AndReturn({ |
| 1084 | 'build': 'veyron_minnie-cheets-release/R52-8248.0.0', |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 1085 | 'parent_job_id': 'p_id', |
| 1086 | 'suite': 'arc-cts' |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 1087 | }) |
| 1088 | |
| 1089 | gs_offloader.get_cmd_list( |
| 1090 | False, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 1091 | ['test', '-d', path]) |
| 1092 | gs_offloader.get_cmd_list( |
| 1093 | False, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 1094 | ['test', '-d', path]) |
| 1095 | |
| 1096 | self.mox.ReplayAll() |
| 1097 | gs_offloader._upload_files(host_folder, path, pattern, False) |
| 1098 | self.mox.VerifyAll() |
| 1099 | self.mox.ResetAll() |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 1100 | |
| 1101 | shutil.rmtree(results_folder) |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 1102 | |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 1103 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1104 | class JobDirectoryOffloadTests(_TempResultsDirTestBase): |
| 1105 | """Tests for `_JobDirectory.enqueue_offload()`. |
| 1106 | |
| 1107 | When testing with a `days_old` parameter of 0, we use |
| 1108 | `set_finished()` instead of `set_expired()`. This causes the |
| 1109 | job's timestamp to be set in the future. This is done so as |
| 1110 | to test that when `days_old` is 0, the job is always treated |
| 1111 | as eligible for offload, regardless of the timestamp's value. |
| 1112 | |
| 1113 | Testing covers the following assertions: |
| 1114 | A. Each time `enqueue_offload()` is called, a message that |
| 1115 | includes the job's directory name will be logged using |
| 1116 | `logging.debug()`, regardless of whether the job was |
| 1117 | enqueued. Nothing else is allowed to be logged. |
| 1118 | B. If the job is not eligible to be offloaded, |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1119 | `get_failure_time()` and `get_failure_count()` are 0. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1120 | C. If the job is not eligible for offload, nothing is |
| 1121 | enqueued in `queue`. |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1122 | D. When the job is offloaded, `get_failure_count()` increments |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1123 | each time. |
| 1124 | E. When the job is offloaded, the appropriate parameters are |
| 1125 | enqueued exactly once. |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1126 | F. The first time a job is offloaded, `get_failure_time()` is |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1127 | set to the current time. |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1128 | G. `get_failure_time()` only changes the first time that the |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1129 | job is offloaded. |
| 1130 | |
| 1131 | The test cases below are designed to exercise all of the |
| 1132 | meaningful state transitions at least once. |
| 1133 | |
| 1134 | """ |
| 1135 | |
| 1136 | def setUp(self): |
| 1137 | super(JobDirectoryOffloadTests, self).setUp() |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1138 | self._job = self.make_job(self.REGULAR_JOBLIST[0]) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1139 | self._queue = Queue.Queue() |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1140 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1141 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1142 | def _offload_unexpired_job(self, days_old): |
| 1143 | """Make calls to `enqueue_offload()` for an unexpired job. |
| 1144 | |
| 1145 | This method tests assertions B and C that calling |
| 1146 | `enqueue_offload()` has no effect. |
| 1147 | |
| 1148 | """ |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1149 | self.assertEqual(self._job.get_failure_count(), 0) |
| 1150 | self.assertEqual(self._job.get_failure_time(), 0) |
| 1151 | self._job.enqueue_offload(self._queue, days_old) |
| 1152 | self._job.enqueue_offload(self._queue, days_old) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1153 | self.assertTrue(self._queue.empty()) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1154 | self.assertEqual(self._job.get_failure_count(), 0) |
| 1155 | self.assertEqual(self._job.get_failure_time(), 0) |
| 1156 | self.assertFalse(self._job.is_reportable()) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1157 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1158 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1159 | def _offload_expired_once(self, days_old, count): |
| 1160 | """Make one call to `enqueue_offload()` for an expired job. |
| 1161 | |
| 1162 | This method tests assertions D and E regarding side-effects |
| 1163 | expected when a job is offloaded. |
| 1164 | |
| 1165 | """ |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1166 | self._job.enqueue_offload(self._queue, days_old) |
| 1167 | self.assertEqual(self._job.get_failure_count(), count) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1168 | self.assertFalse(self._queue.empty()) |
| 1169 | v = self._queue.get_nowait() |
| 1170 | self.assertTrue(self._queue.empty()) |
J. Richard Barnette | 2c72ddd | 2014-05-20 12:17:37 -0700 | [diff] [blame] | 1171 | self.assertEqual(v, self._job.queue_args) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1172 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1173 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1174 | def _offload_expired_job(self, days_old): |
| 1175 | """Make calls to `enqueue_offload()` for a just-expired job. |
| 1176 | |
| 1177 | This method directly tests assertions F and G regarding |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1178 | side-effects on `get_failure_time()`. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1179 | |
| 1180 | """ |
| 1181 | t0 = time.time() |
| 1182 | self._offload_expired_once(days_old, 1) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1183 | self.assertFalse(self._job.is_reportable()) |
| 1184 | t1 = self._job.get_failure_time() |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1185 | self.assertLessEqual(t1, time.time()) |
| 1186 | self.assertGreaterEqual(t1, t0) |
| 1187 | self._offload_expired_once(days_old, 2) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1188 | self.assertTrue(self._job.is_reportable()) |
| 1189 | self.assertEqual(self._job.get_failure_time(), t1) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1190 | self._offload_expired_once(days_old, 3) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1191 | self.assertTrue(self._job.is_reportable()) |
| 1192 | self.assertEqual(self._job.get_failure_time(), t1) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1193 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1194 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1195 | def test_case_1_no_expiration(self): |
| 1196 | """Test a series of `enqueue_offload()` calls with `days_old` of 0. |
| 1197 | |
| 1198 | This tests that offload works as expected if calls are |
| 1199 | made both before and after the job becomes expired. |
| 1200 | |
| 1201 | """ |
| 1202 | self._offload_unexpired_job(0) |
| 1203 | self._job.set_finished(0) |
| 1204 | self._offload_expired_job(0) |
| 1205 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1206 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1207 | def test_case_2_no_expiration(self): |
| 1208 | """Test a series of `enqueue_offload()` calls with `days_old` of 0. |
| 1209 | |
| 1210 | This tests that offload works as expected if calls are made |
| 1211 | only after the job becomes expired. |
| 1212 | |
| 1213 | """ |
| 1214 | self._job.set_finished(0) |
| 1215 | self._offload_expired_job(0) |
| 1216 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1217 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1218 | def test_case_1_with_expiration(self): |
| 1219 | """Test a series of `enqueue_offload()` calls with `days_old` non-zero. |
| 1220 | |
| 1221 | This tests that offload works as expected if calls are made |
| 1222 | before the job finishes, before the job expires, and after |
| 1223 | the job expires. |
| 1224 | |
| 1225 | """ |
| 1226 | self._offload_unexpired_job(_TEST_EXPIRATION_AGE) |
| 1227 | self._job.set_finished(_TEST_EXPIRATION_AGE) |
| 1228 | self._offload_unexpired_job(_TEST_EXPIRATION_AGE) |
| 1229 | self._job.set_expired(_TEST_EXPIRATION_AGE) |
| 1230 | self._offload_expired_job(_TEST_EXPIRATION_AGE) |
| 1231 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1232 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1233 | def test_case_2_with_expiration(self): |
| 1234 | """Test a series of `enqueue_offload()` calls with `days_old` non-zero. |
| 1235 | |
| 1236 | This tests that offload works as expected if calls are made |
| 1237 | between finishing and expiration, and after the job expires. |
| 1238 | |
| 1239 | """ |
| 1240 | self._job.set_finished(_TEST_EXPIRATION_AGE) |
| 1241 | self._offload_unexpired_job(_TEST_EXPIRATION_AGE) |
| 1242 | self._job.set_expired(_TEST_EXPIRATION_AGE) |
| 1243 | self._offload_expired_job(_TEST_EXPIRATION_AGE) |
| 1244 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1245 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1246 | def test_case_3_with_expiration(self): |
| 1247 | """Test a series of `enqueue_offload()` calls with `days_old` non-zero. |
| 1248 | |
| 1249 | This tests that offload works as expected if calls are made |
| 1250 | only before finishing and after expiration. |
| 1251 | |
| 1252 | """ |
| 1253 | self._offload_unexpired_job(_TEST_EXPIRATION_AGE) |
| 1254 | self._job.set_expired(_TEST_EXPIRATION_AGE) |
| 1255 | self._offload_expired_job(_TEST_EXPIRATION_AGE) |
| 1256 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1257 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1258 | def test_case_4_with_expiration(self): |
| 1259 | """Test a series of `enqueue_offload()` calls with `days_old` non-zero. |
| 1260 | |
| 1261 | This tests that offload works as expected if calls are made |
| 1262 | only after expiration. |
| 1263 | |
| 1264 | """ |
| 1265 | self._job.set_expired(_TEST_EXPIRATION_AGE) |
| 1266 | self._offload_expired_job(_TEST_EXPIRATION_AGE) |
| 1267 | |
| 1268 | |
| 1269 | class GetJobDirectoriesTests(_TempResultsDirTestBase): |
| 1270 | """Tests for `_JobDirectory.get_job_directories()`.""" |
| 1271 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1272 | def setUp(self): |
| 1273 | super(GetJobDirectoriesTests, self).setUp() |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1274 | self.make_job_hierarchy() |
| 1275 | os.mkdir('not-a-job') |
| 1276 | open('not-a-dir', 'w').close() |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1277 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1278 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1279 | def _run_get_directories(self, cls, expected_list): |
| 1280 | """Test `get_job_directories()` for the given class. |
| 1281 | |
| 1282 | Calls the method, and asserts that the returned list of |
| 1283 | directories matches the expected return value. |
| 1284 | |
| 1285 | @param expected_list Expected return value from the call. |
| 1286 | """ |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1287 | dirlist = cls.get_job_directories() |
| 1288 | self.assertEqual(set(dirlist), set(expected_list)) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1289 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1290 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1291 | def test_get_regular_jobs(self): |
| 1292 | """Test `RegularJobDirectory.get_job_directories()`.""" |
| 1293 | self._run_get_directories(job_directories.RegularJobDirectory, |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1294 | self.REGULAR_JOBLIST) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1295 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1296 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1297 | def test_get_special_jobs(self): |
| 1298 | """Test `SpecialJobDirectory.get_job_directories()`.""" |
| 1299 | self._run_get_directories(job_directories.SpecialJobDirectory, |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1300 | self.SPECIAL_JOBLIST) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1301 | |
| 1302 | |
| 1303 | class AddJobsTests(_TempResultsDirTestBase): |
| 1304 | """Tests for `Offloader._add_new_jobs()`.""" |
| 1305 | |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1306 | MOREJOBS = ['115-fubar', '116-fubar', '117-fubar', '118-snafu'] |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1307 | |
| 1308 | def setUp(self): |
| 1309 | super(AddJobsTests, self).setUp() |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1310 | self._initial_job_names = ( |
| 1311 | set(self.REGULAR_JOBLIST) | set(self.SPECIAL_JOBLIST)) |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1312 | self.make_job_hierarchy() |
| 1313 | self._offloader = gs_offloader.Offloader(_get_options(['-a'])) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1314 | self.mox.StubOutWithMock(logging, 'debug') |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1315 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1316 | |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1317 | def _run_add_new_jobs(self, expected_key_set): |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1318 | """Basic test assertions for `_add_new_jobs()`. |
| 1319 | |
| 1320 | Asserts the following: |
| 1321 | * The keys in the offloader's `_open_jobs` dictionary |
| 1322 | matches the expected set of keys. |
| 1323 | * For every job in `_open_jobs`, the job has the expected |
| 1324 | directory name. |
| 1325 | |
| 1326 | """ |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1327 | count = len(expected_key_set) - len(self._offloader._open_jobs) |
| 1328 | logging.debug(mox.IgnoreArg(), count) |
| 1329 | self.mox.ReplayAll() |
| 1330 | self._offloader._add_new_jobs() |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1331 | self.assertEqual(expected_key_set, |
| 1332 | set(self._offloader._open_jobs.keys())) |
| 1333 | for jobkey, job in self._offloader._open_jobs.items(): |
| 1334 | self.assertEqual(jobkey, job._dirname) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1335 | self.mox.VerifyAll() |
| 1336 | self.mox.ResetAll() |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1337 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1338 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1339 | def test_add_jobs_empty(self): |
| 1340 | """Test adding jobs to an empty dictionary. |
| 1341 | |
| 1342 | Calls the offloader's `_add_new_jobs()`, then perform |
| 1343 | the assertions of `self._check_open_jobs()`. |
| 1344 | |
| 1345 | """ |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1346 | self._run_add_new_jobs(self._initial_job_names) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1347 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1348 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1349 | def test_add_jobs_non_empty(self): |
| 1350 | """Test adding jobs to a non-empty dictionary. |
| 1351 | |
| 1352 | Calls the offloader's `_add_new_jobs()` twice; once from |
| 1353 | initial conditions, and then again after adding more |
| 1354 | directories. After the second call, perform the assertions |
| 1355 | of `self._check_open_jobs()`. Additionally, assert that |
| 1356 | keys added by the first call still map to their original |
| 1357 | job object after the second call. |
| 1358 | |
| 1359 | """ |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1360 | self._run_add_new_jobs(self._initial_job_names) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1361 | jobs_copy = self._offloader._open_jobs.copy() |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1362 | for d in self.MOREJOBS: |
| 1363 | os.mkdir(d) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1364 | self._run_add_new_jobs(self._initial_job_names | |
| 1365 | set(self.MOREJOBS)) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1366 | for key in jobs_copy.keys(): |
| 1367 | self.assertIs(jobs_copy[key], |
| 1368 | self._offloader._open_jobs[key]) |
| 1369 | |
| 1370 | |
| 1371 | class JobStateTests(_TempResultsDirTestBase): |
| 1372 | """Tests for job state predicates. |
| 1373 | |
| 1374 | This tests for the expected results from the |
| 1375 | `is_offloaded()` and `is_reportable()` predicate |
| 1376 | methods. |
| 1377 | |
| 1378 | """ |
| 1379 | |
| 1380 | def test_unfinished_job(self): |
| 1381 | """Test that an unfinished job reports the correct state. |
| 1382 | |
| 1383 | A job is "unfinished" if it isn't marked complete in the |
| 1384 | database. A job in this state is neither "complete" nor |
| 1385 | "reportable". |
| 1386 | |
| 1387 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1388 | job = self.make_job(self.REGULAR_JOBLIST[0]) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1389 | self.assertFalse(job.is_offloaded()) |
| 1390 | self.assertFalse(job.is_reportable()) |
| 1391 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1392 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1393 | def test_incomplete_job(self): |
| 1394 | """Test that an incomplete job reports the correct state. |
| 1395 | |
| 1396 | A job is "incomplete" if exactly one attempt has been made |
| 1397 | to offload the job, but its results directory still exists. |
| 1398 | A job in this state is neither "complete" nor "reportable". |
| 1399 | |
| 1400 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1401 | job = self.make_job(self.REGULAR_JOBLIST[0]) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1402 | job.set_incomplete() |
| 1403 | self.assertFalse(job.is_offloaded()) |
| 1404 | self.assertFalse(job.is_reportable()) |
| 1405 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1406 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1407 | def test_reportable_job(self): |
| 1408 | """Test that a reportable job reports the correct state. |
| 1409 | |
| 1410 | A job is "reportable" if more than one attempt has been made |
| 1411 | to offload the job, and its results directory still exists. |
| 1412 | A job in this state is "reportable", but not "complete". |
| 1413 | |
| 1414 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1415 | job = self.make_job(self.REGULAR_JOBLIST[0]) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1416 | job.set_reportable() |
| 1417 | self.assertFalse(job.is_offloaded()) |
| 1418 | self.assertTrue(job.is_reportable()) |
| 1419 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1420 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1421 | def test_completed_job(self): |
| 1422 | """Test that a completed job reports the correct state. |
| 1423 | |
| 1424 | A job is "completed" if at least one attempt has been made |
| 1425 | to offload the job, and its results directory still exists. |
| 1426 | A job in this state is "complete", and not "reportable". |
| 1427 | |
| 1428 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1429 | job = self.make_job(self.REGULAR_JOBLIST[0]) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1430 | job.set_complete() |
| 1431 | self.assertTrue(job.is_offloaded()) |
| 1432 | self.assertFalse(job.is_reportable()) |
| 1433 | |
| 1434 | |
| 1435 | class ReportingTests(_TempResultsDirTestBase): |
| 1436 | """Tests for `Offloader._update_offload_results()`.""" |
| 1437 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1438 | def setUp(self): |
| 1439 | super(ReportingTests, self).setUp() |
| 1440 | self._offloader = gs_offloader.Offloader(_get_options([])) |
| 1441 | self.mox.StubOutWithMock(email_manager.manager, |
| 1442 | 'send_email') |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1443 | self.mox.StubOutWithMock(logging, 'debug') |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1444 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1445 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1446 | def _add_job(self, jobdir): |
| 1447 | """Add a job to the dictionary of unfinished jobs.""" |
| 1448 | j = self.make_job(jobdir) |
| 1449 | self._offloader._open_jobs[j._dirname] = j |
| 1450 | return j |
| 1451 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1452 | |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1453 | def _expect_log_message(self, new_open_jobs, with_failures): |
| 1454 | """Mock expected logging calls. |
| 1455 | |
| 1456 | `_update_offload_results()` logs one message with the number |
| 1457 | of jobs removed from the open job set and the number of jobs |
| 1458 | still remaining. Additionally, if there are reportable |
| 1459 | jobs, then it logs the number of jobs that haven't yet |
| 1460 | offloaded. |
| 1461 | |
| 1462 | This sets up the logging calls using `new_open_jobs` to |
| 1463 | figure the job counts. If `with_failures` is true, then |
| 1464 | the log message is set up assuming that all jobs in |
| 1465 | `new_open_jobs` have offload failures. |
| 1466 | |
| 1467 | @param new_open_jobs New job set for calculating counts |
| 1468 | in the messages. |
| 1469 | @param with_failures Whether the log message with a |
| 1470 | failure count is expected. |
| 1471 | |
| 1472 | """ |
| 1473 | count = len(self._offloader._open_jobs) - len(new_open_jobs) |
| 1474 | logging.debug(mox.IgnoreArg(), count, len(new_open_jobs)) |
| 1475 | if with_failures: |
| 1476 | logging.debug(mox.IgnoreArg(), len(new_open_jobs)) |
| 1477 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1478 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1479 | def _run_update_no_report(self, new_open_jobs): |
| 1480 | """Call `_update_offload_results()` expecting no report. |
| 1481 | |
| 1482 | Initial conditions are set up by the caller. This calls |
| 1483 | `_update_offload_results()` once, and then checks these |
| 1484 | assertions: |
| 1485 | * The offloader's `_next_report_time` field is unchanged. |
| 1486 | * The offloader's new `_open_jobs` field contains only |
| 1487 | the entries in `new_open_jobs`. |
| 1488 | * The email_manager's `send_email` stub wasn't called. |
| 1489 | |
| 1490 | @param new_open_jobs A dictionary representing the expected |
| 1491 | new value of the offloader's |
| 1492 | `_open_jobs` field. |
| 1493 | """ |
| 1494 | self.mox.ReplayAll() |
| 1495 | next_report_time = self._offloader._next_report_time |
| 1496 | self._offloader._update_offload_results() |
| 1497 | self.assertEqual(next_report_time, |
| 1498 | self._offloader._next_report_time) |
| 1499 | self.assertEqual(self._offloader._open_jobs, new_open_jobs) |
| 1500 | self.mox.VerifyAll() |
| 1501 | self.mox.ResetAll() |
| 1502 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1503 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1504 | def _run_update_with_report(self, new_open_jobs): |
| 1505 | """Call `_update_offload_results()` expecting an e-mail report. |
| 1506 | |
| 1507 | Initial conditions are set up by the caller. This calls |
| 1508 | `_update_offload_results()` once, and then checks these |
| 1509 | assertions: |
| 1510 | * The offloader's `_next_report_time` field is updated |
| 1511 | to an appropriate new time. |
| 1512 | * The offloader's new `_open_jobs` field contains only |
| 1513 | the entries in `new_open_jobs`. |
| 1514 | * The email_manager's `send_email` stub was called. |
| 1515 | |
| 1516 | @param new_open_jobs A dictionary representing the expected |
| 1517 | new value of the offloader's |
| 1518 | `_open_jobs` field. |
| 1519 | """ |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1520 | logging.debug(mox.IgnoreArg()) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1521 | email_manager.manager.send_email( |
| 1522 | mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) |
| 1523 | self.mox.ReplayAll() |
| 1524 | t0 = time.time() + gs_offloader.REPORT_INTERVAL_SECS |
| 1525 | self._offloader._update_offload_results() |
| 1526 | t1 = time.time() + gs_offloader.REPORT_INTERVAL_SECS |
| 1527 | next_report_time = self._offloader._next_report_time |
| 1528 | self.assertGreaterEqual(next_report_time, t0) |
| 1529 | self.assertLessEqual(next_report_time, t1) |
| 1530 | self.assertEqual(self._offloader._open_jobs, new_open_jobs) |
| 1531 | self.mox.VerifyAll() |
| 1532 | self.mox.ResetAll() |
| 1533 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1534 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1535 | def test_no_jobs(self): |
| 1536 | """Test `_update_offload_results()` with no open jobs. |
| 1537 | |
| 1538 | Initial conditions are an empty `_open_jobs` list and |
| 1539 | `_next_report_time` in the past. Expected result is no |
| 1540 | e-mail report, and an empty `_open_jobs` list. |
| 1541 | |
| 1542 | """ |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1543 | self._expect_log_message({}, False) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1544 | self._run_update_no_report({}) |
| 1545 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1546 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1547 | def test_all_completed(self): |
| 1548 | """Test `_update_offload_results()` with only complete jobs. |
| 1549 | |
| 1550 | Initial conditions are an `_open_jobs` list consisting of |
| 1551 | only completed jobs and `_next_report_time` in the past. |
| 1552 | Expected result is no e-mail report, and an empty |
| 1553 | `_open_jobs` list. |
| 1554 | |
| 1555 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1556 | for d in self.REGULAR_JOBLIST: |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1557 | self._add_job(d).set_complete() |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1558 | self._expect_log_message({}, False) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1559 | self._run_update_no_report({}) |
| 1560 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1561 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1562 | def test_none_finished(self): |
| 1563 | """Test `_update_offload_results()` with only unfinished jobs. |
| 1564 | |
| 1565 | Initial conditions are an `_open_jobs` list consisting of |
| 1566 | only unfinished jobs and `_next_report_time` in the past. |
| 1567 | Expected result is no e-mail report, and no change to the |
| 1568 | `_open_jobs` list. |
| 1569 | |
| 1570 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1571 | for d in self.REGULAR_JOBLIST: |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1572 | self._add_job(d) |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1573 | new_jobs = self._offloader._open_jobs.copy() |
| 1574 | self._expect_log_message(new_jobs, False) |
| 1575 | self._run_update_no_report(new_jobs) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1576 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1577 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1578 | def test_none_reportable(self): |
| 1579 | """Test `_update_offload_results()` with only incomplete jobs. |
| 1580 | |
| 1581 | Initial conditions are an `_open_jobs` list consisting of |
| 1582 | only incomplete jobs and `_next_report_time` in the past. |
| 1583 | Expected result is no e-mail report, and no change to the |
| 1584 | `_open_jobs` list. |
| 1585 | |
| 1586 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1587 | for d in self.REGULAR_JOBLIST: |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1588 | self._add_job(d).set_incomplete() |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1589 | new_jobs = self._offloader._open_jobs.copy() |
| 1590 | self._expect_log_message(new_jobs, False) |
| 1591 | self._run_update_no_report(new_jobs) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1592 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1593 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1594 | def test_report_not_ready(self): |
| 1595 | """Test `_update_offload_results()` e-mail throttling. |
| 1596 | |
| 1597 | Initial conditions are an `_open_jobs` list consisting of |
| 1598 | only reportable jobs but with `_next_report_time` in |
| 1599 | the future. Expected result is no e-mail report, and no |
| 1600 | change to the `_open_jobs` list. |
| 1601 | |
| 1602 | """ |
| 1603 | # N.B. This test may fail if its run time exceeds more than |
| 1604 | # about _MARGIN_SECS seconds. |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1605 | for d in self.REGULAR_JOBLIST: |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1606 | self._add_job(d).set_reportable() |
| 1607 | self._offloader._next_report_time += _MARGIN_SECS |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1608 | new_jobs = self._offloader._open_jobs.copy() |
| 1609 | self._expect_log_message(new_jobs, True) |
| 1610 | self._run_update_no_report(new_jobs) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1611 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1612 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1613 | def test_reportable(self): |
| 1614 | """Test `_update_offload_results()` with reportable jobs. |
| 1615 | |
| 1616 | Initial conditions are an `_open_jobs` list consisting of |
| 1617 | only reportable jobs and with `_next_report_time` in |
| 1618 | the past. Expected result is an e-mail report, and no |
| 1619 | change to the `_open_jobs` list. |
| 1620 | |
| 1621 | """ |
J. Richard Barnette | 0880032 | 2014-05-16 14:49:46 -0700 | [diff] [blame] | 1622 | for d in self.REGULAR_JOBLIST: |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1623 | self._add_job(d).set_reportable() |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1624 | new_jobs = self._offloader._open_jobs.copy() |
| 1625 | self._expect_log_message(new_jobs, True) |
| 1626 | self._run_update_with_report(new_jobs) |
| 1627 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 1628 | |
J. Richard Barnette | 22dd748 | 2014-06-23 12:25:02 -0700 | [diff] [blame] | 1629 | def test_reportable_mixed(self): |
| 1630 | """Test `_update_offload_results()` with a mixture of jobs. |
| 1631 | |
| 1632 | Initial conditions are an `_open_jobs` list consisting of |
| 1633 | one reportable jobs and the remainder of the jobs |
| 1634 | incomplete. The value of `_next_report_time` is in the |
| 1635 | past. Expected result is an e-mail report that includes |
| 1636 | both the reportable and the incomplete jobs, and no change |
| 1637 | to the `_open_jobs` list. |
| 1638 | |
| 1639 | """ |
| 1640 | self._add_job(self.REGULAR_JOBLIST[0]).set_reportable() |
| 1641 | for d in self.REGULAR_JOBLIST[1:]: |
| 1642 | self._add_job(d).set_incomplete() |
| 1643 | new_jobs = self._offloader._open_jobs.copy() |
| 1644 | self._expect_log_message(new_jobs, True) |
| 1645 | self._run_update_with_report(new_jobs) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1646 | |
| 1647 | |
| 1648 | if __name__ == '__main__': |
| 1649 | unittest.main() |