test_import.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/python -tt
  2. # vim: ai ts=4 sts=4 et sw=4
  3. #
  4. # Copyright (c) 2012 Intel, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; version 2 of the License
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc., 59
  17. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. """Functionality tests of gbs import."""
  19. import os
  20. import shutil
  21. import unittest
  22. import tempfile
  23. import imp
  24. from functools import wraps
  25. from nose.tools import eq_, raises
  26. from gitbuildsys.errors import GbsError
  27. from gbp.git.repository import GitRepository
  28. GBS = imp.load_source("gbs", "./tools/gbs").main
  29. def with_data(fname):
  30. """
  31. Parametrized decorator for testcase methods.
  32. Gets name of the directory or file in tests/testdata/
  33. Copies it to the temporary working directory and
  34. runs testcase method there.
  35. Adds fname as a parameter for the testcase method
  36. """
  37. def decorator(func):
  38. """Decorator itself."""
  39. @wraps(func)
  40. def wrapper(*args, **kwargs):
  41. """Main functionality is here."""
  42. obj = args[0] # TestCase object
  43. # Copy required data(fname) to object temporary directory
  44. fpath = os.path.join(obj.cdir, "./tests/testdata", fname)
  45. if os.path.isdir(fpath):
  46. shutil.copytree(fpath, os.path.join(obj.tmp, fname))
  47. else:
  48. shutil.copy(fpath, obj.tmp)
  49. # Append fname to testcase method parameters
  50. args = list(args)
  51. args.append(fpath)
  52. args = tuple(args)
  53. return func(*args, **kwargs)
  54. return wrapper
  55. return decorator
  56. class TestImport(unittest.TestCase):
  57. """Test help output of gbs commands"""
  58. def __init__(self, method):
  59. super(TestImport, self).__init__(method)
  60. def setUp(self):
  61. self.tmp = tempfile.mkdtemp(prefix="test-gbs-import-")
  62. shutil.copy('.gbs.conf', self.tmp)
  63. self.cdir = os.getcwd()
  64. os.chdir(self.tmp)
  65. def tearDown(self):
  66. os.chdir(self.cdir)
  67. shutil.rmtree(self.tmp)
  68. @with_data("ail-0.2.29-2.3.src.rpm")
  69. def test_import_srcrpm(self, srcrpm):
  70. """Test importing from source rpm."""
  71. eq_(GBS(argv=["gbs", "import", srcrpm]), None)
  72. repo = GitRepository("./ail")
  73. eq_(repo.get_local_branches(), ['master', 'pristine-tar', 'upstream'])
  74. eq_(repo.get_tags(), ['upstream/0.2.29', 'vendor/0.2.29-2.3'])
  75. @with_data("bluez_unpacked")
  76. def test_import_spec(self, srcdir):
  77. """Test importing from spec."""
  78. eq_(GBS(argv=["gbs", "import",
  79. os.path.join(srcdir, 'bluez.spec')]), None)
  80. repo = GitRepository("./bluez")
  81. eq_(repo.get_local_branches(), ['master', 'pristine-tar', 'upstream'])
  82. # No packging tag as patch-import fails
  83. eq_(repo.get_tags(), ['upstream/4.87'])
  84. eq_(len(repo.get_commits(until='master')), 2)
  85. #raise Exception(os.listdir('./bluez'))
  86. @with_data("ail-0.2.29-2.5.src.rpm")
  87. def test_running_from_git_tree(self, srcrpm):
  88. """Test running gbs import from git tree."""
  89. # Create empty git repo
  90. repo = GitRepository.create("./repo_dir")
  91. os.chdir(repo.path)
  92. eq_(GBS(argv=["gbs", "import", srcrpm]), None)
  93. eq_(repo.get_local_branches(), ['master', 'pristine-tar', 'upstream'])
  94. eq_(repo.get_tags(), ['upstream/0.2.29', 'vendor/0.2.29-2.5'])
  95. #raise Exception(os.listdir('./bluez'))
  96. @with_data("app-core-1.2-19.3.src.rpm")
  97. def test_set_author_name_email(self, srcrpm):
  98. """Test --author-name and --author-email command line options."""
  99. eq_(GBS(argv=["gbs", "import", "--author-name=test",
  100. "--author-email=test@otctools.jf.intel.com",
  101. srcrpm]), None)
  102. repo = GitRepository("./app-core")
  103. eq_(repo.get_local_branches(), ['master', 'pristine-tar', 'upstream'])
  104. eq_(repo.get_tags(), ['upstream/1.2', 'vendor/1.2-19.3'])
  105. @with_data("ail-0.2.29-2.3.src.rpm")
  106. def test_specify_upstream(self, srcrpm):
  107. """Test --upstream command line option."""
  108. eq_(GBS(argv=["gbs", "import", "--upstream-branch=upstream",
  109. srcrpm]), None)
  110. repo = GitRepository("./ail")
  111. eq_(repo.get_local_branches(), ['master', 'pristine-tar', 'upstream'])
  112. eq_(repo.get_tags(), ['upstream/0.2.29', 'vendor/0.2.29-2.3'])
  113. @raises(GbsError)
  114. @with_data("bison-1.27.tar.gz")
  115. def test_is_not_git_repository(self, tarball):
  116. """Test raising exception when importing tarball outside of git."""
  117. GBS(argv=["gbs", "import", tarball])
  118. @raises(GbsError)
  119. @with_data("bad.src.rpm")
  120. def test_error_reading_pkg_header(self, srcrpm):
  121. """Test raising exception when importing from bad package."""
  122. GBS(argv=["gbs", "import", srcrpm])
  123. @raises(GbsError)
  124. @with_data("bad.spec")
  125. def test_cant_parse_specfile(self, spec):
  126. """Test raising exception when importing from non-parseable spec."""
  127. GBS(argv=["gbs", "import", spec])
  128. @raises(SystemExit)
  129. def test_missing_argument(self):
  130. """Test raising exception when running gbs without any arguments."""
  131. GBS(argv=["gbs", "import"])
  132. @raises(SystemExit)
  133. def test_too_many_arguments(self):
  134. """Test raising exception when running gbs with too many arguments."""
  135. GBS(argv=["gbs", "import", "1", "2"])
  136. @raises(GbsError)
  137. def test_path_doesnt_exist(self):
  138. """Test raising exception when running gbs with not existing path."""
  139. GBS(argv=["gbs", "import", "I don't exist!"])