def test_abspath(self): # The abspath is always relative to the chroot_url. server = chroot.ChrootServer( transport.get_transport_from_url('memory:///foo/bar/')) self.start_server(server) t = transport.get_transport_from_url(server.get_url()) self.assertEqual(server.get_url(), t.abspath('/')) subdir_t = t.clone('subdir') self.assertEqual(server.get_url(), subdir_t.abspath('/'))
def test_clone(self): server = chroot.ChrootServer( transport.get_transport_from_url('memory:///foo/bar/')) self.start_server(server) t = transport.get_transport_from_url(server.get_url()) # relpath from root and root path are the same relpath_cloned = t.clone('foo') abspath_cloned = t.clone('/foo') self.assertEqual(server, relpath_cloned.server) self.assertEqual(server, abspath_cloned.server)
def test_ssh_hints(self): """Transport ssh:// should raise an error pointing out bzr+ssh://""" try: transport.get_transport_from_url('ssh://fooserver/foo') except errors.UnsupportedProtocol, e: e_str = str(e) self.assertEqual( 'Unsupported protocol' ' for url "ssh://fooserver/foo":' ' bzr supports bzr+ssh to operate over ssh,' ' use "bzr+ssh://fooserver/foo".', str(e))
def test_ssh_hints(self): """Transport ssh:// should raise an error pointing out bzr+ssh://""" try: transport.get_transport_from_url('ssh://fooserver/foo') except errors.UnsupportedProtocol, e: e_str = str(e) self.assertEquals('Unsupported protocol' ' for url "ssh://fooserver/foo":' ' bzr supports bzr+ssh to operate over ssh,' ' use "bzr+ssh://fooserver/foo".', str(e))
def test_abspath(self): # The abspath is always relative to the base of the backing transport. server = pathfilter.PathFilteringServer( transport.get_transport_from_url('memory:///foo/bar/'), lambda x: x) server.start_server() t = transport.get_transport_from_url(server.get_url()) self.assertEqual(server.get_url(), t.abspath('/')) subdir_t = t.clone('subdir') self.assertEqual(server.get_url(), subdir_t.abspath('/')) server.stop_server()
def make_pf_transport(self, filter_func=None): """Make a PathFilteringTransport backed by a MemoryTransport. :param filter_func: by default this will be a no-op function. Use this parameter to override it.""" if filter_func is None: filter_func = lambda x: x server = pathfilter.PathFilteringServer( transport.get_transport_from_url('memory:///foo/bar/'), filter_func) server.start_server() self.addCleanup(server.stop_server) return transport.get_transport_from_url(server.get_url())
def test_urljoin_preserves_chroot(self): """Using urlutils.join(url, '..') on a chroot URL should not produce a URL that escapes the intended chroot. This is so that it is not possible to escape a chroot by doing:: url = chroot_transport.base parent_url = urlutils.join(url, '..') new_t = transport.get_transport_from_url(parent_url) """ server = chroot.ChrootServer( transport.get_transport_from_url('memory:///path/')) self.start_server(server) t = transport.get_transport_from_url(server.get_url()) self.assertRaises(errors.InvalidURLJoin, urlutils.join, t.base, '..')
def test_urljoin_preserves_chroot(self): """Using urlutils.join(url, '..') on a chroot URL should not produce a URL that escapes the intended chroot. This is so that it is not possible to escape a chroot by doing:: url = chroot_transport.base parent_url = urlutils.join(url, '..') new_t = transport.get_transport_from_url(parent_url) """ server = chroot.ChrootServer( transport.get_transport_from_url('memory:///path/')) self.start_server(server) t = transport.get_transport_from_url(server.get_url()) self.assertRaises( errors.InvalidURLJoin, urlutils.join, t.base, '..')
def _make_backing_transport(self, transport): """Chroot transport, and decorate with userdir expander.""" self.base_path = self.get_base_path(transport) chroot_server = chroot.ChrootServer(transport) chroot_server.start_server() self.cleanups.append(chroot_server.stop_server) transport = _mod_transport.get_transport_from_url(chroot_server.get_url()) if self.base_path is not None: # Decorate the server's backing transport with a filter that can # expand homedirs. expand_userdirs = self._make_expand_userdirs_filter(transport) expand_userdirs.start_server() self.cleanups.append(expand_userdirs.stop_server) transport = _mod_transport.get_transport_from_url(expand_userdirs.get_url()) self.transport = transport
def start_server(self, backing_transport_server=None, client_path_extra='/extra/'): """Set up server for testing. :param backing_transport_server: backing server to use. If not specified, a LocalURLServer at the current working directory will be used. :param client_path_extra: a path segment starting with '/' to append to the root URL for this server. For instance, a value of '/foo/bar/' will mean the root of the backing transport will be published at a URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than `bzr://127.0.0.1:nnnn/`. Default value is `extra`, so that tests by default will fail unless they do the necessary path translation. """ if not client_path_extra.startswith('/'): raise ValueError(client_path_extra) self.root_client_path = self.client_path_extra = client_path_extra from bzrlib.transport.chroot import ChrootServer if backing_transport_server is None: backing_transport_server = LocalURLServer() self.chroot_server = ChrootServer( self.get_backing_transport(backing_transport_server)) self.chroot_server.start_server() self.backing_transport = transport.get_transport_from_url( self.chroot_server.get_url()) super(SmartTCPServer_for_testing, self).start_server()
def test_format_initialize_find_open(self): # loopback test to check the current format initializes to itself. if not self.branch_format.is_supported(): # unsupported formats are not loopback testable # because the default open will not open them and # they may not be initializable. return # supported formats must be able to init and open t = self.get_transport() readonly_t = transport.get_transport_from_url(self.get_readonly_url()) made_branch = self.make_branch(".") self.assertIsInstance(made_branch, _mod_branch.Branch) # find it via bzrdir opening: opened_control = controldir.ControlDir.open(readonly_t.base) direct_opened_branch = opened_control.open_branch() self.assertEqual(direct_opened_branch.__class__, made_branch.__class__) self.assertEqual(opened_control, direct_opened_branch.bzrdir) self.assertIsInstance(direct_opened_branch._format, self.branch_format.__class__) # find it via Branch.open opened_branch = _mod_branch.Branch.open(readonly_t.base) self.assertIsInstance(opened_branch, made_branch.__class__) self.assertEqual(made_branch._format.__class__, opened_branch._format.__class__) # if it has a unique id string, can we probe for it ? try: self.branch_format.get_format_string() except NotImplementedError: return self.assertEqual(self.branch_format, opened_control.find_branch_format())
def test_bzr_serve_graceful_shutdown(self): big_contents = 'a' * 64 * 1024 self.build_tree_contents([('bigfile', big_contents)]) process, url = self.start_server_port(['--client-timeout=1.0']) t = transport.get_transport_from_url(url) m = t.get_smart_medium() c = client._SmartClient(m) # Start, but don't finish a response resp, response_handler = c.call_expecting_body('get', 'bigfile') self.assertEqual(('ok', ), resp) # Note: process.send_signal is a Python 2.6ism process.send_signal(signal.SIGHUP) # Wait for the server to notice the signal, and then read the actual # body of the response. That way we know that it is waiting for the # request to finish self.assertEqual('Requested to stop gracefully\n', process.stderr.readline()) self.assertEqual('Waiting for 1 client(s) to finish\n', process.stderr.readline()) body = response_handler.read_body_bytes() if body != big_contents: self.fail('Failed to properly read the contents of "bigfile"') # Now that our request is finished, the medium should notice it has # been disconnected. self.assertEqual('', m.read_bytes(1)) # And the server should be stopping self.assertEqual(0, process.wait())
def test_format_initialize_find_open(self): # loopback test to check the current format initializes to itself. if not self.branch_format.is_supported(): # unsupported formats are not loopback testable # because the default open will not open them and # they may not be initializable. return # supported formats must be able to init and open t = self.get_transport() readonly_t = transport.get_transport_from_url(self.get_readonly_url()) made_branch = self.make_branch('.') self.assertIsInstance(made_branch, _mod_branch.Branch) # find it via bzrdir opening: opened_control = controldir.ControlDir.open(readonly_t.base) direct_opened_branch = opened_control.open_branch() self.assertEqual(direct_opened_branch.__class__, made_branch.__class__) self.assertEqual(opened_control, direct_opened_branch.bzrdir) self.assertIsInstance(direct_opened_branch._format, self.branch_format.__class__) # find it via Branch.open opened_branch = _mod_branch.Branch.open(readonly_t.base) self.assertIsInstance(opened_branch, made_branch.__class__) self.assertEqual(made_branch._format.__class__, opened_branch._format.__class__) # if it has a unique id string, can we probe for it ? try: self.branch_format.get_format_string() except NotImplementedError: return self.assertEqual(self.branch_format, opened_control.find_branch_format())
def test_bzr_serve_graceful_shutdown(self): big_contents = 'a'*64*1024 self.build_tree_contents([('bigfile', big_contents)]) process, url = self.start_server_port(['--client-timeout=1.0']) t = transport.get_transport_from_url(url) m = t.get_smart_medium() c = client._SmartClient(m) # Start, but don't finish a response resp, response_handler = c.call_expecting_body('get', 'bigfile') self.assertEqual(('ok',), resp) # Note: process.send_signal is a Python 2.6ism process.send_signal(signal.SIGHUP) # Wait for the server to notice the signal, and then read the actual # body of the response. That way we know that it is waiting for the # request to finish self.assertEqual('Requested to stop gracefully\n', process.stderr.readline()) self.assertEqual('Waiting for 1 client(s) to finish\n', process.stderr.readline()) body = response_handler.read_body_bytes() if body != big_contents: self.fail('Failed to properly read the contents of "bigfile"') # Now that our request is finished, the medium should notice it has # been disconnected. self.assertEqual('', m.read_bytes(1)) # And the server should be stopping self.assertEqual(0, process.wait())
def test_aftp_degrade(self): t = transport.get_transport_from_url('aftp://host/path') self.assertTrue(t.is_active) parent = t.clone('..') self.assertTrue(parent.is_active) self.assertEqual('aftp://host/path', t.abspath(''))
def start_server(self, backing_server=None): """Setup the Chroot on backing_server.""" if backing_server is not None: self.backing_transport = transport.get_transport_from_url( backing_server.get_url()) else: self.backing_transport = transport.get_transport_from_path('.') super(TestingChrootServer, self).start_server()
def test_transport_dependency(self): """Transport with missing dependency causes no error""" saved_handlers = transport._get_protocol_handlers() self.addCleanup(transport._set_protocol_handlers, saved_handlers) # don't pollute the current handlers transport._clear_protocol_handlers() transport.register_transport_proto('foo') transport.register_lazy_transport( 'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler') try: transport.get_transport_from_url('foo://fooserver/foo') except errors.UnsupportedProtocol, e: e_str = str(e) self.assertEquals('Unsupported protocol' ' for url "foo://fooserver/foo":' ' Unable to import library "some_lib":' ' testing missing dependency', str(e))
def _make_backing_transport(self, transport): """Chroot transport, and decorate with userdir expander.""" self.base_path = self.get_base_path(transport) chroot_server = chroot.ChrootServer(transport) chroot_server.start_server() self.cleanups.append(chroot_server.stop_server) transport = _mod_transport.get_transport_from_url( chroot_server.get_url()) if self.base_path is not None: # Decorate the server's backing transport with a filter that can # expand homedirs. expand_userdirs = self._make_expand_userdirs_filter(transport) expand_userdirs.start_server() self.cleanups.append(expand_userdirs.stop_server) transport = _mod_transport.get_transport_from_url( expand_userdirs.get_url()) self.transport = transport
def test_chroot_url_preserves_chroot(self): """Calling get_transport on a chroot transport's base should produce a transport with exactly the same behaviour as the original chroot transport. This is so that it is not possible to escape a chroot by doing:: url = chroot_transport.base parent_url = urlutils.join(url, '..') new_t = transport.get_transport_from_url(parent_url) """ server = chroot.ChrootServer( transport.get_transport_from_url('memory:///path/subpath')) self.start_server(server) t = transport.get_transport_from_url(server.get_url()) new_t = transport.get_transport_from_url(t.base) self.assertEqual(t.server, new_t.server) self.assertEqual(t.base, new_t.base)
def test_with_url_and_segment_parameters(self): url = urlutils.local_path_to_url(self.test_dir) + ",branch=foo" t = transport.get_transport_from_url(url) self.assertIsInstance(t, local.LocalTransport) self.assertEqual(t.base.rstrip("/"), url) with open(os.path.join(self.test_dir, "afile"), 'w') as f: f.write("data") self.assertTrue(t.has("afile"))
def test_with_url_and_segment_parameters(self): url = urlutils.local_path_to_url(self.test_dir)+",branch=foo" t = transport.get_transport_from_url(url) self.assertIsInstance(t, local.LocalTransport) self.assertEquals(t.base.rstrip("/"), url) with open(os.path.join(self.test_dir, "afile"), 'w') as f: f.write("data") self.assertTrue(t.has("afile"))
def test_abspath_root_sibling_server(self): server = stub_sftp.SFTPSiblingAbsoluteServer() server.start_server() self.addCleanup(server.stop_server) transport = _mod_transport.get_transport_from_url(server.get_url()) self.assertFalse(transport.abspath('/').endswith('/~/')) self.assertTrue(transport.abspath('/').endswith('/')) del transport
def test_transport_dependency(self): """Transport with missing dependency causes no error""" saved_handlers = transport._get_protocol_handlers() self.addCleanup(transport._set_protocol_handlers, saved_handlers) # don't pollute the current handlers transport._clear_protocol_handlers() transport.register_transport_proto('foo') transport.register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'BadTransportHandler') try: transport.get_transport_from_url('foo://fooserver/foo') except errors.UnsupportedProtocol, e: e_str = str(e) self.assertEqual( 'Unsupported protocol' ' for url "foo://fooserver/foo":' ' Unable to import library "some_lib":' ' testing missing dependency', str(e))
def test_http_parameters(self): from bzrlib.tests.http_server import HttpServer # connect to '.' via http which is not listable server = HttpServer() self.start_server(server) t = transport.get_transport_from_url('readonly+' + server.get_url()) self.assertIsInstance(t, readonly.ReadonlyTransportDecorator) self.assertEqual(False, t.listable()) self.assertEqual(True, t.is_readonly())
def test_different_format_not_equal(self): """Different format repositories are comparable and not the same. Comparing different format repository objects should give a negative result, rather than trigger an exception (which could happen with a naive __eq__ implementation, e.g. due to missing attributes). """ repo = self.make_repository('repo') other_repo = self.make_repository('other', format='default') if repo._format == other_repo._format: # We're testing the default format! So we have to use a non-default # format for other_repo. transport.get_transport_from_url( self.get_vfs_only_url()).delete_tree('other') other_repo = self.make_repository('other', format='knit') # Make sure the other_repo is not a RemoteRepository. other_bzrdir = controldir.ControlDir.open(self.get_vfs_only_url('other')) other_repo = other_bzrdir.open_repository() self.assertDifferentRepo(repo, other_repo)
def start_server(self, backing_server=None): """Setup the Chroot on backing_server.""" if backing_server is not None: self.backing_transport = transport.get_transport_from_url( backing_server.get_url()) else: self.backing_transport = transport.get_transport_from_path('.') self.backing_transport.clone('added-by-filter').ensure_base() self.filter_func = lambda x: 'added-by-filter/' + x super(TestingPathFilteringServer, self).start_server()
def test_fakenfs_server_default(self): # a FakeNFSServer() should bring up a local relpath server for itself server = test_server.FakeNFSServer() self.start_server(server) # the url should be decorated appropriately self.assertStartsWith(server.get_url(), 'fakenfs+') # and we should be able to get a transport for it t = transport.get_transport_from_url(server.get_url()) # which must be a FakeNFSTransportDecorator instance. self.assertIsInstance(t, fakenfs.FakeNFSTransportDecorator)
def test_open_containing(self): self.assertRaises(errors.NotBranchError, _mod_branch.Branch.open_containing, self.get_readonly_url("")) self.assertRaises(errors.NotBranchError, _mod_branch.Branch.open_containing, self.get_readonly_url("g/p/q")) branch = self.make_branch(".") if not branch.bzrdir._format.supports_transport(transport.get_transport_from_url(self.get_readonly_url("."))): raise tests.TestNotApplicable("format does not support transport") branch, relpath = _mod_branch.Branch.open_containing(self.get_readonly_url("")) self.assertEqual("", relpath) branch, relpath = _mod_branch.Branch.open_containing(self.get_readonly_url("g/p/q")) self.assertEqual("g/p/q", relpath)
def test_create_server(self): server = memory.MemoryServer() server.start_server() url = server.get_url() self.assertTrue(url in transport.transport_list_registry) t = transport.get_transport_from_url(url) del t server.stop_server() self.assertFalse(url in transport.transport_list_registry) self.assertRaises(errors.UnsupportedProtocol, transport.get_transport, url)
def test_get(self): t = transport.get_transport_from_url('trace+memory:///') t.put_bytes('foo', 'barish') t.get('foo') expected_result = [] # put_bytes records the bytes, not the content to avoid memory # pressure. expected_result.append(('put_bytes', 'foo', 6, None)) # get records the file name only. expected_result.append(('get', 'foo')) self.assertEqual(expected_result, t._activity)
def test_transport_activity(self): # It doesn't matter what the implementation does, we just want to make # sure the interface is there t = transport.get_transport_from_url('memory:///') self.factory.report_transport_activity(t, 1000, 'write') self.factory.report_transport_activity(t, 2000, 'read') self.factory.report_transport_activity(t, 4000, None) self.factory.log_transport_activity() self._check_log_transport_activity_noarg() self.factory.log_transport_activity(display=True) self._check_log_transport_activity_display()
def __init__(self, base, _from_transport=None, http_transport=None): if http_transport is None: # FIXME: the password may be lost here because it appears in the # url only for an intial construction (when the url came from the # command-line). http_url = base[len('bzr+'):] self._http_transport = transport.get_transport_from_url(http_url) else: self._http_transport = http_transport super(RemoteHTTPTransport, self).__init__( base, _from_transport=_from_transport)
def __init__(self, base, _from_transport=None, http_transport=None): if http_transport is None: # FIXME: the password may be lost here because it appears in the # url only for an intial construction (when the url came from the # command-line). http_url = base[len('bzr+'):] self._http_transport = transport.get_transport_from_url(http_url) else: self._http_transport = http_transport super(RemoteHTTPTransport, self).__init__(base, _from_transport=_from_transport)
def do_POST(self): """Hand the request off to a smart server instance.""" backing = transport.get_transport_from_path( self.server.test_case_server._home_dir) chroot_server = chroot.ChrootServer(backing) chroot_server.start_server() try: t = transport.get_transport_from_url(chroot_server.get_url()) self.do_POST_inner(t) finally: chroot_server.stop_server()
def test_different_format_not_equal(self): """Different format repositories are comparable and not the same. Comparing different format repository objects should give a negative result, rather than trigger an exception (which could happen with a naive __eq__ implementation, e.g. due to missing attributes). """ repo = self.make_repository('repo') other_repo = self.make_repository('other', format='default') if repo._format == other_repo._format: # We're testing the default format! So we have to use a non-default # format for other_repo. transport.get_transport_from_url( self.get_vfs_only_url()).delete_tree('other') other_repo = self.make_repository('other', format='knit') # Make sure the other_repo is not a RemoteRepository. other_bzrdir = controldir.ControlDir.open( self.get_vfs_only_url('other')) other_repo = other_bzrdir.open_repository() self.assertDifferentRepo(repo, other_repo)
def get_transport_for_connection(self, set_config): port = self.get_server().port if set_config: conf = config.AuthenticationConfig() conf._get_config().update( {'sftptest': {'scheme': 'ssh', 'port': port, 'user': '******'}}) conf._save() t = _mod_transport.get_transport_from_url( 'sftp://localhost:%d' % port) # force a connection to be performed. t.has('foo') return t
def test_bad_connection_ssh(self): """None => auto-detect vendor""" f = file(os.devnull, "wb") self.addCleanup(f.close) self.set_vendor(None, f) t = _mod_transport.get_transport_from_url(self.bogus_url) try: self.assertRaises(errors.ConnectionError, t.get, 'foobar') except NameError, e: if "global name 'SSHException'" in str(e): self.knownFailure('Known NameError bug in paramiko 1.6.1') raise
def test_transport_fallback(self): """Transport with missing dependency causes no error""" saved_handlers = transport._get_protocol_handlers() self.addCleanup(transport._set_protocol_handlers, saved_handlers) transport._clear_protocol_handlers() transport.register_transport_proto('foo') transport.register_lazy_transport( 'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler') transport.register_lazy_transport( 'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler') t = transport.get_transport_from_url('foo://fooserver/foo') self.assertTrue(isinstance(t, BackupTransportHandler))
def test_readv(self): t = transport.get_transport_from_url('trace+memory:///') t.put_bytes('foo', 'barish') list(t.readv('foo', [(0, 1), (3, 2)], adjust_for_latency=True, upper_limit=6)) expected_result = [] # put_bytes records the bytes, not the content to avoid memory # pressure. expected_result.append(('put_bytes', 'foo', 6, None)) # readv records the supplied offset request expected_result.append(('readv', 'foo', [(0, 1), (3, 2)], True, 6)) self.assertEqual(expected_result, t._activity)
def test_empty_branch_api(self): """The branch_obj.push API should make a limited number of HPSS calls. """ t = transport.get_transport_from_url( self.smart_server.get_url()).clone('target') target = branch.Branch.open_from_transport(t) self.empty_branch.push(target) self.assertEqual([ 'BzrDir.open_2.1', 'BzrDir.open_branchV3', 'BzrDir.find_repositoryV3', 'Branch.get_stacked_on_url', 'Branch.lock_write', 'Branch.last_revision_info', 'Branch.unlock' ], self.hpss_calls)
def test_transport_fallback(self): """Transport with missing dependency causes no error""" saved_handlers = transport._get_protocol_handlers() self.addCleanup(transport._set_protocol_handlers, saved_handlers) transport._clear_protocol_handlers() transport.register_transport_proto('foo') transport.register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler') transport.register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'BadTransportHandler') t = transport.get_transport_from_url('foo://fooserver/foo') self.assertTrue(isinstance(t, BackupTransportHandler))
def test_readv(self): t = transport.get_transport_from_url('trace+memory:///') t.put_bytes('foo', 'barish') list( t.readv('foo', [(0, 1), (3, 2)], adjust_for_latency=True, upper_limit=6)) expected_result = [] # put_bytes records the bytes, not the content to avoid memory # pressure. expected_result.append(('put_bytes', 'foo', 6, None)) # readv records the supplied offset request expected_result.append(('readv', 'foo', [(0, 1), (3, 2)], True, 6)) self.assertEqual(expected_result, t._activity)
def make_remote_repository(self, path, shared=None): """Make a RemoteRepository object backed by a real repository that will be created at the given path.""" repo = self.make_repository(path, shared=shared) smart_server = test_server.SmartTCPServer_for_testing() self.start_server(smart_server, self.get_server()) remote_transport = transport.get_transport_from_url( smart_server.get_url()).clone(path) if not repo.bzrdir._format.supports_transport(remote_transport): raise tests.TestNotApplicable("format does not support transport") remote_bzrdir = controldir.ControlDir.open_from_transport( remote_transport) remote_repo = remote_bzrdir.open_repository() return remote_repo