aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/component/deb/test_import_dsc.py
blob: a3b0aea963363bc5bc7085d0d8a3219e4ef66757 (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# vim: set fileencoding=utf-8 :
#
# (C) 2013,2014,2015,2017 Guido Günther <agx@sigxcpu.org>
#
#    This program 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; either version 2 of the License, or
#    (at your option) any later version.
#
#    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, please see
#    <http://www.gnu.org/licenses/>

import os

from tests.component import (ComponentTestBase,
                             ComponentTestGitRepository,
                             skipUnless)
from tests.component.deb import DEB_TEST_DATA_DIR, DEB_TEST_DOWNLOAD_URL
from tests.component.deb.fixtures import RepoFixtures

from nose.tools import ok_, eq_

from gbp.scripts.import_dsc import main as import_dsc
from gbp.deb.pristinetar import DebianPristineTar
from gbp.deb.dscfile import DscFile
from gbp.git.repository import GitRepository


class TestImportDsc(ComponentTestBase):
    """Test importing of debian source packages"""

    def _dsc30(self, version):
        return os.path.join(DEB_TEST_DATA_DIR,
                            'dsc-3.0',
                            'hello-debhelper_%s.dsc' % version)

    def _check_reflog(self, repo):
        reflog, ret = repo._git_getoutput('reflog')
        # Recent (>= 2.10) git versions create a reflog entry for "git
        # update-ref HEAD HEAD" while older ones (2.9.4) don't so
        # reflog[0] is either there or not.
        if len(reflog) == 3:
            ok_(b"gbp: Import Debian changes" in reflog[1])
            ok_(b"gbp: Import Upstream version 2.6" in reflog[2])
        elif len(reflog) == 2:
            # Furthermore some older git versions (<2.10) fail to set
            # the reflog correctly on the initial commit so only check
            # the second
            ok_(b"gbp: Import Debian changes" in reflog[0])
        else:
            ok_(len(reflog) > 3, "Reflog %s has too many lines" % reflog)
            ok_(len(reflog) < 2, "Reflog %s has too few lines" % reflog)

    def test_import_debian_native(self):
        """Test that importing of debian native packages works"""
        def _dsc(version):
            return os.path.join(DEB_TEST_DATA_DIR,
                                'dsc-native',
                                'git-buildpackage_%s.dsc' % version)

        dsc = _dsc('0.4.14')
        assert import_dsc(['arg0', dsc]) == 0
        repo = ComponentTestGitRepository('git-buildpackage')
        self._check_repo_state(repo, 'master', ['master'])
        assert len(repo.get_commits()) == 1
        commitmsg = repo.get_commit_info('HEAD')['body']
        ok_("git-buildpackage (0.01) unstable; urgency=low" in commitmsg)
        ok_("git-buildpackage (0.4.14) unstable; urgency=low" in commitmsg)

        os.chdir('git-buildpackage')
        dsc = _dsc('0.4.15')
        assert import_dsc(['arg0', dsc]) == 0
        self._check_repo_state(repo, 'master', ['master'])
        assert len(repo.get_commits()) == 2
        commitmsg = repo.get_commit_info('HEAD')['body']
        ok_("git-buildpackage (0.4.14) unstable; urgency=low" not in commitmsg)
        ok_("git-buildpackage (0.4.15) unstable; urgency=low" in commitmsg)

        dsc = _dsc('0.4.16')
        assert import_dsc(['arg0', dsc]) == 0
        self._check_repo_state(repo, 'master', ['master'])
        assert len(repo.get_commits()) == 3
        commitmsg = repo.get_commit_info('HEAD')['body']
        ok_("git-buildpackage (0.4.14) unstable; urgency=low" not in commitmsg)
        ok_("git-buildpackage (0.4.15) unstable; urgency=low" not in commitmsg)
        ok_("git-buildpackage (0.4.16) unstable; urgency=low" in commitmsg)

    @skipUnless(os.getenv("GBP_NETWORK_TESTS"), "network tests disabled")
    def test_download(self):
        def _dsc(version):
            return os.path.join(DEB_TEST_DOWNLOAD_URL,
                                'dsc-native',
                                'git-buildpackage_%s.dsc' % version)
        dsc = _dsc('0.4.14')
        assert import_dsc(['arg0',
                           '--allow-unauthenticated',
                           dsc]) == 0
        repo = ComponentTestGitRepository('git-buildpackage')
        self._check_repo_state(repo, 'master', ['master'])
        assert len(repo.get_commits()) == 1

    @skipUnless(os.getenv("GBP_NETWORK_TESTS"), "network tests disabled")
    def test_broken_download(self):
        def _not_a_dsc(version):
            return os.path.join(DEB_TEST_DOWNLOAD_URL,
                                'dsc-3.0',
                                'hello-debhelper_%s.orig.tar.gz' % version)

        f = _not_a_dsc('2.6')
        assert import_dsc(['arg0',
                           '--allow-unauthenticated',
                           f]) == 1
        self._check_log(-1, "gbp:error: Did not find a dsc file at")

    def test_create_branches(self):
        """Test that creating missing branches works"""
        dsc = self._dsc30('2.6-2')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           dsc]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        os.chdir('hello-debhelper')
        assert len(repo.get_commits()) == 2
        self._check_reflog(repo)
        self._check_repo_state(repo, 'master', ['master', 'pristine-tar', 'upstream'])
        dsc = self._dsc30('2.8-1')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=foo',
                           '--upstream-branch=bar',
                           '--create-missing-branches',
                           dsc]) == 0
        self._check_repo_state(repo, 'master', ['bar', 'foo', 'master', 'pristine-tar', 'upstream'])
        commits, expected = len(repo.get_commits()), 2
        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))

    def test_import_30_pristine_tar(self):
        dscfile = self._dsc30('2.6-1')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           dscfile]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        self._check_repo_state(repo, 'master', ['master', 'pristine-tar', 'upstream'])
        commits, expected = len(repo.get_commits()), 2
        commitmsg = repo.get_commit_info('HEAD')['body']
        eq_("hello-debhelper (2.6-1) unstable; urgency=low", commitmsg.split('\n')[0])
        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))

        os.chdir(repo.path)
        dscfile = self._dsc30('2.6-2')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           dscfile]) == 0
        commits, expected = len(repo.get_commits()), 3
        commitmsg = repo.get_commit_info('HEAD')['body']
        eq_("hello-debhelper (2.6-2) unstable; urgency=medium", commitmsg.split('\n')[0])
        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))

        commits, expected = len(repo.get_commits(until='pristine-tar')), 1
        ok_(commits == expected, "Found %d pristine-tar commits instead of %d" % (commits, expected))

    def test_import_30_additional_tarball_pristine_tar(self):
        """Test that importing a package with additional tarballs works"""
        def _dsc(version):
            return os.path.join(DEB_TEST_DATA_DIR,
                                'dsc-3.0-additional-tarballs',
                                'hello-debhelper_%s.dsc' % version)

        dscfile = _dsc('2.8-1')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           dscfile]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        self._check_repo_state(repo, 'master', ['master', 'pristine-tar', 'upstream'])
        commits, expected = len(repo.get_commits()), 2
        commitmsg = repo.get_commit_info('HEAD')['body']
        ok_("hello-debhelper (2.8-1) unstable; urgency=low" in commitmsg)
        ok_("hello (1.3-7) experimental; urgency=LOW" in commitmsg)

        for file in [b'foo/test1', b'foo/test2']:
            ok_(file in repo.ls_tree('HEAD'),
                "Could not find component tarball file %s in %s" % (file, repo.ls_tree('HEAD')))

        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))

        dsc = DscFile.parse(dscfile)
        # Check if we can rebuild the tarball and component
        ptars = [('hello-debhelper_2.8.orig.tar.gz', 'pristine-tar', '', dsc.tgz),
                 ('hello-debhelper_2.8.orig-foo.tar.gz', 'pristine-tar^', 'foo', dsc.additional_tarballs['foo'])]

        p = DebianPristineTar(repo)
        outdir = os.path.abspath('.')
        for f, w, s, o in ptars:
            eq_(repo.get_subject(w), 'pristine-tar data for %s' % f)
            old = self.hash_file(o)
            p.checkout('hello-debhelper', '2.8', 'gzip', outdir, component=s)
            new = self.hash_file(os.path.join(outdir, f))
            eq_(old, new, "Checksum %s of regenerated tarball %s does not match original %s" %
                (f, old, new))

    def test_existing_dir(self):
        """
        Importing outside of git repository with existing target
        dir must fail
        """

        # Create directory we should stumble upon
        os.makedirs('hello-debhelper')
        dsc = self._dsc30('2.8-1')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           dsc]) == 1
        self._check_log(0, "gbp:error: Directory 'hello-debhelper' already exists. If you want to import into it, "
                        "please change into this directory otherwise move it away first")

    def test_import_10(self):
        """Test that importing a 1.0 source format package works"""
        def _dsc(version):
            return os.path.join(DEB_TEST_DATA_DIR,
                                'dsc-1.0',
                                'hello-debhelper_%s.dsc' % version)

        dsc = _dsc('2.6-2')
        assert import_dsc(['arg0', dsc]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        self._check_repo_state(repo, 'master', ['master', 'upstream'],
                               tags=['upstream/2.6', 'debian/2.6-2'])
        assert len(repo.get_commits()) == 2

    def test_target_dir(self):
        """Test that setting the target dir works"""
        dsc = self._dsc30('2.6-2')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--no-pristine-tar',
                           dsc,
                           'targetdir']) == 0
        assert os.path.exists('targetdir')
        repo = ComponentTestGitRepository('targetdir')
        self._check_repo_state(repo, 'master', ['master', 'upstream'])

    def test_bare(self):
        """Test that importing into bare repository works"""
        dsc = self._dsc30('2.6-2')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           dsc]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        os.chdir('hello-debhelper')
        assert len(repo.get_commits()) == 2
        self._check_reflog(repo)
        self._check_repo_state(repo, 'master', ['master', 'pristine-tar', 'upstream'])
        commitmsg = repo.get_commit_info('HEAD')['body']
        ok_("hello-debhelper (2.6-2) unstable; urgency=medium" in commitmsg)
        ok_("hello (1.3-7) experimental; urgency=LOW" in commitmsg)

        dsc = self._dsc30('2.8-1')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           dsc]) == 0
        commits, expected = len(repo.get_commits()), 4
        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))
        commitmsg = repo.get_commit_info('HEAD')['body']
        ok_("hello-debhelper (2.8-1) unstable; urgency=low" in commitmsg)
        ok_("ello-debhelper (2.7-1) unstable; urgency=low" in commitmsg)
        ok_("hello-debhelper (2.6-2) unstable; urgency=medium" not in commitmsg)

    def test_import_environ(self):
        """Test that environment variables influence git configuration"""
        os.environ['DEBFULLNAME'] = 'testing tester'
        os.environ['DEBEMAIL'] = 'gbp-tester@debian.invalid'
        repo = RepoFixtures.import_native()
        got = repo.get_config("user.email")
        want = os.environ['DEBEMAIL']
        ok_(got == want, "unexpected git config user.email: got %s, want %s" % (got, want))

        got = repo.get_config("user.name")
        want = os.environ['DEBFULLNAME']
        ok_(got == want, "unexpected git config user.name: got %s, want %s" % (got, want))

    def test_debian_branch_not_master(self):
        """Make sure we only have debian-branch and upstream-branch after an initial import"""
        dsc = self._dsc30('2.6-2')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--no-pristine-tar',
                           '--debian-branch=pk4',
                           '--upstream-branch=upstream',
                           dsc]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        self._check_repo_state(repo, 'pk4', ['pk4', 'upstream'])
        commits, expected = len(repo.get_commits()), 2
        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))

    def test_empty_repo(self):
        """Make sure we can import into an empty repository"""
        dsc = self._dsc30('2.6-2')
        repo = GitRepository.create("hello-debhelper")
        self._check_repo_state(repo, None, [])
        os.chdir('hello-debhelper')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--no-pristine-tar',
                           '--debian-branch=pk4',
                           '--upstream-branch=upstream',
                           dsc]) == 0
        self._check_repo_state(repo, 'pk4', ['pk4', 'upstream'])
        commits, expected = len(repo.get_commits()), 2
        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))

    def test_upstream_branch_is_master(self):
        """Make sure we can import when upstream-branch == master (#750962)"""
        dsc = self._dsc30('2.6-2')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--no-pristine-tar',
                           '--debian-branch=debian',
                           '--upstream-branch=master',
                           dsc]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        self._check_repo_state(repo, 'debian', ['debian', 'master'])
        commits, expected = len(repo.get_commits()), 2
        ok_(commits == expected, "Found %d commit instead of %d" % (commits, expected))

    def test_import_30_filters(self):
        dscfile = self._dsc30('2.6-1')
        assert import_dsc(['arg0',
                           '--verbose',
                           '--no-pristine-tar',
                           '--debian-branch=master',
                           '--upstream-branch=upstream',
                           '--filter=debian/patches/*',
                           '--filter=AUTHORS',
                           dscfile]) == 0
        repo = ComponentTestGitRepository('hello-debhelper')
        self._check_repo_state(repo, 'master', ['master', 'upstream'])
        os.chdir('hello-debhelper')
        ok_(os.path.exists("./debian/changelog"))
        ok_(os.path.exists("./configure.ac"))
        ok_(not os.path.exists("./debian/patches/series"))
        ok_(not os.path.exists("./debian/patches/AUTHORS"))