~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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for the dynamic RewriteMap used to serve branches over HTTP."""

__metaclass__ = type

import os
import re
import signal
import subprocess
import unittest

import transaction
from zope.security.proxy import removeSecurityProxy

from canonical.config import config
from canonical.testing.layers import DatabaseFunctionalLayer
from lp.code.interfaces.codehosting import branch_id_alias
from lp.codehosting.rewrite import BranchRewriter
from lp.codehosting.vfs import branch_id_to_path
from lp.services.log.logger import BufferLogger
from lp.testing import (
    FakeTime,
    TestCaseWithFactory,
    )


class TestBranchRewriter(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestBranchRewriter, self).setUp()
        self.fake_time = FakeTime(0)

    def makeRewriter(self):
        return BranchRewriter(BufferLogger(), self.fake_time.now)

    def getLoggerOutput(self, rewriter):
        return rewriter.logger.getLogBuffer()

    def test_rewriteLine_found_dot_bzr(self):
        # Requests for /$branch_name/.bzr/... are redirected to where the
        # branches are served from by ID.
        rewriter = self.makeRewriter()
        branches = [
            self.factory.makeProductBranch(),
            self.factory.makePersonalBranch(),
            self.factory.makePackageBranch()]
        transaction.commit()
        output = [
            rewriter.rewriteLine("/%s/.bzr/README" % branch.unique_name)
            for branch in branches]
        expected = [
            'file:///var/tmp/bazaar.launchpad.dev/mirrors/%s/.bzr/README'
            % branch_id_to_path(branch.id)
            for branch in branches]
        self.assertEqual(expected, output)

    def test_rewriteLine_found_not_dot_bzr(self):
        # Requests for /$branch_name/... that are not to .bzr directories are
        # redirected to codebrowse.
        rewriter = self.makeRewriter()
        branches = [
            self.factory.makeProductBranch(),
            self.factory.makePersonalBranch(),
            self.factory.makePackageBranch()]
        transaction.commit()
        output = [
            rewriter.rewriteLine("/%s/changes" % branch.unique_name)
            for branch in branches]
        expected = [
            'http://localhost:8080/%s/changes' % branch.unique_name
            for branch in branches]
        self.assertEqual(expected, output)

    def test_rewriteLine_private(self):
        # All requests for /$branch_name/... for private branches are
        # rewritten to codebrowse, which will then redirect them to https and
        # handle them there.
        rewriter = self.makeRewriter()
        branch = self.factory.makeAnyBranch(private=True)
        unique_name = removeSecurityProxy(branch).unique_name
        transaction.commit()
        output = [
            rewriter.rewriteLine("/%s/changes" % unique_name),
            rewriter.rewriteLine("/%s/.bzr" % unique_name)
            ]
        self.assertEqual(
            ['http://localhost:8080/%s/changes' % unique_name,
             'http://localhost:8080/%s/.bzr' % unique_name],
            output)

    def test_rewriteLine_id_alias_found_dot_bzr(self):
        # Requests for /+branch-id/$id/.bzr/... are redirected to where the
        # branches are served from by ID if they are public.
        rewriter = self.makeRewriter()
        branches = [
            self.factory.makeProductBranch(private=False),
            self.factory.makePersonalBranch(private=False),
            self.factory.makePackageBranch(private=False)]
        transaction.commit()
        output = [
            rewriter.rewriteLine(
                "%s/.bzr/README" % branch_id_alias(branch))
            for branch in branches]
        expected = [
            'file:///var/tmp/bazaar.launchpad.dev/mirrors/%s/.bzr/README'
            % branch_id_to_path(branch.id)
            for branch in branches]
        self.assertEqual(expected, output)

    def test_rewriteLine_id_alias_private(self):
        # All requests for /+branch-id/$id/... for private branches return
        # 'NULL'.  This is translated by apache to a 404.
        rewriter = self.makeRewriter()
        branch = self.factory.makeAnyBranch(private=True)
        path = branch_id_alias(removeSecurityProxy(branch))
        transaction.commit()
        output = [
            rewriter.rewriteLine("%s/changes" % path),
            rewriter.rewriteLine("%s/.bzr" % path)
            ]
        self.assertEqual(['NULL', 'NULL'], output)

    def test_rewriteLine_id_alias_logs_cache_hit(self):
        # The second request for a branch using the alias hits the cache.
        rewriter = self.makeRewriter()
        branch = self.factory.makeAnyBranch()
        transaction.commit()
        path = "%s/.bzr/README" % branch_id_alias(branch)
        rewriter.rewriteLine(path)
        rewriter.rewriteLine(path)
        logging_output_lines = self.getLoggerOutput(
            rewriter).strip().split('\n')
        self.assertEqual(2, len(logging_output_lines))
        self.assertIsNot(
            None,
            re.match("INFO .* -> .* (.*s, cache: HIT)",
                     logging_output_lines[-1]),
            "No hit found in %r" % logging_output_lines[-1])

    def test_rewriteLine_static(self):
        # Requests to /static are rewritten to codebrowse urls.
        rewriter = self.makeRewriter()
        output = rewriter.rewriteLine("/static/foo")
        self.assertEqual(
            'http://localhost:8080/static/foo',
            output)

    def test_rewriteLine_not_found(self):
        # If the request does not map to a branch, we redirect it to
        # codebrowse as it can generate a 404.
        rewriter = self.makeRewriter()
        not_found_path = "/~nouser/noproduct"
        output = rewriter.rewriteLine(not_found_path)
        self.assertEqual(
            'http://localhost:8080%s' % not_found_path,
            output)

    def test_rewriteLine_logs_cache_miss(self):
        # The first request for a branch misses the cache and logs this fact.
        rewriter = self.makeRewriter()
        branch = self.factory.makeAnyBranch()
        transaction.commit()
        rewriter.rewriteLine('/' + branch.unique_name + '/.bzr/README')
        logging_output = self.getLoggerOutput(rewriter)
        self.assertIsNot(
            None,
            re.match("INFO .* -> .* (.*s, cache: MISS)", logging_output),
            "No miss found in %r" % logging_output)

    def test_rewriteLine_logs_cache_hit(self):
        # The second request for a branch misses the cache and logs this fact.
        rewriter = self.makeRewriter()
        branch = self.factory.makeAnyBranch()
        transaction.commit()
        rewriter.rewriteLine('/' + branch.unique_name + '/.bzr/README')
        rewriter.rewriteLine('/' + branch.unique_name + '/.bzr/README')
        logging_output_lines = self.getLoggerOutput(rewriter).strip().split('\n')
        self.assertEqual(2, len(logging_output_lines))
        self.assertIsNot(
            None,
            re.match("INFO .* -> .* (.*s, cache: HIT)",
                     logging_output_lines[-1]),
            "No hit found in %r" % logging_output_lines[-1])

    def test_rewriteLine_cache_expires(self):
        # The second request for a branch misses the cache and logs this fact.
        rewriter = self.makeRewriter()
        branch = self.factory.makeAnyBranch()
        transaction.commit()
        rewriter.rewriteLine('/' + branch.unique_name + '/.bzr/README')
        self.fake_time.advance(
            config.codehosting.branch_rewrite_cache_lifetime + 1)
        rewriter.rewriteLine('/' + branch.unique_name + '/.bzr/README')
        logging_output_lines = self.getLoggerOutput(rewriter).strip().split('\n')
        self.assertEqual(2, len(logging_output_lines))
        self.assertIsNot(
            None,
            re.match("INFO .* -> .* (.*s, cache: MISS)",
                     logging_output_lines[-1]),
            "No miss found in %r" % logging_output_lines[-1])

    def test_getBranchIdAndTrailingPath_cached(self):
        """When results come from cache, they should be the same."""
        rewriter = self.makeRewriter()
        branch = self.factory.makeAnyBranch()
        transaction.commit()
        id_path = (branch.id, u'/.bzr/README',)
        result = rewriter._getBranchIdAndTrailingPath(
            '/' + branch.unique_name + '/.bzr/README')
        self.assertEqual(id_path + ('MISS',), result)
        result = rewriter._getBranchIdAndTrailingPath(
            '/' + branch.unique_name + '/.bzr/README')
        self.assertEqual(id_path + ('HIT',), result)


class TestBranchRewriterScript(TestCaseWithFactory):
    """Acceptance test for the branch-rewrite.py script."""

    layer = DatabaseFunctionalLayer

    def test_script(self):
        branches = [
            self.factory.makeProductBranch(),
            self.factory.makePersonalBranch(),
            self.factory.makePackageBranch()]
        input_lines = [
            "/%s/.bzr/README" % branch.unique_name for branch in branches] + [
            "/%s/changes" % branch.unique_name for branch in branches]
        expected_lines = [
            'file:///var/tmp/bazaar.launchpad.dev/mirrors/%s/.bzr/README'
            % branch_id_to_path(branch.id)
            for branch in branches] + [
            'http://localhost:8080/%s/changes' % branch.unique_name
            for branch in branches]
        transaction.commit()
        script_file = os.path.join(
            config.root, 'scripts', 'branch-rewrite.py')
        proc = subprocess.Popen(
            [script_file], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            stderr=subprocess.PIPE, bufsize=0)
        output_lines = []
        # For each complete line of input, the script should, without
        # buffering, write a complete line of output.
        for input_line in input_lines:
            proc.stdin.write(input_line + '\n')
            output_lines.append(proc.stdout.readline().rstrip('\n'))
        # If we create a new branch after the branch-rewrite.py script has
        # connected to the database, or edit a branch name that has already
        # been rewritten, both are rewritten successfully.
        new_branch = self.factory.makeAnyBranch()
        edited_branch = removeSecurityProxy(branches[0])
        edited_branch.name = self.factory.getUniqueString()
        transaction.commit()

        new_branch_input = '/%s/.bzr/README' % new_branch.unique_name
        expected_lines.append(
            'file:///var/tmp/bazaar.launchpad.dev/mirrors/%s/.bzr/README'
            % branch_id_to_path(new_branch.id))
        proc.stdin.write(new_branch_input + '\n')
        output_lines.append(proc.stdout.readline().rstrip('\n'))

        edited_branch_input = '/%s/.bzr/README' % edited_branch.unique_name
        expected_lines.append(
            'file:///var/tmp/bazaar.launchpad.dev/mirrors/%s/.bzr/README'
            % branch_id_to_path(edited_branch.id))
        proc.stdin.write(edited_branch_input + '\n')
        output_lines.append(proc.stdout.readline().rstrip('\n'))

        os.kill(proc.pid, signal.SIGINT)
        err = proc.stderr.read()
        # The script produces logging output, but not to stderr.
        self.assertEqual('', err)
        self.assertEqual(expected_lines, output_lines)


def test_suite():
    return unittest.TestLoader().loadTestsFromName(__name__)