lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""Tests for job_directories."""
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import contextlib
import datetime
import mox
import os
import shutil
import tempfile
import unittest
 
import common
from autotest_lib.site_utils import job_directories
from autotest_lib.client.common_lib import time_utils
 
 
class SwarmingJobDirectoryTestCase(unittest.TestCase):
    """Tests SwarmingJobDirectory."""
 
    def test_get_job_directories_legacy(self):
        with _change_to_tempdir():
            os.makedirs("swarming-3e4391423c3a4311/b")
            os.mkdir("not-a-swarming-dir")
            results = job_directories.SwarmingJobDirectory.get_job_directories()
            self.assertEqual(set(results), {"swarming-3e4391423c3a4311"})
 
    def test_get_job_directories(self):
        with _change_to_tempdir():
            os.makedirs("swarming-3e4391423c3a4310/1")
            os.makedirs("swarming-3e4391423c3a4310/0")
            os.makedirs("swarming-3e4391423c3a4310/a")
            os.mkdir("not-a-swarming-dir")
            results = job_directories.SwarmingJobDirectory.get_job_directories()
            self.assertEqual(set(results),
                             {"swarming-3e4391423c3a4310/1",
                              "swarming-3e4391423c3a4310/a"})
 
 
class GetJobIDOrTaskID(unittest.TestCase):
    """Tests get_job_id_or_task_id."""
 
    def test_legacy_swarming_path(self):
        self.assertEqual(
                "3e4391423c3a4311",
                job_directories.get_job_id_or_task_id(
                        "/autotest/results/swarming-3e4391423c3a4311"),
        )
        self.assertEqual(
                "3e4391423c3a4311",
                job_directories.get_job_id_or_task_id(
                        "swarming-3e4391423c3a4311"),
        )
 
    def test_swarming_path(self):
        self.assertEqual(
                "3e4391423c3a4311",
                job_directories.get_job_id_or_task_id(
                        "/autotest/results/swarming-3e4391423c3a4310/1"),
        )
        self.assertEqual(
                "3e4391423c3a431f",
                job_directories.get_job_id_or_task_id(
                        "swarming-3e4391423c3a4310/f"),
        )
 
 
 
class JobDirectorySubclassTests(mox.MoxTestBase):
    """Test specific to RegularJobDirectory and SpecialJobDirectory.
 
    This provides coverage for the implementation in both
    RegularJobDirectory and SpecialJobDirectory.
 
    """
 
    def setUp(self):
        super(JobDirectorySubclassTests, self).setUp()
        self.mox.StubOutWithMock(job_directories, '_AFE')
 
 
    def test_regular_job_fields(self):
        """Test the constructor for `RegularJobDirectory`.
 
        Construct a regular job, and assert that the `dirname`
        and `_id` attributes are set as expected.
 
        """
        resultsdir = '118-fubar'
        job = job_directories.RegularJobDirectory(resultsdir)
        self.assertEqual(job.dirname, resultsdir)
        self.assertEqual(job._id, '118')
 
 
    def test_special_job_fields(self):
        """Test the constructor for `SpecialJobDirectory`.
 
        Construct a special job, and assert that the `dirname`
        and `_id` attributes are set as expected.
 
        """
        destdir = 'hosts/host1'
        resultsdir = destdir + '/118-reset'
        job = job_directories.SpecialJobDirectory(resultsdir)
        self.assertEqual(job.dirname, resultsdir)
        self.assertEqual(job._id, '118')
 
 
    def _check_finished_job(self, jobtime, hqetimes, expected):
        """Mock and test behavior of a finished job.
 
        Initialize the mocks for a call to
        `get_timestamp_if_finished()`, then simulate one call.
        Assert that the returned timestamp matches the passed
        in expected value.
 
        @param jobtime Time used to construct a _MockJob object.
        @param hqetimes List of times used to construct
                        _MockHostQueueEntry objects.
        @param expected Expected time to be returned by
                        get_timestamp_if_finished
 
        """
        job = job_directories.RegularJobDirectory('118-fubar')
        job_directories._AFE.get_jobs(
                id=job._id, finished=True).AndReturn(
                        [_MockJob(jobtime)])
        job_directories._AFE.get_host_queue_entries(
                finished_on__isnull=False,
                job_id=job._id).AndReturn(
                        [_MockHostQueueEntry(t) for t in hqetimes])
        self.mox.ReplayAll()
        self.assertEqual(expected, job.get_timestamp_if_finished())
        self.mox.VerifyAll()
 
 
    def test_finished_regular_job(self):
        """Test getting the timestamp for a finished regular job.
 
        Tests the return value for
        `RegularJobDirectory.get_timestamp_if_finished()` when
        the AFE indicates the job is finished.
 
        """
        created_timestamp = make_timestamp(1, True)
        hqe_timestamp = make_timestamp(0, True)
        self._check_finished_job(created_timestamp,
                                 [hqe_timestamp],
                                 hqe_timestamp)
 
 
    def test_finished_regular_job_multiple_hqes(self):
        """Test getting the timestamp for a regular job with multiple hqes.
 
        Tests the return value for
        `RegularJobDirectory.get_timestamp_if_finished()` when
        the AFE indicates the job is finished and the job has multiple host
        queue entries.
 
        Tests that the returned timestamp is the latest timestamp in
        the list of HQEs, regardless of the returned order.
 
        """
        created_timestamp = make_timestamp(2, True)
        older_hqe_timestamp = make_timestamp(1, True)
        newer_hqe_timestamp = make_timestamp(0, True)
        hqe_list = [older_hqe_timestamp,
                    newer_hqe_timestamp]
        self._check_finished_job(created_timestamp,
                                 hqe_list,
                                 newer_hqe_timestamp)
        self.mox.ResetAll()
        hqe_list.reverse()
        self._check_finished_job(created_timestamp,
                                 hqe_list,
                                 newer_hqe_timestamp)
 
 
    def test_finished_regular_job_null_finished_times(self):
        """Test getting the timestamp for an aborted regular job.
 
        Tests the return value for
        `RegularJobDirectory.get_timestamp_if_finished()` when
        the AFE indicates the job is finished and the job has aborted host
        queue entries.
 
        """
        timestamp = make_timestamp(0, True)
        self._check_finished_job(timestamp, [], timestamp)
 
 
    def test_unfinished_regular_job(self):
        """Test getting the timestamp for an unfinished regular job.
 
        Tests the return value for
        `RegularJobDirectory.get_timestamp_if_finished()` when
        the AFE indicates the job is not finished.
 
        """
        job = job_directories.RegularJobDirectory('118-fubar')
        job_directories._AFE.get_jobs(
                id=job._id, finished=True).AndReturn([])
        self.mox.ReplayAll()
        self.assertIsNone(job.get_timestamp_if_finished())
        self.mox.VerifyAll()
 
 
    def test_finished_special_job(self):
        """Test getting the timestamp for a finished special job.
 
        Tests the return value for
        `SpecialJobDirectory.get_timestamp_if_finished()` when
        the AFE indicates the job is finished.
 
        """
        job = job_directories.SpecialJobDirectory(
                'hosts/host1/118-reset')
        timestamp = make_timestamp(0, True)
        job_directories._AFE.get_special_tasks(
                id=job._id, is_complete=True).AndReturn(
                    [_MockSpecialTask(timestamp)])
        self.mox.ReplayAll()
        self.assertEqual(timestamp,
                         job.get_timestamp_if_finished())
        self.mox.VerifyAll()
 
 
    def test_unfinished_special_job(self):
        """Test getting the timestamp for an unfinished special job.
 
        Tests the return value for
        `SpecialJobDirectory.get_timestamp_if_finished()` when
        the AFE indicates the job is not finished.
 
        """
        job = job_directories.SpecialJobDirectory(
                'hosts/host1/118-reset')
        job_directories._AFE.get_special_tasks(
                id=job._id, is_complete=True).AndReturn([])
        self.mox.ReplayAll()
        self.assertIsNone(job.get_timestamp_if_finished())
        self.mox.VerifyAll()
 
 
class JobExpirationTests(unittest.TestCase):
    """Tests to exercise `job_directories.is_job_expired()`."""
 
    def test_expired(self):
        """Test detection of an expired job."""
        timestamp = make_timestamp(_TEST_EXPIRATION_AGE, True)
        self.assertTrue(
            job_directories.is_job_expired(
                _TEST_EXPIRATION_AGE, timestamp))
 
 
    def test_alive(self):
        """Test detection of a job that's not expired."""
        # N.B.  This test may fail if its run time exceeds more than
        # about _MARGIN_SECS seconds.
        timestamp = make_timestamp(_TEST_EXPIRATION_AGE, False)
        self.assertFalse(
            job_directories.is_job_expired(
                _TEST_EXPIRATION_AGE, timestamp))
 
 
# When constructing sample time values for testing expiration,
# allow this many seconds between the expiration time and the
# current time.
_MARGIN_SECS = 10.0
# Test value to use for `days_old`, if nothing else is required.
_TEST_EXPIRATION_AGE = 7
 
 
class _MockJob(object):
    """Class to mock the return value of `AFE.get_jobs()`."""
    def __init__(self, created):
        self.created_on = created
 
 
class _MockHostQueueEntry(object):
    """Class to mock the return value of `AFE.get_host_queue_entries()`."""
    def __init__(self, finished):
        self.finished_on = finished
 
 
class _MockSpecialTask(object):
    """Class to mock the return value of `AFE.get_special_tasks()`."""
    def __init__(self, finished):
        self.time_finished = finished
 
 
@contextlib.contextmanager
def _change_to_tempdir():
    old_dir = os.getcwd()
    tempdir = tempfile.mkdtemp('job_directories_unittest')
    try:
        os.chdir(tempdir)
        yield
    finally:
        os.chdir(old_dir)
        shutil.rmtree(tempdir)
 
 
def make_timestamp(age_limit, is_expired):
    """Create a timestamp for use by `job_directories.is_job_expired()`.
 
    The timestamp will meet the syntactic requirements for
    timestamps used as input to `is_job_expired()`.  If
    `is_expired` is true, the timestamp will be older than
    `age_limit` days before the current time; otherwise, the
    date will be younger.
 
    @param age_limit    The number of days before expiration of the
                        target timestamp.
    @param is_expired   Whether the timestamp should be expired
                        relative to `age_limit`.
 
    """
    seconds = -_MARGIN_SECS
    if is_expired:
        seconds = -seconds
    delta = datetime.timedelta(days=age_limit, seconds=seconds)
    reference_time = datetime.datetime.now() - delta
    return reference_time.strftime(time_utils.TIME_FMT)
 
 
if __name__ == '__main__':
    unittest.main()