OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2015 The Chromium 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 import codecs |
| 7 import copy |
| 8 import json |
| 9 import os |
| 10 import sys |
| 11 import unittest |
| 12 |
| 13 #import test_env # pylint: disable=W0403,W0611 |
| 14 |
| 15 sys.path.insert(0, os.path.join( |
| 16 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 17 'recipe_modules', 'bot_update', 'resources')) |
| 18 sys.platform = 'linux2' # For consistency, ya know? |
| 19 import bot_update |
| 20 |
| 21 DEFAULT_PARAMS = { |
| 22 'solutions': [{ |
| 23 'name': 'somename', |
| 24 'url': 'https://fake.com' |
| 25 }], |
| 26 'revisions': [], |
| 27 'first_sln': 'somename', |
| 28 'target_os': None, |
| 29 'target_os_only': None, |
| 30 'patch_root': None, |
| 31 'issue': None, |
| 32 'patchset': None, |
| 33 'rietveld_server': None, |
| 34 'gerrit_repo': None, |
| 35 'gerrit_ref': None, |
| 36 'gerrit_rebase_patch_ref': None, |
| 37 'revision_mapping': {}, |
| 38 'apply_issue_email_file': None, |
| 39 'apply_issue_key_file': None, |
| 40 'buildspec': False, |
| 41 'gyp_env': None, |
| 42 'shallow': False, |
| 43 'runhooks': False, |
| 44 'refs': [], |
| 45 'git_cache_dir': '', |
| 46 'gerrit_reset': None, |
| 47 } |
| 48 |
| 49 |
| 50 class MockedPopen(object): |
| 51 """A fake instance of a called subprocess. |
| 52 |
| 53 This is meant to be used in conjunction with MockedCall. |
| 54 """ |
| 55 def __init__(self, args=None, kwargs=None): |
| 56 self.args = args or [] |
| 57 self.kwargs = kwargs or {} |
| 58 self.return_value = None |
| 59 self.fails = False |
| 60 |
| 61 def returns(self, rv): |
| 62 """Set the return value when this popen is called. |
| 63 |
| 64 rv can be a string, or a callable (eg function). |
| 65 """ |
| 66 self.return_value = rv |
| 67 return self |
| 68 |
| 69 def check(self, args, kwargs): |
| 70 """Check to see if the given args/kwargs call match this instance. |
| 71 |
| 72 This does a partial match, so that a call to "git clone foo" will match |
| 73 this instance if this instance was recorded as "git clone" |
| 74 """ |
| 75 if any(input_arg != expected_arg |
| 76 for (input_arg, expected_arg) in zip(args, self.args)): |
| 77 return False |
| 78 return self.return_value |
| 79 |
| 80 def __call__(self, args, kwargs): |
| 81 """Actually call this popen instance.""" |
| 82 if hasattr(self.return_value, '__call__'): |
| 83 return self.return_value(*args, **kwargs) |
| 84 return self.return_value |
| 85 |
| 86 |
| 87 class MockedCall(object): |
| 88 """A fake instance of bot_update.call(). |
| 89 |
| 90 This object is pre-seeded with "answers" in self.expectations. The type |
| 91 is a MockedPopen object, or any object with a __call__() and check() method. |
| 92 The check() method is used to check to see if the correct popen object is |
| 93 chosen (can be a partial match, eg a "git clone" popen module would match |
| 94 a "git clone foo" call). |
| 95 By default, if no answers have been pre-seeded, the call() returns successful |
| 96 with an empty string. |
| 97 """ |
| 98 def __init__(self, fake_filesystem): |
| 99 self.expectations = [] |
| 100 self.records = [] |
| 101 |
| 102 def expect(self, args=None, kwargs=None): |
| 103 args = args or [] |
| 104 kwargs = kwargs or {} |
| 105 popen = MockedPopen(args, kwargs) |
| 106 self.expectations.append(popen) |
| 107 return popen |
| 108 |
| 109 def __call__(self, *args, **kwargs): |
| 110 self.records.append((args, kwargs)) |
| 111 for popen in self.expectations: |
| 112 if popen.check(args, kwargs): |
| 113 self.expectations.remove(popen) |
| 114 return popen(args, kwargs) |
| 115 return '' |
| 116 |
| 117 |
| 118 class MockedGclientSync(): |
| 119 """A class producing a callable instance of gclient sync. |
| 120 |
| 121 Because for bot_update, gclient sync also emits an output json file, we need |
| 122 a callable object that can understand where the output json file is going, and |
| 123 emit a (albite) fake file for bot_update to consume. |
| 124 """ |
| 125 def __init__(self, fake_filesystem): |
| 126 self.output = {} |
| 127 self.fake_filesystem = fake_filesystem |
| 128 |
| 129 def __call__(self, *args, **_): |
| 130 output_json_index = args.index('--output-json') + 1 |
| 131 with self.fake_filesystem.open(args[output_json_index], 'w') as f: |
| 132 json.dump(self.output, f) |
| 133 |
| 134 |
| 135 class FakeFile(): |
| 136 def __init__(self): |
| 137 self.contents = '' |
| 138 |
| 139 def write(self, buf): |
| 140 self.contents += buf |
| 141 |
| 142 def read(self): |
| 143 return self.contents |
| 144 |
| 145 def __enter__(self): |
| 146 return self |
| 147 |
| 148 def __exit__(self, _, __, ___): |
| 149 pass |
| 150 |
| 151 |
| 152 class FakeFilesystem(): |
| 153 def __init__(self): |
| 154 self.files = {} |
| 155 |
| 156 def open(self, target, mode='r', encoding=None): |
| 157 if 'w' in mode: |
| 158 self.files[target] = FakeFile() |
| 159 return self.files[target] |
| 160 return self.files[target] |
| 161 |
| 162 |
| 163 def fake_git(*args, **kwargs): |
| 164 return bot_update.call('git', *args, **kwargs) |
| 165 |
| 166 |
| 167 class BotUpdateUnittests(unittest.TestCase): |
| 168 def setUp(self): |
| 169 self.filesystem = FakeFilesystem() |
| 170 self.call = MockedCall(self.filesystem) |
| 171 self.gclient = MockedGclientSync(self.filesystem) |
| 172 self.call.expect(('gclient', 'sync')).returns(self.gclient) |
| 173 self.old_call = getattr(bot_update, 'call') |
| 174 self.params = copy.deepcopy(DEFAULT_PARAMS) |
| 175 setattr(bot_update, 'call', self.call) |
| 176 setattr(bot_update, 'git', fake_git) |
| 177 |
| 178 self.old_os_cwd = os.getcwd |
| 179 setattr(os, 'getcwd', lambda: '/b/build/slave/foo/build') |
| 180 |
| 181 setattr(bot_update, 'open', self.filesystem.open) |
| 182 self.old_codecs_open = codecs.open |
| 183 setattr(codecs, 'open', self.filesystem.open) |
| 184 |
| 185 def tearDown(self): |
| 186 setattr(bot_update, 'call', self.old_call) |
| 187 setattr(os, 'getcwd', self.old_os_cwd) |
| 188 delattr(bot_update, 'open') |
| 189 setattr(codecs, 'open', self.old_codecs_open) |
| 190 |
| 191 def testBasic(self): |
| 192 bot_update.ensure_checkout(**self.params) |
| 193 return self.call.records |
| 194 |
| 195 def testBasicBuildspec(self): |
| 196 self.params['buildspec'] = bot_update.BUILDSPEC_TYPE( |
| 197 container='branches', |
| 198 version='1.1.1.1' |
| 199 ) |
| 200 bot_update.ensure_checkout(**self.params) |
| 201 return self.call.records |
| 202 |
| 203 def testBasicShallow(self): |
| 204 self.params['shallow'] = True |
| 205 bot_update.ensure_checkout(**self.params) |
| 206 return self.call.records |
| 207 |
| 208 |
| 209 if __name__ == '__main__': |
| 210 unittest.main() |
OLD | NEW |