1
# Copyright 2009 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6
from canonical.librarian.libraryprotocol import FileUploadProtocol
7
from canonical.librarian.storage import WrongDatabaseError
8
from lp.testing.systemdocs import LayeredDocFileSuite
15
connectionLost = False
17
def write(self, bytes):
18
self.bytesWritten += bytes
20
def loseConnection(self):
21
self.connectionLost = True
22
self.disconnecting = True
27
def startAddFile(self, name, size):
28
self.file = MockFile(name)
39
def __init__(self, name):
42
def append(self, bytes):
46
databaseName = self.databaseName
47
if databaseName is not None and databaseName != 'right_database':
48
raise WrongDatabaseError(databaseName, 'right_database')
53
def upload_request(request):
54
"""Librarian upload server test helper, process a request and report what
57
Hands a request to a librarian file upload protocol, and prints the reply
58
from the server, a summary of the file uploaded, and whether the connection
62
file u'foo.txt' stored as text/plain, contents: 'Foo!'
66
reply: '400 STORE command expects the filename to be in UTF-8'
69
Note that the Librarian itself except for the protocol logic is stubbed out
70
by this function; it's intended to be used to unit test the protocol
71
implementation, not end-to-end test the Librarian.
73
# Send tracebacks from Twisted to stderr, if they occur, to make debugging
74
# test failures easier.
77
print >> sys.stderr, x
79
x['failure'].printTraceback(file=sys.stderr)
80
from twisted.python import log
81
log.addObserver(log_observer)
83
# Create a FileUploadProtocol, and instrument it for testing:
84
server = FileUploadProtocol()
86
# * hook _storeFile to dispatch straight to newFile.store without
88
from twisted.internet import defer
89
server._storeFile = lambda: defer.maybeDeferred(server.newFile.store)
91
# * give it a fake transport
92
server.transport = MockTransport()
93
server.connectionMade()
95
# * give it a fake factory (itself!), and a fake library.
96
server.factory = server
97
server.fileLibrary = MockLibrary()
100
server.dataReceived(request.replace('\n', '\r\n'))
102
# Report on what happened
103
print "reply: %r" % server.transport.bytesWritten.rstrip('\r\n')
105
if server.transport.connectionLost:
106
print 'connection closed'
108
mockFile = server.fileLibrary.file
109
if mockFile is not None and mockFile.stored:
110
print "file %r stored as %s, contents: %r" % (
111
mockFile.name, mockFile.mimetype, mockFile.bytes)
113
# Cleanup: remove the observer.
114
log.removeObserver(log_observer)
118
return LayeredDocFileSuite(
119
'test_upload.txt', globs={'upload_request': upload_request},
120
stdout_logging=False)