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 34 35class FakeJob(object): 36 """Faked out RPC-client-side Job object.""" 37 def __init__(self, id=0, statuses=[], hostnames=[], parent_job_id=None): 38 self.id = id 39 self.hostnames = hostnames if hostnames else ['host%d' % id] 40 self.owner = 'tester' 41 self.name = 'Fake Job %d' % self.id 42 self.statuses = statuses 43 self.parent_job_id = parent_job_id 44 45 46class FakeHost(object): 47 """Faked out RPC-client-side Host object.""" 48 def __init__(self, hostname='', status='Ready', locked=False, locked_by=''): 49 self.hostname = hostname 50 self.status = status 51 self.locked = locked 52 self.locked_by = locked_by 53 54 55 def __str__(self): 56 return '%s: %s. %s%s' % ( 57 self.hostname, self.status, 58 'Locked' if self.locked else 'Unlocked', 59 ' by %s' % self.locked_by if self.locked else '') 60 61 62class FakeLabel(object): 63 """Faked out RPC-client-side Label object.""" 64 def __init__(self, id=0): 65 self.id = id 66 67 68class FakeStatus(object): 69 """Fake replacement for server-side job status objects. 70 71 @var status: 'GOOD', 'FAIL', 'ERROR', etc. 72 @var test_name: name of the test this is status for 73 @var reason: reason for failure, if any 74 @var aborted: present and True if the job was aborted. Optional. 75 """ 76 def __init__(self, code, name, reason, aborted=None, 77 hostname=None, subdir='fake_Test.tag.subdir_tag', 78 job_tag='id-owner/hostname'): 79 self.status = code 80 self.test_name = name 81 self.reason = reason 82 self.hostname = hostname if hostname else 'hostless' 83 self.entry = {} 84 self.test_started_time = '2012-11-11 11:11:11' 85 self.test_finished_time = '2012-11-11 12:12:12' 86 self.job_tag=job_tag 87 self.subdir=subdir 88 if aborted: 89 self.entry['aborted'] = True 90 if hostname: 91 self.entry['host'] = {'hostname': hostname} 92 93 94 def __repr__(self): 95 return '%s\t%s\t%s: %s' % (self.status, self.test_name, self.reason, 96 self.hostname) 97 98 99 def equals_record(self, status): 100 """Compares this object to a recorded status.""" 101 if 'aborted' in self.entry and self.entry['aborted']: 102 return status._status == 'ABORT' 103 return (self.status == status._status and 104 status._test_name.endswith(self.test_name) and 105 self.reason == status._reason) 106 107 108 def equals_hostname_record(self, status): 109 """Compares this object to a recorded status. 110 111 Expects the test name field of |status| to contain |self.hostname|. 112 """ 113 return (self.status == status._status and 114 self.hostname in status._test_name and 115 self.reason == status._reason) 116 117 118 def record_all(self, record): 119 pass 120 121 122 def is_good(self): 123 pass 124 125 def name(self): 126 return self.test_name 127