def testLocking(self): """Make sure lock is deleted after cleanup""" s1 = BobState() with self.assertRaises(BobError): s2 = BobState() s1.finalize() s2 = BobState()
def testPersistence(self): """Smoke test that tings are persisted""" s1 = BobState() s1.setInputHashes("path", b"hash") s1.finalize() s2 = BobState() self.assertEqual(b"hash", s2.getInputHashes("path")) s2.finalize()
def testUncommitted(self): """Uncommitted state must be picked up on next run""" s1 = BobState() s1.setInputHashes("path", b"hash") # simulate hard crash self.unlock() del s1 s2 = BobState() self.assertEqual(b"hash", s2.getInputHashes("path")) s2.finalize()
def testCannotCommit(self): """Unability to commit state is handled gracefully""" os.mkdir("ro") os.chdir("ro") s1 = BobState() s1.setInputHashes("path", b"hash") os.chmod(".", stat.S_IRUSR | stat.S_IXUSR) s1.finalize() os.chmod(".", stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) self.unlock() os.unlink(".bob-state.pickle.new") s2 = BobState() self.assertEqual(None, s2.getInputHashes("path")) s2.finalize()
def testStickyLock(self): """Test that we do not crash if lock file cannot be removed""" os.mkdir("ro") os.chdir("ro") s1 = BobState() os.chmod(".", stat.S_IRUSR | stat.S_IXUSR) s1.finalize()
def testCannotLock(self): """It's ok to be not able to create a lock file""" os.mkdir("ro") os.chmod("ro", stat.S_IRUSR | stat.S_IXUSR) os.chdir("ro") s1 = BobState() self.assertFalse(os.path.exists(".bob-state.lock"))
def testUnreadable(self): """Test that unreadable state is handled gracefully""" self.writeState() os.chmod(".bob-state.pickle", 0) with self.assertRaises(BobError): s1 = BobState()
def testCorrupt(self): """A corrupted uncommitted state must be discarded""" s1 = BobState() s1.setInputHashes("path", b"hash") s1.finalize() s2 = BobState() s2.setInputHashes("path", b"must-be-discarded") # simulate hard crash and corrupt state self.unlock() del s2 with open(".bob-state.pickle.new", "r+b") as f: f.write(b"garbabe") s3 = BobState() self.assertEqual(b"hash", s3.getInputHashes("path")) s3.finalize()
def testCannotSave(self): """Failure to save state aborts""" os.mkdir("ro") os.chdir("ro") self.writeState() with self.assertRaises(BobError): s1 = BobState() os.chmod(".", stat.S_IRUSR | stat.S_IXUSR) s1.setInputHashes("path", b"hash")
def testVanishedLock(self): """Test that we do not crash if lock file vanished""" s1 = BobState() self.unlock() s1.finalize()
def testTooYoung(self): """Too new states must be rejected gracefully""" self.writeState(99) with self.assertRaises(BobError): s1 = BobState()
def testTooOld(self): """Too old states must be rejected gracefully""" self.writeState(1) with self.assertRaises(BobError): s1 = BobState()
def testCorrupted(self): """Test that corrupted state is handled gracefully""" with open(".bob-state.pickle", "wb") as f: f.write(b"garbabe") with self.assertRaises(BobError): s1 = BobState()