~launchpad-pqm/launchpad/devel

620 by Canonical.com Patch Queue Manager
Now checks pagetests on merge. Much work on page tests. Refactor of forgotten password pages.
1
#!/usr/bin/env python2.3
44 by Canonical.com Patch Queue Manager
added test_on_merge.py
2
# Copyright 2004 Canonical Ltd.  All rights reserved.
3
4
"""Tests that get run automatically on a merge."""
5
6
import sys
657 by Canonical.com Patch Queue Manager
Tabnanny putting on her jackboots for the whitespace impared
7
import os, os.path
759 by Canonical.com Patch Queue Manager
Make test_on_merge.py check test results more accurately (Bug #2155)
8
import popen2
657 by Canonical.com Patch Queue Manager
Tabnanny putting on her jackboots for the whitespace impared
9
import tabnanny
699 by Canonical.com Patch Queue Manager
Now checks for new or changes tags on merge.
10
import checkarchtag
949 by Canonical.com Patch Queue Manager
page template improvements; add page template title check
11
import checktitles
657 by Canonical.com Patch Queue Manager
Tabnanny putting on her jackboots for the whitespace impared
12
from StringIO import StringIO
816 by Canonical.com Patch Queue Manager
Enabled cookie-auth, and various other incidental things.
13
from threading import Thread
940 by Canonical.com Patch Queue Manager
Full text indexing, database schema patches
14
import psycopg
816 by Canonical.com Patch Queue Manager
Enabled cookie-auth, and various other incidental things.
15
16
class NonBlockingReader(Thread):
17
18
    result = None
19
20
    def __init__(self,file):
21
        Thread.__init__(self)
22
        self.file = file
23
24
    def run(self):
25
        self.result = self.file.read()
26
27
    def read(self):
28
        if self.result is None:
29
            raise RuntimeError("read() called before run()")
30
        return self.result
31
32
    def readlines(self):
33
        if self.result is None:
34
            raise RuntimeError("readlines() called before run()")
35
        return self.result.splitlines()
36
44 by Canonical.com Patch Queue Manager
added test_on_merge.py
37
38
def main():
39
    """Call test.py with whatever arguments this script was run with.
40
41
    If the tests ran ok (last line of stderr is 'OK<return>') then suppress
42
    output and exit(0).
43
44
    Otherwise, print output and exit(1).
45
    """
45 by Canonical.com Patch Queue Manager
improve test_on_merge.py
46
    here = os.path.dirname(os.path.realpath(__file__))
657 by Canonical.com Patch Queue Manager
Tabnanny putting on her jackboots for the whitespace impared
47
699 by Canonical.com Patch Queue Manager
Now checks for new or changes tags on merge.
48
    if not checkarchtag.is_tree_good():
49
        return 1
50
949 by Canonical.com Patch Queue Manager
page template improvements; add page template title check
51
    checktitles.summarise_directory("lib/canonical/launchpad/templates")
52
856 by Canonical.com Patch Queue Manager
++resource++ URLs should be absolute and favicon
53
    # Tabnanny
657 by Canonical.com Patch Queue Manager
Tabnanny putting on her jackboots for the whitespace impared
54
    org_stdout = sys.stdout
55
    sys.stdout = StringIO()
56
    tabnanny.check(os.path.join(here, 'lib', 'canonical'))
57
    tabnanny_results = sys.stdout.getvalue()
58
    sys.stdout = org_stdout
59
    if len(tabnanny_results) > 0:
60
        print '---- tabnanny bitching ----'
61
        print tabnanny_results
62
        print '---- end tabnanny bitching ----'
63
        return 1
706 by Canonical.com Patch Queue Manager
Make merge check use full test suite
64
856 by Canonical.com Patch Queue Manager
++resource++ URLs should be absolute and favicon
65
    # Ensure ++resource++ URL's are all absolute - this ensures they
66
    # are cache friendly
67
    results = os.popen(
68
        "find lib/canonical -type f | xargs grep '[^/]++resource++'"
69
        ).readlines()
70
    if results:
71
        print '---- non-absolute ++resource++ URLs found ----'
72
        print ''.join(results)
73
        print '---- end non-absolute ++resource++ URLs found ----'
74
        return 1
75
940 by Canonical.com Patch Queue Manager
Full text indexing, database schema patches
76
    # Drop the template database if it exists - the Makefile does this
77
    # too, but we can explicity check for errors here
78
    con = psycopg.connect('dbname=template1')
79
    cur = con.cursor()
80
    cur.execute('end transaction; drop database launchpad_ftest_template')
81
    cur.close()
82
    con.close()
83
    
84
85
    # Build the template database. Tests duplicate this.
86
    here = os.path.dirname(os.path.realpath(__file__))
87
    schema_dir = os.path.join(here, 'database', 'schema')
88
    if os.system('cd %s; make test > /dev/null 2>&1' % schema_dir) != 0:
89
        print 'Failed to create database'
90
        return 1
91
92
    # Sanity check the database. No point running tests if the
93
    # bedrock is crumbling.
94
    con = psycopg.connect('dbname=launchpad_ftest_template')
95
    cur = con.cursor()
96
    cur.execute('show search_path')
97
    search_path = cur.fetchone()[0]
98
    if search_path != '$user,public,ts2':
99
        print 'Search path incorrect.'
100
        print 'Add the following line to /etc/postgresql/postgresql.conf:'
101
        print "    search_path = '$user,public,ts2'"
102
        return 1
103
    cur.execute("""
104
        select count(*) from person where displayname='Mark Shuttleworth'
105
        """)
106
    cnt = cur.fetchone()[0]
107
    if cnt < 1:
108
        print 'Sample data not loaded.'
109
        return 1
110
    cur.execute("""
111
        select pg_encoding_to_char(encoding) as encoding from pg_database
112
        where datname='launchpad_ftest_template'
113
        """)
114
    enc = cur.fetchone()[0]
115
    if enc != 'UNICODE':
116
        print 'Database encoding incorrectly set'
117
        return 1
118
    # Explicity close our connections - things will fail if we leave open
119
    # connections.
120
    cur.close()
121
    del cur
122
    con.close()
123
    del con
124
    
125
706 by Canonical.com Patch Queue Manager
Make merge check use full test suite
126
    print 'Running tests.'
759 by Canonical.com Patch Queue Manager
Make test_on_merge.py check test results more accurately (Bug #2155)
127
    proc = popen2.Popen3('cd %s; python test.py %s < /dev/null' %
128
        (here, ' '.join(sys.argv[1:])), True)
129
    stdin, out, err = proc.tochild, proc.fromchild, proc.childerr
816 by Canonical.com Patch Queue Manager
Enabled cookie-auth, and various other incidental things.
130
131
    # Use non-blocking reader threads to cope with differing expectations
132
    # from the proess of when to consume data from out and error.
133
    errthread = NonBlockingReader(err)
134
    outthread = NonBlockingReader(out)
135
    errthread.start()
136
    outthread.start()
137
    errthread.join()
138
    outthread.join()
759 by Canonical.com Patch Queue Manager
Make test_on_merge.py check test results more accurately (Bug #2155)
139
    exitcode = proc.wait()
761 by Canonical.com Patch Queue Manager
Make test_on_merge.py rely only on exit code, to avoid spurious failures.
140
    test_ok = (os.WIFEXITED(exitcode) and os.WEXITSTATUS(exitcode) == 0)
44 by Canonical.com Patch Queue Manager
added test_on_merge.py
141
816 by Canonical.com Patch Queue Manager
Enabled cookie-auth, and various other incidental things.
142
    errlines = errthread.readlines()
143
    dataout = outthread.read()
144
44 by Canonical.com Patch Queue Manager
added test_on_merge.py
145
    if test_ok:
658 by Canonical.com Patch Queue Manager
malone reorganisation
146
        print errlines[1]
44 by Canonical.com Patch Queue Manager
added test_on_merge.py
147
        return 0
148
    else:
149
        print '---- test stdout ----'
150
        print dataout
151
        print '---- end test stdout ----'
152
153
        print '---- test stderr ----'
830 by Canonical.com Patch Queue Manager
Work around bug in tales and traversal.
154
        print '\n'.join(errlines)
44 by Canonical.com Patch Queue Manager
added test_on_merge.py
155
        print '---- end test stderr ----'
156
        return 1
157
158
if __name__ == '__main__':
159
    sys.exit(main())