Back to index

obnam  1.1
Classes | Functions | Variables
obnamlib.vfs Namespace Reference

Classes

class  VirtualFileSystem
class  VfsFactory
class  VfsTests

Functions

def test_abspath_returns_input_for_absolute_path
def test_abspath_returns_absolute_path_for_relative_input
def test_abspath_normalizes_path
def test_abspath_returns_plain_string
def test_reinit_works
def test_reinit_to_nonexistent_filename_raises_OSError
def test_reinit_creates_target_if_requested
def test_getcwd_returns_dirname
def test_getcwd_returns_plain_string
def test_chdir_changes_only_fs_cwd_not_process_cwd
def test_chdir_to_nonexistent_raises_exception
def test_chdir_to_relative_works
def test_chdir_to_dotdot_works
def test_creates_lock_file
def test_second_lock_fails
def test_unlock_removes_lock
def test_exists_returns_false_for_nonexistent_file
def test_exists_returns_true_for_existing_file
def test_isdir_returns_false_for_nonexistent_file
def test_isdir_returns_false_for_nondir
def test_isdir_returns_true_for_existing_dir
def test_listdir_returns_plain_strings_only
def test_listdir_raises_oserror_if_directory_does_not_exist
def test_listdir2_returns_name_stat_pairs
def test_listdir2_returns_plain_strings_only
def test_listdir2_raises_oserror_if_directory_does_not_exist
def test_mknod_creates_fifo
def test_mkdir_raises_oserror_if_directory_exists
def test_mkdir_raises_oserror_if_parent_does_not_exist
def test_makedirs_raises_oserror_when_directory_exists
def test_makedirs_creates_directory_when_parent_exists
def test_makedirs_creates_directory_when_parent_does_not_exist
def test_rmdir_removes_directory
def test_rmdir_raises_oserror_if_directory_does_not_exist
def test_rmdir_raises_oserror_if_directory_is_not_empty
def test_rmtree_removes_directory_tree
def test_rmtree_is_silent_when_target_does_not_exist
def test_remove_removes_file
def test_remove_raises_oserror_if_file_does_not_exist
def test_rename_renames_file
def test_rename_raises_oserror_if_file_does_not_exist
def test_rename_works_if_target_exists
def test_lstat_returns_result_with_all_required_fields
def test_lstat_returns_right_filetype_for_directory
def test_lstat_raises_oserror_for_nonexistent_entry
def test_chmod_sets_permissions_correctly
def test_chmod_raises_oserror_for_nonexistent_entry
def test_lutimes_sets_times_correctly
def test_lutimes_raises_oserror_for_nonexistent_entry
def test_link_creates_hard_link
def test_symlink_creates_soft_link
def test_readlink_returns_plain_string
def test_symlink_raises_oserror_if_name_exists
def test_opens_existing_file_ok_for_reading
def test_opens_existing_file_ok_for_writing
def test_open_fails_for_nonexistent_file
def test_cat_reads_existing_file_ok
def test_cat_fails_for_nonexistent_file
def test_has_read_nothing_initially
def test_cat_updates_bytes_read
def test_write_fails_if_file_exists_already
def test_write_creates_missing_directories
def test_write_leaves_existing_file_intact
def test_overwrite_creates_new_file_ok
def test_overwrite_replaces_existing_file
def test_has_written_nothing_initially
def test_write_updates_written
def test_overwrite_updates_written
def set_up_scan_tree
def test_scan_tree_returns_nothing_if_listdir_fails
def test_scan_tree_returns_the_right_stuff
def test_scan_tree_filters_away_unwanted

Variables

string non_ascii_name = u'm\u00e4kel\u00e4'
 dirs
 pathnames

Class Documentation

class obnamlib::vfs::VfsTests

Definition at line 282 of file vfs.py.


Function Documentation

Definition at line 626 of file vfs.py.

00626 
00627     def set_up_scan_tree(self):
00628         self.dirs = ['foo', 'foo/bar', 'foobar']
00629         self.dirs = [os.path.join(self.basepath, x) for x in self.dirs]
00630         for dirname in self.dirs:
00631             self.fs.mkdir(dirname)
00632         self.dirs.insert(0, self.basepath)
00633         self.fs.symlink('foo', 'symfoo')
00634         self.pathnames = self.dirs + [os.path.join(self.basepath, 'symfoo')]

Definition at line 318 of file vfs.py.

00318 
00319     def test_abspath_normalizes_path(self):
00320         self.assertEqual(self.fs.abspath('foo/..'), self.basepath)

Definition at line 314 of file vfs.py.

00314 
00315     def test_abspath_returns_absolute_path_for_relative_input(self):
00316         self.assertEqual(self.fs.abspath('foo'),
00317                          os.path.join(self.basepath, 'foo'))

Definition at line 311 of file vfs.py.

00311 
00312     def test_abspath_returns_input_for_absolute_path(self):
00313         self.assertEqual(self.fs.abspath('/foo/bar'), '/foo/bar')

Definition at line 321 of file vfs.py.

00321 
00322     def test_abspath_returns_plain_string(self):
00323         self.fs.mkdir(self.non_ascii_name)
00324         self.fs.chdir(self.non_ascii_name)
00325         self.assertEqual(type(self.fs.abspath('.')), str)

Definition at line 579 of file vfs.py.

00579 
00580     def test_cat_fails_for_nonexistent_file(self):
00581         self.assertRaises(IOError, self.fs.cat, 'foo')
    

Definition at line 575 of file vfs.py.

00575 
00576     def test_cat_reads_existing_file_ok(self):
00577         self.fs.write_file('foo', 'bar')
00578         self.assertEqual(self.fs.cat('foo'), 'bar')

Definition at line 585 of file vfs.py.

00585 
00586     def test_cat_updates_bytes_read(self):
00587         self.fs.write_file('foo', 'bar')
00588         self.fs.cat('foo')
00589         self.assertEqual(self.fs.bytes_read, 3)

Definition at line 350 of file vfs.py.

00350 
00351     def test_chdir_changes_only_fs_cwd_not_process_cwd(self):
00352         process_cwd = os.getcwd()
00353         self.fs.chdir('/')
00354         self.assertEqual(self.fs.getcwd(), '/')
00355         self.assertEqual(os.getcwd(), process_cwd)

Definition at line 365 of file vfs.py.

00365 
00366     def test_chdir_to_dotdot_works(self):
00367         pathname = os.path.join(self.basepath, 'foo')
00368         os.mkdir(pathname)
00369         self.fs.chdir('foo')
00370         self.fs.chdir('..')
00371         self.assertEqual(self.fs.getcwd(), self.basepath)

Definition at line 356 of file vfs.py.

00356 
00357     def test_chdir_to_nonexistent_raises_exception(self):
00358         self.assertRaises(OSError, self.fs.chdir, '/foobar')

Definition at line 359 of file vfs.py.

00359 
00360     def test_chdir_to_relative_works(self):
00361         pathname = os.path.join(self.basepath, 'foo')
00362         os.mkdir(pathname)
00363         self.fs.chdir('foo')
00364         self.assertEqual(self.fs.getcwd(), pathname)

Definition at line 522 of file vfs.py.

00522 
00523     def test_chmod_raises_oserror_for_nonexistent_entry(self):
00524         self.assertRaises(OSError, self.fs.chmod, 'notexists', 0)

Definition at line 517 of file vfs.py.

00517 
00518     def test_chmod_sets_permissions_correctly(self):
00519         self.fs.mkdir('foo')
00520         self.fs.chmod('foo', 0777)
00521         self.assertEqual(self.fs.lstat('foo').st_mode & 0777, 0777)

Definition at line 372 of file vfs.py.

00372 
00373     def test_creates_lock_file(self):
00374         self.fs.lock('lock', 'lock data')
00375         self.assertTrue(self.fs.exists('lock'))
00376         self.assertEqual(self.fs.cat('lock'), 'lock data')

Definition at line 387 of file vfs.py.

00387 
00388     def test_exists_returns_false_for_nonexistent_file(self):
00389         self.assertFalse(self.fs.exists('foo'))

Definition at line 390 of file vfs.py.

00390 
00391     def test_exists_returns_true_for_existing_file(self):
00392         self.fs.write_file('foo', '')
00393         self.assert_(self.fs.exists('foo'))

Definition at line 342 of file vfs.py.

00342 
00343     def test_getcwd_returns_dirname(self):
00344         self.assertEqual(self.fs.getcwd(), self.basepath)

Definition at line 345 of file vfs.py.

00345 
00346     def test_getcwd_returns_plain_string(self):
00347         self.fs.mkdir(self.non_ascii_name)
00348         self.fs.chdir(self.non_ascii_name)
00349         self.assertEqual(type(self.fs.getcwd()), str)

Definition at line 582 of file vfs.py.

00582 
00583     def test_has_read_nothing_initially(self):
00584         self.assertEqual(self.fs.bytes_read, 0)
    

Definition at line 615 of file vfs.py.

00615 
00616     def test_has_written_nothing_initially(self):
00617         self.assertEqual(self.fs.bytes_written, 0)
    

Definition at line 397 of file vfs.py.

00397 
00398     def test_isdir_returns_false_for_nondir(self):
00399         self.fs.write_file('foo', '')
00400         self.assertFalse(self.fs.isdir('foo'))

Definition at line 394 of file vfs.py.

00394 
00395     def test_isdir_returns_false_for_nonexistent_file(self):
00396         self.assertFalse(self.fs.isdir('foo'))

Definition at line 401 of file vfs.py.

00401 
00402     def test_isdir_returns_true_for_existing_dir(self):
00403         self.fs.mkdir('foo')
00404         self.assert_(self.fs.isdir('foo'))

Definition at line 542 of file vfs.py.

00542 
00543     def test_link_creates_hard_link(self):
00544         self.fs.write_file('foo', 'foo')
00545         self.fs.link('foo', 'bar')
00546         st1 = self.fs.lstat('foo')
00547         st2 = self.fs.lstat('bar')
00548         self.assertEqual(st1, st2)

Definition at line 434 of file vfs.py.

00434 
00435     def test_listdir2_raises_oserror_if_directory_does_not_exist(self):
00436         self.assertRaises(OSError, self.fs.listdir2, 'foo')

Definition at line 414 of file vfs.py.

00414 
00415     def test_listdir2_returns_name_stat_pairs(self):
00416         funny = u'M\u00E4kel\u00E4'.encode('utf-8')
00417         self.fs.write_file(funny, 'data')
00418         pairs = self.fs.listdir2('.')
00419         self.assertEqual(len(pairs), 1)
00420         self.assertEqual(len(pairs[0]), 2)
00421         name, st = pairs[0]
00422         self.assertEqual(type(name), str)
00423         self.assertEqual(name, funny)
00424         self.assert_(hasattr(st, 'st_mode'))
00425         self.assertFalse(hasattr(st, 'st_mtime'))
00426         self.assert_(hasattr(st, 'st_mtime_sec'))
00427         self.assert_(hasattr(st, 'st_mtime_nsec'))

Definition at line 428 of file vfs.py.

00428 
00429     def test_listdir2_returns_plain_strings_only(self):
00430         self.fs.write_file(u'M\u00E4kel\u00E4'.encode('utf-8'), 'data')
00431         names = [name for name, st in self.fs.listdir2('.')]
00432         types = [type(x) for x in names]
00433         self.assertEqual(types, [str])

Definition at line 411 of file vfs.py.

00411 
00412     def test_listdir_raises_oserror_if_directory_does_not_exist(self):
00413         self.assertRaises(OSError, self.fs.listdir, 'foo')

Definition at line 405 of file vfs.py.

00405 
00406     def test_listdir_returns_plain_strings_only(self):
00407         self.fs.write_file(u'M\u00E4kel\u00E4'.encode('utf-8'), 'data')
00408         names = self.fs.listdir('.')
00409         types = [type(x) for x in names]
00410         self.assertEqual(types, [str])

Definition at line 514 of file vfs.py.

00514 
00515     def test_lstat_raises_oserror_for_nonexistent_entry(self):
00516         self.assertRaises(OSError, self.fs.lstat, 'notexists')

Definition at line 504 of file vfs.py.

00504 
00505     def test_lstat_returns_result_with_all_required_fields(self):
00506         st = self.fs.lstat('.')
00507         for field in obnamlib.metadata_fields:
00508             if field.startswith('st_'):
00509                 self.assert_(hasattr(st, field), 'stat must return %s' % field)

Definition at line 510 of file vfs.py.

00510 
00511     def test_lstat_returns_right_filetype_for_directory(self):
00512         st = self.fs.lstat('.')
00513         self.assert_(stat.S_ISDIR(st.st_mode))

Definition at line 539 of file vfs.py.

00539 
00540     def test_lutimes_raises_oserror_for_nonexistent_entry(self):
00541         self.assertRaises(OSError, self.fs.lutimes, 'notexists', 1, 2, 3, 4)

Definition at line 525 of file vfs.py.

00525 
00526     def test_lutimes_sets_times_correctly(self):
00527         self.fs.mkdir('foo')
00528 
00529         self.fs.lutimes('foo', 1, 2*1000, 3, 4*1000)
00530 
00531         self.assertEqual(self.fs.lstat('foo').st_atime_sec, 1)
00532         # not all filesystems support sub-second timestamps; those that 
00533         # do not, return 0, so we have to accept either that or the correct
00534         # value, but no other vlaues
00535         self.assert_(self.fs.lstat('foo').st_atime_nsec in [0, 2*1000])
00536 
00537         self.assertEqual(self.fs.lstat('foo').st_mtime_sec, 3)
00538         self.assert_(self.fs.lstat('foo').st_mtime_nsec in [0, 4*1000])

Definition at line 455 of file vfs.py.

00455 
00456     def test_makedirs_creates_directory_when_parent_does_not_exist(self):
00457         self.fs.makedirs('foo/bar')
00458         self.assert_(self.fs.isdir('foo/bar'))

Definition at line 451 of file vfs.py.

00451 
00452     def test_makedirs_creates_directory_when_parent_exists(self):
00453         self.fs.makedirs('foo')
00454         self.assert_(self.fs.isdir('foo'))
    

Definition at line 447 of file vfs.py.

00447 
00448     def test_makedirs_raises_oserror_when_directory_exists(self):
00449         self.fs.mkdir('foo')
00450         self.assertRaises(OSError, self.fs.makedirs, 'foo')
    

Definition at line 441 of file vfs.py.

00441 
00442     def test_mkdir_raises_oserror_if_directory_exists(self):
00443         self.assertRaises(OSError, self.fs.mkdir, '.')

Definition at line 444 of file vfs.py.

00444 
00445     def test_mkdir_raises_oserror_if_parent_does_not_exist(self):
00446         self.assertRaises(OSError, self.fs.mkdir, 'foo/bar')
    

Definition at line 437 of file vfs.py.

00437 
00438     def test_mknod_creates_fifo(self):
00439         self.fs.mknod('foo', 0600 | stat.S_IFIFO)
00440         self.assertEqual(self.fs.lstat('foo').st_mode, 0600 | stat.S_IFIFO)

Definition at line 572 of file vfs.py.

00572 
00573     def test_open_fails_for_nonexistent_file(self):
00574         self.assertRaises(IOError, self.fs.open, 'foo', 'r')

Definition at line 564 of file vfs.py.

00564 
00565     def test_opens_existing_file_ok_for_reading(self):
00566         self.fs.write_file('foo', '')
00567         self.assert_(self.fs.open('foo', 'r'))

Definition at line 568 of file vfs.py.

00568 
00569     def test_opens_existing_file_ok_for_writing(self):
00570         self.fs.write_file('foo', '')
00571         self.assert_(self.fs.open('foo', 'w'))

Definition at line 606 of file vfs.py.

00606 
00607     def test_overwrite_creates_new_file_ok(self):
00608         self.fs.overwrite_file('foo', 'bar')
00609         self.assertEqual(self.fs.cat('foo'), 'bar')

Definition at line 610 of file vfs.py.

00610 
00611     def test_overwrite_replaces_existing_file(self):
00612         self.fs.write_file('foo', 'bar')
00613         self.fs.overwrite_file('foo', 'foobar')
00614         self.assertEqual(self.fs.cat('foo'), 'foobar')
    

Definition at line 622 of file vfs.py.

00622 
00623     def test_overwrite_updates_written(self):
00624         self.fs.overwrite_file('foo', 'foo')
00625         self.assertEqual(self.fs.bytes_written, 3)

Definition at line 554 of file vfs.py.

00554 
00555     def test_readlink_returns_plain_string(self):
00556         self.fs.symlink(self.non_ascii_name, self.non_ascii_name)
00557         target = self.fs.readlink(self.non_ascii_name)
00558         self.assertEqual(target, self.non_ascii_name)
00559         self.assertEqual(type(target), str)

Definition at line 335 of file vfs.py.

00335 
00336     def test_reinit_creates_target_if_requested(self):
00337         self.fs.chdir('/')
00338         new_baseurl = os.path.join(self.fs.baseurl, 'newdir')
00339         new_basepath = os.path.join(self.basepath, 'newdir')
00340         self.fs.reinit(new_baseurl, create=True)
00341         self.assertEqual(self.fs.getcwd(), new_basepath)

Definition at line 331 of file vfs.py.

00331 
00332     def test_reinit_to_nonexistent_filename_raises_OSError(self):
00333         notexist = os.path.join(self.fs.baseurl, 'thisdoesnotexist')
00334         self.assertRaises(OSError, self.fs.reinit, notexist)

Definition at line 326 of file vfs.py.

00326 
00327     def test_reinit_works(self):
00328         self.fs.chdir('/')
00329         self.fs.reinit(self.fs.baseurl)
00330         self.assertEqual(self.fs.getcwd(), self.basepath)

Definition at line 486 of file vfs.py.

00486 
00487     def test_remove_raises_oserror_if_file_does_not_exist(self):
00488         self.assertRaises(OSError, self.fs.remove, 'foo')

Definition at line 481 of file vfs.py.

00481 
00482     def test_remove_removes_file(self):
00483         self.fs.write_file('foo', '')
00484         self.fs.remove('foo')
00485         self.assertFalse(self.fs.exists('foo'))

Definition at line 495 of file vfs.py.

00495 
00496     def test_rename_raises_oserror_if_file_does_not_exist(self):
00497         self.assertRaises(OSError, self.fs.rename, 'foo', 'bar')

Definition at line 489 of file vfs.py.

00489 
00490     def test_rename_renames_file(self):
00491         self.fs.write_file('foo', 'xxx')
00492         self.fs.rename('foo', 'bar')
00493         self.assertFalse(self.fs.exists('foo'))
00494         self.assertEqual(self.fs.cat('bar'), 'xxx')

Definition at line 498 of file vfs.py.

00498 
00499     def test_rename_works_if_target_exists(self):
00500         self.fs.write_file('foo', 'foo')
00501         self.fs.write_file('bar', 'bar')
00502         self.fs.rename('foo', 'bar')
00503         self.assertEqual(self.fs.cat('bar'), 'foo')

Definition at line 464 of file vfs.py.

00464 
00465     def test_rmdir_raises_oserror_if_directory_does_not_exist(self):
00466         self.assertRaises(OSError, self.fs.rmdir, 'foo')

Definition at line 467 of file vfs.py.

00467 
00468     def test_rmdir_raises_oserror_if_directory_is_not_empty(self):
00469         self.fs.mkdir('foo')
00470         self.fs.write_file('foo/bar', '')
00471         self.assertRaises(OSError, self.fs.rmdir, 'foo')

Definition at line 459 of file vfs.py.

00459 
00460     def test_rmdir_removes_directory(self):
00461         self.fs.mkdir('foo')
00462         self.fs.rmdir('foo')
00463         self.assertFalse(self.fs.exists('foo'))

Definition at line 478 of file vfs.py.

00478 
00479     def test_rmtree_is_silent_when_target_does_not_exist(self):
00480         self.assertEqual(self.fs.rmtree('foo'), None)

Definition at line 472 of file vfs.py.

00472 
00473     def test_rmtree_removes_directory_tree(self):
00474         self.fs.mkdir('foo')
00475         self.fs.write_file('foo/bar', '')
00476         self.fs.rmtree('foo')
00477         self.assertFalse(self.fs.exists('foo'))

Definition at line 653 of file vfs.py.

00653 
00654     def test_scan_tree_filters_away_unwanted(self):
00655         def ok(pathname, st):
00656             return stat.S_ISDIR(st.st_mode)
00657         self.set_up_scan_tree()
00658         result = list(self.fs.scan_tree(self.basepath, ok=ok))
00659         pathnames = [pathname for pathname, st in result]
00660         self.assertEqual(sorted(pathnames), sorted(self.dirs))
00661 

Definition at line 635 of file vfs.py.

00635 
00636     def test_scan_tree_returns_nothing_if_listdir_fails(self):
00637         self.set_up_scan_tree()
00638         def raiser(dirname):
00639             raise OSError((123, 'oops', dirname))
00640         def logerror(msg):
00641             pass
00642         self.fs.listdir2 = raiser
00643         result = list(self.fs.scan_tree(self.basepath, log=logerror))
00644         self.assertEqual(len(result), 1)
00645         pathname, st = result[0]
00646         self.assertEqual(pathname, self.basepath)

Definition at line 647 of file vfs.py.

00647 
00648     def test_scan_tree_returns_the_right_stuff(self):
00649         self.set_up_scan_tree()
00650         result = list(self.fs.scan_tree(self.basepath))
00651         pathnames = [pathname for pathname, st in result]
00652         self.assertEqual(sorted(pathnames), sorted(self.pathnames))
    

Definition at line 377 of file vfs.py.

00377 
00378     def test_second_lock_fails(self):
00379         self.fs.lock('lock', 'lock data')
00380         self.assertRaises(Exception, self.fs.lock, 'lock', 'second lock')
00381         self.assertEqual(self.fs.cat('lock'), 'lock data')

Definition at line 549 of file vfs.py.

00549 
00550     def test_symlink_creates_soft_link(self):
00551         self.fs.symlink('foo', 'bar')
00552         target = self.fs.readlink('bar')
00553         self.assertEqual(target, 'foo')

Definition at line 560 of file vfs.py.

00560 
00561     def test_symlink_raises_oserror_if_name_exists(self):
00562         self.fs.write_file('foo', 'foo')
00563         self.assertRaises(OSError, self.fs.symlink, 'bar', 'foo')

Definition at line 382 of file vfs.py.

00382 
00383     def test_unlock_removes_lock(self):
00384         self.fs.lock('lock', 'lock data')
00385         self.fs.unlock('lock')
00386         self.assertFalse(self.fs.exists('lock'))

Definition at line 594 of file vfs.py.

00594 
00595     def test_write_creates_missing_directories(self):
00596         self.fs.write_file('foo/bar', 'yo')
00597         self.assertEqual(self.fs.cat('foo/bar'), 'yo')

Definition at line 590 of file vfs.py.

00590 
00591     def test_write_fails_if_file_exists_already(self):
00592         self.fs.write_file('foo', 'bar')
00593         self.assertRaises(OSError, self.fs.write_file, 'foo', 'foobar')

Definition at line 598 of file vfs.py.

00598 
00599     def test_write_leaves_existing_file_intact(self):
00600         self.fs.write_file('foo', 'bar')
00601         try:
00602             self.fs.write_file('foo', 'foobar')
00603         except OSError:
00604             pass
00605         self.assertEqual(self.fs.cat('foo'), 'bar')

Definition at line 618 of file vfs.py.

00618 
00619     def test_write_updates_written(self):
00620         self.fs.write_file('foo', 'foo')
00621         self.assertEqual(self.fs.bytes_written, 3)
    

Variable Documentation

Definition at line 627 of file vfs.py.

string obnamlib.vfs.non_ascii_name = u'm\u00e4kel\u00e4'
Re-useable tests for VirtualFileSystem implementations.

The base class can't be usefully instantiated itself.
Instead you are supposed to sub-class it and implement the API in
a suitable way for yourself.

This class implements a number of tests that the API implementation
must pass. The implementation's own test class should inherit from
this class, and unittest.TestCase.

The test sub-class should define a setUp method that sets the following:

* self.fs to an instance of the API implementation sub-class
* self.basepath to the path to the base of the filesystem

basepath must be operable as a pathname using os.path tools. If
the VFS implemenation operates remotely and wants to operate on a
URL like 'http://domain/path' as the baseurl, then basepath must be
just the path portion of the URL.

The directory indicated by basepath must exist, but must be empty
at start.

Definition at line 309 of file vfs.py.

Definition at line 633 of file vfs.py.