~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
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Test the modified branches script."""

__metaclass__ = type

from datetime import datetime
import os

import pytz

from lp.code.enums import BranchType
from lp.codehosting.scripts.modifiedbranches import ModifiedBranchesScript
from lp.codehosting.vfs import branch_id_to_path
from lp.services.config import config
from lp.services.scripts.base import LaunchpadScriptFailure
from lp.testing import (
    TestCase,
    TestCaseWithFactory,
    )
from lp.testing.layers import DatabaseFunctionalLayer


class TestModifiedBranchesLocations(TestCaseWithFactory):
    """Test the ModifiedBranchesScript.branch_locations method."""

    layer = DatabaseFunctionalLayer

    def test_branch(self):
        # A branch location is the physical disk directory.
        branch = self.factory.makeAnyBranch(branch_type=BranchType.HOSTED)
        script = ModifiedBranchesScript('modified-branches', test_args=[])
        location = script.branch_location(branch)
        path = branch_id_to_path(branch.id)
        self.assertEqual(
            os.path.join(config.codehosting.mirrored_branches_root, path),
            location)


class TestModifiedBranchesLastModifiedEpoch(TestCase):
    """Test the calculation of the last modifed date."""

    def test_no_args(self):
        # The script needs one of --since or --last-hours to be specified.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=[])
        self.assertRaises(
            LaunchpadScriptFailure,
            script.get_last_modified_epoch)

    def test_both_args(self):
        # We don't like it if both --since and --last-hours are specified.
        script = ModifiedBranchesScript(
            'modified-branches',
            test_args=['--since=2009-03-02', '--last-hours=12'])
        self.assertRaises(
            LaunchpadScriptFailure,
            script.get_last_modified_epoch)

    def test_modified_since(self):
        # The --since parameter is parsed into a datetime using the fairly
        # standard YYYY-MM-DD format.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--since=2009-03-02'])
        self.assertEqual(
            datetime(2009, 3, 2, tzinfo=pytz.UTC),
            script.get_last_modified_epoch())

    def test_modified_since_bad_format(self):
        # Passing in a bad format string for the --since parameter errors.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--since=2009-03'])
        self.assertRaises(
            LaunchpadScriptFailure,
            script.get_last_modified_epoch)

    def test_modified_last_hours(self):
        # If last_hours is specified, that number of hours is removed from the
        # current timestamp to work out the selection epoch.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--last-hours=12'])
        # Override the script's now_timestamp to have a definitive test.
        # 3pm on the first of January.
        script.now_timestamp = datetime(2009, 1, 1, 15, tzinfo=pytz.UTC)
        # The last modified should be 3am on the same day.
        self.assertEqual(
            datetime(2009, 1, 1, 3, tzinfo=pytz.UTC),
            script.get_last_modified_epoch())


class TestModifiedBranchesStripPrefix(TestCase):
    """Test the prefix stripping."""

    def test_no_args(self):
        # The prefix defaults for '/srv/' for the ease of the main callers.
        # Still need to pass in one of --since or --last-hours.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--last-hours=12'])
        self.assertEqual('/srv/', script.options.strip_prefix)

    def test_override(self):
        # The default can be overrided with the --strip-prefix option.
        # Still need to pass in one of --since or --last-hours.
        script = ModifiedBranchesScript(
            'modified-branches',
            test_args=['--last-hours=12', '--strip-prefix=foo'])
        self.assertEqual('foo', script.options.strip_prefix)

    def test_prefix_is_stripped(self):
        # If the location starts with the prefix, it is stripped.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--last-hours=12'])
        # Override the append_suffix as we aren't testing that here.
        script.options.append_suffix = ''
        location = script.process_location('/srv/testing')
        self.assertEqual('testing', location)

    def test_non_matching_location_unchanged(self):
        # If the location doesn't match, it is left unchanged.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--last-hours=12'])
        # Override the append_suffix as we aren't testing that here.
        script.options.append_suffix = ''
        location = script.process_location('/var/testing')
        self.assertEqual('/var/testing', location)


class TestModifiedBranchesAppendSuffix(TestCase):
    """Test the suffix appending."""

    def test_no_args(self):
        # The suffix defaults for '/**' for the ease of the main callers.
        # Still need to pass in one of --since or --last-hours.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--last-hours=12'])
        self.assertEqual('/**', script.options.append_suffix)

    def test_override(self):
        # The default can be overrided with the --append-suffix option.
        # Still need to pass in one of --since or --last-hours.
        script = ModifiedBranchesScript(
            'modified-branches',
            test_args=['--last-hours=12', '--append-suffix=foo'])
        self.assertEqual('foo', script.options.append_suffix)

    def test_suffix_appended(self):
        # The suffix is appended to all branch locations.
        script = ModifiedBranchesScript(
            'modified-branches', test_args=['--last-hours=12'])
        location = script.process_location('/var/testing')
        self.assertEqual('/var/testing/**', location)


class TestModifiedBranchesUpdateLocations(TestCase):
    """Test the path splitting an reassebly adding to locations."""

    def setUp(self):
        super(TestModifiedBranchesUpdateLocations, self).setUp()
        self.script = ModifiedBranchesScript(
            'modified-branches', test_args=['--last-hours=12'])

    def test_locations_initially_empty(self):
        # The locations starts out as an empty set.
        self.assertEqual(set(), self.script.locations)

    def test_single_path_element(self):
        # Adding a single element should just add that.
        self.script.update_locations('foo')
        self.assertEqual(set(['foo']), self.script.locations)

    def test_single_root_element(self):
        # If the single element starts with a /, the locations do not include
        # an empty string.
        self.script.update_locations('/foo')
        self.assertEqual(set(['/foo']), self.script.locations)

    def test_multi_path_element(self):
        # Adding a "real" path will also include all the parents.
        self.script.update_locations('foo/bar/baz')
        expected = set(['foo', 'foo/bar', 'foo/bar/baz'])
        self.assertEqual(expected, self.script.locations)

    def test_duplicates(self):
        # Adding paths with common parentage doesn't cause duplicates.
        self.script.update_locations('foo/bar/baz')
        self.script.update_locations('foo/bar/who')
        expected = set(['foo', 'foo/bar', 'foo/bar/baz', 'foo/bar/who'])
        self.assertEqual(expected, self.script.locations)