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