summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-03-17 21:42:22 +0000
committerLars Wirzenius <liw@liw.fi>2012-03-17 21:42:22 +0000
commit34d44ccb4e5d36b89daea5553c3770af7e28d677 (patch)
tree0fa1be38307572d6b2673fddb51eeebc32d86bbe
parent3056fd0661225bdc433d8e940a836f7699be7351 (diff)
downloadobnam-34d44ccb4e5d36b89daea5553c3770af7e28d677.tar.gz
Add optional crashing to LocalFS
This makes perfect sense, for crash testing.
-rw-r--r--obnamlib/vfs_local.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/obnamlib/vfs_local.py b/obnamlib/vfs_local.py
index 00ca1f86..c7225fac 100644
--- a/obnamlib/vfs_local.py
+++ b/obnamlib/vfs_local.py
@@ -57,6 +57,19 @@ class LocalFS(obnamlib.VirtualFileSystem):
obnamlib.VirtualFileSystem.__init__(self, baseurl)
self.reinit(baseurl, create=create)
+ # For testing purposes, allow setting a limit on write operations
+ # after which an exception gets raised. If set to None, no crash.
+ # The write_file and overwrite_file methods obey this.
+ self.crash_limit = None
+ self.crash_counter = 0
+
+ def maybe_crash(self): # pragma: no cover
+ if self.crash_limit is not None:
+ self.crash_counter += 1
+ if self.crash_counter >= self.crash_limit:
+ raise Exception('Crashing as requested after %d writes' %
+ self.crash_counter)
+
def reinit(self, baseurl, create=False):
# We fake chdir so that it doesn't mess with the caller's
# perception of current working directory. This also benefits
@@ -256,6 +269,7 @@ class LocalFS(obnamlib.VirtualFileSystem):
os.remove(tempname)
raise
os.remove(tempname)
+ self.maybe_crash()
def overwrite_file(self, pathname, contents, make_backup=True):
tracing.trace('overwrite_file %s', pathname)
@@ -279,6 +293,8 @@ class LocalFS(obnamlib.VirtualFileSystem):
os.remove(bak)
except OSError:
pass
+
+ self.maybe_crash()
def _write_to_tempfile(self, pathname, contents):
path = self.join(pathname)