def testExists(self): with gs.TemporaryURL('chromite.gslib.exists') as tempuri: self.populateUri(tempuri) self.assertTrue(gslib.Exists(tempuri)) bogus_gs_path = 'gs://chromeos-releases-test/bogus/non-existent-url' self.assertFalse(gslib.Exists(bogus_gs_path))
def testDryrunLock(self): """Ensure that lcok can be obtained and released in dry-run mode.""" with gs.TemporaryURL('gslock') as lock_uri: lock = gslock.Lock(lock_uri, dry_run=True) self.assertIsNone(lock.Acquire()) self.assertFalse(self.ctx.Exists(lock_uri)) self.assertIsNone(lock.Release())
def testCopyAndMD5Sum(self): """Higher-level functional test. Test MD5Sum OK.""" with gs.TemporaryURL('chromite.gslib.md5') as tempuri: local_path = self.populateUri(tempuri) local_md5 = gslib.filelib.MD5Sum(local_path) gs_md5 = gslib.MD5Sum(tempuri) self.assertEqual(gs_md5, local_md5)
def testLockRepetition(self): """Test aquiring same lock multiple times.""" # Force a known host name. self.PatchObject(cros_build_lib, 'MachineDetails', return_value='TestHost') with gs.TemporaryURL('gslock') as lock_uri: lock = gslock.Lock(lock_uri) self.assertFalse(self.ctx.Exists(lock_uri)) lock.Acquire() self.assertTrue(self.ctx.Exists(lock_uri)) lock.Acquire() self.assertTrue(self.ctx.Exists(lock_uri)) lock.Release() self.assertFalse(self.ctx.Exists(lock_uri)) lock.Acquire() self.assertTrue(self.ctx.Exists(lock_uri)) lock.Release() self.assertFalse(self.ctx.Exists(lock_uri))
def testDryrunLockRepetition(self): """Test aquiring same lock multiple times in dry-run mode.""" with gs.TemporaryURL('gslock') as lock_uri: lock = gslock.Lock(lock_uri, dry_run=True) self.assertIsNone(lock.Acquire()) self.assertIsNone(lock.Acquire()) self.assertIsNone(lock.Release()) self.assertIsNone(lock.Acquire()) self.assertIsNone(lock.Release())
def testLockTimeout(self): """Test getting a lock when an old timed out one is present.""" with gs.TemporaryURL('gslock') as lock_uri: # Both locks are always timed out. lock1 = gslock.Lock(lock_uri, lock_timeout_mins=-1) lock2 = gslock.Lock(lock_uri, lock_timeout_mins=-1) lock1.Acquire() lock2.Acquire()
def testRaceToAcquire(self): """Have lots of processes race to acquire the same lock.""" count = self.NUM_THREADS pool = multiprocessing.Pool(processes=count) with gs.TemporaryURL('gslock') as lock_uri: results = pool.map(_InProcessAcquire, [lock_uri] * count) # Clean up the lock since the processes explicitly only acquire. self.ctx.Remove(lock_uri) # Ensure that only one of them got the lock. self.assertEqual(results.count(True), 1)
def testMultiProcessDataUpdate(self): """Have lots of processes update a GS file proctected by a lock.""" count = self.NUM_THREADS pool = multiprocessing.Pool(processes=count) with gs.TemporaryURL('gslock') as lock_uri: data_uri = lock_uri + '.data' results = pool.map(_InProcessDataUpdate, [(lock_uri, data_uri)] * count) self.assertEqual(self.ctx.Cat(data_uri), str(count)) # Ensure that all report success self.assertEqual(results.count(True), count)
def testLock(self): """Test getting a lock.""" # Force a known host name. self.PatchObject(cros_build_lib, 'MachineDetails', return_value='TestHost') with gs.TemporaryURL('gslock') as lock_uri: lock = gslock.Lock(lock_uri) self.assertFalse(self.ctx.Exists(lock_uri)) lock.Acquire() self.assertTrue(self.ctx.Exists(lock_uri)) contents = self.ctx.Cat(lock_uri) self.assertEqual(contents, 'TestHost') lock.Release() self.assertFalse(self.ctx.Exists(lock_uri))
def testLockConflict(self): """Test lock conflict.""" with gs.TemporaryURL('gslock') as lock_uri: lock1 = gslock.Lock(lock_uri) lock2 = gslock.Lock(lock_uri) # Manually lock 1, and ensure lock2 can't lock. lock1.Acquire() self.assertRaises(gslock.LockNotAcquired, lock2.Acquire) lock1.Release() # Use a with clause on 2, and ensure 1 can't lock. with lock2: self.assertRaises(gslock.LockNotAcquired, lock1.Acquire) # Ensure we can renew a given lock. lock1.Acquire() lock1.Renew() lock1.Release() # Ensure we get an error renewing a lock we don't hold. self.assertRaises(gslock.LockNotAcquired, lock1.Renew)