sapling/eden/integration/rename_test.py
Wez Furlong 4235784907 add .eden "magic" dir
Summary:
It's not really magic because we don't have a virtual directory
inode base any more.  Instead, we mkdir and populate it at mount time.

What is slightly magical about it is that we give it some special powers:

* We know the inode number of the eden dir and prevent unlink operations
  on it or inside it.
* The .eden dir is present in the contents of the root inode and will
  show up when that directory is `readdir`'d
* When resolving a child of a TreeInode by name, we know to return the
  magic `.eden` inode number.  This means that it is possible to `stat`
  and consume the `.eden` directory from any directory inside the eden
  mount, even though it won't show up in `readdir` for those child dirs.

The contents of the `.eden` dir are:

* `socket` - a symlink back to the unix domain socket that our thrift
  server is listening on.  This means that it is a simple
  `readlink(".eden/socket")` operation to discover both whether a directory
  is part of an eden mount and how to talk to the server.

* `root` - a symlink back to the root of this eden mount.  This allows
  using `readlink(".eden/root")` as a simple 1-step operation to find
  the root of an eden mount, and avoids needing to walk up directory
  by directory as is the common pattern for locating `.hg` or `.git`
  dirs.

Reviewed By: simpkins

Differential Revision: D4637285

fbshipit-source-id: 0eabf98b29144acccef5c83bd367493399dc55bb
2017-03-24 23:07:42 -07:00

158 lines
5.8 KiB
Python

#!/usr/bin/env python3
#
# Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
from .lib import testcase
import errno
import os
@testcase.eden_repo_test
class RenameTest:
def populate_repo(self):
self.repo.write_file('hello', 'hola\n')
self.repo.write_file('adir/file', 'foo!\n')
self.repo.symlink('slink', 'hello')
self.repo.commit('Initial commit.')
def test_rename_errors(self):
''' Test some error cases '''
with self.assertRaises(OSError) as context:
os.rename(os.path.join(self.mount, 'not-exist'),
os.path.join(self.mount, 'also-not-exist'))
self.assertEqual(errno.ENOENT, context.exception.errno,
msg='Renaming a bogus file -> ENOENT')
def test_rename_dir(self):
filename = os.path.join(self.mount, 'adir')
targetname = os.path.join(self.mount, 'a-new-target')
os.rename(filename, targetname)
with self.assertRaises(OSError) as context:
os.lstat(filename)
self.assertEqual(errno.ENOENT, context.exception.errno,
msg='no longer visible as old name')
os.lstat(targetname)
# Check that adir/file is now visible in the new location
targetfile = os.path.join(targetname, 'file')
with open(targetfile, 'r') as f:
self.assertEqual('foo!\n', f.read())
def test_rename_away_tree_entry(self):
''' Rename a tree entry away and back again '''
# We should be able to rename files that are in the Tree
hello = os.path.join(self.mount, 'hello')
targetname = os.path.join(self.mount, 'a-new-target')
os.rename(hello, targetname)
with self.assertRaises(OSError) as context:
os.lstat(hello)
self.assertEqual(errno.ENOENT, context.exception.errno,
msg='no longer visible as old name')
entries = sorted(os.listdir(self.mount))
self.assertEqual(['.eden', 'a-new-target', 'adir', 'slink'], entries)
with open(targetname, 'r') as f:
self.assertEqual('hola\n', f.read(),
msg='materialized correct data')
# Now, while we hold this file open, check that a rename
# leaves the handle connected to the file contents when
# we rename it back to its old name.
os.rename(targetname, hello)
entries = sorted(os.listdir(self.mount))
self.assertEqual(['.eden', 'adir', 'hello', 'slink'], entries)
with open(hello, 'r+') as write_f:
write_f.seek(0, os.SEEK_END)
write_f.write('woot')
f.seek(0)
self.assertEqual('hola\nwoot', f.read())
def test_rename_overlay_only(self):
''' Create a local/overlay only file and rename it '''
# We should be able to rename files that are in the Tree
from_name = os.path.join(self.mount, 'overlay-a')
to_name = os.path.join(self.mount, 'overlay-b')
with open(from_name, 'w') as f:
f.write('overlay-a\n')
os.rename(from_name, to_name)
with self.assertRaises(OSError) as context:
os.lstat(from_name)
self.assertEqual(errno.ENOENT, context.exception.errno,
msg='no longer visible as old name')
entries = sorted(os.listdir(self.mount))
self.assertEqual(['.eden', 'adir', 'hello', 'overlay-b', 'slink'], entries)
with open(to_name, 'r') as f:
self.assertEqual('overlay-a\n', f.read(),
msg='holds correct data')
# Now, while we hold this file open, check that a rename
# leaves the handle connected to the file contents when
# we rename it back to its old name.
os.rename(to_name, from_name)
entries = sorted(os.listdir(self.mount))
self.assertEqual(['.eden', 'adir', 'hello', 'overlay-a', 'slink'], entries)
with open(from_name, 'r+') as write_f:
write_f.seek(0, os.SEEK_END)
write_f.write('woot')
f.seek(0)
self.assertEqual('overlay-a\nwoot', f.read())
def test_rename_overlay_over_tree(self):
''' Make an overlay file and overwrite a tree entry with it '''
from_name = os.path.join(self.mount, 'overlay-a')
to_name = os.path.join(self.mount, 'hello')
with open(from_name, 'w') as f:
f.write('overlay-a\n')
os.rename(from_name, to_name)
with self.assertRaises(OSError) as context:
os.lstat(from_name)
self.assertEqual(errno.ENOENT, context.exception.errno,
msg='no longer visible as old name')
entries = sorted(os.listdir(self.mount))
self.assertEqual(['.eden', 'adir', 'hello', 'slink'], entries)
with open(to_name, 'r') as f:
self.assertEqual('overlay-a\n', f.read(),
msg='holds correct data')
def test_rename_between_different_dirs(self):
adir = os.path.join(self.mount, 'adir')
bdir = os.path.join(self.mount, 'bdir')
os.mkdir(bdir)
os.rename(os.path.join(adir, 'file'), os.path.join(bdir, 'FILE'))
self.assertEqual([], sorted(os.listdir(adir)))
self.assertEqual(['FILE'], sorted(os.listdir(bdir)))
# Restart to ensure that our serialized state is correct
self.eden.shutdown()
self.eden.start()
self.assertEqual([], sorted(os.listdir(adir)))
self.assertEqual(['FILE'], sorted(os.listdir(bdir)))