def testCollision_Upload(self): """Test when upload_json tries to update an old version.""" gsutil = FakeGSUtil() build = 42 version = 'v1' success = False generations = [555, 444] orig = gsutil.upload_json def fake_upload(path, cache, generation): if generation == '555': return orig(path, cache, generation=generation) raise subprocess.CalledProcessError(128, ['gsutil'], None) orig_stat = gsutil.stat def fake_stat(*a, **kw): gsutil.generation = generations.pop() return orig_stat(*a, **kw) def fake_call(*a, **kw): return '[{"hello": 111}]' with Stub(bootstrap, 'random_sleep', Pass): with Stub(gsutil, 'stat', fake_stat): with Stub(gsutil, 'upload_json', fake_upload): with Stub(bootstrap, 'call', fake_call): bootstrap.append_result(gsutil, 'fake_path', build, version, success) self.assertIn('generation', gsutil.jsons[-1][1], gsutil.jsons) self.assertEquals('555', gsutil.jsons[-1][1]['generation'], gsutil.jsons)
def Try(success): gsutil = FakeGSUtil() with Stub(bootstrap, 'call', lambda *a, **kw: ''): bootstrap.append_result(gsutil, 'fake_path', build, version, success) cache = gsutil.jsons[0][0][1] self.assertTrue(isinstance(cache[0]['passed'], bool))
def testCollision_Upload(self): """Test when upload_json tries to update an old version.""" gsutil = FakeGSUtil() build = 42 version = 'v1' success = False generations = [555, 444] orig = gsutil.upload_json def fake_upload(path, cache, generation): if generation == '555': return orig(path, cache, generation=generation) raise subprocess.CalledProcessError(128, ['gsutil'], None) orig_stat = gsutil.stat def fake_stat(*a, **kw): gsutil.generation = generations.pop() return orig_stat(*a, **kw) def fake_call(*a, **kw): return '[{"hello": 111}]' with Stub(bootstrap, 'random_sleep', Pass): with Stub(gsutil, 'stat', fake_stat): with Stub(gsutil, 'upload_json', fake_upload): with Stub(bootstrap, 'call', fake_call): bootstrap.append_result( gsutil, 'fake_path', build, version, success) self.assertIn('generation', gsutil.jsons[-1][1], gsutil.jsons) self.assertEquals('555', gsutil.jsons[-1][1]['generation'], gsutil.jsons)
def testCollision_Cat(self): """cat fails if the cache has been updated.""" gsutil = FakeGSUtil() build = 42 version = 'v1' success = False generations = ['555', '444'] orig_stat = gsutil.stat def fake_stat(*a, **kw): gsutil.generation = generations.pop() return orig_stat(*a, **kw) def fake_cat(_, gen): if gen == '555': # Which version is requested? return '[{"hello": 111}]' raise subprocess.CalledProcessError(1, ['gsutil'], None) with Stub(bootstrap, 'random_sleep', Pass): with Stub(gsutil, 'stat', fake_stat): with Stub(gsutil, 'cat', fake_cat): bootstrap.append_result(gsutil, 'fake_path', build, version, success) self.assertIn('generation', gsutil.jsons[-1][1], gsutil.jsons) self.assertEquals('555', gsutil.jsons[-1][1]['generation'], gsutil.jsons)
def testTruncate(self): old = json.dumps({n: True for n in range(100000)}) gsutil = FakeGSUtil() build = 123 version = 'v.interesting' success = True with Stub(bootstrap, 'call', lambda *a, **kw: old): bootstrap.append_result(gsutil, 'fake_path', build, version, success) cache = gsutil.jsons[0][0][1] self.assertLess(len(cache), len(old))
def testHandleJunk(self): gsutil = FakeGSUtil() build = 123 version = 'v.interesting' success = True with Stub(bootstrap, 'call', lambda *a, **kw: '!@!$!@$@!$'): bootstrap.append_result(gsutil, 'fake_path', build, version, success) cache = gsutil.jsons[0][0][1] self.assertEquals(1, len(cache)) self.assertIn(build, cache[0].values()) self.assertIn(version, cache[0].values())
def testNewJob(self): """Stat fails when the job doesn't exist.""" gsutil = FakeGSUtil() build = 123 version = 'v.interesting' success = True def fake_stat(*a, **kw): raise subprocess.CalledProcessError(1, ['gsutil'], None) gsutil.stat = fake_stat bootstrap.append_result(gsutil, 'fake_path', build, version, success) cache = gsutil.jsons[0][0][1] self.assertEquals(1, len(cache))
def testCollision_Cat(self): """cat fails if the cache has been updated.""" gsutil = FakeGSUtil() build = 42 version = 'v1' success = False generations = ['555', '444'] orig_stat = gsutil.stat def fake_stat(*a, **kw): gsutil.generation = generations.pop() return orig_stat(*a, **kw) def fake_cat(_, gen): if gen == '555': # Which version is requested? return '[{"hello": 111}]' raise subprocess.CalledProcessError(1, ['gsutil'], None) with Stub(bootstrap, 'random_sleep', Pass): with Stub(gsutil, 'stat', fake_stat): with Stub(gsutil, 'cat', fake_cat): bootstrap.append_result( gsutil, 'fake_path', build, version, success) self.assertIn('generation', gsutil.jsons[-1][1], gsutil.jsons) self.assertEquals('555', gsutil.jsons[-1][1]['generation'], gsutil.jsons)
def Try(success): gsutil = FakeGSUtil() with Stub(bootstrap, 'call', lambda *a, **kw: ''): bootstrap.append_result(gsutil, 'fake_path', build, version, success) cache = gsutil.jsons[0][0][1] self.assertTrue(isinstance(cache[0]['passed'], bool))