test_buildpackage.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2015-2017 Guido Günther <agx@sigxcpu.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, please see
  17. # <http://www.gnu.org/licenses/>
  18. import hashlib
  19. import os
  20. import subprocess
  21. from tests.component import (ComponentTestBase,
  22. ComponentTestGitRepository)
  23. from tests.component.deb import DEB_TEST_DATA_DIR
  24. from tests.component.deb.fixtures import RepoFixtures
  25. from nose.tools import ok_, eq_, assert_false, assert_true
  26. from gbp.scripts.import_dsc import main as import_dsc
  27. from gbp.scripts.buildpackage import main as buildpackage
  28. class TestBuildpackage(ComponentTestBase):
  29. """Test building a debian package"""
  30. @staticmethod
  31. def _dsc_name(pkg, version, dir):
  32. return os.path.join(DEB_TEST_DATA_DIR,
  33. dir,
  34. '%s_%s.dsc' % (pkg, version))
  35. def _test_buildpackage(self, repo, opts=[]):
  36. prebuild_out = os.path.join(repo.path, 'prebuild.out')
  37. postbuild_out = os.path.join(repo.path, 'postbuild.out')
  38. args = ['arg0',
  39. '--git-prebuild=printenv > %s' % prebuild_out,
  40. '--git-postbuild=printenv > %s' % postbuild_out,
  41. '--git-builder=/bin/true',
  42. '--git-cleaner=/bin/true'] + opts
  43. os.chdir(repo.path)
  44. ret = buildpackage(args)
  45. ok_(ret == 0, "Building the package failed")
  46. eq_(os.path.exists(prebuild_out), True)
  47. eq_(os.path.exists(postbuild_out), True)
  48. self.check_hook_vars('prebuild', ["GBP_BUILD_DIR",
  49. "GBP_GIT_DIR",
  50. "GBP_BUILD_DIR"])
  51. self.check_hook_vars('postbuild', ["GBP_CHANGES_FILE",
  52. "GBP_BUILD_DIR",
  53. "GBP_CHANGES_FILE",
  54. "GBP_BUILD_DIR"])
  55. @RepoFixtures.native()
  56. def test_debian_buildpackage(self, repo):
  57. """Test that building a native debian package works"""
  58. self._test_buildpackage(repo)
  59. @RepoFixtures.quilt30()
  60. def test_non_native_buildpackage(self, repo):
  61. """Test that building a source 3.0 debian package works"""
  62. self._test_buildpackage(repo)
  63. @RepoFixtures.native()
  64. def test_tag_only(self, repo):
  65. """Test that only tagging a native debian package works"""
  66. repo.delete_tag('debian/0.4.14') # make sure we can tag again
  67. ret = buildpackage(['arg0',
  68. '--git-tag-only',
  69. '--git-posttag=printenv > posttag.out',
  70. '--git-builder=touch builder-run.stamp',
  71. '--git-cleaner=/bin/true'])
  72. ok_(ret == 0, "Building the package failed")
  73. eq_(os.path.exists('posttag.out'), True)
  74. eq_(os.path.exists('builder-run.stamp'), False)
  75. self.check_hook_vars('posttag', [("GBP_TAG", "debian/0.4.14"),
  76. ("GBP_BRANCH", "master"),
  77. "GBP_SHA1"])
  78. def test_component_generation(self):
  79. """Test that generating tarball and additional tarball works without pristine-tar"""
  80. pkg = 'hello-debhelper'
  81. dsc = self._dsc_name(pkg, '2.8-1', 'dsc-3.0-additional-tarballs')
  82. tarballs = ["../%s_2.8.orig-foo.tar.gz" % pkg,
  83. "../%s_2.8.orig.tar.gz" % pkg]
  84. assert import_dsc(['arg0', '--no-pristine-tar', dsc]) == 0
  85. repo = ComponentTestGitRepository(pkg)
  86. os.chdir(pkg)
  87. assert_false(repo.has_branch('pristine-tar'), "Pristine-tar branch must not exist")
  88. for t in tarballs:
  89. self.assertFalse(os.path.exists(t), "Tarball %s must not exist" % t)
  90. ret = buildpackage(['arg0',
  91. '--git-component=foo',
  92. '--git-no-pristine-tar',
  93. '--git-posttag=printenv > posttag.out',
  94. '--git-builder=touch builder-run.stamp',
  95. '--git-cleaner=/bin/true'])
  96. ok_(ret == 0, "Building the package failed")
  97. for t in tarballs:
  98. self.assertTrue(os.path.exists(t), "Tarball %s not found" % t)
  99. def test_pristinetar_component_generation(self):
  100. """Test that generating tarball and additional tarball works with pristine-tar"""
  101. pkg = 'hello-debhelper'
  102. dsc = self._dsc_name(pkg, '2.8-1', 'dsc-3.0-additional-tarballs')
  103. tarballs = ["../%s_2.8.orig-foo.tar.gz" % pkg,
  104. "../%s_2.8.orig.tar.gz" % pkg]
  105. assert import_dsc(['arg0', '--pristine-tar', dsc]) == 0
  106. repo = ComponentTestGitRepository(pkg)
  107. os.chdir(pkg)
  108. assert_true(repo.has_branch('pristine-tar'), "Pristine-tar branch must exist")
  109. for t in tarballs:
  110. self.assertFalse(os.path.exists(t), "Tarball %s must not exist" % t)
  111. # Make sure the tree object for importing the main tarball is recreated
  112. repo.collect_garbage(prune='all', aggressive=True)
  113. ret = buildpackage(['arg0',
  114. '--git-component=foo',
  115. '--git-pristine-tar',
  116. '--git-posttag=printenv > posttag.out',
  117. '--git-builder=touch builder-run.stamp',
  118. '--git-cleaner=/bin/true'])
  119. ok_(ret == 0, "Building the package failed")
  120. for t in tarballs:
  121. self.assertTrue(os.path.exists(t), "Tarball %s not found" % t)
  122. @RepoFixtures.quilt30()
  123. def test_export_dir_buildpackage(self, repo):
  124. """Test that building with a export dir works"""
  125. self._test_buildpackage(repo, ['--git-export-dir=../foo/bar'])
  126. ok_(os.path.exists('../foo/bar'))
  127. @RepoFixtures.native()
  128. def test_argument_quoting(self, repo):
  129. """Test that we quote arguments to builder (#)"""
  130. with open('../arg with spaces', 'w'):
  131. pass
  132. # We use ls as builder to look for a file with spaces. This
  133. # will fail if build arguments are not properly quoted and
  134. # therefore split up
  135. ret = buildpackage(['arg0',
  136. '--git-builder=ls',
  137. '--git-cleaner=/bin/true',
  138. '../arg with spaces'])
  139. ok_(ret == 0, "Building the package failed")
  140. @RepoFixtures.quilt30()
  141. def test_tarball_default_compression(self, repo):
  142. """Test that we use defaults for compression if not given (#820846)"""
  143. self._test_buildpackage(repo, ['--git-no-pristine-tar'])
  144. tarball = "../hello-debhelper_2.8.orig.tar.gz"
  145. out = subprocess.check_output(["file", tarball])
  146. ok_("max compression" not in out)
  147. m1 = hashlib.md5(open(tarball, 'rb').read()).hexdigest()
  148. os.unlink(tarball)
  149. eq_(buildpackage(['arg0',
  150. '--git-ignore-new',
  151. '--git-builder=/bin/true',
  152. '--git-cleaner=/bin/true',
  153. '../arg with spaces']), 0)
  154. m2 = hashlib.md5(open(tarball, 'rb').read()).hexdigest()
  155. eq_(m1, m2, "Regenerated tarball has different checksum")
  156. @RepoFixtures.quilt30
  157. def test_tarball_max_compression(self, repo):
  158. """Test that passing max compression works (#820846)"""
  159. self._test_buildpackage(repo, ['--git-no-pristine-tar', '--git-compression-level=9'])
  160. out = subprocess.check_output(["file", "../hello-debhelper_2.8.orig.tar.gz"])
  161. ok_("max compression" in out)