def setUp(self): frontend = InMemoryFrontend() self.factory = frontend.getLaunchpadObjectFactory() codehosting_api = frontend.getCodehostingEndpoint() self.requester = self.factory.makePerson() self.backing_transport = MemoryTransport() self.server = self.getServer(codehosting_api, self.requester.id, self.backing_transport) self.server.start_server() self.addCleanup(self.server.stop_server)
def test_iter_files_recursive(self): transport = MemoryTransport() transport.mkdir('dir') transport.put_bytes('dir/foo', 'content') transport.put_bytes('dir/bar', 'content') transport.put_bytes('bar', 'content') paths = set(transport.iter_files_recursive()) self.assertEqual(set(['dir/foo', 'dir/bar', 'bar']), paths)
def test_openDirectoryMemory(self): """openDirectory works on MemoryTransport.""" transport = MemoryTransport() transport.put_bytes('hello', 'hello') sftp_server = TransportSFTPServer(AsyncTransport(transport)) deferred = sftp_server.openDirectory('.') def check_directory(directory): with closing(directory): names = [entry[0] for entry in directory] self.assertEqual(['hello'], names) return deferred.addCallback(check_directory)
def test___iter__no_suffix(self): my_store = TextStore(MemoryTransport(), prefixed=False, compressed=False) stream = StringIO("content") my_store.add(stream, "foo") self.assertEqual(set(['foo']), set(my_store.__iter__()))
def test_escaped_uppercase(self): """Uppercase letters are escaped for safety on Windows""" my_store = store.TransportStore(MemoryTransport(), prefixed=True, escaped=True) # a particularly perverse file-id! :-) self.assertEquals(my_store._relpath('C:<>'), 'be/%2543%253a%253c%253e')
def test_only_wraps_once(self): # get_readonly_transport just returns the given transport if its # already readonly. transport = MemoryTransport() readonly_transport = get_readonly_transport(transport) double_readonly = get_readonly_transport(readonly_transport) self.assertIs(readonly_transport, double_readonly)
def test___iter__escaped(self): self.vfstore = VersionedFileStore(MemoryTransport(), prefixed=True, escaped=True, versionedfile_class=WeaveFile) self.vfstore.get_scope = self.get_scope self._transaction = transactions.WriteTransaction() vf = self.vfstore.get_weave_or_empty(' ', self._transaction) vf.add_lines('a', [], []) del vf self._transaction.finish() self.assertEqual([' '], list(self.vfstore))
def test_copy_suffixes(self): from_store = self.get_populated_store() to_store = TextStore(MemoryTransport(), prefixed=True, compressed=True) to_store.register_suffix('sig') to_store.copy_all_ids(from_store) self.assertEqual(1, len(to_store)) self.assertEqual(set(['foo']), set(to_store.__iter__())) self.assertEqual('content', to_store.get('foo').read()) self.assertEqual('signature', to_store.get('foo', 'sig').read()) self.assertRaises(KeyError, to_store.get, 'missing', 'sig')
def setUp(self): frontend = InMemoryFrontend() self.factory = frontend.getLaunchpadObjectFactory() codehosting_api = frontend.getCodehostingEndpoint() self.requester = self.factory.makePerson() self.backing_transport = MemoryTransport() self.server = self.getServer( codehosting_api, self.requester.id, self.backing_transport) self.server.start_server() self.addCleanup(self.server.stop_server)
def test__format_directory_entries_with_MemoryStat(self): """format_directory_entries works with MemoryStat. MemoryStat lacks many fields, but format_directory_entries works around that. """ t = MemoryTransport() stat_result = t.stat('.') entries = self.sftp_server._format_directory_entries( [stat_result], ['filename']) self.assertEqual(list(entries), [ ('filename', 'drwxr-xr-x 0 0 0 0 ' 'Jan 01 1970 filename', {'atime': 0, 'gid': 0, 'mtime': 0, 'permissions': 16877, 'size': 0, 'uid': 0})]) self.assertIs(None, getattr(stat_result, 'st_mtime', None))
def setUp(self): super(TestFilesystem, self).setUp() self.disable_directory_isolation() frontend = InMemoryFrontend() self.factory = frontend.getLaunchpadObjectFactory() endpoint = XMLRPCWrapper(frontend.getCodehostingEndpoint()) self.requester = self.factory.makePerson() self._server = LaunchpadServer( endpoint, self.requester.id, MemoryTransport()) self._server.start_server() self.addCleanup(self._server.stop_server)
def setUp(self): super(TestBranchChangedNotification, self).setUp() self._server = None self._branch_changed_log = [] frontend = InMemoryFrontend() self.factory = frontend.getLaunchpadObjectFactory() self.codehosting_api = frontend.getCodehostingEndpoint() self.codehosting_api.branchChanged = self._replacement_branchChanged self.requester = self.factory.makePerson() self.backing_transport = MemoryTransport() self.disable_directory_isolation()
def get_populated_store(self, prefixed=False, store_class=TextStore, compressed=False): my_store = store_class(MemoryTransport(), prefixed, compressed=compressed) my_store.register_suffix('sig') stream = StringIO("signature") my_store.add(stream, "foo", 'sig') stream = StringIO("content") my_store.add(stream, "foo") stream = StringIO("signature for missing base") my_store.add(stream, "missing", 'sig') return my_store
def _populate_from_branch(self): """Populate the in-tree state from the branch.""" self._set_basis() if self._branch_revision_id == _mod_revision.NULL_REVISION: self._parent_ids = [] else: self._parent_ids = [self._branch_revision_id] self._inventory = Inventory(None, self._basis_tree.get_revision_id()) self._file_transport = MemoryTransport() # TODO copy the revision trees content, or do it lazy, or something. inventory_entries = self._basis_tree.iter_entries_by_dir() for path, entry in inventory_entries: self._inventory.add(entry.copy()) if path == '': continue if entry.kind == 'directory': self._file_transport.mkdir(path) elif entry.kind == 'file': self._file_transport.put_file( path, self._basis_tree.get_file(entry.file_id)) else: raise NotImplementedError(self._populate_from_branch)
def setUp(self): super(TestBranchChangedErrorHandling, self).setUp() self._server = None frontend = InMemoryFrontend() self.factory = frontend.getLaunchpadObjectFactory() self.codehosting_api = frontend.getCodehostingEndpoint() self.codehosting_api.branchChanged = self._replacement_branchChanged self.requester = self.factory.makePerson() self.backing_transport = MemoryTransport() self.disable_directory_isolation() # Trap stderr. self.addCleanup(setattr, sys, 'stderr', sys.stderr) self._real_stderr = sys.stderr sys.stderr = codecs.getwriter('utf8')(StringIO()) # To record generated oopsids self.generated_oopsids = []
def _populate_from_branch(self): """Populate the in-tree state from the branch.""" self._set_basis() if self._branch_revision_id == _mod_revision.NULL_REVISION: self._parent_ids = [] else: self._parent_ids = [self._branch_revision_id] self._inventory = Inventory(None, self._basis_tree.get_revision_id()) self._file_transport = MemoryTransport() # TODO copy the revision trees content, or do it lazy, or something. inventory_entries = self._basis_tree.iter_entries_by_dir() for path, entry in inventory_entries: self._inventory.add(entry.copy()) if path == '': continue if entry.kind == 'directory': self._file_transport.mkdir(path) elif entry.kind == 'file': self._file_transport.put_file(path, self._basis_tree.get_file(entry.file_id)) else: raise NotImplementedError(self._populate_from_branch)
def test_mkdir(self): transport = MemoryTransport() transport.mkdir('dir') transport.append_bytes('dir/path', 'content') self.assertEqual(transport.get('dir/path').read(), 'content')
def test_abspath(self): transport = MemoryTransport() self.assertEqual("memory:///relpath", transport.abspath('relpath'))
def setUp(self): super(TestDirectDatabaseLaunchpadServer, self).setUp() self.requester = self.factory.makePerson() self.server = DirectDatabaseLaunchpadServer('lp-test://', MemoryTransport())
def getLaunchpadServer(self, codehosting_api, user_id): return LaunchpadServer(XMLRPCWrapper(codehosting_api), user_id, MemoryTransport())
def test_makes_readonly_transport(self): # get_readonly_transport wraps a transport so that its readonly. transport = MemoryTransport() self.assertEqual(False, transport.is_readonly()) readonly_transport = get_readonly_transport(transport) self.assertEqual(True, readonly_transport.is_readonly())
def test_tearDown(self): backing_transport = MemoryTransport() server = ChrootServer(backing_transport) server.setUp() server.tearDown() self.assertFalse(server.scheme in _get_protocol_handlers().keys())
def test_stat(self): transport = MemoryTransport() transport.put_bytes('foo', 'content') transport.put_bytes('bar', 'phowar') self.assertEqual(7, transport.stat('foo').st_size) self.assertEqual(6, transport.stat('bar').st_size)
def test_put_and_get(self): transport = MemoryTransport() transport.put_file('path', StringIO('content')) self.assertEqual(transport.get('path').read(), 'content') transport.put_bytes('path', 'content') self.assertEqual(transport.get('path').read(), 'content')
def test_has_missing(self): transport = MemoryTransport() self.assertEquals(False, transport.has('foo'))
class MemoryTree(mutabletree.MutableInventoryTree): """A MemoryTree is a specialisation of MutableTree. It maintains nearly no state outside of read_lock and write_lock transactions. (it keeps a reference to the branch, and its last-revision only). """ def __init__(self, branch, revision_id): """Construct a MemoryTree for branch using revision_id.""" self.branch = branch self.bzrdir = branch.bzrdir self._branch_revision_id = revision_id self._locks = 0 self._lock_mode = None def get_config_stack(self): return self.branch.get_config_stack() def is_control_filename(self, filename): # Memory tree doesn't have any control filenames return False @needs_tree_write_lock def _add(self, files, ids, kinds): """See MutableTree._add.""" for f, file_id, kind in zip(files, ids, kinds): if kind is None: kind = 'file' if file_id is None: self._inventory.add_path(f, kind=kind) else: self._inventory.add_path(f, kind=kind, file_id=file_id) def basis_tree(self): """See Tree.basis_tree().""" return self._basis_tree @staticmethod def create_on_branch(branch): """Create a MemoryTree for branch, using the last-revision of branch.""" revision_id = _mod_revision.ensure_null(branch.last_revision()) return MemoryTree(branch, revision_id) def _gather_kinds(self, files, kinds): """See MutableTree._gather_kinds. This implementation does not care about the file kind of missing files, so is a no-op. """ def get_file(self, file_id, path=None): """See Tree.get_file.""" if path is None: path = self.id2path(file_id) return self._file_transport.get(path) def get_file_sha1(self, file_id, path=None, stat_value=None): """See Tree.get_file_sha1().""" if path is None: path = self.id2path(file_id) stream = self._file_transport.get(path) return sha_file(stream) def get_root_id(self): return self.path2id('') def _comparison_data(self, entry, path): """See Tree._comparison_data.""" if entry is None: return None, False, None return entry.kind, entry.executable, None @needs_tree_write_lock def rename_one(self, from_rel, to_rel): file_id = self.path2id(from_rel) to_dir, to_tail = os.path.split(to_rel) to_parent_id = self.path2id(to_dir) self._file_transport.move(from_rel, to_rel) self._inventory.rename(file_id, to_parent_id, to_tail) def path_content_summary(self, path): """See Tree.path_content_summary.""" id = self.path2id(path) if id is None: return 'missing', None, None, None kind = self.kind(id) if kind == 'file': bytes = self._file_transport.get_bytes(path) size = len(bytes) executable = self._inventory[id].executable sha1 = None # no stat cache return (kind, size, executable, sha1) elif kind == 'directory': # memory tree does not support nested trees yet. return kind, None, None, None elif kind == 'symlink': raise NotImplementedError('symlink support') else: raise NotImplementedError('unknown kind') def _file_size(self, entry, stat_value): """See Tree._file_size.""" if entry is None: return 0 return entry.text_size @needs_read_lock def get_parent_ids(self): """See Tree.get_parent_ids. This implementation returns the current cached value from self._parent_ids. """ return list(self._parent_ids) def has_filename(self, filename): """See Tree.has_filename().""" return self._file_transport.has(filename) def is_executable(self, file_id, path=None): return self._inventory[file_id].executable def kind(self, file_id): return self._inventory[file_id].kind def mkdir(self, path, file_id=None): """See MutableTree.mkdir().""" self.add(path, file_id, 'directory') if file_id is None: file_id = self.path2id(path) self._file_transport.mkdir(path) return file_id @needs_read_lock def last_revision(self): """See MutableTree.last_revision.""" return self._branch_revision_id def lock_read(self): """Lock the memory tree for reading. This triggers population of data from the branch for its revision. """ self._locks += 1 try: if self._locks == 1: self.branch.lock_read() self._lock_mode = "r" self._populate_from_branch() except: self._locks -= 1 raise def lock_tree_write(self): """See MutableTree.lock_tree_write().""" self._locks += 1 try: if self._locks == 1: self.branch.lock_read() self._lock_mode = "w" self._populate_from_branch() elif self._lock_mode == "r": raise errors.ReadOnlyError(self) except: self._locks -= 1 raise def lock_write(self): """See MutableTree.lock_write().""" self._locks += 1 try: if self._locks == 1: self.branch.lock_write() self._lock_mode = "w" self._populate_from_branch() elif self._lock_mode == "r": raise errors.ReadOnlyError(self) except: self._locks -= 1 raise def _populate_from_branch(self): """Populate the in-tree state from the branch.""" self._set_basis() if self._branch_revision_id == _mod_revision.NULL_REVISION: self._parent_ids = [] else: self._parent_ids = [self._branch_revision_id] self._inventory = Inventory(None, self._basis_tree.get_revision_id()) self._file_transport = MemoryTransport() # TODO copy the revision trees content, or do it lazy, or something. inventory_entries = self._basis_tree.iter_entries_by_dir() for path, entry in inventory_entries: self._inventory.add(entry.copy()) if path == '': continue if entry.kind == 'directory': self._file_transport.mkdir(path) elif entry.kind == 'file': self._file_transport.put_file(path, self._basis_tree.get_file(entry.file_id)) else: raise NotImplementedError(self._populate_from_branch) def put_file_bytes_non_atomic(self, file_id, bytes): """See MutableTree.put_file_bytes_non_atomic.""" self._file_transport.put_bytes(self.id2path(file_id), bytes) def unlock(self): """Release a lock. This frees all cached state when the last lock context for the tree is left. """ if self._locks == 1: self._basis_tree = None self._parent_ids = [] self._inventory = None try: self.branch.unlock() finally: self._locks = 0 self._lock_mode = None else: self._locks -= 1 @needs_tree_write_lock def unversion(self, file_ids): """Remove the file ids in file_ids from the current versioned set. When a file_id is unversioned, all of its children are automatically unversioned. :param file_ids: The file ids to stop versioning. :raises: NoSuchId if any fileid is not currently versioned. """ # XXX: This should be in mutabletree, but the inventory-save action # is not relevant to memory tree. Until that is done in unlock by # working tree, we cannot share the implementation. for file_id in file_ids: if self._inventory.has_id(file_id): self._inventory.remove_recursive_id(file_id) else: raise errors.NoSuchId(self, file_id) def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False): """See MutableTree.set_parent_trees().""" for revision_id in revision_ids: _mod_revision.check_not_reserved_id(revision_id) if len(revision_ids) == 0: self._parent_ids = [] self._branch_revision_id = _mod_revision.NULL_REVISION else: self._parent_ids = revision_ids self._branch_revision_id = revision_ids[0] self._allow_leftmost_as_ghost = allow_leftmost_as_ghost self._set_basis() def _set_basis(self): try: self._basis_tree = self.branch.repository.revision_tree( self._branch_revision_id) except errors.NoSuchRevision: if self._allow_leftmost_as_ghost: self._basis_tree = self.branch.repository.revision_tree( _mod_revision.NULL_REVISION) else: raise def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False): """See MutableTree.set_parent_trees().""" if len(parents_list) == 0: self._parent_ids = [] self._basis_tree = self.branch.repository.revision_tree( _mod_revision.NULL_REVISION) else: if parents_list[0][1] is None and not allow_leftmost_as_ghost: # a ghost in the left most parent raise errors.GhostRevisionUnusableHere(parents_list[0][0]) self._parent_ids = [parent_id for parent_id, tree in parents_list] if parents_list[0][1] is None or parents_list[0][1] == 'null:': self._basis_tree = self.branch.repository.revision_tree( _mod_revision.NULL_REVISION) else: self._basis_tree = parents_list[0][1] self._branch_revision_id = parents_list[0][0]
def test_has_present(self): transport = MemoryTransport() transport.append_bytes('foo', 'content') self.assertEquals(True, transport.has('foo'))
def test_list_dir(self): transport = MemoryTransport() transport.put_bytes('foo', 'content') transport.mkdir('dir') transport.put_bytes('dir/subfoo', 'content') transport.put_bytes('dirlike', 'content') self.assertEquals(['dir', 'dirlike', 'foo'], sorted(transport.list_dir('.'))) self.assertEquals(['subfoo'], sorted(transport.list_dir('dir')))
def test_parameters(self): transport = MemoryTransport() self.assertEqual(True, transport.listable()) self.assertEqual(False, transport.is_readonly())
def test_abspath_of_root(self): transport = MemoryTransport() self.assertEqual("memory:///", transport.base) self.assertEqual("memory:///", transport.abspath('/'))
def test_abspath_of_relpath_starting_at_root(self): transport = MemoryTransport() self.assertEqual("memory:///foo", transport.abspath('/foo'))
def test_construct(self): backing_transport = MemoryTransport() server = ChrootServer(backing_transport) self.assertEqual(backing_transport, server.backing_transport)
def test_get_url(self): backing_transport = MemoryTransport() server = ChrootServer(backing_transport) server.setUp() self.assertEqual('chroot-%d:///' % id(server), server.get_url()) server.tearDown()
def test_clone(self): transport = MemoryTransport() self.assertTrue(isinstance(transport, MemoryTransport)) self.assertEqual("memory:///", transport.clone("/").base)
def getLaunchpadServer(self, codehosting_api, user_id): return LaunchpadInternalServer('lp-test:///', XMLRPCWrapper(codehosting_api), MemoryTransport())
class LaunchpadTransportTests: """Tests for a Launchpad transport. These tests are expected to run against two kinds of transport. 1. An asynchronous one that returns Deferreds. 2. A synchronous one that returns actual values. To support that, subclasses must implement `getTransport` and `_ensureDeferred`. See these methods for more information. """ def setUp(self): frontend = InMemoryFrontend() self.factory = frontend.getLaunchpadObjectFactory() codehosting_api = frontend.getCodehostingEndpoint() self.requester = self.factory.makePerson() self.backing_transport = MemoryTransport() self.server = self.getServer(codehosting_api, self.requester.id, self.backing_transport) self.server.start_server() self.addCleanup(self.server.stop_server) def assertFiresFailure(self, exception, function, *args, **kwargs): """Assert that calling `function` will cause `exception` to be fired. In the synchronous tests, this means that `function` raises `exception`. In the asynchronous tests, `function` returns a Deferred that fires `exception` as a Failure. :return: A `Deferred`. You must return this from your test. """ return assert_fails_with( self._ensureDeferred(function, *args, **kwargs), exception) def assertFiresFailureWithSubstring(self, exc_type, msg, function, *args, **kw): """Assert that calling function(*args, **kw) fails in a certain way. This method is like assertFiresFailure() but in addition checks that 'msg' is a substring of the str() of the raised exception. """ deferred = self.assertFiresFailure(exc_type, function, *args, **kw) return deferred.addCallback( lambda exception: self.assertIn(msg, str(exception))) def _ensureDeferred(self, function, *args, **kwargs): """Call `function` and return an appropriate Deferred.""" raise NotImplementedError def getServer(self, codehosting_api, user_id, backing_transport): return LaunchpadServer(XMLRPCWrapper(codehosting_api), user_id, backing_transport) def getTransport(self): """Return the transport to be tested.""" raise NotImplementedError() def test_get_transport(self): # When the server is set up, getting a transport for the server URL # returns a LaunchpadTransport pointing at that URL. That is, the # transport is registered once the server is set up. transport = self.getTransport() self.assertEqual(self.server.get_url(), transport.base) def test_cant_write_to_control_conf(self): # You can't write to the control.conf file if it exists. It's # generated by Launchpad based on info in the database, rather than # being an actual file on disk. transport = self.getTransport() branch = self.factory.makeProductBranch(branch_type=BranchType.HOSTED, owner=self.requester) self.factory.enableDefaultStackingForProduct(branch.product, branch) return self.assertFiresFailure( errors.TransportNotPossible, transport.put_bytes, '~%s/%s/.bzr/control.conf' % (branch.owner.name, branch.product.name), 'hello nurse!') def _makeOnBackingTransport(self, branch): """Make directories for 'branch' on the backing transport. :return: a transport for the .bzr directory of 'branch'. """ backing_transport = self.backing_transport.clone( '%s/.bzr/' % branch_to_path(branch, add_slash=False)) backing_transport.create_prefix() return backing_transport def test_get_mapped_file(self): # Getting a file from a public branch URL gets the file as stored on # the base transport. transport = self.getTransport() branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') deferred = self._ensureDeferred( transport.get_bytes, '%s/.bzr/hello.txt' % branch.unique_name) return deferred.addCallback(self.assertEqual, 'Hello World!') def test_get_mapped_file_escaped_url(self): # Getting a file from a public branch URL gets the file as stored on # the base transport, even when the URL is escaped. branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') url = escape('%s/.bzr/hello.txt' % branch.unique_name) transport = self.getTransport() deferred = self._ensureDeferred(transport.get_bytes, url) return deferred.addCallback(self.assertEqual, 'Hello World!') def test_readv_mapped_file(self): # Using readv on a public branch URL gets chunks of the file as stored # on the base transport. branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) data = 'Hello World!' backing_transport.put_bytes('hello.txt', data) transport = self.getTransport() deferred = self._ensureDeferred( transport.readv, '%s/.bzr/hello.txt' % branch.unique_name, [(3, 2)]) def get_chunk(generator): return generator.next()[1] deferred.addCallback(get_chunk) return deferred.addCallback(self.assertEqual, data[3:5]) def test_put_mapped_file(self): # Putting a file from a public branch URL stores the file in the # mapped URL on the base transport. transport = self.getTransport() branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) deferred = self._ensureDeferred( transport.put_bytes, '%s/.bzr/goodbye.txt' % branch.unique_name, "Goodbye") def check_bytes_written(ignored): self.assertEqual("Goodbye", backing_transport.get_bytes('goodbye.txt')) return deferred.addCallback(check_bytes_written) def test_cloning_updates_base(self): # A transport can be constructed using a path relative to another # transport by using 'clone'. When this happens, it's necessary for # the newly constructed transport to preserve the non-relative path # information from the transport being cloned. It's necessary because # the transport needs to have the '~user/product/branch-name' in order # to translate paths. transport = self.getTransport() self.assertEqual(self.server.get_url(), transport.base) transport = transport.clone('~testuser') self.assertEqual(self.server.get_url() + '~testuser', transport.base) def test_abspath_without_schema(self): # _abspath returns the absolute path for a given relative path, but # without the schema part of the URL that is included by abspath. transport = self.getTransport() self.assertEqual('/~testuser/firefox/baz', transport._abspath('~testuser/firefox/baz')) transport = transport.clone('~testuser') self.assertEqual('/~testuser/firefox/baz', transport._abspath('firefox/baz')) def test_cloning_preserves_path_mapping(self): # The public branch URL -> filesystem mapping uses the base URL to do # its mapping, thus ensuring that clones map correctly. transport = self.getTransport() branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') transport = transport.clone('~%s' % branch.owner.name) deferred = self._ensureDeferred( transport.get_bytes, '%s/%s/.bzr/hello.txt' % (branch.product.name, branch.name)) return deferred.addCallback(self.assertEqual, 'Hello World!') def test_abspath(self): # abspath for a relative path is the same as the base URL for a clone # for that relative path. transport = self.getTransport() self.assertEqual( transport.clone('~testuser').base, transport.abspath('~testuser')) def test_incomplete_path_not_found(self): # For a branch URL to be complete, it needs to have a person, product # and branch. Trying to perform operations on an incomplete URL raises # an error. Which kind of error is not particularly important. transport = self.getTransport() return self.assertFiresFailure(errors.NoSuchFile, transport.get, '~testuser') def test_complete_non_existent_path_not_found(self): # Bazaar looks for files inside a branch directory before it looks for # the branch itself. If the branch doesn't exist, any files it asks # for are not found. i.e. we raise NoSuchFile transport = self.getTransport() return self.assertFiresFailure( errors.NoSuchFile, transport.get, '~testuser/firefox/new-branch/.bzr/branch-format') def test_rename(self): # We can use the transport to rename files where both the source and # target are virtual paths. branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') transport = self.getTransport().clone(branch.unique_name) deferred = self._ensureDeferred(transport.list_dir, '.bzr') deferred.addCallback(set) def rename_file(dir_contents): """Rename a file and return the original directory contents.""" deferred = self._ensureDeferred(transport.rename, '.bzr/hello.txt', '.bzr/goodbye.txt') deferred.addCallback(lambda ignored: dir_contents) return deferred def check_file_was_renamed(dir_contents): """Check that the old name isn't there and the new name is.""" # Replace the old name with the new name. dir_contents.remove('hello.txt') dir_contents.add('goodbye.txt') deferred = self._ensureDeferred(transport.list_dir, '.bzr') deferred.addCallback(set) # Check against the virtual transport. deferred.addCallback(self.assertEqual, dir_contents) # Check against the backing transport. deferred.addCallback(lambda ignored: self.assertEqual( set(backing_transport.list_dir('.')), dir_contents)) return deferred deferred.addCallback(rename_file) return deferred.addCallback(check_file_was_renamed) def test_iter_files_recursive(self): # iter_files_recursive doesn't take a relative path but still needs to # do a path-based operation on the backing transport, so the # implementation can't just be a shim to the backing transport. branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') transport = self.getTransport().clone(branch.unique_name) backing_transport = self.backing_transport.clone( branch_to_path(branch)) deferred = self._ensureDeferred(transport.iter_files_recursive) def check_iter_result(iter_files, expected_files): self.assertEqual(expected_files, list(iter_files)) deferred.addCallback(check_iter_result, list(backing_transport.iter_files_recursive())) return deferred def test_make_two_directories(self): # Bazaar doesn't have a makedirs() facility for transports, so we need # to make sure that we can make a directory on the backing transport # if its parents exist and if they don't exist. product = self.factory.makeProduct() banana = '~%s/%s/banana' % (self.requester.name, product.name) orange = '~%s/%s/orange' % (self.requester.name, product.name) transport = self.getTransport() transport.mkdir(banana) transport.mkdir(orange) self.assertTrue(transport.has(banana)) self.assertTrue(transport.has(orange)) def test_createBranch_not_found_error(self): # When createBranch raises faults.NotFound the transport should # translate this to a PermissionDenied exception (see the comment in # transport.py for why we translate to TransportNotPossible and not # NoSuchFile). transport = self.getTransport() return self.assertFiresFailureWithSubstring( errors.PermissionDenied, "does not exist", transport.mkdir, '~%s/no-such-product/some-name' % self.requester.name) def test_createBranch_permission_denied_error(self): # When createBranch raises faults.PermissionDenied, the transport # should translate this to a PermissionDenied exception. transport = self.getTransport() person = self.factory.makePerson() product = self.factory.makeProduct() message = ("%s cannot create branches owned by %s" % (self.requester.displayname, person.displayname)) return self.assertFiresFailureWithSubstring( errors.PermissionDenied, message, transport.mkdir, '~%s/%s/some-name' % (person.name, product.name)) def test_createBranch_invalid_package_name(self): # When createBranch raises faults.InvalidSourcePackageName, the # transport should translate this to a PermissionDenied exception transport = self.getTransport() series = self.factory.makeDistroSeries() unique_name = '~%s/%s/%s/spaced%%20name/branch' % ( self.requester.name, series.distribution.name, series.name) return self.assertFiresFailureWithSubstring( errors.PermissionDenied, "is not a valid source package name", transport.mkdir, unique_name) def test_rmdir(self): transport = self.getTransport() self.assertFiresFailure(errors.PermissionDenied, transport.rmdir, '~testuser/firefox/baz')
def test_mkdir_twice(self): transport = MemoryTransport() transport.mkdir('dir') self.assertRaises(FileExists, transport.mkdir, 'dir')
def test_mkdir_missing_parent(self): transport = MemoryTransport() self.assertRaises(NoSuchFile, transport.mkdir, 'dir/dir')
def test_get_missing(self): transport = MemoryTransport() self.assertRaises(NoSuchFile, transport.get, 'foo')
class LaunchpadTransportTests: """Tests for a Launchpad transport. These tests are expected to run against two kinds of transport. 1. An asynchronous one that returns Deferreds. 2. A synchronous one that returns actual values. To support that, subclasses must implement `getTransport` and `_ensureDeferred`. See these methods for more information. """ def setUp(self): frontend = InMemoryFrontend() self.factory = frontend.getLaunchpadObjectFactory() codehosting_api = frontend.getCodehostingEndpoint() self.requester = self.factory.makePerson() self.backing_transport = MemoryTransport() self.server = self.getServer( codehosting_api, self.requester.id, self.backing_transport) self.server.start_server() self.addCleanup(self.server.stop_server) def assertFiresFailure(self, exception, function, *args, **kwargs): """Assert that calling `function` will cause `exception` to be fired. In the synchronous tests, this means that `function` raises `exception`. In the asynchronous tests, `function` returns a Deferred that fires `exception` as a Failure. :return: A `Deferred`. You must return this from your test. """ return assert_fails_with( self._ensureDeferred(function, *args, **kwargs), exception) def assertFiresFailureWithSubstring(self, exc_type, msg, function, *args, **kw): """Assert that calling function(*args, **kw) fails in a certain way. This method is like assertFiresFailure() but in addition checks that 'msg' is a substring of the str() of the raised exception. """ deferred = self.assertFiresFailure(exc_type, function, *args, **kw) return deferred.addCallback( lambda exception: self.assertIn(msg, str(exception))) def _ensureDeferred(self, function, *args, **kwargs): """Call `function` and return an appropriate Deferred.""" raise NotImplementedError def getServer(self, codehosting_api, user_id, backing_transport): return LaunchpadServer( XMLRPCWrapper(codehosting_api), user_id, backing_transport) def getTransport(self): """Return the transport to be tested.""" raise NotImplementedError() def test_get_transport(self): # When the server is set up, getting a transport for the server URL # returns a LaunchpadTransport pointing at that URL. That is, the # transport is registered once the server is set up. transport = self.getTransport() self.assertEqual(self.server.get_url(), transport.base) def test_cant_write_to_control_conf(self): # You can't write to the control.conf file if it exists. It's # generated by Launchpad based on info in the database, rather than # being an actual file on disk. transport = self.getTransport() branch = self.factory.makeProductBranch( branch_type=BranchType.HOSTED, owner=self.requester) self.factory.enableDefaultStackingForProduct(branch.product, branch) return self.assertFiresFailure( errors.TransportNotPossible, transport.put_bytes, '~%s/%s/.bzr/control.conf' % ( branch.owner.name, branch.product.name), 'hello nurse!') def _makeOnBackingTransport(self, branch): """Make directories for 'branch' on the backing transport. :return: a transport for the .bzr directory of 'branch'. """ backing_transport = self.backing_transport.clone( '%s/.bzr/' % branch_to_path(branch, add_slash=False)) backing_transport.create_prefix() return backing_transport def test_get_mapped_file(self): # Getting a file from a public branch URL gets the file as stored on # the base transport. transport = self.getTransport() branch = self.factory.makeAnyBranch( branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') deferred = self._ensureDeferred( transport.get_bytes, '%s/.bzr/hello.txt' % branch.unique_name) return deferred.addCallback(self.assertEqual, 'Hello World!') def test_get_mapped_file_escaped_url(self): # Getting a file from a public branch URL gets the file as stored on # the base transport, even when the URL is escaped. branch = self.factory.makeAnyBranch( branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') url = escape('%s/.bzr/hello.txt' % branch.unique_name) transport = self.getTransport() deferred = self._ensureDeferred(transport.get_bytes, url) return deferred.addCallback(self.assertEqual, 'Hello World!') def test_readv_mapped_file(self): # Using readv on a public branch URL gets chunks of the file as stored # on the base transport. branch = self.factory.makeAnyBranch( branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) data = 'Hello World!' backing_transport.put_bytes('hello.txt', data) transport = self.getTransport() deferred = self._ensureDeferred( transport.readv, '%s/.bzr/hello.txt' % branch.unique_name, [(3, 2)]) def get_chunk(generator): return generator.next()[1] deferred.addCallback(get_chunk) return deferred.addCallback(self.assertEqual, data[3:5]) def test_put_mapped_file(self): # Putting a file from a public branch URL stores the file in the # mapped URL on the base transport. transport = self.getTransport() branch = self.factory.makeAnyBranch( branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) deferred = self._ensureDeferred( transport.put_bytes, '%s/.bzr/goodbye.txt' % branch.unique_name, "Goodbye") def check_bytes_written(ignored): self.assertEqual( "Goodbye", backing_transport.get_bytes('goodbye.txt')) return deferred.addCallback(check_bytes_written) def test_cloning_updates_base(self): # A transport can be constructed using a path relative to another # transport by using 'clone'. When this happens, it's necessary for # the newly constructed transport to preserve the non-relative path # information from the transport being cloned. It's necessary because # the transport needs to have the '~user/product/branch-name' in order # to translate paths. transport = self.getTransport() self.assertEqual(self.server.get_url(), transport.base) transport = transport.clone('~testuser') self.assertEqual(self.server.get_url() + '~testuser', transport.base) def test_abspath_without_schema(self): # _abspath returns the absolute path for a given relative path, but # without the schema part of the URL that is included by abspath. transport = self.getTransport() self.assertEqual( '/~testuser/firefox/baz', transport._abspath('~testuser/firefox/baz')) transport = transport.clone('~testuser') self.assertEqual( '/~testuser/firefox/baz', transport._abspath('firefox/baz')) def test_cloning_preserves_path_mapping(self): # The public branch URL -> filesystem mapping uses the base URL to do # its mapping, thus ensuring that clones map correctly. transport = self.getTransport() branch = self.factory.makeAnyBranch( branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') transport = transport.clone('~%s' % branch.owner.name) deferred = self._ensureDeferred( transport.get_bytes, '%s/%s/.bzr/hello.txt' % (branch.product.name, branch.name)) return deferred.addCallback(self.assertEqual, 'Hello World!') def test_abspath(self): # abspath for a relative path is the same as the base URL for a clone # for that relative path. transport = self.getTransport() self.assertEqual( transport.clone('~testuser').base, transport.abspath('~testuser')) def test_incomplete_path_not_found(self): # For a branch URL to be complete, it needs to have a person, product # and branch. Trying to perform operations on an incomplete URL raises # an error. Which kind of error is not particularly important. transport = self.getTransport() return self.assertFiresFailure( errors.NoSuchFile, transport.get, '~testuser') def test_complete_non_existent_path_not_found(self): # Bazaar looks for files inside a branch directory before it looks for # the branch itself. If the branch doesn't exist, any files it asks # for are not found. i.e. we raise NoSuchFile transport = self.getTransport() return self.assertFiresFailure( errors.NoSuchFile, transport.get, '~testuser/firefox/new-branch/.bzr/branch-format') def test_rename(self): # We can use the transport to rename files where both the source and # target are virtual paths. branch = self.factory.makeAnyBranch( branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') transport = self.getTransport().clone(branch.unique_name) deferred = self._ensureDeferred(transport.list_dir, '.bzr') deferred.addCallback(set) def rename_file(dir_contents): """Rename a file and return the original directory contents.""" deferred = self._ensureDeferred( transport.rename, '.bzr/hello.txt', '.bzr/goodbye.txt') deferred.addCallback(lambda ignored: dir_contents) return deferred def check_file_was_renamed(dir_contents): """Check that the old name isn't there and the new name is.""" # Replace the old name with the new name. dir_contents.remove('hello.txt') dir_contents.add('goodbye.txt') deferred = self._ensureDeferred(transport.list_dir, '.bzr') deferred.addCallback(set) # Check against the virtual transport. deferred.addCallback(self.assertEqual, dir_contents) # Check against the backing transport. deferred.addCallback( lambda ignored: self.assertEqual( set(backing_transport.list_dir('.')), dir_contents)) return deferred deferred.addCallback(rename_file) return deferred.addCallback(check_file_was_renamed) def test_iter_files_recursive(self): # iter_files_recursive doesn't take a relative path but still needs to # do a path-based operation on the backing transport, so the # implementation can't just be a shim to the backing transport. branch = self.factory.makeAnyBranch( branch_type=BranchType.HOSTED, owner=self.requester) backing_transport = self._makeOnBackingTransport(branch) backing_transport.put_bytes('hello.txt', 'Hello World!') transport = self.getTransport().clone(branch.unique_name) backing_transport = self.backing_transport.clone( branch_to_path(branch)) deferred = self._ensureDeferred(transport.iter_files_recursive) def check_iter_result(iter_files, expected_files): self.assertEqual(expected_files, list(iter_files)) deferred.addCallback( check_iter_result, list(backing_transport.iter_files_recursive())) return deferred def test_make_two_directories(self): # Bazaar doesn't have a makedirs() facility for transports, so we need # to make sure that we can make a directory on the backing transport # if its parents exist and if they don't exist. product = self.factory.makeProduct() banana = '~%s/%s/banana' % (self.requester.name, product.name) orange = '~%s/%s/orange' % (self.requester.name, product.name) transport = self.getTransport() transport.mkdir(banana) transport.mkdir(orange) self.assertTrue(transport.has(banana)) self.assertTrue(transport.has(orange)) def test_createBranch_not_found_error(self): # When createBranch raises faults.NotFound the transport should # translate this to a PermissionDenied exception (see the comment in # transport.py for why we translate to TransportNotPossible and not # NoSuchFile). transport = self.getTransport() return self.assertFiresFailureWithSubstring( errors.PermissionDenied, "does not exist", transport.mkdir, '~%s/no-such-product/some-name' % self.requester.name) def test_createBranch_permission_denied_error(self): # When createBranch raises faults.PermissionDenied, the transport # should translate this to a PermissionDenied exception. transport = self.getTransport() person = self.factory.makePerson() product = self.factory.makeProduct() message = ( "%s cannot create branches owned by %s" % (self.requester.displayname, person.displayname)) return self.assertFiresFailureWithSubstring( errors.PermissionDenied, message, transport.mkdir, '~%s/%s/some-name' % (person.name, product.name)) def test_createBranch_invalid_package_name(self): # When createBranch raises faults.InvalidSourcePackageName, the # transport should translate this to a PermissionDenied exception transport = self.getTransport() series = self.factory.makeDistroSeries() unique_name = '~%s/%s/%s/spaced%%20name/branch' % ( self.requester.name, series.distribution.name, series.name) return self.assertFiresFailureWithSubstring( errors.PermissionDenied, "is not a valid source package name", transport.mkdir, unique_name) def test_rmdir(self): transport = self.getTransport() self.assertFiresFailure( errors.PermissionDenied, transport.rmdir, '~testuser/firefox/baz')