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