~bzr-pqm/bzr/bzr.dev

6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006, 2007, 2009-2013, 2016 Canonical Ltd
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
2
# -*- coding: utf-8 -*-
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
3
#
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
8
#
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
13
#
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
17
1185.79.3 by John Arbash Meinel
cleanups from Robert
18
"""Black-box tests for bzr sign-my-commits."""
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
19
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
20
from bzrlib import (
21
    gpg,
22
    tests,
23
    )
6352.2.3 by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/.
24
from bzrlib.tests.matchers import ContainsNoVfsCalls
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
25
26
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
27
class SignMyCommits(tests.TestCaseWithTransport):
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
28
29
    def monkey_patch_gpg(self):
30
        """Monkey patch the gpg signing strategy to be a loopback.
31
32
        This also registers the cleanup, so that we will revert to
33
        the original gpg strategy when done.
34
        """
35
        # monkey patch gpg signing mechanism
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
36
        self.overrideAttr(gpg, 'GPGStrategy', gpg.LoopbackGPGStrategy)
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
37
1185.79.3 by John Arbash Meinel
cleanups from Robert
38
    def setup_tree(self, location='.'):
39
        wt = self.make_branch_and_tree(location)
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
40
        wt.commit("base A", allow_pointless=True, rev_id='A')
41
        wt.commit("base B", allow_pointless=True, rev_id='B')
42
        wt.commit("base C", allow_pointless=True, rev_id='C')
43
        wt.commit("base D", allow_pointless=True, rev_id='D',
44
                committer='Alternate <alt@foo.com>')
5972.3.23 by Jelmer Vernooij
Fix handling of ghosts in sign_my_commits.
45
        wt.add_parent_tree_id("aghost")
46
        wt.commit("base E", allow_pointless=True, rev_id='E')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
47
        return wt
48
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
49
    def assertUnsigned(self, repo, revision_id):
50
        """Assert that revision_id is not signed in repo."""
51
        self.assertFalse(repo.has_signature_for_revision_id(revision_id))
52
53
    def assertSigned(self, repo, revision_id):
54
        """Assert that revision_id is signed in repo."""
55
        self.assertTrue(repo.has_signature_for_revision_id(revision_id))
56
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
57
    def test_sign_my_commits(self):
58
        #Test re signing of data.
59
        wt = self.setup_tree()
60
        repo = wt.branch.repository
61
62
        self.monkey_patch_gpg()
63
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
64
        self.assertUnsigned(repo, 'A')
65
        self.assertUnsigned(repo, 'B')
66
        self.assertUnsigned(repo, 'C')
67
        self.assertUnsigned(repo, 'D')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
68
69
        self.run_bzr('sign-my-commits')
70
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
71
        self.assertSigned(repo, 'A')
72
        self.assertSigned(repo, 'B')
73
        self.assertSigned(repo, 'C')
74
        self.assertUnsigned(repo, 'D')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
75
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
76
    def test_sign_my_commits_location(self):
1185.79.3 by John Arbash Meinel
cleanups from Robert
77
        wt = self.setup_tree('other')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
78
        repo = wt.branch.repository
79
80
        self.monkey_patch_gpg()
81
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
82
        self.run_bzr('sign-my-commits other')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
83
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
84
        self.assertSigned(repo, 'A')
85
        self.assertSigned(repo, 'B')
86
        self.assertSigned(repo, 'C')
87
        self.assertUnsigned(repo, 'D')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
88
89
    def test_sign_diff_committer(self):
90
        wt = self.setup_tree()
91
        repo = wt.branch.repository
92
93
        self.monkey_patch_gpg()
94
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
95
        self.run_bzr(['sign-my-commits', '.', 'Alternate <alt@foo.com>'])
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
96
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
97
        self.assertUnsigned(repo, 'A')
98
        self.assertUnsigned(repo, 'B')
99
        self.assertUnsigned(repo, 'C')
100
        self.assertSigned(repo, 'D')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
101
102
    def test_sign_dry_run(self):
103
        wt = self.setup_tree()
104
        repo = wt.branch.repository
105
106
        self.monkey_patch_gpg()
107
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
108
        out = self.run_bzr('sign-my-commits --dry-run')[0]
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
109
5972.3.6 by Jelmer Vernooij
Fix sign_my_commits test.
110
        outlines = out.splitlines()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
111
        self.assertEqual(5, len(outlines))
112
        self.assertEqual('Signed 4 revisions.', outlines[-1])
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
113
        self.assertUnsigned(repo, 'A')
114
        self.assertUnsigned(repo, 'B')
115
        self.assertUnsigned(repo, 'C')
116
        self.assertUnsigned(repo, 'D')
5972.3.23 by Jelmer Vernooij
Fix handling of ghosts in sign_my_commits.
117
        self.assertUnsigned(repo, 'E')
5971.1.9 by Jonathan Riddell
add some tests
118
6283.1.7 by Jelmer Vernooij
Add hpss call count for 'bzr sign-my-commits'.
119
120
class TestSmartServerSignMyCommits(tests.TestCaseWithTransport):
121
122
    def monkey_patch_gpg(self):
123
        """Monkey patch the gpg signing strategy to be a loopback.
124
125
        This also registers the cleanup, so that we will revert to
126
        the original gpg strategy when done.
127
        """
128
        # monkey patch gpg signing mechanism
129
        self.overrideAttr(gpg, 'GPGStrategy', gpg.LoopbackGPGStrategy)
130
131
    def test_sign_single_commit(self):
132
        self.setup_smart_server_with_call_log()
133
        t = self.make_branch_and_tree('branch')
134
        self.build_tree_contents([('branch/foo', 'thecontents')])
135
        t.add("foo")
136
        t.commit("message")
137
        self.reset_smart_call_log()
138
        self.monkey_patch_gpg()
139
        out, err = self.run_bzr(['sign-my-commits', self.get_url('branch')])
140
        # This figure represent the amount of work to perform this use case. It
141
        # is entirely ok to reduce this number if a test fails due to rpc_count
142
        # being too low. If rpc_count increases, more network roundtrips have
143
        # become necessary for this use case. Please do not adjust this number
144
        # upwards without agreement from bzr's network support maintainers.
6282.6.42 by Jelmer Vernooij
merge hpss-get-checkout-format.
145
        self.assertLength(15, self.hpss_calls)
6366.1.4 by Jelmer Vernooij
Test connection count calls for most blackbox commands.
146
        self.assertLength(1, self.hpss_connections)
6282.6.42 by Jelmer Vernooij
merge hpss-get-checkout-format.
147
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)