~asbalderson/jenkins-launchpad-plugin/private-repo

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
import unittest
from mock import patch
from mock import MagicMock
from jlp import BzrRecipe
from textwrap import dedent


class TestBzrRecipeInstantiation(unittest.TestCase):
    def test_check_full_recipe(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            expected = dedent('''\
                              # bzr-builder format 0.4
                              lp:dummy revno:14
                              nest-part ubuntu lp:dummy/packaging debian
                              ''')
            recipe.generateRecipe()
            self.assertEquals(recipe.recipe, expected)
            self.assertEquals(recipe.getTrunkRevno(), 14)

    def test_check_recipe_without_explicit_revision(self):
        bzr = MagicMock()
        bzr.revno = lambda self: 10
        with patch('jlp.bzrrecipe.Bazaar', new=MagicMock(return_value=bzr)):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging')
            expected = dedent('''\
                              # bzr-builder format 0.4
                              lp:dummy revno:10
                              nest-part ubuntu lp:dummy/packaging debian
                              ''')
            recipe.generateRecipe()
            self.assertEquals(recipe.recipe, expected)
            self.assertEquals(recipe.getTrunkRevno(), 10)

    def test_check_recipe_without_packaging_branch(self):
        bzr = MagicMock()
        bzr.revno = lambda self: 10
        with patch('jlp.bzrrecipe.Bazaar', new=MagicMock(return_value=bzr)):
            recipe = BzrRecipe('lp:dummy')
            expected = dedent('''\
                              # bzr-builder format 0.4
                              lp:dummy revno:10
                              # No packaging branch
                              ''')
            recipe.generateRecipe()
            self.assertEquals(recipe.recipe, expected)
            self.assertEquals(recipe.getTrunkRevno(), 10)

    def test_check_recipe_with_inline_packaging(self):
        bzr = MagicMock()
        bzr.revno = lambda self: 10
        with patch('jlp.bzrrecipe.Bazaar', new=MagicMock(return_value=bzr)):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy')
            expected = dedent('''\
                              # bzr-builder format 0.4
                              lp:dummy revno:10
                              # No packaging branch
                              ''')
            recipe.generateRecipe()
            self.assertEquals(recipe.recipe, expected)
            self.assertEquals(recipe.getTrunkRevno(), 10)

    def test_get_packaging_branch_revno(self):
        bzr = MagicMock()
        bzr.revno = lambda self: 10
        with patch('jlp.bzrrecipe.Bazaar', new=MagicMock(return_value=bzr)):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            self.assertEquals(recipe.getPackagingBranchRevno(), 10)

    def test_getDir_before_tree_building(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            self.assertIsNone(recipe.getTrunkDir())
            self.assertIsNone(recipe.getBuildDir())

    def test_revert(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            recipe.bzr = MagicMock()
            recipe.revert()
            self.assertEqual(recipe.bzr.revert.call_count, 1)


class TestBzrRecipeParsin(unittest.TestCase):
    xpathselect = dedent('''\
            Source: xpathselect
            Version: 1.0
            Distribution: unstable
            Urgency: low
            Maintainer: Thomi Richards <thomi.richards@canonical.com>
            Date: Thu, 09 Aug 2012 12:35:54 +1200
            Closes: 1034689
            Changes:
             xpathselect (1.0) unstable; urgency=low
             .
               * Initial release (Closes: #1034689)
            ''')
    unity = dedent('''\
            Source: unity
            Version: 6.2.0-0ubuntu2
            Distribution: UNRELEASED
            Urgency: low
            Maintainer: Didier Roche <didrocks@ubuntu.com>
            Date: Mon, 13 Aug 2012 12:10:54 +0200
            Changes:
             unity (6.2.0-0ubuntu2) UNRELEASED; urgency=low
             .
               * debian/control:
                 - create unity-2d transitional packages
                 - add libdbus-glib-1-dev to build-dep list
            ''')
    invalid = dedent('''\
            Source-65:234 unity
            Distribution: UNRELEASED
            Urgency: low
            Maintainer: Didier Roche <didrocks@ubuntu.com>
            Date: Mon, 13 Aug 2012 12:10:54 +0200
            Changes:
             unity (6.2.0-0ubuntu2) UNRELEASED; urgency=low
             .
               * debian/control:
                 - create unity-2d transitional packages
                 - add libdbus-glib-1-dev to build-dep list
            ''')

    def test_parse_unity_version(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            version = recipe._parseVersion(self.unity)
            self.assertEqual(version, '6.2.0-0ubuntu2')
            self.assertEqual(recipe.upstreamVersion, '6.2.0')

    def test_parse_unity_sourcename(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            name = recipe._parseSourceName(self.unity)
            self.assertEqual(name, 'unity')

    def test_parse_invalid_version(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            version = recipe._parseVersion(self.invalid)
            self.assertIsNone(version)

    def test_parse_invalid_sourcename(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            name = recipe._parseSourceName(self.invalid)
            self.assertIsNone(name)

    def test_parse_xpathselect_version(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            version = recipe._parseVersion(self.xpathselect)
            self.assertEqual(version, '1.0')
            self.assertEqual(recipe.upstreamVersion, '1.0')

    def test_parse_xpathselect_sourcename(self):
        with patch('jlp.bzrrecipe.Bazaar'):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            name = recipe._parseSourceName(self.xpathselect)
            self.assertEqual(name, 'xpathselect')

    def test_parse_changelog(self):
        with patch('jlp.bzrrecipe.Bazaar'),\
                patch('jlp.bzrrecipe.subprocess.check_output',
                      new=MagicMock(return_value=self.unity)):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            self.assertTrue(recipe._parseChangelog())
            self.assertEqual(recipe.getSourceName(), 'unity')
            self.assertEqual(recipe.getUpstreamVersion(), '6.2.0')
            self.assertEqual(recipe.getPackageVersion(), '6.2.0-0ubuntu2')

    def test_parse_changelog_invalid_version(self):
        with patch('jlp.bzrrecipe.Bazaar'),\
            patch('jlp.bzrrecipe.subprocess.check_output',
                  new=MagicMock(return_value=self.invalid)):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            recipe._parseVersion = lambda x: False
            recipe._parseSourceName = lambda x: True
            self.assertFalse(recipe._parseChangelog())

    def test_parse_changelog_invalid_sourcename(self):
        with patch('jlp.bzrrecipe.Bazaar'),\
                patch('jlp.bzrrecipe.subprocess.check_output',
                      new=MagicMock(return_value=self.invalid)):
            recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)
            recipe._parseVersion = lambda x: True
            self.assertFalse(recipe._parseChangelog())


class TestBzrRecipe(unittest.TestCase):
    def setUp(self):
        bzr = MagicMock()
        bzr.revno = lambda self: 10
        bzr.build = lambda recipe, target: ''
        self.temp_dir = '/tmp/yadda'
        self.open_file = MagicMock()
        self.bazaar_patch = patch('jlp.bzrrecipe.Bazaar',
                                  new=MagicMock(return_value=bzr))
        self.bazaar = self.bazaar_patch.start()
        self.mkdtemp_patch = patch(
            'jlp.bzrrecipe.mkdtemp',
            new=MagicMock(return_value=self.temp_dir))
        self.mkdtemp = self.mkdtemp_patch.start()
        self.open_file_patch = patch(
            'jlp.bzrrecipe.open',
            create=True,
            new=MagicMock(return_value=self.open_file))
        self.open_file_patch.start()
        self.recipe = BzrRecipe('lp:dummy', 'lp:dummy/packaging', 14)

    def tearDown(self):
        self.bazaar_patch.stop()
        self.mkdtemp_patch.stop()
        self.open_file_patch.stop()


class TestBzrRecipeBuildTree(TestBzrRecipe):
    def test_buildTree(self):
            self.recipe.buildTree()
            self.assertEquals(
                self.open_file.__enter__.return_value.write.call_count, 1)
            self.assertEquals(self.recipe.getBuildDir(), self.temp_dir)
            self.assertEquals(self.recipe.getTrunkDir(),
                              self.temp_dir + '/trunk')


class TestBzrRecipeBuildTreeTwice(TestBzrRecipe):
    def test_buildTree_twice(self):
            self.recipe.buildTree()
            self.recipe.buildTree()
            self.assertEquals(
                self.open_file.__enter__.return_value.write.call_count, 1)
            self.assertEquals(self.recipe.getBuildDir(), self.temp_dir)
            self.assertEquals(self.recipe.getTrunkDir(),
                              self.temp_dir + '/trunk')


class TestBzrRecipeMethods(TestBzrRecipe):
    def test_create_tarball(self):
        call = MagicMock()
        with patch('jlp.bzrrecipe.subprocess.call', new=call):
                self.recipe.createTarball('filename')
                expected_call = ['tar', '-czf', self.temp_dir + '/filename',
                                 '-C', self.temp_dir, 'trunk']
                call.assert_called_once_with(expected_call)

    def test_cleanup(self):
        call = MagicMock()
        with patch('jlp.bzrrecipe.subprocess.call', new=call):
                self.recipe.buildTree()
                self.recipe.cleanup()
                call.assert_called_once_with(['rm', '-rf', self.temp_dir])