aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_changes_p4poller.py
blob: 02e0f67fa4f36a766298c3ae9ce33cb5eaef57a2 (plain)
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
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import time
from twisted.trial import unittest
from buildbot.changes.p4poller import P4Source, get_simple_split, P4PollerError
from buildbot.test.util import changesource, gpo
from buildbot.util import epoch2datetime

first_p4changes = \
"""Change 1 on 2006/04/13 by slamb@testclient 'first rev'
"""

second_p4changes = \
"""Change 3 on 2006/04/13 by bob@testclient 'short desc truncated'
Change 2 on 2006/04/13 by slamb@testclient 'bar'
"""

third_p4changes = \
"""Change 5 on 2006/04/13 by mpatel@testclient 'first rev'
"""

change_4_log = \
"""Change 4 by mpatel@testclient on 2006/04/13 21:55:39

	short desc truncated because this is a long description.
"""

change_3_log = \
u"""Change 3 by bob@testclient on 2006/04/13 21:51:39

	short desc truncated because this is a long description.
    ASDF-GUI-P3-\u2018Upgrade Icon\u2019 disappears sometimes.
"""

change_2_log = \
"""Change 2 by slamb@testclient on 2006/04/13 21:46:23

	creation
"""

p4change = {
    3: change_3_log +
"""Affected files ...

... //depot/myproject/branch_b/branch_b_file#1 add
... //depot/myproject/branch_b/whatbranch#1 branch
... //depot/myproject/branch_c/whatbranch#1 branch
""",
    2: change_2_log +
"""Affected files ...

... //depot/myproject/trunk/whatbranch#1 add
... //depot/otherproject/trunk/something#1 add
""",
    5: change_4_log +
"""Affected files ...

... //depot/myproject/branch_b/branch_b_file#1 add
... //depot/myproject/branch_b#75 edit
... //depot/myproject/branch_c/branch_c_file#1 add
""",
}


class TestP4Poller(changesource.ChangeSourceMixin,
                   gpo.GetProcessOutputMixin,
                   unittest.TestCase):
    def setUp(self):
        self.setUpGetProcessOutput()
        return self.setUpChangeSource()

    def tearDown(self):
        return self.tearDownChangeSource()

    def add_p4_describe_result(self, number, result):
        self.expectCommands(
                gpo.Expect('p4', 'describe', '-s', str(number)).stdout(result))

    def makeTime(self, timestring):
        datefmt = '%Y/%m/%d %H:%M:%S'
        when = time.mktime(time.strptime(timestring, datefmt))
        return epoch2datetime(when)

    # tests

    def test_describe(self):
        self.attachChangeSource(
                P4Source(p4port=None, p4user=None,
                         p4base='//depot/myproject/',
                         split_file=lambda x: x.split('/', 1)))
        self.assertSubstring("p4source", self.changesource.describe())

    def do_test_poll_successful(self, **kwargs):
        encoding = kwargs.get('encoding', 'utf8')
        self.attachChangeSource(
                P4Source(p4port=None, p4user=None,
                         p4base='//depot/myproject/',
                         split_file=lambda x: x.split('/', 1),
                         **kwargs))
        self.expectCommands(
                gpo.Expect('p4', 'changes', '-m', '1', '//depot/myproject/...').stdout(first_p4changes),
                gpo.Expect('p4', 'changes', '//depot/myproject/...@2,now').stdout(second_p4changes),
                )
        encoded_p4change = p4change.copy()
        encoded_p4change[3] = encoded_p4change[3].encode(encoding)
        self.add_p4_describe_result(2, encoded_p4change[2])
        self.add_p4_describe_result(3, encoded_p4change[3])

        # The first time, it just learns the change to start at.
        self.assert_(self.changesource.last_change is None)
        d = self.changesource.poll()
        def check_first_check(_):
            self.assertEquals(self.changes_added, [])
            self.assertEquals(self.changesource.last_change, 1)
        d.addCallback(check_first_check)

        # Subsequent times, it returns Change objects for new changes.
        d.addCallback(lambda _ : self.changesource.poll())
        def check_second_check(res):
            self.assertEquals(len(self.changes_added), 3)
            self.assertEquals(self.changesource.last_change, 3)

            # They're supposed to go oldest to newest, so this one must be first.
            self.assertEquals(self.changes_added[0],
                dict(author='slamb',
                     files=['whatbranch'],
                     project='',
                     comments=change_2_log,
                     revision='2',
                     when_timestamp=self.makeTime("2006/04/13 21:46:23"),
                     branch='trunk'))

            # These two can happen in either order, since they're from the same
            # Perforce change.
            if self.changes_added[1]['branch'] == 'branch_c':
                self.changes_added[1:] = reversed(self.changes_added[1:])

            self.assertEquals(self.changes_added[1],
                dict(author='bob',
                     files=['branch_b_file',
                            'whatbranch'],
                     project='',
                     comments=change_3_log, # converted to unicode correctly
                     revision='3',
                     when_timestamp=self.makeTime("2006/04/13 21:51:39"),
                     branch='branch_b'))
            self.assertEquals(self.changes_added[2],
                dict(author='bob',
                     files=['whatbranch'],
                     project='',
                     comments=change_3_log, # converted to unicode correctly
                     revision='3',
                     when_timestamp=self.makeTime("2006/04/13 21:51:39"),
                     branch='branch_c'))
            self.assertAllCommandsRan()
        d.addCallback(check_second_check)
        return d

    def test_poll_successful_default_encoding(self):
        return self.do_test_poll_successful()

    def test_poll_successful_macroman_encoding(self):
        return self.do_test_poll_successful(encoding='macroman')

    def test_poll_failed_changes(self):
        self.attachChangeSource(
                P4Source(p4port=None, p4user=None,
                         p4base='//depot/myproject/',
                         split_file=lambda x: x.split('/', 1)))
        self.expectCommands(
                gpo.Expect('p4', 'changes', '-m', '1', '//depot/myproject/...').stdout('Perforce client error:\n...'))

        # call _poll, so we can catch the failure
        d = self.changesource._poll()
        return self.assertFailure(d, P4PollerError)

    def test_poll_failed_describe(self):
        self.attachChangeSource(
                P4Source(p4port=None, p4user=None,
                         p4base='//depot/myproject/',
                         split_file=lambda x: x.split('/', 1)))
        self.expectCommands(
                gpo.Expect('p4', 'changes', '//depot/myproject/...@3,now').stdout(second_p4changes),
                )
        self.add_p4_describe_result(2, p4change[2])
        self.add_p4_describe_result(3, 'Perforce client error:\n...')

        self.changesource.last_change = 2 # tell poll() that it's already been called once

        # call _poll, so we can catch the failure
        d = self.changesource._poll()
        self.assertFailure(d, P4PollerError)
        @d.addCallback
        def check(_):
            # check that 2 was processed OK
            self.assertEquals(self.changesource.last_change, 2)
            self.assertAllCommandsRan()
        return d

    def test_poll_split_file(self):
        """Make sure split file works on branch only changes"""
        self.attachChangeSource(
                P4Source(p4port=None, p4user=None,
                         p4base='//depot/myproject/',
                         split_file=get_simple_split))
        self.expectCommands(
                gpo.Expect('p4', 'changes', '//depot/myproject/...@51,now').stdout(third_p4changes),
                )
        self.add_p4_describe_result(5, p4change[5])

        self.changesource.last_change = 50
        d = self.changesource.poll()
        def check(res):
            self.assertEquals(len(self.changes_added), 2)
            self.assertEquals(self.changesource.last_change, 5)
            self.assertAllCommandsRan()
        d.addCallback(check)
        return d

class TestSplit(unittest.TestCase):
    def test_get_simple_split(self):
        self.assertEqual(get_simple_split('foo/bar'), ('foo', 'bar'))
        self.assertEqual(get_simple_split('foo-bar'), (None, None))
        self.assertEqual(get_simple_split('/bar'), ('', 'bar'))
        self.assertEqual(get_simple_split('foo/'), ('foo', ''))