tests: more extensive unit tests
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Mon, 25 Mar 2013 13:49:13 +0000 (15:49 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 4 Apr 2013 13:06:49 +0000 (16:06 +0300)
Change-Id: I7c030d50d8a5bafa93073c7f33e6633ba6e6365c
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
tests/data/Makefile [new file with mode: 0644]
tests/data/README [new file with mode: 0644]
tests/data/packaging/test-package.spec [new file with mode: 0644]
tests/test_obs_service_gbp.py

diff --git a/tests/data/Makefile b/tests/data/Makefile
new file mode 100644 (file)
index 0000000..522166c
--- /dev/null
@@ -0,0 +1,6 @@
+all:
+       @echo "Building version $(TEST_PKG_VERSION)"
+       @echo "$(TEST_PKG_VERSION)" > VERSION
+
+install:
+       @echo "Installing files"
diff --git a/tests/data/README b/tests/data/README
new file mode 100644 (file)
index 0000000..64ecfbe
--- /dev/null
@@ -0,0 +1 @@
+Dummy package for testing the git-buildpackage OBS source service.
diff --git a/tests/data/packaging/test-package.spec b/tests/data/packaging/test-package.spec
new file mode 100644 (file)
index 0000000..057c35e
--- /dev/null
@@ -0,0 +1,27 @@
+Name:       test-package
+Summary:    Test package for the git-buildpackage OBS source service
+Version:    0.1
+Release:    0
+Group:      Development/Libraries
+License:    GPL-2.0
+Source:     %{name}-%{version}.tar.bz2
+
+%description
+Dummy package for testing the git-buildpackage OBS source service.
+
+
+%prep
+%setup -q
+
+
+%build
+TEST_PKG_VERSION=%{version} make
+
+
+%install
+make install
+
+
+%files
+%defattr(-,root,root,-)
+%doc README VERSION
index 03979c6502863d2193c36efde616c38e00263e4c..1260d4f0f49f50a7d662928f4aa0548d542f57ba 100644 (file)
 # MA 02110-1301, USA.
 """Tests for the git-buildpackage OBS source service"""
 
+import os
+import shutil
+import stat
+import tempfile
 from nose.tools import assert_raises
 
+from gbp.git.repository import GitRepository, GitRepositoryError
+
+from obs_service_gbp import CachedRepo, CachedRepoError
 from obs_service_gbp.command import main as service
 
-class TestBasicFunctionality(object):
-    """Base class for testing cmdline tools of git-buildpackage"""
+
+TEST_DATA_DIR = os.path.abspath(os.path.join('tests', 'data'))
+
+class UnitTestsBase(object):
+    """Base class for unit tests"""
+
+    @classmethod
+    def create_orig_repo(cls, name):
+        """Create test repo"""
+        orig_repo = GitRepository.create(os.path.join(cls.workdir, name))
+        orig_repo.commit_dir(TEST_DATA_DIR, 'Initial version', 'master',
+                             create_missing_branch=True)
+        orig_repo.force_head('master', hard=True)
+        return orig_repo
+
+    @classmethod
+    def setup_class(cls):
+        """Test class setup"""
+        # Don't let git see that we're (possibly) under a git directory
+        os.environ['GIT_CEILING_DIRECTORIES'] = os.getcwd()
+        # Create temporary workdir
+        cls.workdir = os.path.abspath(tempfile.mkdtemp(prefix='%s_' %
+                                     __name__, dir='.'))
+        cls.orig_dir = os.getcwd()
+        os.chdir(cls.workdir)
+        # Use cache in our workdir
+        cls.cachedir = os.path.join(cls.workdir, 'cache')
+        os.environ['CACHEDIR'] = cls.cachedir
+        # Create an orig repo for testing
+        cls.orig_repo = cls.create_orig_repo('orig').path
+
+    @classmethod
+    def teardown_class(cls):
+        """Test class teardown"""
+        os.chdir(cls.orig_dir)
+        if not 'DEBUG_NOSETESTS' in os.environ:
+            shutil.rmtree(cls.workdir)
+
+
+class TestBasicFunctionality(UnitTestsBase):
+    """Base class for unit tests"""
+
+    def setup(self):
+        """Test case setup"""
+        # Change to a temporary directory
+        self.tmpdir = tempfile.mkdtemp(dir=self.workdir)
+        os.chdir(self.tmpdir)
+
+    def teardown(self):
+        """Test case teardown"""
+        # Restore original working dir
+        os.chdir(self.workdir)
+        if not 'DEBUG_NOSETESTS' in os.environ:
+            shutil.rmtree(self.tmpdir)
 
     def test_invalid_options(self):
         """Test invalid options"""
@@ -36,3 +95,80 @@ class TestBasicFunctionality(object):
         # Invalid repo
         assert service(['--url=foo/bar.git']) != 0
 
+    def test_basic_export(self):
+        """Test that export works"""
+        assert service(['--url', self.orig_repo]) == 0
+
+    def test_options_outdir(self):
+        """Test the --outdir option"""
+        outdir = os.path.join(self.tmpdir, 'outdir')
+        assert service(['--url', self.orig_repo, '--outdir=%s' % outdir]) == 0
+        assert os.path.isdir(outdir)
+
+    def test_options_revision(self):
+        """Test the --revision option"""
+        assert service(['--url', self.orig_repo, '--revision=master']) == 0
+        assert service(['--url', self.orig_repo, '--revision=foobar']) == 1
+
+    def test_options_verbose(self):
+        """Test the --verbose option"""
+        assert service(['--url', self.orig_repo, '--verbose=yes']) == 0
+        with assert_raises(SystemExit):
+            service(['--url', self.orig_repo, '--verbose=foob'])
+
+    def test_options_spec_vcs_tag(self):
+        """Test the --spec-vcs-tag option"""
+        assert service(['--url', self.orig_repo,
+                        '--spec-vcs-tag=orig/%(tagname)s']) == 0
+
+
+class TestCachedRepo(UnitTestsBase):
+    """Test CachedRepo class"""
+
+    def test_invalid_url(self):
+        """Test invalid url"""
+        with assert_raises(CachedRepoError):
+            CachedRepo('foo/bar.git')
+
+    def test_clone_and_fetch(self):
+        """Basic test for cloning and fetching"""
+        # Clone
+        repo = CachedRepo(self.orig_repo)
+        assert repo
+        assert repo.repo.bare
+        repo._release_lock()
+        # Fetch
+        repo2 = CachedRepo(self.orig_repo)
+        assert repo2
+        assert repo.repo.path == repo2.repo.path
+
+    def test_corrupted_cache(self):
+        """Test recovering from corrupted cache"""
+        # Clone
+        repo = CachedRepo(self.orig_repo)
+        # Corrupt repo
+        shutil.rmtree(os.path.join(repo.repo.path, 'refs'))
+        with assert_raises(GitRepositoryError):
+            repo.repo.rev_parse('HEAD')
+        repo._release_lock()
+        # Update and check status
+        repo = CachedRepo(self.orig_repo)
+        assert repo.repo.rev_parse('HEAD')
+
+    def test_cache_access_error(self):
+        """Test cached directory with invalid permissions"""
+        # Check base cachedir creation access error
+        os.chmod(self.workdir, 0)
+        with assert_raises(CachedRepoError):
+            repo = CachedRepo(self.orig_repo)
+        os.chmod(self.workdir, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
+        repo = CachedRepo(self.orig_repo)
+        repo._release_lock()
+
+        # Check cache access error
+        os.chmod(self.cachedir, 0)
+        with assert_raises(CachedRepoError):
+            repo = CachedRepo(self.orig_repo)
+        os.chmod(self.cachedir, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
+        repo = CachedRepo(self.orig_repo)
+