1# Lint as: python2, python3
2#pylint: disable-msg=C0111
3# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Fakes for dynamic_suite-related unit tests."""
8
9from __future__ import absolute_import
10from __future__ import division
11from __future__ import print_function
12import common
13from autotest_lib.client.common_lib import control_data
14from six.moves import map
15
16
17class FakeControlData(control_data.ControlData):
18    """A fake parsed control file data structure."""
19    def __init__(self, suite, attributes, data, time='LONG', expr=False,
20                 dependencies=None, job_retries=0):
21        self.string = 'text-' + data
22        self.name = 'name-' + data
23        self.path = None  # Will be set during 'parsing'.
24        self.data = data
25        self.suite = suite
26        self.attributes = attributes
27        self.test_type = 'Client'
28        self.experimental = expr
29        if not dependencies:
30            dependencies=[]
31        self.dependencies = dependencies
32        self.time = time
33        self.sync_count = 1
34        self.job_retries = job_retries
35        self.bug_template = {}
36        self.require_ssp = None
37        self.priority = 10
38        self.fast = False
39
40
41class FakeJob(object):
42    """Faked out RPC-client-side Job object."""
43    def __init__(self, id=0, statuses=[], hostnames=[], parent_job_id=None):
44        self.id = id
45        self.hostnames = hostnames if hostnames else ['host%d' % id]
46        self.owner = 'tester'
47        self.name = 'Fake Job %d' % self.id
48        self.statuses = statuses
49        self.parent_job_id = parent_job_id
50
51
52class FakeHost(object):
53    """Faked out RPC-client-side Host object."""
54    def __init__(self, hostname='', status='Ready', locked=False, locked_by=''):
55        self.hostname = hostname
56        self.status = status
57        self.locked = locked
58        self.locked_by = locked_by
59
60
61    def __str__(self):
62        return '%s: %s.  %s%s' % (
63            self.hostname, self.status,
64            'Locked' if self.locked else 'Unlocked',
65            ' by %s' % self.locked_by if self.locked else '')
66
67
68class FakeLabel(object):
69    """Faked out RPC-client-side Label object."""
70    def __init__(self, id=0):
71        self.id = id
72
73
74class FakeStatus(object):
75    """Fake replacement for server-side job status objects.
76
77    @var status: 'GOOD', 'FAIL', 'ERROR', etc.
78    @var test_name: name of the test this is status for
79    @var reason: reason for failure, if any
80    @var aborted: present and True if the job was aborted.  Optional.
81    """
82    def __init__(self, code, name, reason, aborted=None,
83                 hostname=None, subdir='fake_Test.tag.subdir_tag',
84                 job_tag='id-owner/hostname'):
85        self.status = code
86        self.test_name = name
87        self.reason = reason
88        self.hostname = hostname if hostname else 'hostless'
89        self.entry = {}
90        self.test_started_time = '2012-11-11 11:11:11'
91        self.test_finished_time = '2012-11-11 12:12:12'
92        self.job_tag=job_tag
93        self.subdir=subdir
94        if aborted:
95            self.entry['aborted'] = True
96        if hostname:
97            self.entry['host'] = {'hostname': hostname}
98
99
100    def __repr__(self):
101        return '%s\t%s\t%s: %s' % (self.status, self.test_name, self.reason,
102                                   self.hostname)
103
104
105    def equals_record(self, status):
106        """Compares this object to a recorded status."""
107        if 'aborted' in self.entry and self.entry['aborted']:
108            return status._status == 'ABORT'
109        return (self.status == status._status and
110                status._test_name.endswith(self.test_name) and
111                self.reason == status._reason)
112
113
114    def equals_hostname_record(self, status):
115        """Compares this object to a recorded status.
116
117        Expects the test name field of |status| to contain |self.hostname|.
118        """
119        return (self.status == status._status and
120                self.hostname in status._test_name and
121                self.reason == status._reason)
122
123
124    def record_all(self, record):
125        pass
126
127
128    def is_good(self):
129        pass
130
131    def name(self):
132        return self.test_name
133
134
135class FakeMultiprocessingPool(object):
136    """Fake multiprocessing pool to mock out the map method."""
137
138
139    def __init__(self, processes=None, initializer=None, initargs=(),
140                 maxtasksperchild=None):
141        pass
142
143
144    def map(self, func, iterable, chunksize=None):
145        """Use the standard map() built-in instead of Pool.map()"""
146        return list(map(func, iterable))
147
148
149    def close(self):
150        pass
151
152
153    def join(self):
154        pass
155