~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/smoke-test-librarian.py

  • Committer: Benji York
  • Date: 2010-12-03 21:59:08 UTC
  • mto: This revision was merged to the branch mainline in revision 12032.
  • Revision ID: benji.york@canonical.com-20101203215908-m3hbbj8i2h1on8ln
add a script that smoke-tests the current librarian configuration

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
"""Create a static WADL file describing the current webservice.
 
7
 
 
8
Example:
 
9
 
 
10
    % LPCONFIG=development bin/py utilities/create-lp-wadl-and-apidoc.py \\
 
11
      "lib/canonical/launchpad/apidoc/wadl-development-%(version)s.xml"
 
12
"""
 
13
import _pythonpath # Not lint, actually needed.
 
14
 
 
15
from cStringIO import StringIO
 
16
import datetime
 
17
import optparse
 
18
import os
 
19
import sys
 
20
import urllib
 
21
 
 
22
from zope.component import getUtility
 
23
import pytz
 
24
import transaction
 
25
 
 
26
from canonical.config import config
 
27
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
 
28
from canonical.launchpad.scripts import execute_zcml_for_scripts
 
29
from canonical.librarian.interfaces import (
 
30
    IRestrictedLibrarianClient,
 
31
    ILibrarianClient,
 
32
    )
 
33
 
 
34
 
 
35
FILE_SIZE = 1024
 
36
FILE_DATA = 'x' * FILE_SIZE
 
37
FILE_LIFETIME = datetime.timedelta(hours=1)
 
38
 
 
39
 
 
40
def store_file(client):
 
41
    file_id = client.addFile(
 
42
        'smoke-test-file', FILE_SIZE, StringIO(FILE_DATA), 'text/plain',
 
43
        expires=datetime.datetime.now(pytz.UTC)+FILE_LIFETIME)
 
44
    alias = getUtility(ILibraryFileAliasSet)[file_id]
 
45
    transaction.commit()
 
46
    return alias.http_url
 
47
 
 
48
 
 
49
def main():
 
50
    print 'adding a private file to the librarian...'
 
51
    restricted_client = getUtility(IRestrictedLibrarianClient)
 
52
    private_url = store_file(restricted_client)
 
53
    print 'retrieving private file from', private_url
 
54
    if urllib.urlopen(private_url).read() != FILE_DATA:
 
55
        print 'ERROR: data fetched does not match data written'
 
56
        return 1
 
57
 
 
58
    print 'adding a public file to the librarian...'
 
59
    regular_client = getUtility(ILibrarianClient)
 
60
    public_url = store_file(regular_client)
 
61
    print 'retrieving public file from', public_url
 
62
    if urllib.urlopen(public_url).read() != FILE_DATA:
 
63
        print 'ERROR: data fetched does not match data written'
 
64
        return 1
 
65
 
 
66
    return 0
 
67
 
 
68
 
 
69
if __name__ == '__main__':
 
70
    execute_zcml_for_scripts()
 
71
    sys.exit(main())