~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Robey Pointer <robey@lag.net>, Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008-2011 Robey Pointer <robey@lag.net>, Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
 
22
22
import os
23
23
import paramiko
24
 
import select
25
24
import socket
26
25
import SocketServer
27
26
import sys
28
 
import threading
29
27
import time
30
28
 
31
29
from bzrlib import (
120
118
    else:
121
119
        def canonicalize(self, path):
122
120
            if os.path.isabs(path):
123
 
                return os.path.normpath(path)
 
121
                return osutils.normpath(path)
124
122
            else:
125
 
                return os.path.normpath('/' + os.path.join(self.home, path))
 
123
                return osutils.normpath('/' + os.path.join(self.home, path))
126
124
 
127
125
    def chattr(self, path, attr):
128
126
        try:
344
342
    def setup(self):
345
343
        self.wrap_for_latency()
346
344
        tcs = self.server.test_case_server
347
 
        ssh_server = paramiko.Transport(self.request)
348
 
        ssh_server.add_server_key(tcs.get_host_key())
349
 
        ssh_server.set_subsystem_handler('sftp', paramiko.SFTPServer,
350
 
                                         StubSFTPServer, root=tcs._root,
351
 
                                         home=tcs._server_homedir)
 
345
        ptrans = paramiko.Transport(self.request)
 
346
        self.paramiko_transport = ptrans
 
347
        # Set it to a channel under 'bzr' so that we get debug info
 
348
        ptrans.set_log_channel('bzr.paramiko.transport')
 
349
        ptrans.add_server_key(tcs.get_host_key())
 
350
        ptrans.set_subsystem_handler('sftp', paramiko.SFTPServer,
 
351
                                     StubSFTPServer, root=tcs._root,
 
352
                                     home=tcs._server_homedir)
352
353
        server = tcs._server_interface(tcs)
353
 
        ssh_server.start_server(None, server)
354
 
        # FIXME: Long story short:
355
 
        # bt.test_transport.TestSSHConnections.test_bzr_connect_to_bzr_ssh
356
 
        # fails if we wait less than 0.2 seconds... paramiko uses a lot of
357
 
        # timeouts internally which probably mask a synchronisation
358
 
        # problem. Note that this is the only test that requires this hack and
359
 
        # the test may need to be fixed instead, but it's late and the test is
360
 
        # horrible as mentioned in its comments :) -- vila 20100623
361
 
        import time
362
 
        time.sleep(0.2)
 
354
        # This blocks until the key exchange has been done
 
355
        ptrans.start_server(None, server)
 
356
 
 
357
    def finish(self):
 
358
        # Wait for the conversation to finish, when the paramiko.Transport
 
359
        # thread finishes
 
360
        # TODO: Consider timing out after XX seconds rather than hanging.
 
361
        #       Also we could check paramiko_transport.active and possibly
 
362
        #       paramiko_transport.getException().
 
363
        self.paramiko_transport.join()
363
364
 
364
365
    def wrap_for_latency(self):
365
366
        tcs = self.server.test_case_server
380
381
            def get_transport(self):
381
382
                return self
382
383
            def get_log_channel(self):
383
 
                return 'paramiko'
 
384
                return 'bzr.paramiko'
384
385
            def get_name(self):
385
386
                return '1'
386
387
            def get_hexdump(self):
389
390
                pass
390
391
 
391
392
        tcs = self.server.test_case_server
392
 
        server = paramiko.SFTPServer(
 
393
        sftp_server = paramiko.SFTPServer(
393
394
            FakeChannel(), 'sftp', StubServer(tcs), StubSFTPServer,
394
395
            root=tcs._root, home=tcs._server_homedir)
 
396
        self.sftp_server = sftp_server
 
397
        sys_stderr = sys.stderr # Used in error reporting during shutdown
395
398
        try:
396
 
            server.start_subsystem(
 
399
            sftp_server.start_subsystem(
397
400
                'sftp', None, ssh.SocketAsChannelAdapter(self.request))
398
401
        except socket.error, e:
399
402
            if (len(e.args) > 0) and (e.args[0] == errno.EPIPE):
409
412
            # seems to be the best we can do.
410
413
            # FIXME: All interpreter shutdown errors should have been related
411
414
            # to daemon threads, cleanup needed -- vila 20100623
412
 
            import sys
413
 
            sys.stderr.write('\nEXCEPTION %r: ' % (e.__class__,))
414
 
            sys.stderr.write('%s\n\n' % (e,))
415
 
        server.finish_subsystem()
 
415
            sys_stderr.write('\nEXCEPTION %r: ' % (e.__class__,))
 
416
            sys_stderr.write('%s\n\n' % (e,))
 
417
 
 
418
    def finish(self):
 
419
        self.sftp_server.finish_subsystem()
416
420
 
417
421
 
418
422
class TestingSFTPServer(test_server.TestingThreadingTCPServer):
549
553
 
550
554
    def get_url(self):
551
555
        """See bzrlib.transport.Server.get_url."""
552
 
        return self._get_sftp_url("~/")
 
556
        return self._get_sftp_url("%7E/")
553
557
 
554
558
 
555
559
class SFTPSiblingAbsoluteServer(SFTPAbsoluteServer):