~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/librarian/smoketest.py

  • Committer: Benji York
  • Date: 2010-12-06 22:45:22 UTC
  • mto: This revision was merged to the branch mainline in revision 12032.
  • Revision ID: benji.york@canonical.com-20101206224522-w0l61jdjp506f16n
add tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python -S
 
2
#
 
3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
4
# GNU Affero General Public License version 3 (see the file LICENSE).
 
5
 
 
6
"""Perform simple librarian operations to verify the current configuration.
 
7
"""
 
8
 
 
9
from cStringIO import StringIO
 
10
import datetime
 
11
import urllib
 
12
 
 
13
from zope.component import getUtility
 
14
import pytz
 
15
import transaction
 
16
 
 
17
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
 
18
 
 
19
 
 
20
FILE_SIZE = 1024
 
21
FILE_DATA = 'x' * FILE_SIZE
 
22
FILE_LIFETIME = datetime.timedelta(hours=1)
 
23
 
 
24
 
 
25
def store_file(client):
 
26
    file_id = client.addFile(
 
27
        'smoke-test-file', FILE_SIZE, StringIO(FILE_DATA), 'text/plain',
 
28
        expires=datetime.datetime.now(pytz.UTC)+FILE_LIFETIME)
 
29
    # To be able to retrieve the file, we must commit the current transaction.
 
30
    transaction.commit()
 
31
    alias = getUtility(ILibraryFileAliasSet)[file_id]
 
32
    return alias.http_url
 
33
 
 
34
 
 
35
def read_file(url):
 
36
    try:
 
37
        data = urllib.urlopen(url).read()
 
38
    except (MemoryError, KeyboardInterrupt, SystemExit):
 
39
        # Re-raise catastrophic errors.
 
40
        raise
 
41
    except:
 
42
        # An error is represented by returning None, which won't match when
 
43
        # comapred against FILE_DATA.
 
44
        return None
 
45
 
 
46
    return data
 
47
 
 
48
 
 
49
def main(restricted_client, regular_client):
 
50
    print 'adding a private file to the librarian...'
 
51
    private_url = store_file(restricted_client)
 
52
    print 'retrieving private file from', private_url
 
53
    if read_file(private_url) != FILE_DATA:
 
54
        print 'ERROR: data fetched does not match data written'
 
55
        return 1
 
56
 
 
57
    print 'adding a public file to the librarian...'
 
58
    public_url = store_file(regular_client)
 
59
    print 'retrieving public file from', public_url
 
60
    if read_file(public_url) != FILE_DATA:
 
61
        print 'ERROR: data fetched does not match data written'
 
62
        return 1
 
63
 
 
64
    return 0