~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
= Launchpad's WADL caching =

Because Launchpad's main WADL files are so big, we cache them
internally: one WADL file for every version of the web service.
Because the WADL only changes when the Launchpad software changes,
these documents are cached to files.

This test shows how the cache works. We'll start by temporarily
clearing the cache.

    >>> from canonical.launchpad.systemhomes import WebServiceApplication
    >>> old_cached_wadl = WebServiceApplication.cached_wadl
    >>> WebServiceApplication.cached_wadl = {}

If WADL is present in a certain file on disk--the filename depends on
the Launchpad configuration and the web service version--it will be
loaded from disk and not generated from scratch. But the test config
does not have any WADL files written to disk.

    >>> import os
    >>> from canonical.config import config
    >>> wadl_filename = WebServiceApplication.cachedWADLPath(
    ...     config.instance_name, 'devel')
    >>> os.path.exists(wadl_filename)
    False

Let's write some fake WADL to disk.

    >>> fd = open(wadl_filename, "w")
    >>> fd.write("Some fake WADL.")
    >>> fd.close()

When we request the WADL for version "devel", the fake WADL is loaded
from disk.

    >>> print webservice.get(
    ...     '/', 'application/vd.sun.wadl+xml', api_version='devel').body
    Some fake WADL.

The fake WADL is now present in the cache.

    >>> WebServiceApplication.cached_wadl
    {u'devel': u'Some fake WADL.'}

Change the cached value, and we change the document served.

    >>> WebServiceApplication.cached_wadl['devel'] = "More fake WADL."

    >>> print webservice.get(
    ...     '/', 'application/vd.sun.wadl+xml', api_version='devel').body
    More fake WADL.

If there's no value in the cache and no cached file on disk, the WADL
is generated from scratch.

    >>> WebServiceApplication.cached_wadl = {}
    >>> os.remove(wadl_filename)

    >>> wadl = webservice.get(
    ...     '/', 'application/vd.sun.wadl+xml', api_version='devel').body
    >>> wadl = wadl.decode('UTF-8')

Unlike the test strings we used earlier, this is a valid WADL file.

    >>> from canonical.lazr.xml import XMLValidator
    >>> from lazr.restful import WADL_SCHEMA_FILE
    >>> wadl_schema = XMLValidator(WADL_SCHEMA_FILE)

    # We need to replace the nbsp entity, because the validator
    # doesn't support embedded definition.
    >>> wadl_schema.validate(
    ...     wadl.replace(' ', ' ').encode('UTF-8'))
    True

The WADL we received is keyed to the 'devel' version of the web
service. The URL to this version's service root will always be
present.

    >>> 'http://api.launchpad.dev/devel/' in wadl
    True

If we retrieve the WADL for the '1.0' version of the web service,
we'll get a document keyed to the '1.0' version.

    >>> wadl_10 = webservice.get(
    ...     '/', 'application/vd.sun.wadl+xml', api_version='1.0').body
    >>> wadl_10 = wadl_10.decode('UTF-8')

    >>> 'http://api.launchpad.dev/1.0/' in wadl_10
    True

All of these documents were cached as they were generated:

    >>> sorted(WebServiceApplication.cached_wadl.keys())
    [u'1.0', u'devel']

Finally, restore the cache so that other tests will have a clean
slate.

    >>> WebServiceApplication.cached_wadl = old_cached_wadl